aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/blake2/blake2s.c37
-rw-r--r--lib/blake2/blake2s.h24
-rw-r--r--lib/libk/endian.h8
-rw-r--r--lib/libk/endian/little.c4
-rw-r--r--lib/libk/stdio.h22
-rw-r--r--lib/libk/stdio/fprintf.c5
-rw-r--r--lib/libk/stdio/printf.c5
-rw-r--r--lib/libk/stdio/vfprintf.c5
-rw-r--r--lib/libk/stdlib.h10
-rw-r--r--lib/libk/stdlib/linked_list_allocator.c11
-rw-r--r--lib/libk/stdlib/memcpy.c2
-rw-r--r--lib/libk/stdlib/memset.c3
-rw-r--r--lib/libk/string.h13
13 files changed, 63 insertions, 86 deletions
diff --git a/lib/blake2/blake2s.c b/lib/blake2/blake2s.c
index b924a1e..9206c89 100644
--- a/lib/blake2/blake2s.c
+++ b/lib/blake2/blake2s.c
@@ -34,14 +34,16 @@ G(uint32_t v[16], unsigned a, unsigned b, unsigned c, unsigned d, uint32_t x, ui
void
F(struct BLAKE2s_ctx *ctx, uint32_t m[16], uint32_t f)
{
- // Initialize local work vector v
+ unsigned i;
+
+ /* Initialize local work vector v */
uint32_t v[16] = {ctx->h[0], ctx->h[1], ctx->h[2], ctx->h[3], ctx->h[4], ctx->h[5],
ctx->h[6], ctx->h[7], IV[0], IV[1], IV[2], IV[3],
IV[4] ^ ctx->t[0], IV[5] ^ ctx->t[1], IV[6], IV[7]};
- if (f) v[14] = ~v[14]; // if last block flag, invert all bits
+ if (f) v[14] = ~v[14]; /* if last block flag, invert all bits */
- // cryptographic mixing
- for (unsigned i = 0; i < 10; ++i) {
+ /* cryptographic mixing */
+ for (i = 0; i < 10; ++i) {
G(v, 0, 4, 8, 12, m[SIGMA[i][0]], m[SIGMA[i][1]]);
G(v, 1, 5, 9, 13, m[SIGMA[i][2]], m[SIGMA[i][3]]);
G(v, 2, 6, 10, 14, m[SIGMA[i][4]], m[SIGMA[i][5]]);
@@ -53,8 +55,8 @@ F(struct BLAKE2s_ctx *ctx, uint32_t m[16], uint32_t f)
G(v, 3, 4, 9, 14, m[SIGMA[i][14]], m[SIGMA[i][15]]);
}
- // xor the two halves
- for (unsigned i = 0; i < 8; ++i) ctx->h[i] ^= (v[i] ^ v[i + 8]);
+ /* xor the two halves */
+ for (i = 0; i < 8; ++i) ctx->h[i] ^= (v[i] ^ v[i + 8]);
}
int
@@ -70,14 +72,14 @@ BLAKE2s_init(struct BLAKE2s_ctx *ctx, uint8_t outlen, const void *key, uint8_t k
ctx->param.fanout = 1;
ctx->param.depth = 1;
- // copy IV into state vector h
+ /* copy IV into state vector h */
memcpy(ctx->h, IV, 32);
- // copy param block 0 onto h[0]
+ /* copy param block 0 onto h[0] */
ctx->h[0] ^= (ctx->param.depth << 24) ^ (ctx->param.fanout << 16) ^ (keylen << 8) ^ outlen;
if (keylen > 0) {
BLAKE2s_update(ctx, key, keylen);
- ctx->c = 64; // at the end
+ ctx->c = 64; /* at the end */
}
return 0;
@@ -88,15 +90,16 @@ BLAKE2s_init(struct BLAKE2s_ctx *ctx, uint8_t outlen, const void *key, uint8_t k
void
BLAKE2s_update(struct BLAKE2s_ctx *ctx, const void *d, size_t dd)
{
- for (unsigned i = 0; i < dd;) {
+ unsigned i, j;
+ for (i = 0; i < dd;) {
- if (ctx->c == 64) { // if block is full, consume block
+ if (ctx->c == 64) { /* if block is full, consume block */
ctx->t[0] += ctx->c;
if (ctx->t[0] < ctx->c) ctx->t[1] += 1;
- ctx->c = 0; // reset counter
+ ctx->c = 0; /* reset counter */
uint32_t *m = (uint32_t *)ctx->b;
- for (unsigned j = 0; j < 16; ++j) m[j] = htole32(m[j]);
+ for (j = 0; j < 16; ++j) m[j] = htole32(m[j]);
F(ctx, m, 0);
}
@@ -110,14 +113,16 @@ BLAKE2s_update(struct BLAKE2s_ctx *ctx, const void *d, size_t dd)
void
BLAKE2s_final(struct BLAKE2s_ctx *ctx, void *out)
{
+ unsigned i;
+
ctx->t[0] += ctx->c;
if (ctx->t[0] < ctx->c) ctx->t[1] += 1;
- for (; ctx->c < 64; ++(ctx->c)) ctx->b[ctx->c] = 0; // fill up block with zeroes
+ for (; ctx->c < 64; ++(ctx->c)) ctx->b[ctx->c] = 0; /* fill up block with zeroes */
uint32_t *m = (uint32_t *)ctx->b;
- for (unsigned i = 0; i < 16; ++i) m[i] = htole32(m[i]);
+ for (i = 0; i < 16; ++i) m[i] = htole32(m[i]);
F(ctx, m, 1);
- for (unsigned i = 0; i < ctx->param.outlen; ++i) ((uint8_t *)out)[i] = (ctx->h[i >> 2] >> (8 * (i & 3))) & 0xff;
+ for (i = 0; i < ctx->param.outlen; ++i) ((uint8_t *)out)[i] = (ctx->h[i >> 2] >> (8 * (i & 3))) & 0xff;
}
diff --git a/lib/blake2/blake2s.h b/lib/blake2/blake2s.h
index 64b4156..ede170c 100644
--- a/lib/blake2/blake2s.h
+++ b/lib/blake2/blake2s.h
@@ -4,8 +4,8 @@
#include <stdint.h>
struct BLAKE2s_param {
- uint8_t outlen; // digest length
- uint8_t keylen; // key length
+ uint8_t outlen; /* digest length */
+ uint8_t keylen; /* key length */
uint8_t fanout;
uint8_t depth;
uint32_t leaf_length;
@@ -18,11 +18,11 @@ struct BLAKE2s_param {
};
struct BLAKE2s_ctx {
- uint8_t b[64]; // input buffer
- size_t c; // pointer for b[]
- uint32_t h[8]; // chained state vector h
- uint32_t t[2]; // total number of bytes
- struct BLAKE2s_param param; // parameter block
+ uint8_t b[64]; /* input buffer */
+ size_t c; /* pointer for b[] */
+ uint32_t h[8]; /* chained state vector h */
+ uint32_t t[2]; /* total number of bytes */
+ struct BLAKE2s_param param; /* parameter block */
};
/**
@@ -41,11 +41,11 @@ int BLAKE2s_init(struct BLAKE2s_ctx *ctx, uint8_t outlen, const void *key, uint8
void BLAKE2s_update(struct BLAKE2s_ctx *ctx, const void *d, size_t dd);
void BLAKE2s_final(struct BLAKE2s_ctx *ctx, void *out);
-// All-in-one convenience function.
-static inline int
-BLAKE2s(void *out, uint8_t outlen, // return buffer for digest
- const void *key, uint8_t keylen, // optional secret key
- const void *in, size_t inlen) // data to be hashed
+/* All-in-one convenience function. */
+static __inline__ int
+BLAKE2s(void *out, uint8_t outlen, /* return buffer for digest */
+ const void *key, uint8_t keylen, /* optional secret key */
+ const void *in, size_t inlen) /* data to be hashed */
{
struct BLAKE2s_ctx ctx;
if (BLAKE2s_init(&ctx, outlen, key, keylen)) return -1;
diff --git a/lib/libk/endian.h b/lib/libk/endian.h
index 70bc5f7..6aa2669 100644
--- a/lib/libk/endian.h
+++ b/lib/libk/endian.h
@@ -1,13 +1,11 @@
-//=====================================================================
-// spdx-license-identifier: ISC
-//=====================================================================
+/* spdx-license-identifier: ISC */
#pragma once
#include <stdint.h>
-// These functions convert the byte encoding of integer values from host byte order to and from little-endian and
-// big-endian byte order
+/* These functions convert the byte encoding of integer values from host byte order to and from little-endian and
+ * big-endian byte order */
uint16_t htole16(uint16_t host_16b);
uint32_t htole32(uint32_t host_32b);
uint64_t htole64(uint64_t host_64b);
diff --git a/lib/libk/endian/little.c b/lib/libk/endian/little.c
index 042bb55..56a20ad 100644
--- a/lib/libk/endian/little.c
+++ b/lib/libk/endian/little.c
@@ -1,6 +1,4 @@
-//=====================================================================
-// spdx-license-identifier: ISC
-//=====================================================================
+/* spdx-license-identifier: ISC */
#include "endian.h"
diff --git a/lib/libk/stdio.h b/lib/libk/stdio.h
index b28eb5e..7a6e663 100644
--- a/lib/libk/stdio.h
+++ b/lib/libk/stdio.h
@@ -2,22 +2,17 @@
#include <stdarg.h>
-///@defgroup libk libk
-///@{
-///@defgroup stdio stdio
-///@{
-
-/// An object type used for streams
+/** An object type used for streams */
typedef struct FILE {
int id;
- /// Function that prints a character to the stream
+ /** Function that prints a character to the stream */
void (*putc)(const struct FILE *, char);
- /// Function that prints a string to the stream
+ /** Function that prints a string to the stream */
int (*puts)(const struct FILE *, const char *, int);
- /// Flush write buffers
+ /** Flush write buffers */
void (*flush)(const struct FILE *);
} FILE;
@@ -33,17 +28,14 @@ extern FILE *stderr;
* Supports ``%s`` (string), ``%d`` (decimal), ``%u`` (unsigned), ``%x`` (hexadecimal)
* @return number of bytes written
*/
-int printf(const char *restrict format, ...);
+int printf(const char *__restrict__ format, ...);
/**
* Write the formatted string to stream; see printf
*/
-int fprintf(FILE *restrict stream, const char *restrict format, ...);
+int fprintf(FILE *__restrict__ stream, const char *__restrict__ format, ...);
/**
* Write the formatted string to stream; see printf
*/
-int vfprintf(FILE *restrict stream, const char *restrict format, va_list ap);
-
-///@}
-///@}
+int vfprintf(FILE *__restrict__ stream, const char *__restrict__ format, va_list ap);
diff --git a/lib/libk/stdio/fprintf.c b/lib/libk/stdio/fprintf.c
index 9a96dc6..c088f54 100644
--- a/lib/libk/stdio/fprintf.c
+++ b/lib/libk/stdio/fprintf.c
@@ -1,11 +1,12 @@
#include <stdio.h>
int
-fprintf(FILE *restrict stream, const char *restrict format, ...)
+fprintf(FILE *__restrict__ stream, const char *__restrict__ format, ...)
{
+ int c = 0;
va_list ap;
va_start(ap, format);
- int c = vfprintf(stream, format, ap);
+ c += vfprintf(stream, format, ap);
va_end(ap);
return c;
}
diff --git a/lib/libk/stdio/printf.c b/lib/libk/stdio/printf.c
index 4efc1ac..4c45593 100644
--- a/lib/libk/stdio/printf.c
+++ b/lib/libk/stdio/printf.c
@@ -1,11 +1,12 @@
#include <stdio.h>
int
-printf(const char *restrict format, ...)
+printf(const char *__restrict__ format, ...)
{
+ int c = 0;
va_list ap;
va_start(ap, format);
- int c = vfprintf(stdout, format, ap);
+ c += vfprintf(stdout, format, ap);
va_end(ap);
return c;
}
diff --git a/lib/libk/stdio/vfprintf.c b/lib/libk/stdio/vfprintf.c
index d24e43e..a48600b 100644
--- a/lib/libk/stdio/vfprintf.c
+++ b/lib/libk/stdio/vfprintf.c
@@ -4,13 +4,14 @@
static char buffer[3 * sizeof(int) + 2];
int
-vfprintf(FILE *restrict stream, const char *restrict format, va_list params)
+vfprintf(FILE *__restrict__ stream, const char *__restrict__ format, va_list params)
{
int written = 0;
+ int i;
int s = 0;
int l = 0;
- for (int i = 0; format[i] != '\0'; ++i) {
+ for (i = 0; format[i] != '\0'; ++i) {
if (format[i] == '%') {
written += stream->puts(stream, &format[s], l);
s = i + 2;
diff --git a/lib/libk/stdlib.h b/lib/libk/stdlib.h
index 84d9b2d..143c931 100644
--- a/lib/libk/stdlib.h
+++ b/lib/libk/stdlib.h
@@ -2,11 +2,6 @@
#include <stddef.h>
-///@defgroup libk libk
-///@{
-///@defgroup stdlib stdlib
-///@{
-
/**
* Allocate size bytes and return a pointer to the allocated memory
*/
@@ -25,7 +20,4 @@ void *memset(void *s, int c, long unsigned n);
/**
* Copy n bytes from memory area src to memory area dest. The memory areas must not overlap.
*/
-void *memcpy(void *restrict dest, const void *restrict src, long unsigned n);
-
-///@}
-///@}
+void *memcpy(void *__restrict__ dest, const void *__restrict__ src, long unsigned n);
diff --git a/lib/libk/stdlib/linked_list_allocator.c b/lib/libk/stdlib/linked_list_allocator.c
index 66c63d1..898fd89 100644
--- a/lib/libk/stdlib/linked_list_allocator.c
+++ b/lib/libk/stdlib/linked_list_allocator.c
@@ -31,13 +31,14 @@ alloc_init(void *mem, size_t size)
void *
malloc(size_t size)
{
+ struct Chunk *iter;
if (begin == NULL) return NULL;
- // find free chunk that is at least (size + sizeof(struct Chunk))
- for (struct Chunk *iter = begin; iter != NULL; iter = iter->next) {
+ /* find free chunk that is at least (size + sizeof(struct Chunk)) */
+ for (iter = begin; iter != NULL; iter = iter->next) {
if (iter->used != 0 || iter->size < size) continue;
- // if there's at least sizeof(struct Chunk) bytes left over, create a new Chunk
+ /* if there's at least sizeof(struct Chunk) bytes left over, create a new Chunk */
if (iter->size >= (size + 2 * sizeof(struct Chunk))) {
struct Chunk *next = (struct Chunk *)((uintptr_t)iter + sizeof(struct Chunk) + size);
Chunk_ctor(next, iter->size - size - sizeof(struct Chunk));
@@ -61,14 +62,14 @@ free(void *ptr)
struct Chunk *chunk = (struct Chunk *)((uintptr_t)ptr - sizeof(struct Chunk));
chunk->used = 0;
- // merge next chunk
+ /* merge next chunk */
if (chunk->next != NULL && chunk->next->used == 0) {
chunk->size += chunk->next->size + sizeof(struct Chunk);
chunk->next = chunk->next->next;
if (chunk->next != NULL) chunk->next->prev = chunk;
}
- // merge into prev chunk
+ /* merge into prev chunk */
if (chunk->prev != NULL && chunk->prev->used == 0) {
chunk->prev->size += chunk->size + sizeof(struct Chunk);
chunk->prev->next = chunk->next;
diff --git a/lib/libk/stdlib/memcpy.c b/lib/libk/stdlib/memcpy.c
index 90470d5..db7d21e 100644
--- a/lib/libk/stdlib/memcpy.c
+++ b/lib/libk/stdlib/memcpy.c
@@ -1,5 +1,5 @@
void *
-memcpy(void *restrict dest, const void *restrict src, long unsigned n)
+memcpy(void *__restrict__ dest, const void *__restrict__ src, long unsigned n)
{
char *pDest = (char *)dest;
const char *pSrc = (const char *)src;
diff --git a/lib/libk/stdlib/memset.c b/lib/libk/stdlib/memset.c
index a16bd05..2a86f8e 100644
--- a/lib/libk/stdlib/memset.c
+++ b/lib/libk/stdlib/memset.c
@@ -1,7 +1,8 @@
void *
memset(void *s, int c, long unsigned n)
{
+ unsigned i;
char *pDest = (char *)s;
- for (unsigned i = 0; i < n; ++i) pDest[i] = (char)c;
+ for (i = 0; i < n; ++i) pDest[i] = (char)c;
return s;
}
diff --git a/lib/libk/string.h b/lib/libk/string.h
index c8196c8..46d8636 100644
--- a/lib/libk/string.h
+++ b/lib/libk/string.h
@@ -1,17 +1,9 @@
#pragma once
-///@defgroup libk libk
-///@{
-///@defgroup string string
-///@{
-
#define OCTAL 8
#define DECIMAL 10
#define HEX 16
-#ifdef __cplusplus
-extern "C" {
-#endif
/**
* Convert int into a string
*/
@@ -20,9 +12,4 @@ char *itoa(char *p, int x, int base);
* Convert unsigned int into a string
*/
char *utoa(char *p, unsigned x, int base);
-#ifdef __cplusplus
-}
-#endif
-///@}
-///@}