Format source code.

This commit is contained in:
Jordan Bancino 2023-06-18 02:53:52 +00:00
parent 4abd0e9c22
commit c03b092536
5 changed files with 184 additions and 123 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;
} }