From 8031e31abb2e0e5a263cd64b2b6bec4226cd5a17 Mon Sep 17 00:00:00 2001 From: Michael R Sweet Date: Fri, 30 Apr 2021 08:21:21 -0400 Subject: [PATCH] Do some cleanup for the array copy implmentation. Add binary string accessors for dictionaries. Add pdfioDictCopy implementation. --- pdfio-array.c | 7 +-- pdfio-dict.c | 154 +++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 148 insertions(+), 13 deletions(-) diff --git a/pdfio-array.c b/pdfio-array.c index 81c6b66..b76c9a4 100644 --- a/pdfio-array.c +++ b/pdfio-array.c @@ -252,8 +252,8 @@ pdfioArrayCopy(pdfio_file_t *pdf, // I - PDF file if (!_pdfioValueCopy(pdf, &vdst, a->pdf, vsrc)) return (NULL); // Let pdfioFileClose do the cleanup... - if (!append_value(na, &vdst)) - return (NULL); // Let pdfioFileClose do the cleanup... + // Cannot fail since we already allocated memory... + append_value(na, &vdst); } // Successfully copied the array, so return it... @@ -520,9 +520,6 @@ static bool // O - `true` on success, `false` otherwise append_value(pdfio_array_t *a, // I - Array _pdfio_value_t *v) // I - Value { - if (!a) - return (false); - if (a->num_values >= a->alloc_values) { _pdfio_value_t *temp = (_pdfio_value_t *)realloc(a->values, (a->alloc_values + 16) * sizeof(_pdfio_value_t)); diff --git a/pdfio-dict.c b/pdfio-dict.c index 2fa4fac..8cafaf3 100644 --- a/pdfio-dict.c +++ b/pdfio-dict.c @@ -29,10 +29,43 @@ pdfio_dict_t * // O - New dictionary pdfioDictCopy(pdfio_file_t *pdf, // I - PDF file pdfio_dict_t *dict) // I - Original dictionary { - // TODO: Implement me - (void)pdf; - (void)dict; - return (NULL); + pdfio_dict_t *ndict; // New dictionary + size_t i; // Looping var + _pdfio_pair_t *p; // Current source pair + const char *key; // Current destination key + _pdfio_value_t v; // Current destination value + + + // Create the new dictionary... + if ((ndict = pdfioDictCreate(pdf)) == NULL) + return (NULL); + + // Pre-allocate the pairs array to make this a little faster... + if ((ndict->pairs = (_pdfio_pair_t *)malloc(dict->num_pairs * sizeof(_pdfio_pair_t))) == NULL) + return (NULL); // Let pdfioFileClose do the cleanup... + + ndict->alloc_pairs = dict->num_pairs; + + // Copy and add each of the source dictionary's key/value pairs... + for (i = dict->num_pairs, p = dict->pairs; i > 0; i --, p ++) + { + if (!_pdfioValueCopy(pdf, &v, dict->pdf, &p->value)) + return (NULL); // Let pdfioFileClose do the cleanup... + + if (_pdfioStringIsAllocated(dict->pdf, p->key)) + key = pdfioStringCreate(pdf, p->key); + else + key = p->key; + + if (!key) + return (NULL); // Let pdfioFileClose do the cleanup... + + // Cannot fail since we already allocated space for the pairs... + _pdfioDictSetValue(ndict, key, &v); + } + + // Successfully copied the dictionary, so return it... + return (ndict); } @@ -105,6 +138,33 @@ pdfioDictGetArray(pdfio_dict_t *dict, // I - Dictionary } +// +// 'pdfioDictGetBinary()' - Get a key binary string value from a dictionary. +// + +unsigned char * // O - Value +pdfioDictGetBinary(pdfio_dict_t *dict, // I - Dictionary + const char *key, // I - Key + size_t *length)// O - Length of value +{ + _pdfio_value_t *value = _pdfioDictGetValue(dict, key); + + if (!length) + return (NULL); + + if (value && value->type == PDFIO_VALTYPE_BINARY) + { + *length = value->value.binary.datalen; + return (value->value.binary.data); + } + else + { + *length = 0; + return (NULL); + } +} + + // // 'pdfioDictGetBoolean()' - Get a key boolean value from a dictionary. // @@ -284,6 +344,11 @@ pdfioDictSetArray(pdfio_dict_t *dict, // I - Dictionary _pdfio_value_t temp; // New value + // Range check input... + if (!dict || !key || !value) + return (false); + + // Set the key/value pair... temp.type = PDFIO_VALTYPE_ARRAY; temp.value.array = value; @@ -291,6 +356,38 @@ pdfioDictSetArray(pdfio_dict_t *dict, // I - Dictionary } +// +// 'pdfioDictSetBinary()' - Set a key binary string in a dictionary. +// + + +bool // O - `true` on success, `false` on failure +pdfioDictSetBinary( + pdfio_dict_t *dict, // I - Dictionary + const char *key, // I - Key + unsigned char *value, // I - Value + size_t valuelen) // I - Length of value +{ + _pdfio_value_t temp; // New value + + + // Range check input... + if (!dict || !key || !value || !valuelen) + return (false); + + // Set the key/value pair... + temp.type = PDFIO_VALTYPE_BINARY; + temp.value.binary.datalen = valuelen; + + if ((temp.value.binary.data = (unsigned char *)malloc(valuelen)) == NULL) + return (false); + + memcpy(temp.value.binary.data, value, valuelen); + + return (_pdfioDictSetValue(dict, key, &temp)); +} + + // // 'pdfioDictSetBoolean()' - Set a key boolean in a dictionary. // @@ -303,6 +400,11 @@ pdfioDictSetBoolean(pdfio_dict_t *dict, // I - Dictionary _pdfio_value_t temp; // New value + // Range check input... + if (!dict || !key) + return (false); + + // Set the key/value pair... temp.type = PDFIO_VALTYPE_BOOLEAN; temp.value.boolean = value; @@ -322,6 +424,11 @@ pdfioDictSetDict(pdfio_dict_t *dict, // I - Dictionary _pdfio_value_t temp; // New value + // Range check input... + if (!dict || !key || !value) + return (false); + + // Set the key/value pair... temp.type = PDFIO_VALTYPE_DICT; temp.value.dict = value; @@ -341,6 +448,11 @@ pdfioDictSetName(pdfio_dict_t *dict, // I - Dictionary _pdfio_value_t temp; // New value + // Range check input... + if (!dict || !key || !value) + return (false); + + // Set the key/value pair... temp.type = PDFIO_VALTYPE_NAME; temp.value.name = value; @@ -359,6 +471,11 @@ pdfioDictSetNull(pdfio_dict_t *dict, // I - Dictionary _pdfio_value_t temp; // New value + // Range check input... + if (!dict || !key) + return (false); + + // Set the key/value pair... temp.type = PDFIO_VALTYPE_NULL; return (_pdfioDictSetValue(dict, key, &temp)); @@ -377,6 +494,11 @@ pdfioDictSetNumber(pdfio_dict_t *dict, // I - Dictionary _pdfio_value_t temp; // New value + // Range check input... + if (!dict || !key) + return (false); + + // Set the key/value pair... temp.type = PDFIO_VALTYPE_NUMBER; temp.value.number = value; @@ -396,6 +518,11 @@ pdfioDictSetObject(pdfio_dict_t *dict, // I - Dictionary _pdfio_value_t temp; // New value + // Range check input... + if (!dict || !key || !value) + return (false); + + // Set the key/value pair... temp.type = PDFIO_VALTYPE_INDIRECT; temp.value.obj = value; @@ -415,6 +542,11 @@ pdfioDictSetRect(pdfio_dict_t *dict, // I - Dictionary _pdfio_value_t temp; // New value + // Range check input... + if (!dict || !key || !value) + return (false); + + // Set the key/value pair... temp.type = PDFIO_VALTYPE_ARRAY; temp.value.array = pdfioArrayCreate(dict->pdf); @@ -439,6 +571,11 @@ pdfioDictSetString(pdfio_dict_t *dict, // I - Dictionary _pdfio_value_t temp; // New value + // Range check input... + if (!dict || !key || !value) + return (false); + + // Set the key/value pair... temp.type = PDFIO_VALTYPE_STRING; temp.value.string = value; @@ -461,6 +598,11 @@ pdfioDictSetStringf( va_list ap; // Argument list + // Range check input... + if (!dict || !key || !format) + return (false); + + // Set the key/value pair... va_start(ap, format); vsnprintf(buffer, sizeof(buffer), format, ap); va_end(ap); @@ -483,10 +625,6 @@ _pdfioDictSetValue( *pair; // Current pair - // Range check input... - if (!dict || !key || !value) - return (false); - // See if the key is already set... if (dict->num_pairs > 0) {