Skip to content

Commit ecfde26

Browse files
3.1.6 (#22)
* update md5 calculation using gcrypt * update github actions
1 parent 6230c52 commit ecfde26

6 files changed

Lines changed: 126 additions & 63 deletions

File tree

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515

1616
steps:
1717
- name: Install third party dependencies
18-
run: sudo apt install build-essential libssl-dev zlib1g-dev libsqlite3-dev libz-dev curl gem ruby unzip p7zip-full unrar-free
18+
run: sudo apt install build-essential libgcrypt-dev zlib1g-dev libsqlite3-dev libz-dev
1919

2020
- uses: actions/checkout@v3
2121

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
steps:
1717

1818
- name: Install third party dependencies
19-
run: sudo apt install build-essential libssl-dev zlib1g-dev
19+
run: sudo apt install build-essential libgcrypt-dev zlib1g-dev
2020

2121
- uses: actions/checkout@v3
2222
with:

Makefile

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ CC=gcc
33
endif
44
CCFLAGS?=-O -g -Wall -std=gnu99
55
LIBFLAGS=$(CCFLAGS) -fPIC -c
6-
LIBS=-lm -lpthread -lz
6+
LIBS=-lm -lpthread -lz -lgcrypt
77

88
VERSION=$(shell ./version.sh)
99

@@ -18,13 +18,13 @@ help: ## This help
1818
all: clean lib shell ## Clean dev data and build the ldb's library and shell binaries
1919

2020
lib: src/ldb.c src/mz.c src/ldb.h ## Build only the ldb library
21-
@$(CC) $(LIBFLAGS) -D_LARGEFILE64_SOURCE src/ldb.c src/mz.c $(LIBS)
22-
@$(CC) -shared -Wl,-soname,libldb.so -o libldb.so ldb.o mz.o $(LIBS)
21+
@$(CC) $(LIBFLAGS) -D_LARGEFILE64_SOURCE src/ldb.c src/mz.c src/md5.c $(LIBS)
22+
@$(CC) -shared -Wl,-soname,libldb.so -o libldb.so ldb.o mz.o md5.o $(LIBS)
2323
@echo Library is built
2424

2525
shell: src/shell.c src/command.c ## Build only the shell binary
26-
@$(CC) $(CCFLAGS) -D_LARGEFILE64_SOURCE -c src/shell.c src/mz.c $(LIBS)
27-
@$(CC) $(CCFLAGS) -o ldb ldb.o shell.o -lcrypto $(LIBS)
26+
@$(CC) $(CCFLAGS) -D_LARGEFILE64_SOURCE -c src/shell.c src/mz.c src/md5.c $(LIBS)
27+
@$(CC) $(CCFLAGS) -o ldb ldb.o shell.o md5.o $(LIBS)
2828
@echo Shell is built
2929

3030
static: src/ldb.c src/ldb.h src/shell.c ## Static build of the shell binary

src/ldb.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
#include <unistd.h>
3737
#include <openssl/md5.h>
3838

39-
#define LDB_VERSION "3.1.5"
39+
#define LDB_VERSION "3.2.0"
4040
#define LDB_MAX_PATH 1024
4141
#define LDB_MAX_NAME 64
4242
#define LDB_MAX_RECORDS 500000 // Max number of records per list
@@ -277,8 +277,7 @@ void mz_id_fill(char *md5, uint8_t *mz_id);
277277
void mz_parse(struct mz_job *job, bool (*mz_parse_handler) ());
278278
void file_write(char *filename, uint8_t *src, uint64_t src_ln);
279279
void mz_id_fill(char *md5, uint8_t *mz_id);
280-
void mz_deflate(struct mz_job *job);
281-
void mz_corrupted(void);
280+
void mz_corrupted();
282281
void mz_add(char *mined_path, uint8_t *md5, char *src, int src_ln, bool check, uint8_t *zsrc, struct mz_cache_item *mz_cache);
283282
bool mz_check(char *path);
284283
void mz_flush(char *mined_path, struct mz_cache_item *mz_cache);
@@ -288,4 +287,9 @@ void mz_cat(struct mz_job *job, char *key);
288287
uint8_t *file_md5 (char *path);
289288
void calc_md5(char *data, int size, uint8_t *out);
290289

291-
//normalized_license *load_licenses();
290+
void md5_string(const unsigned char *input, int len, unsigned char output[16]);
291+
uint8_t * md5_file(char *path);
292+
293+
#define MD5(a, b, c) md5_string(a, b, c)
294+
#define MZ_DEFLATE(job) mz_deflate(job)
295+

src/md5.c

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include "ldb.h"
2+
#include <gcrypt.h>
3+
4+
/* Adapter function for compatibility with openssl*/
5+
void md5_string(const unsigned char *input, int len, unsigned char output[16])
6+
{
7+
gcry_md_hd_t h;
8+
gcry_md_open(&h, GCRY_MD_MD5, GCRY_MD_FLAG_SECURE); // initialize the hash context
9+
gcry_md_write(h, input, len); // hash the input data
10+
const unsigned char *hash_result = gcry_md_read(h, GCRY_MD_MD5); // get the result
11+
12+
// Copy the hash result to the output array
13+
memcpy(output, hash_result, 16);
14+
15+
// Free memory for the hash result
16+
gcry_md_close(h);
17+
}
18+
19+
/**
20+
* @brief Returns the hexadecimal md5 sum for "path"
21+
*
22+
* @param path string path
23+
* @return pointer to file md5 array
24+
*/
25+
uint8_t *file_md5(char *path)
26+
{
27+
uint8_t *c = calloc(1, gcry_md_get_algo_dlen(GCRY_MD_MD5)); // Allocate memory for MD5 hash
28+
FILE *fp = fopen(path, "rb");
29+
30+
if (!fp) {
31+
fprintf(stderr, "Unable to open file for reading.\n");
32+
return NULL;
33+
}
34+
35+
gcry_md_hd_t md5_hd;
36+
gcry_md_open(&md5_hd, GCRY_MD_MD5, GCRY_MD_FLAG_SECURE);
37+
38+
uint8_t *buffer = malloc(BUFFER_SIZE);
39+
size_t bytes;
40+
41+
while ((bytes = fread(buffer, 1, BUFFER_SIZE, fp)) > 0) {
42+
gcry_md_write(md5_hd, buffer, bytes);
43+
}
44+
45+
fclose(fp);
46+
free(buffer);
47+
48+
const uint8_t *digest = gcry_md_read(md5_hd, GCRY_MD_MD5);
49+
memcpy(c, digest, gcry_md_get_algo_dlen(GCRY_MD_MD5));
50+
51+
gcry_md_close(md5_hd);
52+
return c;
53+
}

src/mz.c

Lines changed: 58 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
* @see https://github.com/scanoss/ldb/blob/master/src/lock.c
3131
*/
3232

33-
#include <openssl/md5.h>
3433
#include <fcntl.h>
3534
#include <libgen.h>
3635
#include <stdbool.h>
@@ -41,48 +40,6 @@
4140
#include <zlib.h>
4241
#include "ldb.h"
4342

44-
/**
45-
* @brief Returns the hexadecimal md5 sum for "path"
46-
*
47-
* @param path string path
48-
* @return pointer to file md5 array
49-
*/
50-
uint8_t *file_md5 (char *path)
51-
{
52-
uint8_t *c = calloc(16,1);
53-
FILE *fp = fopen(path, "rb");
54-
MD5_CTX mdContext;
55-
uint32_t bytes;
56-
57-
if (fp != NULL)
58-
{
59-
uint8_t *buffer = malloc(BUFFER_SIZE);
60-
MD5_Init (&mdContext);
61-
62-
while ((bytes = fread(buffer, 1, BUFFER_SIZE, fp)) != 0)
63-
MD5_Update(&mdContext, buffer, bytes);
64-
65-
MD5_Final(c, &mdContext);
66-
fclose(fp);
67-
free(buffer);
68-
}
69-
return c;
70-
}
71-
72-
/**
73-
* @brief Calculates the MD5 hash for data
74-
*
75-
* @param data input data
76-
* @param size data size
77-
* @param out[out] pointer to MD5 array
78-
*/
79-
void calc_md5(char *data, int size, uint8_t *out)
80-
{
81-
MD5_CTX mdContext;
82-
MD5_Init (&mdContext);
83-
MD5_Update(&mdContext, data, size);
84-
MD5_Final(out, &mdContext);
85-
}
8643

8744
/**
8845
* @brief compare two MZ keys
@@ -169,7 +126,7 @@ bool mz_list_handler(struct mz_job *job)
169126

170127
/* Calculate resulting data MD5 */
171128
uint8_t actual_md5[MD5_LEN];
172-
calc_md5(job->data, job->data_ln, actual_md5);
129+
md5_string((unsigned char*) job->data, job->data_ln, actual_md5);
173130

174131
/* Compare data checksum to validate */
175132
char actual[MD5_LEN * 2 + 1] = "\0";
@@ -322,7 +279,7 @@ bool mz_extract_handler(struct mz_job *job)
322279

323280
/* Calculate resulting data MD5 */
324281
uint8_t actual_md5[MD5_LEN];
325-
calc_md5(job->data, job->data_ln, actual_md5);
282+
md5_string((unsigned char*) job->data, job->data_ln, actual_md5);
326283

327284
/* Compare data checksum to validate */
328285
char actual[MD5_LEN * 2 + 1] = "\0";
@@ -644,7 +601,7 @@ void mz_add(char *mined_path, uint8_t *md5, char *src, int src_ln, bool check, u
644601
/* We save the first bytes of zsrc to accomodate the MZ header */
645602
compress(zsrc + MZ_HEAD, &zsrc_ln, (uint8_t *) src, src_ln + 1);
646603
uint32_t zln = zsrc_ln;
647-
604+
648605
/* Only the last 14 bytes of the MD5 go to the mz record (first two bytes are the file name) */
649606
memcpy(zsrc, md5 + 2, MZ_MD5);
650607

@@ -653,7 +610,6 @@ void mz_add(char *mined_path, uint8_t *md5, char *src, int src_ln, bool check, u
653610

654611
int mzid = uint16(md5);
655612
int mzlen = zsrc_ln + MZ_HEAD;
656-
657613
/* If it won't fit in the cache, write it directly */
658614
if (mzlen > MZ_CACHE_SIZE)
659615
{
@@ -767,18 +723,68 @@ void mz_corrupted()
767723
exit(EXIT_FAILURE);
768724
}
769725

726+
770727
/**
771728
* @brief Decompress a MZ job
772729
*
773730
* @param job MZ job
774731
*/
732+
#define CHUNK_SIZE 1024
733+
734+
int uncompress_by_chunks(uint8_t **data, uint8_t *zdata, size_t zdata_len) {
735+
int ret;
736+
z_stream strm;
737+
unsigned char out[CHUNK_SIZE];
738+
size_t data_size = 0; // Current size of decompressed data
739+
740+
// Initialize the z_stream structure
741+
memset(&strm, 0, sizeof(strm));
742+
ret = inflateInit(&strm);
743+
if (ret != Z_OK) {
744+
fprintf(stderr, "inflateInit failed with error %d\n", ret);
745+
exit(EXIT_FAILURE);
746+
}
747+
*data = malloc(CHUNK_SIZE);
748+
// Process the compressed data
749+
strm.avail_in = zdata_len; // Size of the compressed data
750+
strm.next_in = zdata;
751+
752+
do {
753+
strm.avail_out = CHUNK_SIZE;
754+
strm.next_out = out;
755+
756+
ret = inflate(&strm, Z_NO_FLUSH);
757+
if (ret == Z_STREAM_ERROR) {
758+
fprintf(stderr, "inflate failed with error Z_STREAM_ERROR\n");
759+
inflateEnd(&strm);
760+
mz_corrupted();
761+
}
762+
763+
unsigned have = CHUNK_SIZE - strm.avail_out;
764+
765+
// Realloc to increase the size of data
766+
*data = realloc(*data, data_size + have);
767+
if (*data == NULL)
768+
{
769+
fprintf(stderr, "Error reallocating memory to store decompressed data");
770+
inflateEnd(&strm);
771+
exit(EXIT_FAILURE);
772+
}
773+
774+
// Copy the decompressed data to the end of data
775+
memcpy(*data + data_size, out, have);
776+
data_size += have;
777+
} while (ret != Z_STREAM_END);
778+
779+
// Free resources
780+
inflateEnd(&strm);
781+
return data_size;
782+
}
783+
775784
void mz_deflate(struct mz_job *job)
776785
{
777786
/* Decompress data */
778-
job->data_ln = MZ_MAX_FILE;
779-
if (Z_OK != uncompress((uint8_t *)job->data, &job->data_ln, job->zdata, job->zdata_ln))
780-
{
781-
mz_corrupted();
782-
}
787+
job->data_ln = uncompress_by_chunks((uint8_t **) &job->data, job->zdata, job->zdata_ln);
783788
job->data_ln--;
784789
}
790+

0 commit comments

Comments
 (0)