From 9019a184494e6fc220bcc1eb8f47f33fe0f3e506 Mon Sep 17 00:00:00 2001 From: yzrh Date: Sun, 1 Jan 2023 10:44:27 +0000 Subject: Split md5 function. Signed-off-by: yzrh --- src/Makefile | 4 ++-- src/md5.c | 24 ++++++++++++++++++++++++ src/md5.h | 9 +++++++++ src/pdf_writer.c | 16 ++++++++-------- 4 files changed, 43 insertions(+), 10 deletions(-) create mode 100644 src/md5.c create mode 100644 src/md5.h 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 + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include + +#include + +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 + * + * 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 -#include #include -#include - +#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; } -- cgit v1.2.3