diff options
author | yzrh <yzrh@noema.org> | 2023-01-01 10:44:27 +0000 |
---|---|---|
committer | yzrh <yzrh@noema.org> | 2023-01-01 11:11:56 +0000 |
commit | 9019a184494e6fc220bcc1eb8f47f33fe0f3e506 (patch) | |
tree | 1615ce42fec0e7e84d86178f08ae6fc2b55ccb7c /src | |
parent | a18de8f2ef4be4a92b2e08161ce4b8f164ad4f7f (diff) | |
download | melon-9019a184494e6fc220bcc1eb8f47f33fe0f3e506.tar.gz melon-9019a184494e6fc220bcc1eb8f47f33fe0f3e506.tar.zst |
Split md5 function.
Signed-off-by: yzrh <yzrh@noema.org>
Diffstat (limited to 'src')
-rw-r--r-- | src/Makefile | 4 | ||||
-rw-r--r-- | src/md5.c | 24 | ||||
-rw-r--r-- | src/md5.h | 9 | ||||
-rw-r--r-- | src/pdf_writer.c | 16 |
4 files changed, 43 insertions, 10 deletions
diff --git a/src/Makefile b/src/Makefile index 74aff8e..b2346a7 100644 --- a/src/Makefile +++ b/src/Makefile @@ -4,11 +4,11 @@ # SPDX-License-Identifier: Apache-2.0 # -src = melon.c iconv.c zlib.c jbig2.c jpeg.c jp2.c \ +src = melon.c iconv.c zlib.c jbig2.c jpeg.c jp2.c md5.c \ cnki_caj.c cnki_hn.c cnki_kdh.c cnki_outline_tree.c cnki_pdf.c \ cnki_zlib.c cnki_jbig.c cnki_jbig_dec.c cnki_jbig2.c cnki.c \ pdf_cnki.c pdf_get.c pdf_parser.c pdf_writer.c pdf.c -inc = extern.h version.h iconv.h zlib.h jbig2.h jpeg.h jp2.h \ +inc = extern.h version.h iconv.h zlib.h jbig2.h jpeg.h jp2.h md5.h \ cnki.h pdf_cnki.h cnki_jbig.h cnki_jbig_dec.h pdf.h obj = ${src:.c=.o} diff --git a/src/md5.c b/src/md5.c new file mode 100644 index 0000000..e5ab95e --- /dev/null +++ b/src/md5.c @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2023, yzrh <yzrh@noema.org> + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include <stdlib.h> + +#include <openssl/md5.h> + +int +strmd5(unsigned char **dst, int *dst_size, + const unsigned char * restrict src, int src_size) +{ + *dst_size = MD5_DIGEST_LENGTH; + *dst = malloc(*dst_size); + + if (*dst == NULL) + return 1; + + MD5(src, src_size, *dst); + + return 0; +} diff --git a/src/md5.h b/src/md5.h new file mode 100644 index 0000000..9c1745d --- /dev/null +++ b/src/md5.h @@ -0,0 +1,9 @@ +/* + * Copyright (c) 2023, yzrh <yzrh@noema.org> + * + * SPDX-License-Identifier: Apache-2.0 + */ + +int +strmd5(unsigned char **dst, int *dst_size, + const unsigned char * restrict src, int src_size); diff --git a/src/pdf_writer.c b/src/pdf_writer.c index be64e49..465d26b 100644 --- a/src/pdf_writer.c +++ b/src/pdf_writer.c @@ -5,11 +5,9 @@ */ #include <stdlib.h> -#include <string.h> #include <time.h> -#include <openssl/md5.h> - +#include "md5.h" #include "pdf.h" int @@ -144,11 +142,11 @@ pdf_dump_trailer(pdf_object_t **pdf, FILE **fp, int xref) buf_size = snprintf(buf, 64, "%lx%x", timestamp, size); #endif - unsigned char str[64]; - memcpy(str, buf, 64); + int fid_size; + unsigned char *fid; - unsigned char fid[MD5_DIGEST_LENGTH]; - MD5(str, buf_size, fid); + if (strmd5(&fid, &fid_size, (unsigned char *) buf, buf_size) != 0) + return 1; pdf_object_t *ptr = *pdf; while (ptr->next != NULL) @@ -172,7 +170,7 @@ pdf_dump_trailer(pdf_object_t **pdf, FILE **fp, int xref) for (int i = 0; i < 2; i++) { fputs("<", *fp); - for (int j = 0; j < MD5_DIGEST_LENGTH; j++) + for (int j = 0; j < fid_size; j++) fprintf(*fp, "%02x", fid[j]); fputs(">", *fp); @@ -191,5 +189,7 @@ pdf_dump_trailer(pdf_object_t **pdf, FILE **fp, int xref) fputs("%%EOF\n", *fp); + free(fid); + return 0; } |