Format source code.

This commit is contained in:
Jordan Bancino 2023-06-18 02:53:52 +00:00
parent 861d4146c0
commit e3badbd55c
8 changed files with 208 additions and 147 deletions

View file

@ -3,7 +3,7 @@
#include <Log.h> #include <Log.h>
int int
Main(Array * args, HashMap * env) Main(Array * args, HashMap * env)
{ {
size_t i; size_t i;

View file

@ -84,7 +84,7 @@ main(int argc, char **argv)
if (!MemoryRuntimeInit()) if (!MemoryRuntimeInit())
{ {
errLen = snprintf(errBuf, ERR_BUF_SIZE, "Fatal: Unable to initialize Memory runtime.\n"); errLen = snprintf(errBuf, ERR_BUF_SIZE, "Fatal: Unable to initialize Memory runtime.\n");
write(STDERR_FILENO, errBuf, errLen); write(STDERR_FILENO, errBuf, errLen);
goto finish; goto finish;
} }
@ -109,6 +109,7 @@ main(int argc, char **argv)
for (i = 0; i < (size_t) argc; i++) for (i = 0; i < (size_t) argc; i++)
{ {
char *arg = StrDuplicate(argv[i]); char *arg = StrDuplicate(argv[i]);
if (!arg || !ArrayAdd(args.args, arg)) if (!arg || !ArrayAdd(args.args, arg))
{ {
errLen = snprintf(errBuf, ERR_BUF_SIZE, "Fatal: Unable to allocate heap memory for program argument.\n"); errLen = snprintf(errBuf, ERR_BUF_SIZE, "Fatal: Unable to allocate heap memory for program argument.\n");

View file

@ -30,19 +30,19 @@
char * char *
ShaToHex(unsigned char *bytes) ShaToHex(unsigned char *bytes)
{ {
size_t i = 0; size_t i = 0;
char *str = Malloc(((strlen((char *) bytes) * 2) + 1) * sizeof(char)); char *str = Malloc(((strlen((char *) bytes) * 2) + 1) * sizeof(char));
if (!str) if (!str)
{ {
return NULL; return NULL;
} }
while (bytes[i] != '\0') while (bytes[i] != '\0')
{ {
snprintf(str + (2 * i), 3, "%02x", bytes[i]); snprintf(str + (2 * i), 3, "%02x", bytes[i]);
i++; i++;
} }
return str; return str;
} }

View file

@ -49,159 +49,219 @@
typedef union typedef union
{ {
UInt8 c[64]; UInt8 c[64];
UInt32 l[16]; UInt32 l[16];
} Char64Long16; } Char64Long16;
typedef struct typedef struct
{ {
UInt32 state[5]; UInt32 state[5];
UInt32 count[2]; UInt32 count[2];
UInt8 buffer[64]; UInt8 buffer[64];
} Sha1Context; } Sha1Context;
static void static void
Sha1Transform(UInt32 state[5], const UInt8 buffer[64]) Sha1Transform(UInt32 state[5], const UInt8 buffer[64])
{ {
UInt32 a, b, c, d, e, i; UInt32 a, b, c, d, e, i;
UInt8 workspace[64]; UInt8 workspace[64];
Char64Long16 *block = (Char64Long16 *) workspace; Char64Long16 *block = (Char64Long16 *) workspace;
for (i = 0; i < 16; i++) for (i = 0; i < 16; i++)
{ {
LOAD32H(block->l[i], buffer + (i * 4)); LOAD32H(block->l[i], buffer + (i * 4));
} }
a = state[0]; a = state[0];
b = state[1]; b = state[1];
c = state[2]; c = state[2];
d = state[3]; d = state[3];
e = state[4]; e = state[4];
R0(a,b,c,d,e, 0); R0(e,a,b,c,d, 1); R0(d,e,a,b,c, 2); R0(c,d,e,a,b, 3); R0(a, b, c, d, e, 0);
R0(b,c,d,e,a, 4); R0(a,b,c,d,e, 5); R0(e,a,b,c,d, 6); R0(d,e,a,b,c, 7); R0(e, a, b, c, d, 1);
R0(c,d,e,a,b, 8); R0(b,c,d,e,a, 9); R0(a,b,c,d,e,10); R0(e,a,b,c,d,11); R0(d, e, a, b, c, 2);
R0(d,e,a,b,c,12); R0(c,d,e,a,b,13); R0(b,c,d,e,a,14); R0(a,b,c,d,e,15); R0(c, d, e, a, b, 3);
R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19); R0(b, c, d, e, a, 4);
R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23); R0(a, b, c, d, e, 5);
R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27); R0(e, a, b, c, d, 6);
R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31); R0(d, e, a, b, c, 7);
R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35); R0(c, d, e, a, b, 8);
R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39); R0(b, c, d, e, a, 9);
R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43); R0(a, b, c, d, e, 10);
R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47); R0(e, a, b, c, d, 11);
R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51); R0(d, e, a, b, c, 12);
R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55); R0(c, d, e, a, b, 13);
R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59); R0(b, c, d, e, a, 14);
R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63); R0(a, b, c, d, e, 15);
R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67); R1(e, a, b, c, d, 16);
R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71); R1(d, e, a, b, c, 17);
R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75); R1(c, d, e, a, b, 18);
R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79); R1(b, c, d, e, a, 19);
R2(a, b, c, d, e, 20);
R2(e, a, b, c, d, 21);
R2(d, e, a, b, c, 22);
R2(c, d, e, a, b, 23);
R2(b, c, d, e, a, 24);
R2(a, b, c, d, e, 25);
R2(e, a, b, c, d, 26);
R2(d, e, a, b, c, 27);
R2(c, d, e, a, b, 28);
R2(b, c, d, e, a, 29);
R2(a, b, c, d, e, 30);
R2(e, a, b, c, d, 31);
R2(d, e, a, b, c, 32);
R2(c, d, e, a, b, 33);
R2(b, c, d, e, a, 34);
R2(a, b, c, d, e, 35);
R2(e, a, b, c, d, 36);
R2(d, e, a, b, c, 37);
R2(c, d, e, a, b, 38);
R2(b, c, d, e, a, 39);
R3(a, b, c, d, e, 40);
R3(e, a, b, c, d, 41);
R3(d, e, a, b, c, 42);
R3(c, d, e, a, b, 43);
R3(b, c, d, e, a, 44);
R3(a, b, c, d, e, 45);
R3(e, a, b, c, d, 46);
R3(d, e, a, b, c, 47);
R3(c, d, e, a, b, 48);
R3(b, c, d, e, a, 49);
R3(a, b, c, d, e, 50);
R3(e, a, b, c, d, 51);
R3(d, e, a, b, c, 52);
R3(c, d, e, a, b, 53);
R3(b, c, d, e, a, 54);
R3(a, b, c, d, e, 55);
R3(e, a, b, c, d, 56);
R3(d, e, a, b, c, 57);
R3(c, d, e, a, b, 58);
R3(b, c, d, e, a, 59);
R4(a, b, c, d, e, 60);
R4(e, a, b, c, d, 61);
R4(d, e, a, b, c, 62);
R4(c, d, e, a, b, 63);
R4(b, c, d, e, a, 64);
R4(a, b, c, d, e, 65);
R4(e, a, b, c, d, 66);
R4(d, e, a, b, c, 67);
R4(c, d, e, a, b, 68);
R4(b, c, d, e, a, 69);
R4(a, b, c, d, e, 70);
R4(e, a, b, c, d, 71);
R4(d, e, a, b, c, 72);
R4(c, d, e, a, b, 73);
R4(b, c, d, e, a, 74);
R4(a, b, c, d, e, 75);
R4(e, a, b, c, d, 76);
R4(d, e, a, b, c, 77);
R4(c, d, e, a, b, 78);
R4(b, c, d, e, a, 79);
state[0] += a; state[0] += a;
state[1] += b; state[1] += b;
state[2] += c; state[2] += c;
state[3] += d; state[3] += d;
state[4] += e; state[4] += e;
} }
static void static void
Sha1Init(Sha1Context *ctx) Sha1Init(Sha1Context * ctx)
{ {
ctx->state[0] = 0x67452301; ctx->state[0] = 0x67452301;
ctx->state[1] = 0xEFCDAB89; ctx->state[1] = 0xEFCDAB89;
ctx->state[2] = 0x98BADCFE; ctx->state[2] = 0x98BADCFE;
ctx->state[3] = 0x10325476; ctx->state[3] = 0x10325476;
ctx->state[4] = 0xC3D2E1F0; ctx->state[4] = 0xC3D2E1F0;
ctx->count[0] = 0; ctx->count[0] = 0;
ctx->count[1] = 0; ctx->count[1] = 0;
} }
static void static void
Sha1Update(Sha1Context *ctx, const void *buf, UInt32 size) Sha1Update(Sha1Context * ctx, const void *buf, UInt32 size)
{ {
UInt32 i, j; UInt32 i, j;
j = (ctx->count[0] >> 3) & 63; j = (ctx->count[0] >> 3) & 63;
if ((ctx->count[0] += size << 3) < (size << 3)) if ((ctx->count[0] += size << 3) < (size << 3))
{ {
ctx->count[1]++; ctx->count[1]++;
} }
ctx->count[1] += (size >> 29); ctx->count[1] += (size >> 29);
if ((j + size) > 63) if ((j + size) > 63)
{ {
i = 64 - j; i = 64 - j;
memcpy(&ctx->buffer[j], buf, i); memcpy(&ctx->buffer[j], buf, i);
Sha1Transform(ctx->state, ctx->buffer); Sha1Transform(ctx->state, ctx->buffer);
for ( ; i + 63 < size; i += 64) for (; i + 63 < size; i += 64)
{ {
Sha1Transform(ctx->state, (UInt8 *) buf + i); Sha1Transform(ctx->state, (UInt8 *) buf + i);
} }
j = 0; j = 0;
} }
else else
{ {
i = 0; i = 0;
} }
memcpy(&ctx->buffer[j], &((UInt8 *)buf)[i], size - i); memcpy(&ctx->buffer[j], &((UInt8 *) buf)[i], size - i);
} }
static void static void
Sha1Calculate(Sha1Context *ctx, unsigned char *out) Sha1Calculate(Sha1Context * ctx, unsigned char *out)
{ {
UInt32 i; UInt32 i;
UInt8 count[8]; UInt8 count[8];
for (i = 0; i < 8; i++) for (i = 0; i < 8; i++)
{ {
count[i] = (unsigned char) ((ctx->count[(i >= 4 ? 0 : 1)] count[i] = (unsigned char) ((ctx->count[(i >= 4 ? 0 : 1)]
>> ((3 - (i & 3)) * 8)) & 255); >> ((3 - (i & 3)) * 8)) & 255);
} }
Sha1Update(ctx, (UInt8 *) "\x80", 1); Sha1Update(ctx, (UInt8 *) "\x80", 1);
while ((ctx->count[0] & 504) != 448) while ((ctx->count[0] & 504) != 448)
{ {
Sha1Update(ctx, (UInt8 *) "\0", 1); Sha1Update(ctx, (UInt8 *) "\0", 1);
} }
Sha1Update(ctx, count, 8); Sha1Update(ctx, count, 8);
for (i = 0; i < (160 / 8); i++) for (i = 0; i < (160 / 8); i++)
{ {
out[i] = (UInt8) ((ctx->state[i >> 2] >> ((3 - (i & 3)) * 8)) & 255); out[i] = (UInt8) ((ctx->state[i >> 2] >> ((3 - (i & 3)) * 8)) & 255);
} }
} }
unsigned char * unsigned char *
Sha1(char *str) Sha1(char *str)
{ {
Sha1Context ctx; Sha1Context ctx;
unsigned char *out; unsigned char *out;
if (!str) if (!str)
{ {
return NULL; return NULL;
} }
out = Malloc(((160 / 8) + 1) * sizeof(unsigned char)); out = Malloc(((160 / 8) + 1) * sizeof(unsigned char));
if (!out) if (!out)
{ {
return NULL; return NULL;
} }
Sha1Init(&ctx); Sha1Init(&ctx);
Sha1Update(&ctx, str, strlen(str)); Sha1Update(&ctx, str, strlen(str));
Sha1Calculate(&ctx, out); Sha1Calculate(&ctx, out);
out[160 / 8] = '\0'; out[160 / 8] = '\0';
return out; return out;
} }

View file

@ -227,7 +227,7 @@ Sha256(char *str)
PUT_UINT32(&out[4 * i], context.state[i]); PUT_UINT32(&out[4 * i], context.state[i]);
} }
out[32] = '\0'; out[32] = '\0';
return out; return out;
} }

View file

@ -32,13 +32,13 @@
#include <Str.h> #include <Str.h>
static HashMap * static HashMap *
ValidateRoomFilter(HashMap *json) ValidateRoomFilter(HashMap * json)
{ {
return NULL; return NULL;
} }
static HashMap * static HashMap *
ValidateEventFields(Array *fields) ValidateEventFields(Array * fields)
{ {
return NULL; return NULL;
} }
@ -50,7 +50,7 @@ ValidateEventFormat(char *fmt)
} }
static HashMap * static HashMap *
ValidateEventFilter(HashMap *json) ValidateEventFilter(HashMap * json)
{ {
JsonValue *val; JsonValue *val;
@ -60,6 +60,7 @@ ValidateEventFilter(HashMap *json)
if (JsonValueType(val) == JSON_INTEGER) if (JsonValueType(val) == JSON_INTEGER)
{ {
long limit = JsonValueAsInteger(val); long limit = JsonValueAsInteger(val);
if (limit <= 0 || limit > 100) if (limit <= 0 || limit > 100)
{ {
return MatrixErrorCreate(M_BAD_JSON); return MatrixErrorCreate(M_BAD_JSON);
@ -75,7 +76,7 @@ ValidateEventFilter(HashMap *json)
} }
HashMap * HashMap *
FilterValidate(HashMap *json) FilterValidate(HashMap * json)
{ {
JsonValue *val; JsonValue *val;
HashMap *response = NULL; HashMap *response = NULL;
@ -109,4 +110,3 @@ FilterValidate(HashMap *json)
finish: finish:
return response; return response;
} }

View file

@ -35,7 +35,7 @@
#include <string.h> #include <string.h>
static char * static char *
GetServerName(Db *db) GetServerName(Db * db)
{ {
char *name; char *name;

View file

@ -35,7 +35,7 @@
#include <Json.h> #include <Json.h>
static void static void
HexDump(size_t off, char *hexBuf , char *asciiBuf, void *args) HexDump(size_t off, char *hexBuf, char *asciiBuf, void *args)
{ {
char *fmt; char *fmt;
@ -90,7 +90,7 @@ TelodendriaMemoryHook(MemoryAction a, MemoryInfo * i, void *args)
MemoryInfoGetFile(i), MemoryInfoGetLine(i), MemoryInfoGetFile(i), MemoryInfoGetLine(i),
action, MemoryInfoGetSize(i), action, MemoryInfoGetSize(i),
MemoryInfoGetPointer(i)); MemoryInfoGetPointer(i));
if (a != MEMORY_ALLOCATE && a != MEMORY_REALLOCATE) if (a != MEMORY_ALLOCATE && a != MEMORY_REALLOCATE)
{ {
MemoryHexDump(i, HexDump, NULL); MemoryHexDump(i, HexDump, NULL);
@ -112,16 +112,16 @@ SignalHandle(int signal)
} }
struct Args struct Args
{ {
Db *db; Db *db;
HttpRouter *router; HttpRouter *router;
HttpServerContext *cx; HttpServerContext *cx;
}; };
static void * static void *
TestFunc(Array *path, void *argp) TestFunc(Array * path, void *argp)
{ {
struct Args *args = argp; struct Args *args = argp;
HttpServerContext *cx = args->cx; HttpServerContext *cx = args->cx;
HashMap *headers = HttpRequestHeaders(cx); HashMap *headers = HttpRequestHeaders(cx);
HttpRequestMethod method = HttpRequestMethodGet(cx); HttpRequestMethod method = HttpRequestMethodGet(cx);
Db *db = args->db; Db *db = args->db;
@ -137,7 +137,7 @@ TestFunc(Array *path, void *argp)
(void) path; (void) path;
Log(LOG_INFO, "%s %s", HttpRequestMethodToString(method), Log(LOG_INFO, "%s %s", HttpRequestMethodToString(method),
HttpRequestPath(cx)); HttpRequestPath(cx));
while (HashMapIterate(headers, &key, (void **) &val)) while (HashMapIterate(headers, &key, (void **) &val))
{ {
@ -160,17 +160,17 @@ TestFunc(Array *path, void *argp)
JsonEncode(DbJson(ref), HttpServerStream(cx), JSON_DEFAULT); JsonEncode(DbJson(ref), HttpServerStream(cx), JSON_DEFAULT);
DbUnlock(db, ref); DbUnlock(db, ref);
return NULL; return NULL;
} }
void void
HttpHandle(HttpServerContext *cx, void *argp) HttpHandle(HttpServerContext * cx, void *argp)
{ {
struct Args *args = argp; struct Args *args = argp;
args->cx = cx; args->cx = cx;
HttpRouterRoute(args->router, HttpRequestPath(cx), args, NULL); HttpRouterRoute(args->router, HttpRequestPath(cx), args, NULL);
} }
int int
@ -179,7 +179,7 @@ Main(void)
struct sigaction sa; struct sigaction sa;
HttpServerConfig cfg; HttpServerConfig cfg;
struct Args args; struct Args args;
LogConfigLevelSet(LogConfigGlobal(), LOG_DEBUG); LogConfigLevelSet(LogConfigGlobal(), LOG_DEBUG);
@ -197,10 +197,10 @@ Main(void)
cfg.handlerArgs = &args; cfg.handlerArgs = &args;
args.db = DbOpen("data", 0); args.db = DbOpen("data", 0);
args.router = HttpRouterCreate(); args.router = HttpRouterCreate();
HttpRouterAdd(args.router, "/test", TestFunc); HttpRouterAdd(args.router, "/test", TestFunc);
Log(LOG_DEBUG, "Creating server..."); Log(LOG_DEBUG, "Creating server...");