From 790cd440eab4eb70108fccc1d1943bd8902ed08d Mon Sep 17 00:00:00 2001 From: Michael R Sweet Date: Mon, 25 Oct 2021 21:22:59 -0400 Subject: [PATCH] Fix up copying objects from unencrypted to AES-encrypted documents (still looks like there are some issues with strings in dicts) --- pdfio-dict.c | 33 +++++++++++++++++++++++++++++++++ pdfio-object.c | 3 +++ pdfio-private.h | 1 + pdfio-stream.c | 4 +++- 4 files changed, 40 insertions(+), 1 deletion(-) diff --git a/pdfio-dict.c b/pdfio-dict.c index a969fa8..a678e11 100644 --- a/pdfio-dict.c +++ b/pdfio-dict.c @@ -21,6 +21,39 @@ static int compare_pairs(_pdfio_pair_t *a, _pdfio_pair_t *b); +// +// '_pdfioDictClear()' - Remove a key/value pair from a dictionary. +// + +void +_pdfioDictClear(pdfio_dict_t *dict, // I - Dictionary + const char *key) // I - Key +{ + size_t idx; // Index into pairs + _pdfio_pair_t *pair, // Current pair + pkey; // Search key + + + PDFIO_DEBUG("_pdfioDictClear(dict=%p, key=\"%s\")\n", dict, key); + + // See if the key is already set... + if (dict->num_pairs > 0) + { + pkey.key = key; + + if ((pair = (_pdfio_pair_t *)bsearch(&pkey, dict->pairs, dict->num_pairs, sizeof(_pdfio_pair_t), (int (*)(const void *, const void *))compare_pairs)) != NULL) + { + // Yes, remove it... + idx = (size_t)(pair - dict->pairs); + dict->num_pairs --; + + if (idx < dict->num_pairs) + memmove(pair, pair + 1, (dict->num_pairs - idx) * sizeof(_pdfio_pair_t)); + } + } +} + + // // 'pdfioDictCopy()' - Copy a dictionary to a PDF file. // diff --git a/pdfio-object.c b/pdfio-object.c index 9848ba5..f7dab8c 100644 --- a/pdfio-object.c +++ b/pdfio-object.c @@ -96,6 +96,9 @@ pdfioObjCopy(pdfio_file_t *pdf, // I - PDF file if (!_pdfioValueCopy(pdf, &dstobj->value, srcobj->pdf, &srcobj->value)) return (NULL); + if (dstobj->value.type == PDFIO_VALTYPE_DICT) + _pdfioDictClear(dstobj->value.value.dict, "Length"); + if (srcobj->stream_offset) { // Copy stream data... diff --git a/pdfio-private.h b/pdfio-private.h index 17e6202..8140582 100644 --- a/pdfio-private.h +++ b/pdfio-private.h @@ -361,6 +361,7 @@ extern void _pdfioCryptoSHA256Init(_pdfio_sha256_t *ctx) _PDFIO_INTERNAL; extern void _pdfioCryptoSHA256Finish(_pdfio_sha256_t *ctx, uint8_t *Message_Digest) _PDFIO_INTERNAL; extern bool _pdfioCryptoUnlock(pdfio_file_t *pdf, pdfio_password_cb_t password_cb, void *password_data) _PDFIO_INTERNAL; +extern void _pdfioDictClear(pdfio_dict_t *dict, const char *key) _PDFIO_INTERNAL; extern void _pdfioDictDebug(pdfio_dict_t *dict, FILE *fp) _PDFIO_INTERNAL; extern void _pdfioDictDelete(pdfio_dict_t *dict) _PDFIO_INTERNAL; extern _pdfio_value_t *_pdfioDictGetValue(pdfio_dict_t *dict, const char *key) _PDFIO_INTERNAL; diff --git a/pdfio-stream.c b/pdfio-stream.c index fcd07af..f22f892 100644 --- a/pdfio-stream.c +++ b/pdfio-stream.c @@ -1266,7 +1266,9 @@ stream_write(pdfio_stream_t *st, // I - Stream outbytes = bytes; } - if (!_pdfioFileWrite(st->pdf, st->cbuffer, bytes)) +// fprintf(stderr, "stream_write: bytes=%u, outbytes=%u\n", (unsigned)bytes, (unsigned)outbytes); + + if (!_pdfioFileWrite(st->pdf, st->cbuffer, outbytes)) return (false); if (bytes > outbytes)