From 1e338785062c0f2ac999035656b3a647e91b801b Mon Sep 17 00:00:00 2001 From: Michael R Sweet Date: Tue, 2 Nov 2021 09:12:43 -0400 Subject: [PATCH] Fix conversion of nul-containing strings to binary. Move key length checks to a common place. --- pdfio-crypto.c | 12 ++++-------- pdfio-token.c | 8 ++++++-- pdfio-value.c | 5 ++++- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/pdfio-crypto.c b/pdfio-crypto.c index 51c917e..3eac055 100644 --- a/pdfio-crypto.c +++ b/pdfio-crypto.c @@ -640,17 +640,13 @@ _pdfioCryptoUnlock( } else { + if (length < 40 || length > 128) + length = 128; // Default to 128 bits + if (!strcmp(cfm, "V2")) - { pdf->encryption = PDFIO_ENCRYPTION_RC4_128; - if (length < 40 || length > 128) - length = 128; - } - if (!strcmp(cfm, "AESV2")) - { + else if (!strcmp(cfm, "AESV2")) pdf->encryption = PDFIO_ENCRYPTION_AES_128; - length = 128; - } } } else if (version == 1 || version == 2) diff --git a/pdfio-token.c b/pdfio-token.c index e9008ad..1331bb9 100644 --- a/pdfio-token.c +++ b/pdfio-token.c @@ -284,7 +284,9 @@ _pdfioTokenRead(_pdfio_token_t *tb, // I - Token buffer/stack int tch = get_char(tb); // Next char if (tch >= '0' && tch <= '7') + { ch = (char)((ch << 3) | (tch - '0')); + } else { tb->bufptr --; @@ -377,8 +379,10 @@ _pdfioTokenRead(_pdfio_token_t *tb, // I - Token buffer/stack *buffer = '<'; for (litptr = bufptr - 1, hexptr = buffer + 2 * bytes - 1; litptr > buffer; litptr --, hexptr -= 2) { - hexptr[0] = hexchars[(*litptr >> 4) & 15]; - hexptr[1] = hexchars[*litptr & 15]; + int litch = *litptr; // Grab the character + + hexptr[0] = hexchars[(litch >> 4) & 15]; + hexptr[1] = hexchars[litch & 15]; } bufptr = buffer + 2 * bytes + 1; } diff --git a/pdfio-value.c b/pdfio-value.c index a4651be..eef0278 100644 --- a/pdfio-value.c +++ b/pdfio-value.c @@ -400,6 +400,9 @@ _pdfioValueRead(pdfio_file_t *pdf, // I - PDF file tempptr = tb->bufptr; + while (tempptr < tb->bufend && isspace(*tempptr & 255)) + tempptr ++; // Skip whitespace as needed... + if (tempptr < tb->bufend && isdigit(*tempptr & 255)) { // Integer... @@ -412,7 +415,7 @@ _pdfioValueRead(pdfio_file_t *pdf, // I - PDF file } while (tempptr < tb->bufend && isspace(*tempptr & 255)) - tempptr ++; + tempptr ++; // Skip whitespace if (tempptr < tb->bufend && *tempptr == 'R') {