C99 Compliance (#29)

This pull request brings Cytoplasm up from C89 to C99, which makes it much more portable across platforms. In particular, this pull request solves a number of issues with 32-bit platforms.

Closes #28.
Closes #12.
Closes #20.

Reviewed-on: https://git.telodendria.io/Telodendria/Cytoplasm/pulls/29
This commit is contained in:
Jordan Bancino 2024-01-13 17:13:45 -05:00
parent d0969d0dd7
commit 662696ce12
40 changed files with 384 additions and 1667 deletions

View file

@ -23,7 +23,6 @@
*/
#include <Sha.h>
#include <Memory.h>
#include <Int.h>
#include <string.h>
@ -31,10 +30,10 @@
#define LOAD32H(x, y) \
{ \
x = ((UInt32)((y)[0] & 255) << 24) | \
((UInt32)((y)[1] & 255) << 16) | \
((UInt32)((y)[2] & 255) << 8) | \
((UInt32)((y)[3] & 255)); \
x = ((uint32_t)((y)[0] & 255) << 24) | \
((uint32_t)((y)[1] & 255) << 16) | \
((uint32_t)((y)[2] & 255) << 8) | \
((uint32_t)((y)[3] & 255)); \
}
#define ROL(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
@ -49,22 +48,22 @@
typedef union
{
UInt8 c[64];
UInt32 l[16];
uint8_t c[64];
uint32_t l[16];
} Char64Long16;
typedef struct
{
UInt32 state[5];
UInt32 count[2];
UInt8 buffer[64];
uint32_t state[5];
uint32_t count[2];
uint8_t buffer[64];
} Sha1Context;
static void
Sha1Transform(UInt32 state[5], const UInt8 buffer[64])
Sha1Transform(uint32_t state[5], const uint8_t *buffer)
{
UInt32 a, b, c, d, e, i;
UInt8 workspace[64];
uint32_t a, b, c, d, e, i;
uint8_t workspace[64];
Char64Long16 *block = (Char64Long16 *) workspace;
for (i = 0; i < 16; i++)
@ -180,9 +179,9 @@ Sha1Init(Sha1Context * ctx)
}
static void
Sha1Update(Sha1Context * ctx, const void *buf, UInt32 size)
Sha1Update(Sha1Context * ctx, const void *buf, uint32_t size)
{
UInt32 i, j;
uint32_t i, j;
j = (ctx->count[0] >> 3) & 63;
@ -202,7 +201,7 @@ Sha1Update(Sha1Context * ctx, const void *buf, UInt32 size)
for (; i + 63 < size; i += 64)
{
Sha1Transform(ctx->state, (UInt8 *) buf + i);
Sha1Transform(ctx->state, (uint8_t *) buf + i);
}
j = 0;
@ -212,14 +211,14 @@ Sha1Update(Sha1Context * ctx, const void *buf, UInt32 size)
i = 0;
}
memcpy(&ctx->buffer[j], &((UInt8 *) buf)[i], size - i);
memcpy(&ctx->buffer[j], &((uint8_t *) buf)[i], size - i);
}
static void
Sha1Calculate(Sha1Context * ctx, unsigned char *out)
{
UInt32 i;
UInt8 count[8];
uint32_t i;
uint8_t count[8];
for (i = 0; i < 8; i++)
{
@ -227,16 +226,16 @@ Sha1Calculate(Sha1Context * ctx, unsigned char *out)
>> ((3 - (i & 3)) * 8)) & 255);
}
Sha1Update(ctx, (UInt8 *) "\x80", 1);
Sha1Update(ctx, (uint8_t *) "\x80", 1);
while ((ctx->count[0] & 504) != 448)
{
Sha1Update(ctx, (UInt8 *) "\0", 1);
Sha1Update(ctx, (uint8_t *) "\0", 1);
}
Sha1Update(ctx, count, 8);
for (i = 0; i < (160 / 8); i++)
{
out[i] = (UInt8) ((ctx->state[i >> 2] >> ((3 - (i & 3)) * 8)) & 255);
out[i] = (uint8_t) ((ctx->state[i >> 2] >> ((3 - (i & 3)) * 8)) & 255);
}
}

View file

@ -23,7 +23,6 @@
*/
#include <Sha.h>
#include <Memory.h>
#include <Int.h>
#include <stdio.h>
#include <string.h>
@ -31,10 +30,10 @@
#include <limits.h>
#define GET_UINT32(x) \
(((UInt32)(x)[0] << 24) | \
((UInt32)(x)[1] << 16) | \
((UInt32)(x)[2] << 8) | \
((UInt32)(x)[3]))
(((uint32_t)(x)[0] << 24) | \
((uint32_t)(x)[1] << 16) | \
((uint32_t)(x)[2] << 8) | \
((uint32_t)(x)[3]))
#define PUT_UINT32(dst, x) { \
(dst)[0] = (x) >> 24; \
@ -56,8 +55,8 @@
#define WW(i) (w[i] = w[i - 16] + S0(w[i - 15]) + w[i - 7] + S1(w[i - 2]))
#define ROUND(a, b, c, d, e, f, g, h, k, w) { \
UInt32 tmp0 = h + T0(e) + CH(e, f, g) + k + w; \
UInt32 tmp1 = T1(a) + MAJ(a, b, c); \
uint32_t tmp0 = h + T0(e) + CH(e, f, g) + k + w; \
uint32_t tmp1 = T1(a) + MAJ(a, b, c); \
h = tmp0 + tmp1; \
d += tmp0; \
}
@ -65,7 +64,7 @@
typedef struct Sha256Context
{
size_t length;
UInt32 state[8];
uint32_t state[8];
size_t bufLen;
unsigned char buffer[64];
} Sha256Context;
@ -73,7 +72,7 @@ typedef struct Sha256Context
static void
Sha256Chunk(Sha256Context * context, unsigned char chunk[64])
{
const UInt32 rk[64] = {
const uint32_t rk[64] = {
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1,
0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786,
@ -87,8 +86,8 @@ Sha256Chunk(Sha256Context * context, unsigned char chunk[64])
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
};
UInt32 w[64];
UInt32 a, b, c, d, e, f, g, h;
uint32_t w[64];
uint32_t a, b, c, d, e, f, g, h;
int i;
@ -178,10 +177,10 @@ Sha256(char *str)
unsigned char *out;
unsigned char fill[64];
UInt32 fillLen;
uint32_t fillLen;
unsigned char buf[8];
UInt32 hiLen;
UInt32 loLen;
uint32_t hiLen;
uint32_t loLen;
if (!str)
{
@ -213,8 +212,8 @@ Sha256(char *str)
fill[0] = 0x80;
fillLen = (context.bufLen < 56) ? 56 - context.bufLen : 120 - context.bufLen;
hiLen = (UInt32) (context.length >> 29);
loLen = (UInt32) (context.length << 3);
hiLen = (uint32_t) (context.length >> 29);
loLen = (uint32_t) (context.length << 3);
PUT_UINT32(&buf[0], hiLen);
PUT_UINT32(&buf[4], loLen);