Compare commits

...

34 commits

Author SHA1 Message Date
8987802437 Merge pull request 'Fix LMDB use-after-free' (#53) from lda/Cytoplasm:fix-deadlock into master
Some checks failed
Compile Cytoplasm / Compile Cytoplasm (x86, alpine-v3.19) (push) Has been cancelled
Compile Cytoplasm / Compile Cytoplasm (x86, debian-v12.4) (push) Has been cancelled
Compile Cytoplasm / Compile Cytoplasm (x86, freebsd-v14.0) (push) Has been cancelled
Compile Cytoplasm / Compile Cytoplasm (x86, netbsd-v9.3) (push) Has been cancelled
Compile Cytoplasm / Compile Cytoplasm (x86_64, alpine-v3.19) (push) Has been cancelled
Compile Cytoplasm / Compile Cytoplasm (x86_64, debian-v12.4) (push) Has been cancelled
Compile Cytoplasm / Compile Cytoplasm (x86_64, freebsd-v14.0) (push) Has been cancelled
Compile Cytoplasm / Compile Cytoplasm (x86_64, netbsd-v9.3) (push) Has been cancelled
Compile Cytoplasm / Compile Cytoplasm (x86_64, openbsd-v7.4) (push) Has been cancelled
Reviewed-on: Telodendria/Cytoplasm#53
2024-09-13 20:11:12 -04:00
LDA
9fed42d2ac [FIX] Fix LMDB use-after-free 2024-09-09 13:05:30 +02:00
9c6781c458 Merge pull request 'Fix j2s generation issue' (#52) from lda/Cytoplasm:master into master
Some checks failed
Compile Cytoplasm / Compile Cytoplasm (x86, alpine-v3.19) (push) Has been cancelled
Compile Cytoplasm / Compile Cytoplasm (x86, debian-v12.4) (push) Has been cancelled
Compile Cytoplasm / Compile Cytoplasm (x86, freebsd-v14.0) (push) Has been cancelled
Compile Cytoplasm / Compile Cytoplasm (x86, netbsd-v9.3) (push) Has been cancelled
Compile Cytoplasm / Compile Cytoplasm (x86_64, alpine-v3.19) (push) Has been cancelled
Compile Cytoplasm / Compile Cytoplasm (x86_64, debian-v12.4) (push) Has been cancelled
Compile Cytoplasm / Compile Cytoplasm (x86_64, freebsd-v14.0) (push) Has been cancelled
Compile Cytoplasm / Compile Cytoplasm (x86_64, netbsd-v9.3) (push) Has been cancelled
Compile Cytoplasm / Compile Cytoplasm (x86_64, openbsd-v7.4) (push) Has been cancelled
Reviewed-on: Telodendria/Cytoplasm#52
2024-09-02 14:38:02 -04:00
LDA
33139510b9 [FIX] Fix j2s generation issue
Aie...
2024-08-27 18:50:56 +02:00
4831f2e03d Merge pull request 'Add key->[known structure] schemas to j2s' (#50) from lda/Cytoplasm:master into master
Some checks failed
Compile Cytoplasm / Compile Cytoplasm (x86, alpine-v3.19) (push) Has been cancelled
Compile Cytoplasm / Compile Cytoplasm (x86, debian-v12.4) (push) Has been cancelled
Compile Cytoplasm / Compile Cytoplasm (x86, freebsd-v14.0) (push) Has been cancelled
Compile Cytoplasm / Compile Cytoplasm (x86, netbsd-v9.3) (push) Has been cancelled
Compile Cytoplasm / Compile Cytoplasm (x86_64, alpine-v3.19) (push) Has been cancelled
Compile Cytoplasm / Compile Cytoplasm (x86_64, debian-v12.4) (push) Has been cancelled
Compile Cytoplasm / Compile Cytoplasm (x86_64, freebsd-v14.0) (push) Has been cancelled
Compile Cytoplasm / Compile Cytoplasm (x86_64, netbsd-v9.3) (push) Has been cancelled
Compile Cytoplasm / Compile Cytoplasm (x86_64, openbsd-v7.4) (push) Has been cancelled
Reviewed-on: Telodendria/Cytoplasm#50
2024-08-27 12:34:49 -04:00
lda
cc665ac7fc Merge branch 'master' into master 2024-08-27 12:25:28 -04:00
a121793795 Merge pull request 'Fix out the configure for the AUR' (#51) from lda/Cytoplasm:fix-configure into master
Some checks are pending
Compile Cytoplasm / Compile Cytoplasm (x86, alpine-v3.19) (push) Waiting to run
Compile Cytoplasm / Compile Cytoplasm (x86, debian-v12.4) (push) Waiting to run
Compile Cytoplasm / Compile Cytoplasm (x86, freebsd-v14.0) (push) Waiting to run
Compile Cytoplasm / Compile Cytoplasm (x86, netbsd-v9.3) (push) Waiting to run
Compile Cytoplasm / Compile Cytoplasm (x86_64, alpine-v3.19) (push) Waiting to run
Compile Cytoplasm / Compile Cytoplasm (x86_64, debian-v12.4) (push) Waiting to run
Compile Cytoplasm / Compile Cytoplasm (x86_64, freebsd-v14.0) (push) Waiting to run
Compile Cytoplasm / Compile Cytoplasm (x86_64, netbsd-v9.3) (push) Waiting to run
Compile Cytoplasm / Compile Cytoplasm (x86_64, openbsd-v7.4) (push) Waiting to run
Reviewed-on: Telodendria/Cytoplasm#51
2024-08-27 09:44:49 -04:00
9e1026d893 Merge pull request 'Add raw SHA inputs and hashtypes to ShaToHex' (#47) from lda/Cytoplasm:sha-revamp into master
Some checks are pending
Compile Cytoplasm / Compile Cytoplasm (x86, alpine-v3.19) (push) Waiting to run
Compile Cytoplasm / Compile Cytoplasm (x86, debian-v12.4) (push) Waiting to run
Compile Cytoplasm / Compile Cytoplasm (x86, freebsd-v14.0) (push) Waiting to run
Compile Cytoplasm / Compile Cytoplasm (x86, netbsd-v9.3) (push) Waiting to run
Compile Cytoplasm / Compile Cytoplasm (x86_64, alpine-v3.19) (push) Waiting to run
Compile Cytoplasm / Compile Cytoplasm (x86_64, debian-v12.4) (push) Waiting to run
Compile Cytoplasm / Compile Cytoplasm (x86_64, freebsd-v14.0) (push) Waiting to run
Compile Cytoplasm / Compile Cytoplasm (x86_64, netbsd-v9.3) (push) Waiting to run
Compile Cytoplasm / Compile Cytoplasm (x86_64, openbsd-v7.4) (push) Waiting to run
Reviewed-on: Telodendria/Cytoplasm#47
2024-08-27 09:38:26 -04:00
LDA
5889bec95f [MOD] Use macro to determine if it is delimited 2024-08-27 11:24:18 +02:00
lda
d0b5c441dd [FIX] Fix out the configure 2024-08-26 21:28:44 +02:00
LDA
d3379d8157 [ADD] {Field}s in j2s 2024-08-26 15:38:20 +02:00
LDA
5df458f568 [FIX] Fix out define hjinks 2024-08-26 15:28:30 +02:00
LDA
adb7322823 [ADD] Raw SHA inputs, hashtypes to ShaToHex 2024-08-25 21:40:23 +02:00
f5ce4f5238 Merge pull request 'Start optionally using the SHA implementation from the existing crypto API' (#44) from lda/Cytoplasm:opt-ssl-for-sha into master
Some checks failed
Compile Cytoplasm / Compile Cytoplasm (x86, alpine-v3.19) (push) Has been cancelled
Compile Cytoplasm / Compile Cytoplasm (x86, debian-v12.4) (push) Has been cancelled
Compile Cytoplasm / Compile Cytoplasm (x86, freebsd-v14.0) (push) Has been cancelled
Compile Cytoplasm / Compile Cytoplasm (x86, netbsd-v9.3) (push) Has been cancelled
Compile Cytoplasm / Compile Cytoplasm (x86_64, alpine-v3.19) (push) Has been cancelled
Compile Cytoplasm / Compile Cytoplasm (x86_64, debian-v12.4) (push) Has been cancelled
Compile Cytoplasm / Compile Cytoplasm (x86_64, freebsd-v14.0) (push) Has been cancelled
Compile Cytoplasm / Compile Cytoplasm (x86_64, netbsd-v9.3) (push) Has been cancelled
Compile Cytoplasm / Compile Cytoplasm (x86_64, openbsd-v7.4) (push) Has been cancelled
Reviewed-on: Telodendria/Cytoplasm#44
2024-08-24 13:06:04 -04:00
LDA
7752ea7b86 [MOD] Drop LibreSSL "support" in SHA for now
We'll test later...
2024-08-23 22:45:24 +02:00
LDA
d00dcc9b50 Merge branch 'master' of https://git.telodendria.io/Telodendria/Cytoplasm into opt-ssl-for-sha 2024-08-23 22:44:33 +02:00
494be7b4dc Merge pull request 'Fast memory allocation' (#36) from lda/Cytoplasm:mem-moment into master
Some checks are pending
Compile Cytoplasm / Compile Cytoplasm (x86, alpine-v3.19) (push) Waiting to run
Compile Cytoplasm / Compile Cytoplasm (x86, debian-v12.4) (push) Waiting to run
Compile Cytoplasm / Compile Cytoplasm (x86, freebsd-v14.0) (push) Waiting to run
Compile Cytoplasm / Compile Cytoplasm (x86, netbsd-v9.3) (push) Waiting to run
Compile Cytoplasm / Compile Cytoplasm (x86_64, alpine-v3.19) (push) Waiting to run
Compile Cytoplasm / Compile Cytoplasm (x86_64, debian-v12.4) (push) Waiting to run
Compile Cytoplasm / Compile Cytoplasm (x86_64, freebsd-v14.0) (push) Waiting to run
Compile Cytoplasm / Compile Cytoplasm (x86_64, netbsd-v9.3) (push) Waiting to run
Compile Cytoplasm / Compile Cytoplasm (x86_64, openbsd-v7.4) (push) Waiting to run
Reviewed-on: Telodendria/Cytoplasm#36
2024-08-23 16:26:12 -04:00
b6b915530c Merge pull request 'Getting optional LMDB support into Cytoplasm' (#43) from lda/Cytoplasm:lmdbwerk into master
Some checks are pending
Compile Cytoplasm / Compile Cytoplasm (x86, alpine-v3.19) (push) Waiting to run
Compile Cytoplasm / Compile Cytoplasm (x86, debian-v12.4) (push) Waiting to run
Compile Cytoplasm / Compile Cytoplasm (x86, freebsd-v14.0) (push) Waiting to run
Compile Cytoplasm / Compile Cytoplasm (x86, netbsd-v9.3) (push) Waiting to run
Compile Cytoplasm / Compile Cytoplasm (x86_64, alpine-v3.19) (push) Waiting to run
Compile Cytoplasm / Compile Cytoplasm (x86_64, debian-v12.4) (push) Waiting to run
Compile Cytoplasm / Compile Cytoplasm (x86_64, freebsd-v14.0) (push) Waiting to run
Compile Cytoplasm / Compile Cytoplasm (x86_64, netbsd-v9.3) (push) Waiting to run
Compile Cytoplasm / Compile Cytoplasm (x86_64, openbsd-v7.4) (push) Waiting to run
Reviewed-on: Telodendria/Cytoplasm#43
2024-08-23 16:19:59 -04:00
56257fb3da Merge pull request 'remove use of install in Makefile' (#46) from Levitating/Cytoplasm:remove-install into master
Some checks are pending
Compile Cytoplasm / Compile Cytoplasm (x86, alpine-v3.19) (push) Waiting to run
Compile Cytoplasm / Compile Cytoplasm (x86, debian-v12.4) (push) Waiting to run
Compile Cytoplasm / Compile Cytoplasm (x86, freebsd-v14.0) (push) Waiting to run
Compile Cytoplasm / Compile Cytoplasm (x86, netbsd-v9.3) (push) Waiting to run
Compile Cytoplasm / Compile Cytoplasm (x86_64, alpine-v3.19) (push) Waiting to run
Compile Cytoplasm / Compile Cytoplasm (x86_64, debian-v12.4) (push) Waiting to run
Compile Cytoplasm / Compile Cytoplasm (x86_64, freebsd-v14.0) (push) Waiting to run
Compile Cytoplasm / Compile Cytoplasm (x86_64, netbsd-v9.3) (push) Waiting to run
Compile Cytoplasm / Compile Cytoplasm (x86_64, openbsd-v7.4) (push) Waiting to run
Reviewed-on: Telodendria/Cytoplasm#46
2024-08-23 16:15:00 -04:00
1d0eb9d49a remove use of install in Makefile
not posix
2024-08-22 03:49:43 +02:00
39c25e5a17 Merge pull request 'Copy filename into preallocated field with Cytoplasm's Memory API' (#33) from lda/Cytoplasm:direct-filenames into master
Some checks failed
Compile Cytoplasm / Compile Cytoplasm (x86, alpine-v3.19) (push) Has been cancelled
Compile Cytoplasm / Compile Cytoplasm (x86, debian-v12.4) (push) Has been cancelled
Compile Cytoplasm / Compile Cytoplasm (x86, freebsd-v14.0) (push) Has been cancelled
Compile Cytoplasm / Compile Cytoplasm (x86, netbsd-v9.3) (push) Has been cancelled
Compile Cytoplasm / Compile Cytoplasm (x86_64, alpine-v3.19) (push) Has been cancelled
Compile Cytoplasm / Compile Cytoplasm (x86_64, debian-v12.4) (push) Has been cancelled
Compile Cytoplasm / Compile Cytoplasm (x86_64, freebsd-v14.0) (push) Has been cancelled
Compile Cytoplasm / Compile Cytoplasm (x86_64, netbsd-v9.3) (push) Has been cancelled
Compile Cytoplasm / Compile Cytoplasm (x86_64, openbsd-v7.4) (push) Has been cancelled
Reviewed-on: Telodendria/Cytoplasm#33
2024-08-18 19:26:18 -04:00
lda
c7204f316c Merge branch 'master' into opt-ssl-for-sha 2024-08-16 12:33:11 -04:00
LDA
e8543bdb2a [FIX/WIP] Don't include Log. 2024-08-16 18:26:19 +02:00
LDA
3843a8d114 [MOD/WIP] Get SHA to use Open/LibreSSL if present
I still haven't tested on LibreSSL (Debian doesn't seem to actually like
it all that much), but manuals seems to state that they're the same in
that regard. If anyone is up to verify, let me know so that I'm aware
it's safe to merge it.
2024-08-16 18:22:53 +02:00
LDA
0d122976d4 Merge branch 'master' of https://git.telodendria.io/Telodendria/Cytoplasm into mem-moment 2024-07-18 13:20:17 +02:00
LDA
402d73c866 Merge branch 'master' of https://git.telodendria.io/Telodendria/Cytoplasm into mem-moment 2024-06-27 16:43:38 +02:00
LDA
bec672c92c [MOD] Use a different constant 2024-06-19 17:51:13 +02:00
LDA
8b2bdbe220 [FIX] Actually do proper stringification.
It just kept bothering me.
2024-06-15 13:48:39 +02:00
LDA
1ad6f0d976 Merge branch 'mem-moment' of https://git.telodendria.io/lda/Cytoplasm into mem-moment 2024-06-15 13:44:13 +02:00
LDA
9f706102c4 Merge branch 'master' of https://git.telodendria.io/Telodendria/Cytoplasm into mem-moment 2024-06-15 13:43:45 +02:00
lda
5003ddc281 Merge branch 'master' of https://git.telodendria.io/Telodendria/Cytoplasm into mem-moment 2024-06-08 12:08:30 +02:00
lda
007b8f6d43 [MOD/WIP] Blazing-fast memory allocator 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀
This still however removes `MemoryIterate' from allocations, because
it's a real performance pickle. From my tests parsing large sync
replies, this commit is near instant(pv reports 23MiB/s on a 2MB sync).

Still need a good compromise along MemoryIterate(like maybe find out a
clever way to only make it run on a small subset, or maybe just randomly
run it from time to time, or maybe just roll without it except on some
soft of debug mode????)
2024-06-03 16:18:29 +02:00
lda
138ea1c8e9 [FIX] Oops. 2024-05-26 22:06:56 +02:00
lda
cdf4430a9e [FIX] Fix issue with const strings which may be invalidated 2024-05-26 22:01:06 +02:00
10 changed files with 445 additions and 166 deletions

6
configure vendored
View file

@ -269,8 +269,10 @@ ${TAB}done
${LIB_NAME}: ${OUT}/lib/lib${LIB_NAME}.a ${OUT}/lib/lib${LIB_NAME}.so
install: ${LIB_NAME}
${TAB}install -D ${OUT}/lib/lib${LIB_NAME}.a \$(PREFIX)/lib/lib${LIB_NAME}.a
${TAB}install -D ${OUT}/lib/lib${LIB_NAME}.so \$(PREFIX)/lib/lib${LIB_NAME}.so
${TAB}mkdir -p \$(PREFIX)/${OUT}/lib
${TAB}mkdir -p \$(PREFIX)/lib
${TAB}cp ${OUT}/lib/lib${LIB_NAME}.a \$(PREFIX)/lib/lib${LIB_NAME}.a
${TAB}cp ${OUT}/lib/lib${LIB_NAME}.so \$(PREFIX)/lib/lib${LIB_NAME}.so
$(collect ${INCLUDE}/ '' '' \$\(PREFIX\)/include/${LIB_NAME}/ install_out)
$(collect ${INCLUDE}/ .h .3 \$\(PREFIX\)/man/man3/${LIB_NAME}- install_man)
$(collect ${TOOLS}/ '.c' '' \$\(PREFIX\)/bin/ install_tool)

View file

@ -41,6 +41,16 @@
* due to the lack of a 64-bit integer type, so that hash
* function has been omitted.
*/
#include <stddef.h>
/**
* This is an enum to be used to identify the type of SHA used.
*/
typedef enum HashType
{
HASH_SHA1,
HASH_SHA256
} HashType;
/**
* This function takes a pointer to a NULL-terminated C string, and
@ -64,6 +74,20 @@ extern unsigned char * Sha256(char *);
*/
extern unsigned char * Sha1(char *);
/**
* This function behaves just like
* .Fn Sha256 ,
* except that it allows for a generic byte array, instead of a string.
*/
extern unsigned char * Sha256Raw(unsigned char *, size_t);
/**
* This function behaves just like
* .Fn Sha1 ,
* except that it allows for a generic byte array, instead of a string.
*/
extern unsigned char * Sha1Raw(unsigned char *, size_t);
/**
* Convert a SHA byte buffer into a hex string. These hex strings
* are typically what is transmitted, stored, and compared, however
@ -71,6 +95,6 @@ extern unsigned char * Sha1(char *);
* bytes directly, which is why the conversion to a hex string is
* a separate step.
*/
extern char * ShaToHex(unsigned char *);
extern char * ShaToHex(unsigned char *, HashType);
#endif /* CYTOPLASM_SHA_H */

View file

@ -293,6 +293,7 @@ LMDBUnlock(Db *d, DbRef *r)
Stream *stream;
MDB_val key, val;
bool ret = true;
DbHint hint = r ? r->hint : 0;
if (!d || !r)
{
@ -324,7 +325,7 @@ LMDBUnlock(Db *d, DbRef *r)
{
free(val.mv_data);
}
if (ret && r->hint == DB_HINT_WRITE)
if (ret && hint == DB_HINT_WRITE)
{
pthread_mutex_unlock(&d->lock);
}

View file

@ -40,16 +40,24 @@
#define MEMORY_HEXDUMP_WIDTH 16
#endif
#define MEMORY_FILE_SIZE 256
struct MemoryInfo
{
uint64_t magic;
size_t size;
const char *file;
char file[MEMORY_FILE_SIZE];
int line;
void *pointer;
MemoryInfo *prev;
MemoryInfo *next;
};
#define MEM_BOUND_TYPE uint32_t
#define MEM_BOUND 0xDEADBEEF
#define MEM_MAGIC 0xDEADBEEFDEADBEEF
#define MEM_BOUND_LOWER(p) *((MEM_BOUND_TYPE *) p)
#define MEM_BOUND_UPPER(p, x) *(((MEM_BOUND_TYPE *) (((uint8_t *) p) + x)) + 1)
@ -59,10 +67,10 @@ static pthread_mutex_t lock;
static void (*hook) (MemoryAction, MemoryInfo *, void *) = MemoryDefaultHook;
static void *hookArgs = NULL;
static MemoryInfo **allocations = NULL;
static size_t allocationsSize = 0;
static size_t allocationsLen = 0;
static MemoryInfo *allocationTail = NULL;
int
MemoryRuntimeInit(void)
{
@ -91,70 +99,18 @@ MemoryRuntimeDestroy(void)
return pthread_mutex_destroy(&lock) == 0;
}
static size_t
MemoryHash(void *p)
{
return (((size_t) p) >> 2 * 7) % allocationsSize;
}
static int
MemoryInsert(MemoryInfo * a)
{
size_t hash;
if (!allocations)
if (allocationTail)
{
allocationsSize = MEMORY_TABLE_CHUNK;
allocations = calloc(allocationsSize, sizeof(void *));
if (!allocations)
{
return 0;
}
allocationTail->next = a;
}
a->next = NULL;
a->prev = allocationTail;
a->magic = MEM_MAGIC;
/* If the next insertion would cause the table to be at least 3/4
* full, re-allocate and re-hash. */
if ((allocationsLen + 1) >= ((allocationsSize * 3) >> 2))
{
size_t i;
size_t tmpAllocationsSize = allocationsSize;
MemoryInfo **tmpAllocations;
allocationsSize += MEMORY_TABLE_CHUNK;
tmpAllocations = calloc(allocationsSize, sizeof(void *));
if (!tmpAllocations)
{
return 0;
}
for (i = 0; i < tmpAllocationsSize; i++)
{
if (allocations[i])
{
hash = MemoryHash(allocations[i]->pointer);
while (tmpAllocations[hash])
{
hash = (hash + 1) % allocationsSize;
}
tmpAllocations[hash] = allocations[i];
}
}
free(allocations);
allocations = tmpAllocations;
}
hash = MemoryHash(a->pointer);
while (allocations[hash])
{
hash = (hash + 1) % allocationsSize;
}
allocations[hash] = a;
allocationTail = a;
allocationsLen++;
return 1;
@ -163,23 +119,24 @@ MemoryInsert(MemoryInfo * a)
static void
MemoryDelete(MemoryInfo * a)
{
size_t hash = MemoryHash(a->pointer);
size_t count = 0;
MemoryInfo *aPrev = a->prev;
MemoryInfo *aNext = a->next;
while (count <= allocationsSize)
if (aPrev)
{
if (allocations[hash] && allocations[hash] == a)
{
allocations[hash] = NULL;
allocationsLen--;
return;
}
else
{
hash = (hash + 1) % allocationsSize;
count++;
}
aPrev->next = aNext;
}
if (aNext)
{
aNext->prev = aPrev;
}
if (a == allocationTail)
{
allocationTail = aPrev;
}
a->magic = ~MEM_MAGIC;
}
static int
@ -203,38 +160,31 @@ MemoryAllocate(size_t size, const char *file, int line)
void *p;
MemoryInfo *a;
MemoryIterate(NULL, NULL);
//MemoryIterate(NULL, NULL);
pthread_mutex_lock(&lock);
a = malloc(sizeof(MemoryInfo));
a = malloc(sizeof(MemoryInfo) + MEM_SIZE_ACTUAL(size));
if (!a)
{
pthread_mutex_unlock(&lock);
return NULL;
}
p = malloc(MEM_SIZE_ACTUAL(size));
if (!p)
{
free(a);
pthread_mutex_unlock(&lock);
return NULL;
}
p = a + 1;
memset(p, 0, MEM_SIZE_ACTUAL(size));
MEM_BOUND_LOWER(p) = MEM_BOUND;
MEM_BOUND_UPPER(p, size) = MEM_BOUND;
a->size = MEM_SIZE_ACTUAL(size);
a->file = file;
strncpy(a->file, file, MEMORY_FILE_SIZE - 1);
a->line = line;
a->pointer = p;
if (!MemoryInsert(a))
{
free(a);
free(p);
pthread_mutex_unlock(&lock);
return NULL;
}
@ -254,7 +204,7 @@ MemoryReallocate(void *p, size_t size, const char *file, int line)
MemoryInfo *a;
void *new = NULL;
MemoryIterate(NULL, NULL);
//MemoryIterate(NULL, NULL);
if (!p)
{
@ -265,15 +215,17 @@ MemoryReallocate(void *p, size_t size, const char *file, int line)
if (a)
{
pthread_mutex_lock(&lock);
new = realloc(a->pointer, MEM_SIZE_ACTUAL(size));
MemoryDelete(a);
new = realloc(a, sizeof(MemoryInfo) + MEM_SIZE_ACTUAL(size));
if (new)
{
MemoryDelete(a);
a = new;
a->size = MEM_SIZE_ACTUAL(size);
a->file = file;
strncpy(a->file, file, MEMORY_FILE_SIZE - 1);
a->line = line;
a->magic = MEM_MAGIC;
a->pointer = new;
a->pointer = a + 1;
MemoryInsert(a);
MEM_BOUND_LOWER(a->pointer) = MEM_BOUND;
@ -283,7 +235,7 @@ MemoryReallocate(void *p, size_t size, const char *file, int line)
{
hook(MEMORY_REALLOCATE, a, hookArgs);
}
}
pthread_mutex_unlock(&lock);
}
@ -293,7 +245,7 @@ MemoryReallocate(void *p, size_t size, const char *file, int line)
if (a)
{
a->size = 0;
a->file = file;
strncpy(a->file, file, MEMORY_FILE_SIZE - 1);
a->line = line;
a->pointer = p;
hook(MEMORY_BAD_POINTER, a, hookArgs);
@ -301,7 +253,7 @@ MemoryReallocate(void *p, size_t size, const char *file, int line)
}
}
return ((MEM_BOUND_TYPE *) new) + 1;
return ((MEM_BOUND_TYPE *) a->pointer) + 1;
}
void
@ -309,7 +261,7 @@ MemoryFree(void *p, const char *file, int line)
{
MemoryInfo *a;
MemoryIterate(NULL, NULL);
//MemoryIterate(NULL, NULL);
if (!p)
{
@ -322,12 +274,11 @@ MemoryFree(void *p, const char *file, int line)
pthread_mutex_lock(&lock);
if (hook)
{
a->file = file;
strncpy(a->file, file, MEMORY_FILE_SIZE - 1);
a->line = line;
hook(MEMORY_FREE, a, hookArgs);
}
MemoryDelete(a);
free(a->pointer);
free(a);
pthread_mutex_unlock(&lock);
@ -337,7 +288,7 @@ MemoryFree(void *p, const char *file, int line)
a = malloc(sizeof(MemoryInfo));
if (a)
{
a->file = file;
strncpy(a->file, file, MEMORY_FILE_SIZE - 1);
a->line = line;
a->size = 0;
a->pointer = p;
@ -350,17 +301,15 @@ MemoryFree(void *p, const char *file, int line)
size_t
MemoryAllocated(void)
{
size_t i;
size_t total = 0;
MemoryInfo *cur;
pthread_mutex_lock(&lock);
for (i = 0; i < allocationsSize; i++)
/* TODO */
for (cur = allocationTail; cur; cur = cur->prev)
{
if (allocations[i])
{
total += MemoryInfoGetSize(allocations[i]);
}
total += MemoryInfoGetSize(cur);
}
pthread_mutex_unlock(&lock);
@ -371,55 +320,40 @@ MemoryAllocated(void)
void
MemoryFreeAll(void)
{
size_t i;
MemoryInfo *cur;
MemoryInfo *prev;
pthread_mutex_lock(&lock);
for (i = 0; i < allocationsSize; i++)
/* TODO */
for (cur = allocationTail; cur; cur = prev)
{
if (allocations[i])
{
free(allocations[i]->pointer);
free(allocations[i]);
}
prev = cur->prev;
free(cur);
}
free(allocations);
allocations = NULL;
allocationsSize = 0;
allocationsLen = 0;
pthread_mutex_unlock(&lock);
}
MemoryInfo *
MemoryInfoGet(void *p)
MemoryInfoGet(void *po)
{
size_t hash, count;
void *p = po;
pthread_mutex_lock(&lock);
p = ((MEM_BOUND_TYPE *) p) - 1;
hash = MemoryHash(p);
p = ((MEM_BOUND_TYPE *) po) - 1;
p = ((MemoryInfo *) p) - 1;
count = 0;
while (count <= allocationsSize)
if (((MemoryInfo *)p)->magic != MEM_MAGIC)
{
if (!allocations[hash] || allocations[hash]->pointer != p)
{
hash = (hash + 1) % allocationsSize;
count++;
}
else
{
MemoryCheck(allocations[hash]);
pthread_mutex_unlock(&lock);
return allocations[hash];
}
p = NULL;
}
pthread_mutex_unlock(&lock);
return NULL;
return p;
}
size_t
@ -467,21 +401,18 @@ MemoryInfoGetPointer(MemoryInfo * a)
}
void
MemoryIterate(void (*iterFunc) (MemoryInfo *, void *), void *args)
MemoryIterate(void (*iterFunc) (MemoryInfo *, void *), void *args)
{
size_t i;
MemoryInfo *cur;
pthread_mutex_lock(&lock);
for (i = 0; i < allocationsSize; i++)
for (cur = allocationTail; cur; cur = cur->prev)
{
if (allocations[i])
MemoryCheck(cur);
if (iterFunc)
{
MemoryCheck(allocations[i]);
if (iterFunc)
{
iterFunc(allocations[i], args);
}
iterFunc(cur, args);
}
}

View file

@ -28,21 +28,44 @@
#include <string.h>
char *
ShaToHex(unsigned char *bytes)
ShaToHex(unsigned char *bytes, HashType type)
{
size_t i = 0;
char *str = Malloc(((strlen((char *) bytes) * 2) + 1) * sizeof(char));
size_t i = 0, size;
char *str;
switch (type)
{
case HASH_SHA1:
size = 20;
break;
case HASH_SHA256:
size = 32;
break;
default:
return NULL;
}
str = Malloc(((size * 2) + 1) * sizeof(char));
if (!str)
{
return NULL;
}
while (bytes[i] != '\0')
for (i = 0; i < size; i++)
{
snprintf(str + (2 * i), 3, "%02x", bytes[i]);
i++;
}
return str;
}
unsigned char *
Sha256(char *str)
{
return Sha256Raw((unsigned char *) str, str ? strlen(str) : 0);
}
unsigned char *
Sha1(char *str)
{
return Sha1Raw((unsigned char *) str, str ? strlen(str) : 0);
}

View file

@ -28,6 +28,27 @@
#include <limits.h>
/* TODO: Verify LibreSSL support later */
#if defined(TLS_IMPL) && (TLS_IMPL == TLS_OPENSSL)
#include <openssl/sha.h>
unsigned char *
Sha1Raw(unsigned char *str, size_t len)
{
unsigned char *digest;
if (!str)
{
return NULL;
}
digest = Malloc(20 + 1);
SHA1(str, len, digest);
digest[20] = '\0';
return digest;
}
#else
#define LOAD32H(x, y) \
{ \
x = ((uint32_t)((y)[0] & 255) << 24) | \
@ -240,7 +261,7 @@ Sha1Calculate(Sha1Context * ctx, unsigned char *out)
}
unsigned char *
Sha1(char *str)
Sha1Raw(unsigned char *str, size_t len)
{
Sha1Context ctx;
unsigned char *out;
@ -257,10 +278,11 @@ Sha1(char *str)
}
Sha1Init(&ctx);
Sha1Update(&ctx, str, strlen(str));
Sha1Update(&ctx, str, len);
Sha1Calculate(&ctx, out);
out[160 / 8] = '\0';
return out;
}
#endif

View file

@ -21,14 +21,36 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <Sha.h>
#include <Memory.h>
#include <Sha.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>
/* TODO: Verify LibreSSL support later */
#if defined(TLS_IMPL) && (TLS_IMPL == TLS_OPENSSL)
#include <openssl/sha.h>
unsigned char *
Sha256Raw(unsigned char *str, size_t len)
{
unsigned char *digest;
if (!str)
{
return NULL;
}
digest = Malloc(32 + 1);
SHA256(str, len, digest);
digest[32] = '\0';
return digest;
}
#else
#define GET_UINT32(x) \
(((uint32_t)(x)[0] << 24) | \
((uint32_t)(x)[1] << 16) | \
@ -170,7 +192,7 @@ Sha256Process(Sha256Context * context, unsigned char *data, size_t length)
}
unsigned char *
Sha256(char *str)
Sha256Raw(unsigned char *str, size_t len)
{
Sha256Context context;
size_t i;
@ -206,7 +228,7 @@ Sha256(char *str)
context.length = 0;
memset(context.buffer, 0, 64);
Sha256Process(&context, (unsigned char *) str, strlen(str));
Sha256Process(&context, str, len);
memset(fill, 0, 64);
fill[0] = 0x80;
@ -230,3 +252,4 @@ Sha256(char *str)
return out;
}
#endif

View file

@ -23,7 +23,7 @@
*/
#include <Tls.h>
#if TLS_IMPL == TLS_LIBRESSL
#if defined(TLS_IMPL) && (TLS_IMPL == TLS_LIBRESSL)
#include <Memory.h>
#include <Log.h>

View file

@ -23,7 +23,7 @@
*/
#include <Tls.h>
#if TLS_IMPL == TLS_OPENSSL
#if defined(TLS_IMPL) && (TLS_IMPL == TLS_OPENSSL)
#include <Memory.h>
#include <Log.h>

View file

@ -38,6 +38,7 @@
#define MAX_DEPENDENCIES 32
#define IsDelimited(field, c1, c2) (*field == c1 && field[strlen(field) - 1] == c2)
static char *
Trim(char c, char *str)
{
@ -75,10 +76,14 @@ TypeToJsonType(char *type)
}
else
{
if (*type == '[' && type[strlen(type) - 1] == ']')
if (IsDelimited(type, '[', ']'))
{
return JSON_ARRAY;
}
else if (IsDelimited(type, '{', '}'))
{
return JSON_OBJECT;
}
else
{
return JSON_OBJECT;
@ -91,7 +96,7 @@ JsonTypeToStr(JsonType type)
{
switch (type)
{
case JSON_OBJECT:
case JSON_OBJECT:
return "JSON_OBJECT";
case JSON_ARRAY:
return "JSON_ARRAY";
@ -325,6 +330,7 @@ Main(Array * args)
{
char *fieldType;
bool isArrType = false;
bool isObjType = false;
JsonValue *requiredVal;
JsonValue *ignoreVal;
@ -343,12 +349,18 @@ Main(Array * args)
goto finish;
}
if (*fieldType == '[' && fieldType[strlen(fieldType) - 1] == ']')
if (IsDelimited(fieldType, '[', ']'))
{
fieldType++;
fieldType[strlen(fieldType) - 1] = '\0';
isArrType = true;
}
else if (IsDelimited(fieldType, '{', '}'))
{
fieldType++;
fieldType[strlen(fieldType) - 1] = '\0';
isObjType = true;
}
if (!StrEquals(fieldType, "object") &&
!StrEquals(fieldType, "array") &&
@ -374,6 +386,10 @@ Main(Array * args)
{
fieldType[strlen(fieldType)] = ']';
}
else if (isObjType)
{
fieldType[strlen(fieldType)] = '}';
}
requiredVal = HashMapGet(fieldObj, "required");
if (requiredVal && JsonValueType(requiredVal) != JSON_BOOLEAN)
@ -529,11 +545,13 @@ Main(Array * args)
{
cType = "double";
}
else if (StrEquals(fieldType, "object"))
else if (StrEquals(fieldType, "object") ||
IsDelimited(fieldType, '{', '}'))
{
cType = "HashMap *";
}
else if (StrEquals(fieldType, "array") || (*fieldType == '[' && fieldType[strlen(fieldType) - 1] == ']'))
else if (StrEquals(fieldType, "array") ||
IsDelimited(fieldType, '[', ']'))
{
cType = "Array *";
}
@ -542,7 +560,20 @@ Main(Array * args)
cType = fieldType;
}
StreamPrintf(headerFile, " %s %s;\n", cType, field);
if (IsDelimited(fieldType, '{', '}') ||
IsDelimited(fieldType, '[', ']'))
{
char end = fieldType[strlen(fieldType) - 1];
fieldType[strlen(fieldType) - 1] = '\0';
StreamPrintf(headerFile, " %s /* of %s */ %s;\n", cType, fieldType + 1, field);
fieldType[strlen(fieldType)] = end;
}
else
{
StreamPrintf(headerFile, " %s %s;\n", cType, field);
}
}
StreamPrintf(headerFile, "} %s;\n\n", type);
@ -673,7 +704,134 @@ Main(Array * args)
StreamPrintf(implFile, " out->%s = JsonValueAsObject(val);\n", key);
StreamPrintf(implFile, " Free(val); /* Not JsonValueFree() because we want the inner value. */\n");
}
else if (*fieldType == '[' && fieldType[strlen(fieldType) - 1] == ']')
else if (IsDelimited(fieldType, '{', '}'))
{
fieldType++;
fieldType[strlen(fieldType) - 1] = '\0';
isEnum = StrEquals(JsonValueAsString(JsonGet(types, 2, fieldType, "type")), "enum");
jsonType = isEnum ? JSON_STRING : TypeToJsonType(fieldType);
StreamPrintf(implFile, " out->%s = HashMapCreate();\n", key);
StreamPrintf(implFile, " if (!out->%s)\n", key);
StreamPrintf(implFile, " {\n");
StreamPrintf(implFile, " *errp = \"Failed to allocate memory for %s.%s.\";\n", type, key);
StreamPrintf(implFile, " return false;\n");
StreamPrintf(implFile, " }\n");
StreamPrintf(implFile, " else\n");
StreamPrintf(implFile, " {\n");
StreamPrintf(implFile, " HashMap *obj = JsonValueAsObject(val);\n");
StreamPrintf(implFile, " char *objKey;\n");
StreamPrintf(implFile, " JsonValue *v;\n");
StreamPrintf(implFile, "\n");
StreamPrintf(implFile, " while (HashMapIterate(obj, &objKey, (void **) &v))\n");
StreamPrintf(implFile, " {\n");
if (StrEquals(fieldType, "integer") ||
StrEquals(fieldType, "float") ||
StrEquals(fieldType, "boolean"))
{
char *cType;
if (StrEquals(fieldType, "integer"))
{
cType = "int64_t";
}
else if (StrEquals(fieldType, "float"))
{
cType = "double";
}
else if (StrEquals(fieldType, "boolean"))
{
cType = "bool";
}
else
{
/* Should never happen */
cType = NULL;
}
*fieldType = toupper(*fieldType);
StreamPrintf(implFile, " %s *ref;\n", cType);
StreamPrintf(implFile, " if (JsonValueType(v) != %s)\n", JsonTypeToStr(jsonType));
StreamPrintf(implFile, " {\n");
StreamPrintf(implFile, " *errp = \"%s.%s{} contains an invalid value.\";\n", type, key);
StreamPrintf(implFile, " return false;\n");
StreamPrintf(implFile, " }\n");
StreamPrintf(implFile, " ref = Malloc(sizeof(%s));\n", cType);
StreamPrintf(implFile, " if (!ref)\n");
StreamPrintf(implFile, " {\n");
StreamPrintf(implFile, " *errp = \"Unable to allocate memory for object value.\";\n");
StreamPrintf(implFile, " return false;\n");
StreamPrintf(implFile, " }\n");
StreamPrintf(implFile, " *ref = JsonValueAs%s(v);\n", fieldType);
StreamPrintf(implFile, " HashMapSet(out->%s, objKey, ref);\n", key);
*fieldType = tolower(*fieldType);
}
else if (StrEquals(fieldType, "string"))
{
StreamPrintf(implFile, " if (JsonValueType(v) != %s)\n", JsonTypeToStr(jsonType));
StreamPrintf(implFile, " {\n");
StreamPrintf(implFile, " *errp = \"%s.%s[] contains an invalid value.\";\n", type, key);
StreamPrintf(implFile, " return false;\n");
StreamPrintf(implFile, " }\n");
StreamPrintf(implFile, " HashMapSet(out->%s, objKey, StrDuplicate(JsonValueAsString(v)));\n", key);
}
else if (StrEquals(fieldType, "object"))
{
StreamPrintf(implFile, " if (JsonValueType(v) != %s)\n", JsonTypeToStr(jsonType));
StreamPrintf(implFile, " {\n");
StreamPrintf(implFile, " *errp = \"%s.%s[] contains an invalid value.\";\n", type, key);
StreamPrintf(implFile, " return false;\n");
StreamPrintf(implFile, " }\n");
StreamPrintf(implFile, " HashMapSet(out->%s, objKey, JsonDuplicate(JsonValueAsObject(v)));\n", key);
}
else
{
if (isEnum)
{
StreamPrintf(implFile, " int parseResult;\n");
}
StreamPrintf(implFile, " %s *parsed;\n", fieldType);
StreamPrintf(implFile, " if (JsonValueType(v) != %s)\n", JsonTypeToStr(jsonType));
StreamPrintf(implFile, " {\n");
StreamPrintf(implFile, " *errp = \"%s.%s[] contains an invalid value.\";\n", type, key);
StreamPrintf(implFile, " return false;\n");
StreamPrintf(implFile, " }\n");
StreamPrintf(implFile, " parsed = Malloc(sizeof(%s));\n", fieldType);
StreamPrintf(implFile, " if (!parsed)\n");
StreamPrintf(implFile, " {\n");
StreamPrintf(implFile, " *errp = \"Unable to allocate memory for array value.\";\n");
StreamPrintf(implFile, " return false;\n");
StreamPrintf(implFile, " }\n");
if (isEnum)
{
StreamPrintf(implFile, " parseResult = %sFromStr(JsonValueAsString(v));\n", fieldType);
StreamPrintf(implFile, " *parsed = parseResult;\n");
StreamPrintf(implFile, " if (parseResult == -1)\n");
}
else
{
StreamPrintf(implFile, " if (!%sFromJson(JsonValueAsObject(v), parsed, errp))\n", fieldType);
}
StreamPrintf(implFile, " {\n");
if (!isEnum)
{
StreamPrintf(implFile, " %sFree(parsed);\n", fieldType);
}
StreamPrintf(implFile, " Free(parsed);\n");
StreamPrintf(implFile, " return false;\n");
StreamPrintf(implFile, " }\n");
StreamPrintf(implFile, " HashMapSet(out->%s, objKey, parsed);\n", key);
}
StreamPrintf(implFile, " }\n");
StreamPrintf(implFile, " }\n");
fieldType[strlen(fieldType)] = '}';
}
else if (IsDelimited(fieldType, '[', ']'))
{
fieldType++;
fieldType[strlen(fieldType) - 1] = '\0';
@ -915,7 +1073,73 @@ Main(Array * args)
{
StreamPrintf(implFile, " HashMapSet(json, \"%s\", JsonValueObject(JsonDuplicate(val->%s)));\n", Trim('_', key), key);
}
else if (*fieldType == '[' && fieldType[strlen(fieldType) - 1] == ']')
else if (IsDelimited(fieldType, '{', '}'))
{
int isPrimitive;
fieldType++;
fieldType[strlen(fieldType) - 1] = '\0';
isEnum = StrEquals(JsonValueAsString(JsonGet(types, 2, fieldType, "type")), "enum");
isPrimitive = StrEquals(fieldType, "integer") ||
StrEquals(fieldType, "boolean") ||
StrEquals(fieldType, "float");
StreamPrintf(implFile, " if (val->%s)\n", key);
StreamPrintf(implFile, " {\n");
StreamPrintf(implFile, " char *objKey;\n");
StreamPrintf(implFile, " void *objVal;\n");
StreamPrintf(implFile, " HashMap *jsonObj = HashMapCreate();\n");
StreamPrintf(implFile, " if (!jsonObj)\n");
StreamPrintf(implFile, " {\n");
StreamPrintf(implFile, " JsonFree(json);\n");
StreamPrintf(implFile, " return NULL;\n");
StreamPrintf(implFile, " }\n");
StreamPrintf(implFile, " while (HashMapIterate(val->%s, &objKey, &objVal))\n", key);
StreamPrintf(implFile, " {\n");
if (StrEquals(fieldType, "string"))
{
StreamPrintf(implFile, " HashMapSet(jsonObj, objKey, JsonValueString(objVal));\n", key);
}
else if (!isPrimitive)
{
StreamPrintf(implFile, " HashMapSet(jsonObj, objKey, JsonValueObject(%sToJson(objVal)));\n", fieldType, key);
}
else
{
char *cType;
if (StrEquals(fieldType, "integer"))
{
cType = "int64_t";
}
else if (StrEquals(fieldType, "float"))
{
cType = "double";
}
else if (StrEquals(fieldType, "boolean"))
{
cType = "bool";
}
else
{
/* Should never happen */
cType = NULL;
}
*fieldType = toupper(*fieldType);
StreamPrintf(implFile, " HashMapSet(jsonObj, objKey, (JsonValue%s(*((%s *) objVal))));\n", fieldType, cType, key);
*fieldType = tolower(*fieldType);
}
StreamPrintf(implFile, " }\n");
StreamPrintf(implFile, " HashMapSet(json, \"%s\", JsonValueObject(jsonObj));\n", Trim('_', key));
StreamPrintf(implFile, " }\n");
fieldType[strlen(fieldType)] = '}';
}
else if (IsDelimited(fieldType, '[', ']'))
{
int isPrimitive;
@ -1036,7 +1260,36 @@ Main(Array * args)
{
StreamPrintf(implFile, " Free(val->%s);\n", key);
}
else if (*fieldType == '[' && fieldType[strlen(fieldType) - 1] == ']')
else if (IsDelimited(fieldType, '{', '}'))
{
int isPrimitive;
fieldType++;
fieldType[strlen(fieldType) - 1] = '\0';
isEnum = StrEquals(JsonValueAsString(JsonGet(types, 2, fieldType, "type")), "enum");
isPrimitive = StrEquals(fieldType, "boolean") ||
StrEquals(fieldType, "float") ||
StrEquals(fieldType, "integer") ||
StrEquals(fieldType, "string");
StreamPrintf(implFile, " if (val->%s)\n", key);
StreamPrintf(implFile, " {\n");
StreamPrintf(implFile, " char *objKey;\n");
StreamPrintf(implFile, " void *objVal;\n");
StreamPrintf(implFile, " while (HashMapIterate(val->%s, &objKey, &objVal))\n", key);
StreamPrintf(implFile, " {\n");
StreamPrintf(implFile, " %sFree(objVal);\n", (!isEnum && !isPrimitive) ? fieldType : "", key);
if (!isEnum && !isPrimitive)
{
StreamPrintf(implFile, " Free(objVal);\n", key);
}
StreamPrintf(implFile, " }\n");
StreamPrintf(implFile, " HashMapFree(val->%s);\n", key);
StreamPrintf(implFile, " }\n");
fieldType[strlen(fieldType)] = '}';
}
else if (IsDelimited(fieldType, '[', ']'))
{
int isPrimitive;