diff options
Diffstat (limited to 'source/pdf')
-rw-r--r-- | source/pdf/pdf-form.c | 39 | ||||
-rw-r--r-- | source/pdf/pdf-write.c | 30 |
2 files changed, 47 insertions, 22 deletions
diff --git a/source/pdf/pdf-form.c b/source/pdf/pdf-form.c index af0f572d..d5dad22b 100644 --- a/source/pdf/pdf-form.c +++ b/source/pdf/pdf-form.c @@ -1289,40 +1289,45 @@ void pdf_signature_set_value(fz_context *ctx, pdf_document *doc, pdf_obj *field, int vnum; pdf_obj *byte_range; pdf_obj *contents; - char buf[2048]; - - memset(buf, 0, sizeof(buf)); + int max_digest_size; + char *buf = NULL; vnum = pdf_create_object(ctx, doc); indv = pdf_new_indirect(ctx, doc, vnum, 0); pdf_dict_put_drop(ctx, field, PDF_NAME(V), indv); + max_digest_size = signer->max_digest_size(signer); + fz_var(v); + fz_var(buf); fz_try(ctx) { v = pdf_new_dict(ctx, doc, 4); pdf_update_object(ctx, doc, vnum, v); + + buf = fz_calloc(ctx, max_digest_size, 1); + + byte_range = pdf_new_array(ctx, doc, 4); + pdf_dict_put_drop(ctx, v, PDF_NAME(ByteRange), byte_range); + + contents = pdf_new_string(ctx, buf, max_digest_size); + pdf_dict_put_drop(ctx, v, PDF_NAME(Contents), contents); + + pdf_dict_put(ctx, v, PDF_NAME(Filter), PDF_NAME(Adobe_PPKLite)); + pdf_dict_put(ctx, v, PDF_NAME(SubFilter), PDF_NAME(adbe_pkcs7_detached)); + + /* Record details within the document structure so that contents + * and byte_range can be updated with their correct values at + * saving time */ + pdf_xref_store_unsaved_signature(ctx, doc, field, signer); } fz_always(ctx) { pdf_drop_obj(ctx, v); + fz_free(ctx, buf); } fz_catch(ctx) { fz_rethrow(ctx); } - - byte_range = pdf_new_array(ctx, doc, 4); - pdf_dict_put_drop(ctx, v, PDF_NAME(ByteRange), byte_range); - - contents = pdf_new_string(ctx, buf, sizeof(buf)); - pdf_dict_put_drop(ctx, v, PDF_NAME(Contents), contents); - - pdf_dict_put(ctx, v, PDF_NAME(Filter), PDF_NAME(Adobe_PPKLite)); - pdf_dict_put(ctx, v, PDF_NAME(SubFilter), PDF_NAME(adbe_pkcs7_detached)); - - /* Record details within the document structure so that contents - * and byte_range can be updated with their correct values at - * saving time */ - pdf_xref_store_unsaved_signature(ctx, doc, field, signer); } diff --git a/source/pdf/pdf-write.c b/source/pdf/pdf-write.c index 26fcd9d1..227258d8 100644 --- a/source/pdf/pdf-write.c +++ b/source/pdf/pdf-write.c @@ -13,6 +13,8 @@ /* #define DEBUG_HEAP_SORT */ /* #define DEBUG_WRITING */ +#define SIG_EXTRAS_SIZE (1024) + typedef struct pdf_write_state_s pdf_write_state; /* @@ -2627,12 +2629,14 @@ static void presize_unsaved_signature_byteranges(fz_context *ctx, pdf_document * static void complete_signatures(fz_context *ctx, pdf_document *doc, pdf_write_state *opts) { pdf_unsaved_sig *usig; - char buf[5120]; + char *buf = NULL; + int buf_size; int s; int i; int last_end; fz_stream *stm = NULL; fz_var(stm); + fz_var(buf); fz_try(ctx) { @@ -2643,6 +2647,18 @@ static void complete_signatures(fz_context *ctx, pdf_document *doc, pdf_write_st if (xref->unsaved_sigs) { pdf_obj *byte_range; + buf_size = 0; + + for (usig = xref->unsaved_sigs; usig; usig = usig->next) + { + int size = usig->signer->max_digest_size(usig->signer); + + buf_size = fz_maxi(buf_size, size); + } + + buf_size = buf_size * 2 + SIG_EXTRAS_SIZE; + + buf = fz_calloc(ctx, buf_size, 1); stm = fz_stream_from_output(ctx, opts->out); /* Locate the byte ranges and contents in the saved file */ @@ -2651,8 +2667,8 @@ static void complete_signatures(fz_context *ctx, pdf_document *doc, pdf_write_st char *bstr, *cstr, *fstr; int pnum = pdf_obj_parent_num(ctx, pdf_dict_getl(ctx, usig->field, PDF_NAME(V), PDF_NAME(ByteRange), NULL)); fz_seek(ctx, stm, opts->ofs_list[pnum], SEEK_SET); - (void)fz_read(ctx, stm, (unsigned char *)buf, sizeof(buf)); - buf[sizeof(buf)-1] = 0; + (void)fz_read(ctx, stm, (unsigned char *)buf, buf_size); + buf[buf_size-1] = 0; bstr = strstr(buf, "/ByteRange"); cstr = strstr(buf, "/Contents"); @@ -2690,8 +2706,8 @@ static void complete_signatures(fz_context *ctx, pdf_document *doc, pdf_write_st pdf_dict_putl_drop(ctx, usig->field, pdf_copy_array(ctx, byte_range), PDF_NAME(V), PDF_NAME(ByteRange), NULL); /* Write the byte range into buf, padding with spaces*/ - i = pdf_sprint_obj(ctx, buf, sizeof(buf), byte_range, 1); - memset(buf+i, ' ', sizeof(buf)-i); + i = pdf_sprint_obj(ctx, buf, buf_size, byte_range, 1); + memset(buf+i, ' ', buf_size-i); /* Write the byte range to the file */ for (usig = xref->unsaved_sigs; usig; usig = usig->next) @@ -2714,12 +2730,16 @@ static void complete_signatures(fz_context *ctx, pdf_document *doc, pdf_write_st } xref->unsaved_sigs_end = NULL; + + fz_free(ctx, buf); + buf = NULL; } } } fz_catch(ctx) { fz_drop_stream(ctx, stm); + fz_free(ctx, buf); fz_rethrow(ctx); } } |