From 0a1fe0ce73b5299891bb43d582f6bd23b5f24305 Mon Sep 17 00:00:00 2001 From: David Martin Ferradas Date: Sat, 10 Feb 2024 11:24:39 +0100 Subject: [PATCH] Modifications asked. pdfioDictGetNumKeys -> pdfioDictGetNumPairs pdfioDictGetKeyByIndex: - only return the key string and then only return a const char * test_mod.c: - changed to contemplate the requested modification --- pdfio-dict.c | 482 +++++++++++++++++++++------------------------------ pdfio.h | 458 ++++++++++++++++++++++++------------------------ test_mod.c | 9 +- 3 files changed, 431 insertions(+), 518 deletions(-) diff --git a/pdfio-dict.c b/pdfio-dict.c index 1f89c9a..5c1adf2 100644 --- a/pdfio-dict.c +++ b/pdfio-dict.c @@ -9,26 +9,22 @@ #include "pdfio-private.h" - // // Local functions... // -static int compare_pairs(_pdfio_pair_t *a, _pdfio_pair_t *b); - +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 +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 - + 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); @@ -44,7 +40,7 @@ _pdfioDictClear(pdfio_dict_t *dict, // I - Dictionary free(pair->value.value.binary.data); idx = (size_t)(pair - dict->pairs); - dict->num_pairs --; + dict->num_pairs--; if (idx < dict->num_pairs) memmove(pair, pair + 1, (dict->num_pairs - idx) * sizeof(_pdfio_pair_t)); @@ -52,21 +48,19 @@ _pdfioDictClear(pdfio_dict_t *dict, // I - Dictionary } } - // // 'pdfioDictCopy()' - Copy a dictionary to a PDF file. // -pdfio_dict_t * // O - New dictionary -pdfioDictCopy(pdfio_file_t *pdf, // I - PDF file - pdfio_dict_t *dict) // I - Original dictionary +pdfio_dict_t * // O - New dictionary +pdfioDictCopy(pdfio_file_t *pdf, // I - PDF file + pdfio_dict_t *dict) // I - Original dictionary { - 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 - + 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 PDFIO_DEBUG("pdfioDictCopy(pdf=%p, dict=%p(%p))\n", pdf, dict, dict ? dict->pdf : NULL); @@ -76,18 +70,18 @@ pdfioDictCopy(pdfio_file_t *pdf, // I - PDF file // 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... + 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 ++) + for (i = dict->num_pairs, p = dict->pairs; i > 0; i--, p++) { if (!strcmp(p->key, "Length") && p->value.type == PDFIO_VALTYPE_INDIRECT && dict->pdf != pdf) { // Don't use indirect stream lengths for copied objects... pdfio_obj_t *lenobj = pdfioFileFindObj(dict->pdf, p->value.value.indirect.number); - // Length object + // Length object v.type = PDFIO_VALTYPE_NUMBER; if (lenobj) @@ -95,13 +89,13 @@ pdfioDictCopy(pdfio_file_t *pdf, // I - PDF file if (lenobj->value.type == PDFIO_VALTYPE_NONE) _pdfioObjLoad(lenobj); - v.value.number = lenobj->value.value.number; + v.value.number = lenobj->value.value.number; } else v.value.number = 0.0; } else if (!_pdfioValueCopy(pdf, &v, dict->pdf, &p->value)) - return (NULL); // Let pdfioFileClose do the cleanup... + return (NULL); // Let pdfioFileClose do the cleanup... if (_pdfioStringIsAllocated(dict->pdf, p->key)) key = pdfioStringCreate(pdf, p->key); @@ -109,7 +103,7 @@ pdfioDictCopy(pdfio_file_t *pdf, // I - PDF file key = p->key; if (!key) - return (NULL); // Let pdfioFileClose do the cleanup... + return (NULL); // Let pdfioFileClose do the cleanup... // Cannot fail since we already allocated space for the pairs... _pdfioDictSetValue(ndict, key, &v); @@ -119,16 +113,14 @@ pdfioDictCopy(pdfio_file_t *pdf, // I - PDF file return (ndict); } - // // 'pdfioDictCreate()' - Create a dictionary to hold key/value pairs. // -pdfio_dict_t * // O - New dictionary -pdfioDictCreate(pdfio_file_t *pdf) // I - PDF file +pdfio_dict_t * // O - New dictionary +pdfioDictCreate(pdfio_file_t *pdf) // I - PDF file { - pdfio_dict_t *dict; // New dictionary - + pdfio_dict_t *dict; // New dictionary if (!pdf) return (NULL); @@ -148,31 +140,29 @@ pdfioDictCreate(pdfio_file_t *pdf) // I - PDF file return (NULL); } - pdf->dicts = temp; + pdf->dicts = temp; pdf->alloc_dicts += 16; } - pdf->dicts[pdf->num_dicts ++] = dict; + pdf->dicts[pdf->num_dicts++] = dict; return (dict); } - // // '_pdfioDictDecrypt()' - Decrypt the values in a dictionary. // -bool // O - `true` on success, `false` on error -_pdfioDictDecrypt(pdfio_file_t *pdf, // I - PDF file - pdfio_obj_t *obj, // I - Object - pdfio_dict_t *dict, // I - Dictionary - size_t depth) // I - Depth +bool // O - `true` on success, `false` on error +_pdfioDictDecrypt(pdfio_file_t *pdf, // I - PDF file + pdfio_obj_t *obj, // I - Object + pdfio_dict_t *dict, // I - Dictionary + size_t depth) // I - Depth { - size_t i; // Looping var - _pdfio_pair_t *pair; // Current pair + size_t i; // Looping var + _pdfio_pair_t *pair; // Current pair - - for (i = dict->num_pairs, pair = dict->pairs; i > 0; i --, pair ++) + for (i = dict->num_pairs, pair = dict->pairs; i > 0; i--, pair++) { if (strcmp(pair->key, "ID") && !_pdfioValueDecrypt(pdf, obj, &pair->value, depth + 1)) return (false); @@ -181,40 +171,35 @@ _pdfioDictDecrypt(pdfio_file_t *pdf, // I - PDF file return (true); } - // // '_pdfioDictDebug()' - Dump a dictionary to stderr. // -void -_pdfioDictDebug(pdfio_dict_t *dict, // I - Dictionary - FILE *fp) // I - Output file +void _pdfioDictDebug(pdfio_dict_t *dict, // I - Dictionary + FILE *fp) // I - Output file { - size_t i; // Looping var - _pdfio_pair_t *pair; // Current pair + size_t i; // Looping var + _pdfio_pair_t *pair; // Current pair - - for (i = dict->num_pairs, pair = dict->pairs; i > 0; i --, pair ++) + for (i = dict->num_pairs, pair = dict->pairs; i > 0; i--, pair++) { fprintf(fp, "/%s", pair->key); _pdfioValueDebug(&pair->value, fp); } } - // // '_pdfioDictDelete()' - Free the memory used by a dictionary. // -void -_pdfioDictDelete(pdfio_dict_t *dict) // I - Dictionary +void _pdfioDictDelete(pdfio_dict_t *dict) // I - Dictionary { if (dict) { - size_t i; // Looping var - _pdfio_pair_t *pair; // Current pair + size_t i; // Looping var + _pdfio_pair_t *pair; // Current pair - for (i = dict->num_pairs, pair = dict->pairs; i > 0; i --, pair ++) + for (i = dict->num_pairs, pair = dict->pairs; i > 0; i--, pair++) { if (pair->value.type == PDFIO_VALTYPE_BINARY) free(pair->value.value.binary.data); @@ -226,37 +211,33 @@ _pdfioDictDelete(pdfio_dict_t *dict) // I - Dictionary free(dict); } - // // 'pdfioDictGetArray()' - Get a key array value from a dictionary. // -pdfio_array_t * // O - Value -pdfioDictGetArray(pdfio_dict_t *dict, // I - Dictionary - const char *key) // I - Key +pdfio_array_t * // O - Value +pdfioDictGetArray(pdfio_dict_t *dict, // I - Dictionary + const char *key) // I - Key { _pdfio_value_t *value = _pdfioDictGetValue(dict, key); - if (value && value->type == PDFIO_VALTYPE_ARRAY) return (value->value.array); else return (NULL); } - // // '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 +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); @@ -277,127 +258,113 @@ pdfioDictGetBinary(pdfio_dict_t *dict, // I - Dictionary } } - // // 'pdfioDictGetBoolean()' - Get a key boolean value from a dictionary. // -bool // O - Value -pdfioDictGetBoolean(pdfio_dict_t *dict, // I - Dictionary - const char *key) // I - Key +bool // O - Value +pdfioDictGetBoolean(pdfio_dict_t *dict, // I - Dictionary + const char *key) // I - Key { _pdfio_value_t *value = _pdfioDictGetValue(dict, key); - if (value && value->type == PDFIO_VALTYPE_BOOLEAN) return (value->value.boolean); else return (false); } - // // 'pdfioDictGetDate()' - Get a date value from a dictionary. // -time_t // O - Value -pdfioDictGetDate(pdfio_dict_t *dict, // I - Dictionary - const char *key) // I - Key +time_t // O - Value +pdfioDictGetDate(pdfio_dict_t *dict, // I - Dictionary + const char *key) // I - Key { _pdfio_value_t *value = _pdfioDictGetValue(dict, key); - if (value && value->type == PDFIO_VALTYPE_DATE) return (value->value.date); else return (0); } - // // 'pdfioDictGetDict()' - Get a key dictionary value from a dictionary. // -pdfio_dict_t * // O - Value -pdfioDictGetDict(pdfio_dict_t *dict, // I - Dictionary - const char *key) // I - Key +pdfio_dict_t * // O - Value +pdfioDictGetDict(pdfio_dict_t *dict, // I - Dictionary + const char *key) // I - Key { _pdfio_value_t *value = _pdfioDictGetValue(dict, key); - if (value && value->type == PDFIO_VALTYPE_DICT) return (value->value.dict); else return (NULL); } - // // 'pdfioDictGetName()' - Get a key name value from a dictionary. // -const char * // O - Value -pdfioDictGetName(pdfio_dict_t *dict, // I - Dictionary - const char *key) // I - Key +const char * // O - Value +pdfioDictGetName(pdfio_dict_t *dict, // I - Dictionary + const char *key) // I - Key { _pdfio_value_t *value = _pdfioDictGetValue(dict, key); - if (value && value->type == PDFIO_VALTYPE_NAME) return (value->value.name); else return (NULL); } - // // 'pdfioDictGetNumber()' - Get a key number value from a dictionary. // -double // O - Value -pdfioDictGetNumber(pdfio_dict_t *dict, // I - Dictionary - const char *key) // I - Key +double // O - Value +pdfioDictGetNumber(pdfio_dict_t *dict, // I - Dictionary + const char *key) // I - Key { _pdfio_value_t *value = _pdfioDictGetValue(dict, key); - if (value && value->type == PDFIO_VALTYPE_NUMBER) return (value->value.number); else return (0.0); } - // // 'pdfioDictGetObj()' - Get a key indirect object value from a dictionary. // -pdfio_obj_t * // O - Value -pdfioDictGetObj(pdfio_dict_t *dict, // I - Dictionary - const char *key) // I - Key +pdfio_obj_t * // O - Value +pdfioDictGetObj(pdfio_dict_t *dict, // I - Dictionary + const char *key) // I - Key { _pdfio_value_t *value = _pdfioDictGetValue(dict, key); - if (value && value->type == PDFIO_VALTYPE_INDIRECT) return (pdfioFileFindObj(dict->pdf, value->value.indirect.number)); else return (NULL); } - // // 'pdfioDictGetRect()' - Get a key rectangle value from a dictionary. // -pdfio_rect_t * // O - Rectangle -pdfioDictGetRect(pdfio_dict_t *dict, // I - Dictionary - const char *key, // I - Key - pdfio_rect_t *rect) // I - Rectangle +pdfio_rect_t * // O - Rectangle +pdfioDictGetRect(pdfio_dict_t *dict, // I - Dictionary + const char *key, // I - Key + pdfio_rect_t *rect) // I - Rectangle { _pdfio_value_t *value = _pdfioDictGetValue(dict, key); - if (value && value->type == PDFIO_VALTYPE_ARRAY && pdfioArrayGetSize(value->value.array) == 4) { rect->x1 = pdfioArrayGetNumber(value->value.array, 0); @@ -413,51 +380,45 @@ pdfioDictGetRect(pdfio_dict_t *dict, // I - Dictionary } } - // // 'pdfioDictGetString()' - Get a key string value from a dictionary. // -const char * // O - Value -pdfioDictGetString(pdfio_dict_t *dict, // I - Dictionary - const char *key) // I - Key +const char * // O - Value +pdfioDictGetString(pdfio_dict_t *dict, // I - Dictionary + const char *key) // I - Key { _pdfio_value_t *value = _pdfioDictGetValue(dict, key); - if (value && value->type == PDFIO_VALTYPE_STRING) return (value->value.string); else return (NULL); } - // // 'pdfioDictGetType()' - Get a key value type from a dictionary. // -pdfio_valtype_t // O - Value type -pdfioDictGetType(pdfio_dict_t *dict, // I - Dictionary - const char *key) // I - Key +pdfio_valtype_t // O - Value type +pdfioDictGetType(pdfio_dict_t *dict, // I - Dictionary + const char *key) // I - Key { _pdfio_value_t *value = _pdfioDictGetValue(dict, key); - return (value ? value->type : PDFIO_VALTYPE_NONE); } - // // '_pdfioDictGetValue()' - Get a key value from a dictionary. // -_pdfio_value_t * // O - Value or `NULL` on error -_pdfioDictGetValue(pdfio_dict_t *dict, // I - Dictionary - const char *key) // I - Key +_pdfio_value_t * // O - Value or `NULL` on error +_pdfioDictGetValue(pdfio_dict_t *dict, // I - Dictionary + const char *key) // I - Key { - _pdfio_pair_t temp, // Search key - *match; // Matching key pair - + _pdfio_pair_t temp, // Search key + *match; // Matching key pair PDFIO_DEBUG("_pdfioDictGetValue(dict=%p, key=\"%s\")\n", dict, key); @@ -483,7 +444,6 @@ _pdfioDictGetValue(pdfio_dict_t *dict, // I - Dictionary } } - // // 'pdfioDictIterateKeys()' - Iterate the keys in a dictionary. // @@ -503,21 +463,19 @@ _pdfioDictGetValue(pdfio_dict_t *dict, // I - Dictionary // have been iterated. // -void -pdfioDictIterateKeys( - pdfio_dict_t *dict, // I - Dictionary - pdfio_dict_cb_t cb, // I - Callback function - void *cb_data) // I - Callback data +void pdfioDictIterateKeys( + pdfio_dict_t *dict, // I - Dictionary + pdfio_dict_cb_t cb, // I - Callback function + void *cb_data) // I - Callback data { - size_t i; // Looping var - _pdfio_pair_t *pair; // Current pair - + size_t i; // Looping var + _pdfio_pair_t *pair; // Current pair // Range check input... if (!dict || !cb) return; - for (i = dict->num_pairs, pair = dict->pairs; i > 0; i --, pair ++) + for (i = dict->num_pairs, pair = dict->pairs; i > 0; i--, pair++) { if (!(cb)(dict, pair->key, cb_data)) break; @@ -528,10 +486,12 @@ pdfioDictIterateKeys( // 'pdfioDictGetNumKeys()' - Get the number of keys on the selected dictionary // -size_t // O - Number of keys -pdfioDictGetNumKeys(pdfio_dict_t *dict) // I - Dictionary +size_t // O - Number of keys +pdfioDictGetNumPairs(pdfio_dict_t *dict) // I - Dictionary { - return (dict->num_pairs); + if (dict) + return (dict->num_pairs); + return (0); } // @@ -539,20 +499,13 @@ pdfioDictGetNumKeys(pdfio_dict_t *dict) // I - Dictionary // // Alternative form to enumerate the keys of a dictionary -bool // O - Value -pdfioDictGetKeyByIndex(pdfio_dict_t *dict, // I - Dictionary - size_t index, // I - Index - pdfio_dictKey_t *key) // I - struct pointer +const char * // O - Value +pdfioDictGetKeyByIndex(pdfio_dict_t *dict, // I - Dictionary + size_t index) // I - Index { -if (index < dict->num_pairs) - { - if (key){ - key->key = dict->pairs[index].key; - key->type = (pdfio_valtype_t **)(dict->pairs[index].value.type); - return (true); - } - } - return (false); + if (index < dict->num_pairs) + return (dict->pairs[index].key); + return (NULL); } // @@ -561,16 +514,15 @@ if (index < dict->num_pairs) // At this point we've seen the initial "<<"... // -pdfio_dict_t * // O - New dictionary -_pdfioDictRead(pdfio_file_t *pdf, // I - PDF file - pdfio_obj_t *obj, // I - Object, if any - _pdfio_token_t *tb, // I - Token buffer/stack - size_t depth) // I - Depth of dictionary +pdfio_dict_t * // O - New dictionary +_pdfioDictRead(pdfio_file_t *pdf, // I - PDF file + pdfio_obj_t *obj, // I - Object, if any + _pdfio_token_t *tb, // I - Token buffer/stack + size_t depth) // I - Depth of dictionary { - pdfio_dict_t *dict; // New dictionary - char key[256]; // Dictionary key - _pdfio_value_t value; // Dictionary value - + pdfio_dict_t *dict; // New dictionary + char key[256]; // Dictionary key + _pdfio_value_t value; // Dictionary value PDFIO_DEBUG("_pdfioDictRead(pdf=%p, obj=%p, tb=%p, depth=%lu)\n", pdf, obj, tb, (unsigned long)depth); @@ -618,52 +570,47 @@ _pdfioDictRead(pdfio_file_t *pdf, // I - PDF file return (NULL); } - // // 'pdfioDictSetArray()' - Set a key array in a dictionary. // -bool // O - `true` on success, `false` on failure -pdfioDictSetArray(pdfio_dict_t *dict, // I - Dictionary - const char *key, // I - Key - pdfio_array_t *value) // I - Value +bool // O - `true` on success, `false` on failure +pdfioDictSetArray(pdfio_dict_t *dict, // I - Dictionary + const char *key, // I - Key + pdfio_array_t *value) // I - Value { - _pdfio_value_t temp; // New value - + _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.type = PDFIO_VALTYPE_ARRAY; temp.value.array = value; return (_pdfioDictSetValue(dict, key, &temp)); } - // // 'pdfioDictSetBinary()' - Set a key binary string in a dictionary. // - -bool // O - `true` on success, `false` on failure +bool // O - `true` on success, `false` on failure pdfioDictSetBinary( - pdfio_dict_t *dict, // I - Dictionary - const char *key, // I - Key - const unsigned char *value, // I - Value - size_t valuelen) // I - Length of value + pdfio_dict_t *dict, // I - Dictionary + const char *key, // I - Key + const unsigned char *value, // I - Value + size_t valuelen) // I - Length of value { - _pdfio_value_t temp; // New 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.type = PDFIO_VALTYPE_BINARY; temp.value.binary.datalen = valuelen; if ((temp.value.binary.data = (unsigned char *)malloc(valuelen)) == NULL) @@ -680,113 +627,103 @@ pdfioDictSetBinary( return (true); } - // // 'pdfioDictSetBoolean()' - Set a key boolean in a dictionary. // -bool // O - `true` on success, `false` on failure -pdfioDictSetBoolean(pdfio_dict_t *dict, // I - Dictionary - const char *key, // I - Key - bool value) // I - Value +bool // O - `true` on success, `false` on failure +pdfioDictSetBoolean(pdfio_dict_t *dict, // I - Dictionary + const char *key, // I - Key + bool value) // I - Value { - _pdfio_value_t temp; // New value - + _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.type = PDFIO_VALTYPE_BOOLEAN; temp.value.boolean = value; return (_pdfioDictSetValue(dict, key, &temp)); } - // // 'pdfioDictSetDate()' - Set a date value in a dictionary. // -bool // O - `true` on success, `false` on failure -pdfioDictSetDate(pdfio_dict_t *dict, // I - Dictionary - const char *key, // I - Key - time_t value) // I - Value +bool // O - `true` on success, `false` on failure +pdfioDictSetDate(pdfio_dict_t *dict, // I - Dictionary + const char *key, // I - Key + time_t value) // I - Value { - _pdfio_value_t temp; // New value - + _pdfio_value_t temp; // New value // Range check input... if (!dict || !key) return (false); // Set the key/value pair... - temp.type = PDFIO_VALTYPE_DATE; + temp.type = PDFIO_VALTYPE_DATE; temp.value.date = value; return (_pdfioDictSetValue(dict, key, &temp)); } - // // 'pdfioDictSetDict()' - Set a key dictionary in a dictionary. // -bool // O - `true` on success, `false` on failure -pdfioDictSetDict(pdfio_dict_t *dict, // I - Dictionary - const char *key, // I - Key - pdfio_dict_t *value) // I - Value +bool // O - `true` on success, `false` on failure +pdfioDictSetDict(pdfio_dict_t *dict, // I - Dictionary + const char *key, // I - Key + pdfio_dict_t *value) // I - Value { - _pdfio_value_t temp; // New value - + _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.type = PDFIO_VALTYPE_DICT; temp.value.dict = value; return (_pdfioDictSetValue(dict, key, &temp)); } - // // 'pdfioDictSetName()' - Set a key name in a dictionary. // -bool // O - `true` on success, `false` on failure -pdfioDictSetName(pdfio_dict_t *dict, // I - Dictionary - const char *key, // I - Key - const char *value) // I - Value +bool // O - `true` on success, `false` on failure +pdfioDictSetName(pdfio_dict_t *dict, // I - Dictionary + const char *key, // I - Key + const char *value) // I - Value { - _pdfio_value_t temp; // New value - + _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.type = PDFIO_VALTYPE_NAME; temp.value.name = value; return (_pdfioDictSetValue(dict, key, &temp)); } - // // 'pdfioDictSetNull()' - Set a key null in a dictionary. // -bool // O - `true` on success, `false` on failure -pdfioDictSetNull(pdfio_dict_t *dict, // I - Dictionary - const char *key) // I - Key +bool // O - `true` on success, `false` on failure +pdfioDictSetNull(pdfio_dict_t *dict, // I - Dictionary + const char *key) // I - Key { - _pdfio_value_t temp; // New value - + _pdfio_value_t temp; // New value // Range check input... if (!dict || !key) @@ -798,74 +735,68 @@ pdfioDictSetNull(pdfio_dict_t *dict, // I - Dictionary return (_pdfioDictSetValue(dict, key, &temp)); } - // // 'pdfioDictSetNumber()' - Set a key number in a dictionary. // -bool // O - `true` on success, `false` on failure -pdfioDictSetNumber(pdfio_dict_t *dict, // I - Dictionary - const char *key, // I - Key - double value) // I - Value +bool // O - `true` on success, `false` on failure +pdfioDictSetNumber(pdfio_dict_t *dict, // I - Dictionary + const char *key, // I - Key + double value) // I - Value { - _pdfio_value_t temp; // New value - + _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.type = PDFIO_VALTYPE_NUMBER; temp.value.number = value; return (_pdfioDictSetValue(dict, key, &temp)); } - // // 'pdfioDictSetObj()' - Set a key indirect object reference in a dictionary. // -bool // O - `true` on success, `false` on failure -pdfioDictSetObj(pdfio_dict_t *dict, // I - Dictionary - const char *key, // I - Key - pdfio_obj_t *value) // I - Value +bool // O - `true` on success, `false` on failure +pdfioDictSetObj(pdfio_dict_t *dict, // I - Dictionary + const char *key, // I - Key + pdfio_obj_t *value) // I - Value { - _pdfio_value_t temp; // New value - + _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.indirect.number = value->number; + temp.type = PDFIO_VALTYPE_INDIRECT; + temp.value.indirect.number = value->number; temp.value.indirect.generation = value->generation; return (_pdfioDictSetValue(dict, key, &temp)); } - // // 'pdfioDictSetRect()' - Set a key rectangle in a dictionary. // -bool // O - `true` on success, `false` on failure -pdfioDictSetRect(pdfio_dict_t *dict, // I - Dictionary - const char *key, // I - Key - pdfio_rect_t *value) // I - Value +bool // O - `true` on success, `false` on failure +pdfioDictSetRect(pdfio_dict_t *dict, // I - Dictionary + const char *key, // I - Key + pdfio_rect_t *value) // I - Value { - _pdfio_value_t temp; // New value - + _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.type = PDFIO_VALTYPE_ARRAY; temp.value.array = pdfioArrayCreate(dict->pdf); pdfioArrayAppendNumber(temp.value.array, value->x1); @@ -876,45 +807,41 @@ pdfioDictSetRect(pdfio_dict_t *dict, // I - Dictionary return (_pdfioDictSetValue(dict, key, &temp)); } - // // 'pdfioDictSetString()' - Set a key literal string in a dictionary. // -bool // O - `true` on success, `false` on failure -pdfioDictSetString(pdfio_dict_t *dict, // I - Dictionary - const char *key, // I - Key - const char *value)// I - Value +bool // O - `true` on success, `false` on failure +pdfioDictSetString(pdfio_dict_t *dict, // I - Dictionary + const char *key, // I - Key + const char *value) // I - Value { - _pdfio_value_t temp; // New value - + _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.type = PDFIO_VALTYPE_STRING; temp.value.string = value; return (_pdfioDictSetValue(dict, key, &temp)); } - // // 'pdfioDictSetStringf()' - Set a key formatted string in a dictionary. // -bool // O - `true` on success, `false` on failure +bool // O - `true` on success, `false` on failure pdfioDictSetStringf( - pdfio_dict_t *dict, // I - Dictionary - const char *key, // I - Key - const char *format, // I - `printf`-style format string - ...) // I - Additional arguments as needed + pdfio_dict_t *dict, // I - Dictionary + const char *key, // I - Key + const char *format, // I - `printf`-style format string + ...) // I - Additional arguments as needed { - char buffer[8192]; // String buffer - va_list ap; // Argument list - + char buffer[8192]; // String buffer + va_list ap; // Argument list // Range check input... if (!dict || !key || !format) @@ -928,26 +855,24 @@ pdfioDictSetStringf( return (pdfioDictSetString(dict, key, buffer)); } - // // '_pdfioDictSetValue()' - Set a key value in a dictionary. // -bool // O - `true` on success, `false` on failure +bool // O - `true` on success, `false` on failure _pdfioDictSetValue( - pdfio_dict_t *dict, // I - Dictionary - const char *key, // I - Key - _pdfio_value_t *value) // I - Value + pdfio_dict_t *dict, // I - Dictionary + const char *key, // I - Key + _pdfio_value_t *value) // I - Value { - _pdfio_pair_t *pair; // Current pair - + _pdfio_pair_t *pair; // Current pair PDFIO_DEBUG("_pdfioDictSetValue(dict=%p, key=\"%s\", value=%p)\n", dict, key, (void *)value); // See if the key is already set... if (dict->num_pairs > 0) { - _pdfio_pair_t pkey; // Search key + _pdfio_pair_t pkey; // Search key pkey.key = key; @@ -974,14 +899,14 @@ _pdfioDictSetValue( return (false); } - dict->pairs = temp; + dict->pairs = temp; dict->alloc_pairs += 8; } pair = dict->pairs + dict->num_pairs; - dict->num_pairs ++; + dict->num_pairs++; - pair->key = key; + pair->key = key; pair->value = *value; // Re-sort the dictionary and return... @@ -998,20 +923,18 @@ _pdfioDictSetValue( return (true); } - // // '_pdfioDictWrite()' - Write a dictionary to a PDF file. // -bool // O - `true` on success, `false` on failure -_pdfioDictWrite(pdfio_dict_t *dict, // I - Dictionary - pdfio_obj_t *obj, // I - Object, if any - off_t *length) // I - Offset to length value +bool // O - `true` on success, `false` on failure +_pdfioDictWrite(pdfio_dict_t *dict, // I - Dictionary + pdfio_obj_t *obj, // I - Object, if any + off_t *length) // I - Offset to length value { - pdfio_file_t *pdf = dict->pdf; // PDF file - size_t i; // Looping var - _pdfio_pair_t *pair; // Current key/value pair - + pdfio_file_t *pdf = dict->pdf; // PDF file + size_t i; // Looping var + _pdfio_pair_t *pair; // Current key/value pair if (length) *length = 0; @@ -1021,7 +944,7 @@ _pdfioDictWrite(pdfio_dict_t *dict, // I - Dictionary return (false); // Write all of the key/value pairs... - for (i = dict->num_pairs, pair = dict->pairs; i > 0; i --, pair ++) + for (i = dict->num_pairs, pair = dict->pairs; i > 0; i--, pair++) { if (!_pdfioFilePrintf(pdf, "/%s", pair->key)) return (false); @@ -1041,14 +964,13 @@ _pdfioDictWrite(pdfio_dict_t *dict, // I - Dictionary return (_pdfioFilePuts(pdf, ">>")); } - // // 'compare_pairs()' - Compare the keys for two pairs. // -static int // O - Result of comparison -compare_pairs(_pdfio_pair_t *a, // I - First pair - _pdfio_pair_t *b) // I - Second pair +static int // O - Result of comparison +compare_pairs(_pdfio_pair_t *a, // I - First pair + _pdfio_pair_t *b) // I - Second pair { return (strcmp(a->key, b->key)); } diff --git a/pdfio.h b/pdfio.h index bfc45f0..61b48d0 100644 --- a/pdfio.h +++ b/pdfio.h @@ -8,251 +8,241 @@ // #ifndef PDFIO_H -# define PDFIO_H -# include -# include -# include -# include -# include -# ifdef __cplusplus -extern "C" { -# endif // __cplusplus - - -// -// Version number... -// - -# define PDFIO_VERSION "1.2.1" - - -// -// Visibility and other annotations... -// - -# if defined(__has_extension) || defined(__GNUC__) -# define _PDFIO_PUBLIC __attribute__ ((visibility("default"))) -# define _PDFIO_FORMAT(a,b) __attribute__ ((__format__(__printf__, a,b))) -# define _PDFIO_DEPRECATED __attribute__ ((deprecated)) _PDFIO_PUBLIC -# else -# define _PDFIO_PUBLIC -# define _PDFIO_FORMAT(a,b) -# define _PDFIO_DEPRECATED -# endif // __has_extension || __GNUC__ - - -// -// Types and constants... -// - -# if _WIN32 -typedef __int64 ssize_t; // POSIX type not present on Windows... @private@ -# endif // _WIN32 - -typedef struct _pdfio_array_s pdfio_array_t; - // Array of PDF values -typedef struct _pdfio_dict_s pdfio_dict_t; - // Key/value dictionary -typedef bool (*pdfio_dict_cb_t)(pdfio_dict_t *dict, const char *key, void *cb_data); - // Dictionary iterator callback -typedef struct _pdfio_file_s pdfio_file_t; - // PDF file -typedef bool (*pdfio_error_cb_t)(pdfio_file_t *pdf, const char *message, void *data); - // Error callback -typedef enum pdfio_encryption_e // PDF encryption modes +#define PDFIO_H +#include +#include +#include +#include +#include +#ifdef __cplusplus +extern "C" { - PDFIO_ENCRYPTION_NONE = 0, // No encryption - PDFIO_ENCRYPTION_RC4_40, // 40-bit RC4 encryption (PDF 1.3) - PDFIO_ENCRYPTION_RC4_128, // 128-bit RC4 encryption (PDF 1.4) - PDFIO_ENCRYPTION_AES_128, // 128-bit AES encryption (PDF 1.6) - PDFIO_ENCRYPTION_AES_256 // 256-bit AES encryption (PDF 2.0) @exclude all@ -} pdfio_encryption_t; -typedef enum pdfio_filter_e // Compression/decompression filters for streams -{ - PDFIO_FILTER_NONE, // No filter - PDFIO_FILTER_ASCIIHEX, // ASCIIHexDecode filter (reading only) - PDFIO_FILTER_ASCII85, // ASCII85Decode filter (reading only) - PDFIO_FILTER_CCITTFAX, // CCITTFaxDecode filter - PDFIO_FILTER_CRYPT, // Encryption filter - PDFIO_FILTER_DCT, // DCTDecode (JPEG) filter - PDFIO_FILTER_FLATE, // FlateDecode filter - PDFIO_FILTER_JBIG2, // JBIG2Decode filter - PDFIO_FILTER_JPX, // JPXDecode filter (reading only) - PDFIO_FILTER_LZW, // LZWDecode filter (reading only) - PDFIO_FILTER_RUNLENGTH, // RunLengthDecode filter (reading only) -} pdfio_filter_t; -typedef struct _pdfio_obj_s pdfio_obj_t;// Numbered object in PDF file -typedef ssize_t (*pdfio_output_cb_t)(void *ctx, const void *data, size_t datalen); - // Output callback for pdfioFileCreateOutput -typedef const char *(*pdfio_password_cb_t)(void *data, const char *filename); - // Password callback for pdfioFileOpen -enum pdfio_permission_e // PDF permission bits -{ - PDFIO_PERMISSION_NONE = 0, // No permissions - PDFIO_PERMISSION_PRINT = 0x0004, // PDF allows printing - PDFIO_PERMISSION_MODIFY = 0x0008, // PDF allows modification - PDFIO_PERMISSION_COPY = 0x0010, // PDF allows copying - PDFIO_PERMISSION_ANNOTATE = 0x0020, // PDF allows annotation - PDFIO_PERMISSION_FORMS = 0x0100, // PDF allows filling in forms - PDFIO_PERMISSION_READING = 0x0200, // PDF allows screen reading/accessibility (deprecated in PDF 2.0) - PDFIO_PERMISSION_ASSEMBLE = 0x0400, // PDF allows assembly (insert, delete, or rotate pages, add document outlines and thumbnails) - PDFIO_PERMISSION_PRINT_HIGH = 0x0800, // PDF allows high quality printing - PDFIO_PERMISSION_ALL = ~0 // All permissions -}; -typedef int pdfio_permission_t; // PDF permission bitfield -typedef struct pdfio_rect_s // PDF rectangle -{ - double x1; // Lower-left X coordinate - double y1; // Lower-left Y coordinate - double x2; // Upper-right X coordinate - double y2; // Upper-right Y coordinate -} pdfio_rect_t; -typedef struct _pdfio_stream_s pdfio_stream_t; - // Object data stream in PDF file -typedef enum pdfio_valtype_e // PDF value types -{ - PDFIO_VALTYPE_NONE, // No value, not set - PDFIO_VALTYPE_ARRAY, // Array - PDFIO_VALTYPE_BINARY, // Binary data - PDFIO_VALTYPE_BOOLEAN, // Boolean - PDFIO_VALTYPE_DATE, // Date/time - PDFIO_VALTYPE_DICT, // Dictionary - PDFIO_VALTYPE_INDIRECT, // Indirect object (N G obj) - PDFIO_VALTYPE_NAME, // Name - PDFIO_VALTYPE_NULL, // Null object - PDFIO_VALTYPE_NUMBER, // Number (integer or real) - PDFIO_VALTYPE_STRING // String -} pdfio_valtype_t; - // Alternative PDF dict key enumeration -typedef struct pdfio_dictKey_s -{ - const char *key; - pdfio_valtype_t **type; -} pdfio_dictKey_t; +#endif // __cplusplus + // + // Version number... + // -// -// Functions... -// +#define PDFIO_VERSION "1.2.1" -extern bool pdfioArrayAppendArray(pdfio_array_t *a, pdfio_array_t *value) _PDFIO_PUBLIC; -extern bool pdfioArrayAppendBinary(pdfio_array_t *a, const unsigned char *value, size_t valuelen) _PDFIO_PUBLIC; -extern bool pdfioArrayAppendBoolean(pdfio_array_t *a, bool value) _PDFIO_PUBLIC; -extern bool pdfioArrayAppendDate(pdfio_array_t *a, time_t value) _PDFIO_PUBLIC; -extern bool pdfioArrayAppendDict(pdfio_array_t *a, pdfio_dict_t *value) _PDFIO_PUBLIC; -extern bool pdfioArrayAppendName(pdfio_array_t *a, const char *value) _PDFIO_PUBLIC; -extern bool pdfioArrayAppendNumber(pdfio_array_t *a, double value) _PDFIO_PUBLIC; -extern bool pdfioArrayAppendObj(pdfio_array_t *a, pdfio_obj_t *value) _PDFIO_PUBLIC; -extern bool pdfioArrayAppendString(pdfio_array_t *a, const char *value) _PDFIO_PUBLIC; -extern pdfio_array_t *pdfioArrayCopy(pdfio_file_t *pdf, pdfio_array_t *a) _PDFIO_PUBLIC; -extern pdfio_array_t *pdfioArrayCreate(pdfio_file_t *pdf) _PDFIO_PUBLIC; -extern pdfio_array_t *pdfioArrayGetArray(pdfio_array_t *a, size_t n) _PDFIO_PUBLIC; -extern unsigned char *pdfioArrayGetBinary(pdfio_array_t *a, size_t n, size_t *length) _PDFIO_PUBLIC; -extern bool pdfioArrayGetBoolean(pdfio_array_t *a, size_t n) _PDFIO_PUBLIC; -extern time_t pdfioArrayGetDate(pdfio_array_t *a, size_t n) _PDFIO_PUBLIC; -extern pdfio_dict_t *pdfioArrayGetDict(pdfio_array_t *a, size_t n) _PDFIO_PUBLIC; -extern const char *pdfioArrayGetName(pdfio_array_t *a, size_t n) _PDFIO_PUBLIC; -extern double pdfioArrayGetNumber(pdfio_array_t *a, size_t n) _PDFIO_PUBLIC; -extern pdfio_obj_t *pdfioArrayGetObj(pdfio_array_t *a, size_t n) _PDFIO_PUBLIC; -extern size_t pdfioArrayGetSize(pdfio_array_t *a) _PDFIO_PUBLIC; -extern const char *pdfioArrayGetString(pdfio_array_t *a, size_t n) _PDFIO_PUBLIC; -extern pdfio_valtype_t pdfioArrayGetType(pdfio_array_t *a, size_t n) _PDFIO_PUBLIC; + // + // Visibility and other annotations... + // -extern pdfio_dict_t *pdfioDictCopy(pdfio_file_t *pdf, pdfio_dict_t *dict) _PDFIO_PUBLIC; -extern pdfio_dict_t *pdfioDictCreate(pdfio_file_t *pdf) _PDFIO_PUBLIC; -extern pdfio_array_t *pdfioDictGetArray(pdfio_dict_t *dict, const char *key) _PDFIO_PUBLIC; -extern unsigned char *pdfioDictGetBinary(pdfio_dict_t *dict, const char *key, size_t *length) _PDFIO_PUBLIC; -extern bool pdfioDictGetBoolean(pdfio_dict_t *dict, const char *key) _PDFIO_PUBLIC; -extern time_t pdfioDictGetDate(pdfio_dict_t *dict, const char *key) _PDFIO_PUBLIC; -extern pdfio_dict_t *pdfioDictGetDict(pdfio_dict_t *dict, const char *key) _PDFIO_PUBLIC; -extern const char *pdfioDictGetName(pdfio_dict_t *dict, const char *key) _PDFIO_PUBLIC; -extern double pdfioDictGetNumber(pdfio_dict_t *dict, const char *key) _PDFIO_PUBLIC; -extern pdfio_obj_t *pdfioDictGetObj(pdfio_dict_t *dict, const char *key) _PDFIO_PUBLIC; -extern pdfio_rect_t *pdfioDictGetRect(pdfio_dict_t *dict, const char *key, pdfio_rect_t *rect) _PDFIO_PUBLIC; -extern const char *pdfioDictGetString(pdfio_dict_t *dict, const char *key) _PDFIO_PUBLIC; -extern pdfio_valtype_t pdfioDictGetType(pdfio_dict_t *dict, const char *key) _PDFIO_PUBLIC; -extern size_t pdfioDictGetNumKeys(pdfio_dict_t *dict) _PDFIO_PUBLIC; -extern bool pdfioDictGetKeyByIndex(pdfio_dict_t *dict, size_t index, pdfio_dictKey_t *keyData) _PDFIO_PUBLIC; -extern void pdfioDictIterateKeys(pdfio_dict_t *dict, pdfio_dict_cb_t cb, void *cb_data) _PDFIO_PUBLIC; -extern bool pdfioDictSetArray(pdfio_dict_t *dict, const char *key, pdfio_array_t *value) _PDFIO_PUBLIC; -extern bool pdfioDictSetBinary(pdfio_dict_t *dict, const char *key, const unsigned char *value, size_t valuelen) _PDFIO_PUBLIC; -extern bool pdfioDictSetBoolean(pdfio_dict_t *dict, const char *key, bool value) _PDFIO_PUBLIC; -extern bool pdfioDictSetDate(pdfio_dict_t *dict, const char *key, time_t value) _PDFIO_PUBLIC; -extern bool pdfioDictSetDict(pdfio_dict_t *dict, const char *key, pdfio_dict_t *value) _PDFIO_PUBLIC; -extern bool pdfioDictSetName(pdfio_dict_t *dict, const char *key, const char *value) _PDFIO_PUBLIC; -extern bool pdfioDictSetNull(pdfio_dict_t *dict, const char *key) _PDFIO_PUBLIC; -extern bool pdfioDictSetNumber(pdfio_dict_t *dict, const char *key, double value) _PDFIO_PUBLIC; -extern bool pdfioDictSetObj(pdfio_dict_t *dict, const char *key, pdfio_obj_t *value) _PDFIO_PUBLIC; -extern bool pdfioDictSetRect(pdfio_dict_t *dict, const char *key, pdfio_rect_t *value) _PDFIO_PUBLIC; -extern bool pdfioDictSetString(pdfio_dict_t *dict, const char *key, const char *value) _PDFIO_PUBLIC; -extern bool pdfioDictSetStringf(pdfio_dict_t *dict, const char *key, const char *format, ...) _PDFIO_PUBLIC _PDFIO_FORMAT(3,4); +#if defined(__has_extension) || defined(__GNUC__) +#define _PDFIO_PUBLIC __attribute__((visibility("default"))) +#define _PDFIO_FORMAT(a, b) __attribute__((__format__(__printf__, a, b))) +#define _PDFIO_DEPRECATED __attribute__((deprecated)) _PDFIO_PUBLIC +#else +#define _PDFIO_PUBLIC +#define _PDFIO_FORMAT(a, b) +#define _PDFIO_DEPRECATED +#endif // __has_extension || __GNUC__ -extern bool pdfioFileClose(pdfio_file_t *pdf) _PDFIO_PUBLIC; -extern pdfio_file_t *pdfioFileCreate(const char *filename, const char *version, pdfio_rect_t *media_box, pdfio_rect_t *crop_box, pdfio_error_cb_t error_cb, void *error_data) _PDFIO_PUBLIC; -extern pdfio_obj_t *pdfioFileCreateArrayObj(pdfio_file_t *pdf, pdfio_array_t *array) _PDFIO_PUBLIC; -extern pdfio_obj_t *pdfioFileCreateNumberObj(pdfio_file_t *pdf, double number) _PDFIO_PUBLIC; -extern pdfio_obj_t *pdfioFileCreateObj(pdfio_file_t *pdf, pdfio_dict_t *dict) _PDFIO_PUBLIC; -extern pdfio_file_t *pdfioFileCreateOutput(pdfio_output_cb_t output_cb, void *output_ctx, const char *version, pdfio_rect_t *media_box, pdfio_rect_t *crop_box, pdfio_error_cb_t error_cb, void *error_data) _PDFIO_PUBLIC; -// TODO: Add number, array, string, etc. versions of pdfioFileCreateObject? -extern pdfio_stream_t *pdfioFileCreatePage(pdfio_file_t *pdf, pdfio_dict_t *dict) _PDFIO_PUBLIC; -extern pdfio_obj_t *pdfioFileCreateStringObj(pdfio_file_t *pdf, const char *s) _PDFIO_PUBLIC; -extern pdfio_file_t *pdfioFileCreateTemporary(char *buffer, size_t bufsize, const char *version, pdfio_rect_t *media_box, pdfio_rect_t *crop_box, pdfio_error_cb_t error_cb, void *error_data) _PDFIO_PUBLIC; -extern pdfio_obj_t *pdfioFileFindObj(pdfio_file_t *pdf, size_t number) _PDFIO_PUBLIC; -extern const char *pdfioFileGetAuthor(pdfio_file_t *pdf) _PDFIO_PUBLIC; -extern time_t pdfioFileGetCreationDate(pdfio_file_t *pdf) _PDFIO_PUBLIC; -extern const char *pdfioFileGetCreator(pdfio_file_t *pdf) _PDFIO_PUBLIC; -extern pdfio_array_t *pdfioFileGetID(pdfio_file_t *pdf) _PDFIO_PUBLIC; -extern const char *pdfioFileGetKeywords(pdfio_file_t *pdf) _PDFIO_PUBLIC; -extern const char *pdfioFileGetName(pdfio_file_t *pdf) _PDFIO_PUBLIC; -extern size_t pdfioFileGetNumObjs(pdfio_file_t *pdf) _PDFIO_PUBLIC; -extern size_t pdfioFileGetNumPages(pdfio_file_t *pdf) _PDFIO_PUBLIC; -extern pdfio_obj_t *pdfioFileGetObj(pdfio_file_t *pdf, size_t n) _PDFIO_PUBLIC; -extern pdfio_obj_t *pdfioFileGetPage(pdfio_file_t *pdf, size_t n) _PDFIO_PUBLIC; -extern pdfio_permission_t pdfioFileGetPermissions(pdfio_file_t *pdf, pdfio_encryption_t *encryption) _PDFIO_PUBLIC; -extern const char *pdfioFileGetProducer(pdfio_file_t *pdf) _PDFIO_PUBLIC; -extern const char *pdfioFileGetSubject(pdfio_file_t *pdf) _PDFIO_PUBLIC; -extern const char *pdfioFileGetTitle(pdfio_file_t *pdf) _PDFIO_PUBLIC; -extern const char *pdfioFileGetVersion(pdfio_file_t *pdf) _PDFIO_PUBLIC; -extern pdfio_file_t *pdfioFileOpen(const char *filename, pdfio_password_cb_t password_cb, void *password_data, pdfio_error_cb_t error_cb, void *error_data) _PDFIO_PUBLIC; -extern void pdfioFileSetAuthor(pdfio_file_t *pdf, const char *value) _PDFIO_PUBLIC; -extern void pdfioFileSetCreationDate(pdfio_file_t *pdf, time_t value) _PDFIO_PUBLIC; -extern void pdfioFileSetCreator(pdfio_file_t *pdf, const char *value) _PDFIO_PUBLIC; -extern void pdfioFileSetKeywords(pdfio_file_t *pdf, const char *value) _PDFIO_PUBLIC; -extern bool pdfioFileSetPermissions(pdfio_file_t *pdf, pdfio_permission_t permissions, pdfio_encryption_t encryption, const char *owner_password, const char *user_password) _PDFIO_PUBLIC; -extern void pdfioFileSetSubject(pdfio_file_t *pdf, const char *value) _PDFIO_PUBLIC; -extern void pdfioFileSetTitle(pdfio_file_t *pdf, const char *value) _PDFIO_PUBLIC; + // + // Types and constants... + // -extern bool pdfioObjClose(pdfio_obj_t *obj) _PDFIO_PUBLIC; -extern pdfio_obj_t *pdfioObjCopy(pdfio_file_t *pdf, pdfio_obj_t *srcobj) _PDFIO_PUBLIC; -extern pdfio_stream_t *pdfioObjCreateStream(pdfio_obj_t *obj, pdfio_filter_t compression) _PDFIO_PUBLIC; -extern pdfio_array_t *pdfioObjGetArray(pdfio_obj_t *obj) _PDFIO_PUBLIC; -extern pdfio_dict_t *pdfioObjGetDict(pdfio_obj_t *obj) _PDFIO_PUBLIC; -extern unsigned short pdfioObjGetGeneration(pdfio_obj_t *obj) _PDFIO_PUBLIC; -extern size_t pdfioObjGetLength(pdfio_obj_t *obj) _PDFIO_PUBLIC; -extern size_t pdfioObjGetNumber(pdfio_obj_t *obj) _PDFIO_PUBLIC; -extern const char *pdfioObjGetSubtype(pdfio_obj_t *obj) _PDFIO_PUBLIC; -extern const char *pdfioObjGetType(pdfio_obj_t *obj) _PDFIO_PUBLIC; -extern pdfio_stream_t *pdfioObjOpenStream(pdfio_obj_t *obj, bool decode) _PDFIO_PUBLIC; +#if _WIN32 + typedef __int64 ssize_t; // POSIX type not present on Windows... @private@ +#endif // _WIN32 -extern bool pdfioPageCopy(pdfio_file_t *pdf, pdfio_obj_t *srcpage) _PDFIO_PUBLIC; -extern size_t pdfioPageGetNumStreams(pdfio_obj_t *page) _PDFIO_PUBLIC; -extern pdfio_stream_t *pdfioPageOpenStream(pdfio_obj_t *page, size_t n, bool decode) _PDFIO_PUBLIC; + typedef struct _pdfio_array_s pdfio_array_t; + // Array of PDF values + typedef struct _pdfio_dict_s pdfio_dict_t; + // Key/value dictionary + typedef bool (*pdfio_dict_cb_t)(pdfio_dict_t *dict, const char *key, void *cb_data); + // Dictionary iterator callback + typedef struct _pdfio_file_s pdfio_file_t; + // PDF file + typedef bool (*pdfio_error_cb_t)(pdfio_file_t *pdf, const char *message, void *data); + // Error callback + typedef enum pdfio_encryption_e // PDF encryption modes + { + PDFIO_ENCRYPTION_NONE = 0, // No encryption + PDFIO_ENCRYPTION_RC4_40, // 40-bit RC4 encryption (PDF 1.3) + PDFIO_ENCRYPTION_RC4_128, // 128-bit RC4 encryption (PDF 1.4) + PDFIO_ENCRYPTION_AES_128, // 128-bit AES encryption (PDF 1.6) + PDFIO_ENCRYPTION_AES_256 // 256-bit AES encryption (PDF 2.0) @exclude all@ + } pdfio_encryption_t; + typedef enum pdfio_filter_e // Compression/decompression filters for streams + { + PDFIO_FILTER_NONE, // No filter + PDFIO_FILTER_ASCIIHEX, // ASCIIHexDecode filter (reading only) + PDFIO_FILTER_ASCII85, // ASCII85Decode filter (reading only) + PDFIO_FILTER_CCITTFAX, // CCITTFaxDecode filter + PDFIO_FILTER_CRYPT, // Encryption filter + PDFIO_FILTER_DCT, // DCTDecode (JPEG) filter + PDFIO_FILTER_FLATE, // FlateDecode filter + PDFIO_FILTER_JBIG2, // JBIG2Decode filter + PDFIO_FILTER_JPX, // JPXDecode filter (reading only) + PDFIO_FILTER_LZW, // LZWDecode filter (reading only) + PDFIO_FILTER_RUNLENGTH, // RunLengthDecode filter (reading only) + } pdfio_filter_t; + typedef struct _pdfio_obj_s pdfio_obj_t; // Numbered object in PDF file + typedef ssize_t (*pdfio_output_cb_t)(void *ctx, const void *data, size_t datalen); + // Output callback for pdfioFileCreateOutput + typedef const char *(*pdfio_password_cb_t)(void *data, const char *filename); + // Password callback for pdfioFileOpen + enum pdfio_permission_e // PDF permission bits + { + PDFIO_PERMISSION_NONE = 0, // No permissions + PDFIO_PERMISSION_PRINT = 0x0004, // PDF allows printing + PDFIO_PERMISSION_MODIFY = 0x0008, // PDF allows modification + PDFIO_PERMISSION_COPY = 0x0010, // PDF allows copying + PDFIO_PERMISSION_ANNOTATE = 0x0020, // PDF allows annotation + PDFIO_PERMISSION_FORMS = 0x0100, // PDF allows filling in forms + PDFIO_PERMISSION_READING = 0x0200, // PDF allows screen reading/accessibility (deprecated in PDF 2.0) + PDFIO_PERMISSION_ASSEMBLE = 0x0400, // PDF allows assembly (insert, delete, or rotate pages, add document outlines and thumbnails) + PDFIO_PERMISSION_PRINT_HIGH = 0x0800, // PDF allows high quality printing + PDFIO_PERMISSION_ALL = ~0 // All permissions + }; + typedef int pdfio_permission_t; // PDF permission bitfield + typedef struct pdfio_rect_s // PDF rectangle + { + double x1; // Lower-left X coordinate + double y1; // Lower-left Y coordinate + double x2; // Upper-right X coordinate + double y2; // Upper-right Y coordinate + } pdfio_rect_t; + typedef struct _pdfio_stream_s pdfio_stream_t; + // Object data stream in PDF file + typedef enum pdfio_valtype_e // PDF value types + { + PDFIO_VALTYPE_NONE, // No value, not set + PDFIO_VALTYPE_ARRAY, // Array + PDFIO_VALTYPE_BINARY, // Binary data + PDFIO_VALTYPE_BOOLEAN, // Boolean + PDFIO_VALTYPE_DATE, // Date/time + PDFIO_VALTYPE_DICT, // Dictionary + PDFIO_VALTYPE_INDIRECT, // Indirect object (N G obj) + PDFIO_VALTYPE_NAME, // Name + PDFIO_VALTYPE_NULL, // Null object + PDFIO_VALTYPE_NUMBER, // Number (integer or real) + PDFIO_VALTYPE_STRING // String + } pdfio_valtype_t; -extern bool pdfioStreamClose(pdfio_stream_t *st) _PDFIO_PUBLIC; -extern bool pdfioStreamConsume(pdfio_stream_t *st, size_t bytes) _PDFIO_PUBLIC; -extern bool pdfioStreamGetToken(pdfio_stream_t *st, char *buffer, size_t bufsize) _PDFIO_PUBLIC; -extern ssize_t pdfioStreamPeek(pdfio_stream_t *st, void *buffer, size_t bytes) _PDFIO_PUBLIC; -extern bool pdfioStreamPrintf(pdfio_stream_t *st, const char *format, ...) _PDFIO_PUBLIC _PDFIO_FORMAT(2,3); -extern bool pdfioStreamPutChar(pdfio_stream_t *st, int ch) _PDFIO_PUBLIC; -extern bool pdfioStreamPuts(pdfio_stream_t *st, const char *s) _PDFIO_PUBLIC; -extern ssize_t pdfioStreamRead(pdfio_stream_t *st, void *buffer, size_t bytes) _PDFIO_PUBLIC; -extern bool pdfioStreamWrite(pdfio_stream_t *st, const void *buffer, size_t bytes) _PDFIO_PUBLIC; + // + // Functions... + // -extern char *pdfioStringCreate(pdfio_file_t *pdf, const char *s) _PDFIO_PUBLIC; -extern char *pdfioStringCreatef(pdfio_file_t *pdf, const char *format, ...) _PDFIO_FORMAT(2,3) _PDFIO_PUBLIC; + extern bool pdfioArrayAppendArray(pdfio_array_t *a, pdfio_array_t *value) _PDFIO_PUBLIC; + extern bool pdfioArrayAppendBinary(pdfio_array_t *a, const unsigned char *value, size_t valuelen) _PDFIO_PUBLIC; + extern bool pdfioArrayAppendBoolean(pdfio_array_t *a, bool value) _PDFIO_PUBLIC; + extern bool pdfioArrayAppendDate(pdfio_array_t *a, time_t value) _PDFIO_PUBLIC; + extern bool pdfioArrayAppendDict(pdfio_array_t *a, pdfio_dict_t *value) _PDFIO_PUBLIC; + extern bool pdfioArrayAppendName(pdfio_array_t *a, const char *value) _PDFIO_PUBLIC; + extern bool pdfioArrayAppendNumber(pdfio_array_t *a, double value) _PDFIO_PUBLIC; + extern bool pdfioArrayAppendObj(pdfio_array_t *a, pdfio_obj_t *value) _PDFIO_PUBLIC; + extern bool pdfioArrayAppendString(pdfio_array_t *a, const char *value) _PDFIO_PUBLIC; + extern pdfio_array_t *pdfioArrayCopy(pdfio_file_t *pdf, pdfio_array_t *a) _PDFIO_PUBLIC; + extern pdfio_array_t *pdfioArrayCreate(pdfio_file_t *pdf) _PDFIO_PUBLIC; + extern pdfio_array_t *pdfioArrayGetArray(pdfio_array_t *a, size_t n) _PDFIO_PUBLIC; + extern unsigned char *pdfioArrayGetBinary(pdfio_array_t *a, size_t n, size_t *length) _PDFIO_PUBLIC; + extern bool pdfioArrayGetBoolean(pdfio_array_t *a, size_t n) _PDFIO_PUBLIC; + extern time_t pdfioArrayGetDate(pdfio_array_t *a, size_t n) _PDFIO_PUBLIC; + extern pdfio_dict_t *pdfioArrayGetDict(pdfio_array_t *a, size_t n) _PDFIO_PUBLIC; + extern const char *pdfioArrayGetName(pdfio_array_t *a, size_t n) _PDFIO_PUBLIC; + extern double pdfioArrayGetNumber(pdfio_array_t *a, size_t n) _PDFIO_PUBLIC; + extern pdfio_obj_t *pdfioArrayGetObj(pdfio_array_t *a, size_t n) _PDFIO_PUBLIC; + extern size_t pdfioArrayGetSize(pdfio_array_t *a) _PDFIO_PUBLIC; + extern const char *pdfioArrayGetString(pdfio_array_t *a, size_t n) _PDFIO_PUBLIC; + extern pdfio_valtype_t pdfioArrayGetType(pdfio_array_t *a, size_t n) _PDFIO_PUBLIC; + extern pdfio_dict_t *pdfioDictCopy(pdfio_file_t *pdf, pdfio_dict_t *dict) _PDFIO_PUBLIC; + extern pdfio_dict_t *pdfioDictCreate(pdfio_file_t *pdf) _PDFIO_PUBLIC; + extern pdfio_array_t *pdfioDictGetArray(pdfio_dict_t *dict, const char *key) _PDFIO_PUBLIC; + extern unsigned char *pdfioDictGetBinary(pdfio_dict_t *dict, const char *key, size_t *length) _PDFIO_PUBLIC; + extern bool pdfioDictGetBoolean(pdfio_dict_t *dict, const char *key) _PDFIO_PUBLIC; + extern time_t pdfioDictGetDate(pdfio_dict_t *dict, const char *key) _PDFIO_PUBLIC; + extern pdfio_dict_t *pdfioDictGetDict(pdfio_dict_t *dict, const char *key) _PDFIO_PUBLIC; + extern const char *pdfioDictGetName(pdfio_dict_t *dict, const char *key) _PDFIO_PUBLIC; + extern double pdfioDictGetNumber(pdfio_dict_t *dict, const char *key) _PDFIO_PUBLIC; + extern pdfio_obj_t *pdfioDictGetObj(pdfio_dict_t *dict, const char *key) _PDFIO_PUBLIC; + extern pdfio_rect_t *pdfioDictGetRect(pdfio_dict_t *dict, const char *key, pdfio_rect_t *rect) _PDFIO_PUBLIC; + extern const char *pdfioDictGetString(pdfio_dict_t *dict, const char *key) _PDFIO_PUBLIC; + extern pdfio_valtype_t pdfioDictGetType(pdfio_dict_t *dict, const char *key) _PDFIO_PUBLIC; + extern size_t pdfioDictGetNumPairs(pdfio_dict_t *dict) _PDFIO_PUBLIC; + extern const char *pdfioDictGetKeyByIndex(pdfio_dict_t *dict, size_t index) _PDFIO_PUBLIC; + extern void pdfioDictIterateKeys(pdfio_dict_t *dict, pdfio_dict_cb_t cb, void *cb_data) _PDFIO_PUBLIC; + extern bool pdfioDictSetArray(pdfio_dict_t *dict, const char *key, pdfio_array_t *value) _PDFIO_PUBLIC; + extern bool pdfioDictSetBinary(pdfio_dict_t *dict, const char *key, const unsigned char *value, size_t valuelen) _PDFIO_PUBLIC; + extern bool pdfioDictSetBoolean(pdfio_dict_t *dict, const char *key, bool value) _PDFIO_PUBLIC; + extern bool pdfioDictSetDate(pdfio_dict_t *dict, const char *key, time_t value) _PDFIO_PUBLIC; + extern bool pdfioDictSetDict(pdfio_dict_t *dict, const char *key, pdfio_dict_t *value) _PDFIO_PUBLIC; + extern bool pdfioDictSetName(pdfio_dict_t *dict, const char *key, const char *value) _PDFIO_PUBLIC; + extern bool pdfioDictSetNull(pdfio_dict_t *dict, const char *key) _PDFIO_PUBLIC; + extern bool pdfioDictSetNumber(pdfio_dict_t *dict, const char *key, double value) _PDFIO_PUBLIC; + extern bool pdfioDictSetObj(pdfio_dict_t *dict, const char *key, pdfio_obj_t *value) _PDFIO_PUBLIC; + extern bool pdfioDictSetRect(pdfio_dict_t *dict, const char *key, pdfio_rect_t *value) _PDFIO_PUBLIC; + extern bool pdfioDictSetString(pdfio_dict_t *dict, const char *key, const char *value) _PDFIO_PUBLIC; + extern bool pdfioDictSetStringf(pdfio_dict_t *dict, const char *key, const char *format, ...) _PDFIO_PUBLIC _PDFIO_FORMAT(3, 4); -# ifdef __cplusplus + extern bool pdfioFileClose(pdfio_file_t *pdf) _PDFIO_PUBLIC; + extern pdfio_file_t *pdfioFileCreate(const char *filename, const char *version, pdfio_rect_t *media_box, pdfio_rect_t *crop_box, pdfio_error_cb_t error_cb, void *error_data) _PDFIO_PUBLIC; + extern pdfio_obj_t *pdfioFileCreateArrayObj(pdfio_file_t *pdf, pdfio_array_t *array) _PDFIO_PUBLIC; + extern pdfio_obj_t *pdfioFileCreateNumberObj(pdfio_file_t *pdf, double number) _PDFIO_PUBLIC; + extern pdfio_obj_t *pdfioFileCreateObj(pdfio_file_t *pdf, pdfio_dict_t *dict) _PDFIO_PUBLIC; + extern pdfio_file_t *pdfioFileCreateOutput(pdfio_output_cb_t output_cb, void *output_ctx, const char *version, pdfio_rect_t *media_box, pdfio_rect_t *crop_box, pdfio_error_cb_t error_cb, void *error_data) _PDFIO_PUBLIC; + // TODO: Add number, array, string, etc. versions of pdfioFileCreateObject? + extern pdfio_stream_t *pdfioFileCreatePage(pdfio_file_t *pdf, pdfio_dict_t *dict) _PDFIO_PUBLIC; + extern pdfio_obj_t *pdfioFileCreateStringObj(pdfio_file_t *pdf, const char *s) _PDFIO_PUBLIC; + extern pdfio_file_t *pdfioFileCreateTemporary(char *buffer, size_t bufsize, const char *version, pdfio_rect_t *media_box, pdfio_rect_t *crop_box, pdfio_error_cb_t error_cb, void *error_data) _PDFIO_PUBLIC; + extern pdfio_obj_t *pdfioFileFindObj(pdfio_file_t *pdf, size_t number) _PDFIO_PUBLIC; + extern const char *pdfioFileGetAuthor(pdfio_file_t *pdf) _PDFIO_PUBLIC; + extern time_t pdfioFileGetCreationDate(pdfio_file_t *pdf) _PDFIO_PUBLIC; + extern const char *pdfioFileGetCreator(pdfio_file_t *pdf) _PDFIO_PUBLIC; + extern pdfio_array_t *pdfioFileGetID(pdfio_file_t *pdf) _PDFIO_PUBLIC; + extern const char *pdfioFileGetKeywords(pdfio_file_t *pdf) _PDFIO_PUBLIC; + extern const char *pdfioFileGetName(pdfio_file_t *pdf) _PDFIO_PUBLIC; + extern size_t pdfioFileGetNumObjs(pdfio_file_t *pdf) _PDFIO_PUBLIC; + extern size_t pdfioFileGetNumPages(pdfio_file_t *pdf) _PDFIO_PUBLIC; + extern pdfio_obj_t *pdfioFileGetObj(pdfio_file_t *pdf, size_t n) _PDFIO_PUBLIC; + extern pdfio_obj_t *pdfioFileGetPage(pdfio_file_t *pdf, size_t n) _PDFIO_PUBLIC; + extern pdfio_permission_t pdfioFileGetPermissions(pdfio_file_t *pdf, pdfio_encryption_t *encryption) _PDFIO_PUBLIC; + extern const char *pdfioFileGetProducer(pdfio_file_t *pdf) _PDFIO_PUBLIC; + extern const char *pdfioFileGetSubject(pdfio_file_t *pdf) _PDFIO_PUBLIC; + extern const char *pdfioFileGetTitle(pdfio_file_t *pdf) _PDFIO_PUBLIC; + extern const char *pdfioFileGetVersion(pdfio_file_t *pdf) _PDFIO_PUBLIC; + extern pdfio_file_t *pdfioFileOpen(const char *filename, pdfio_password_cb_t password_cb, void *password_data, pdfio_error_cb_t error_cb, void *error_data) _PDFIO_PUBLIC; + extern void pdfioFileSetAuthor(pdfio_file_t *pdf, const char *value) _PDFIO_PUBLIC; + extern void pdfioFileSetCreationDate(pdfio_file_t *pdf, time_t value) _PDFIO_PUBLIC; + extern void pdfioFileSetCreator(pdfio_file_t *pdf, const char *value) _PDFIO_PUBLIC; + extern void pdfioFileSetKeywords(pdfio_file_t *pdf, const char *value) _PDFIO_PUBLIC; + extern bool pdfioFileSetPermissions(pdfio_file_t *pdf, pdfio_permission_t permissions, pdfio_encryption_t encryption, const char *owner_password, const char *user_password) _PDFIO_PUBLIC; + extern void pdfioFileSetSubject(pdfio_file_t *pdf, const char *value) _PDFIO_PUBLIC; + extern void pdfioFileSetTitle(pdfio_file_t *pdf, const char *value) _PDFIO_PUBLIC; + + extern bool pdfioObjClose(pdfio_obj_t *obj) _PDFIO_PUBLIC; + extern pdfio_obj_t *pdfioObjCopy(pdfio_file_t *pdf, pdfio_obj_t *srcobj) _PDFIO_PUBLIC; + extern pdfio_stream_t *pdfioObjCreateStream(pdfio_obj_t *obj, pdfio_filter_t compression) _PDFIO_PUBLIC; + extern pdfio_array_t *pdfioObjGetArray(pdfio_obj_t *obj) _PDFIO_PUBLIC; + extern pdfio_dict_t *pdfioObjGetDict(pdfio_obj_t *obj) _PDFIO_PUBLIC; + extern unsigned short pdfioObjGetGeneration(pdfio_obj_t *obj) _PDFIO_PUBLIC; + extern size_t pdfioObjGetLength(pdfio_obj_t *obj) _PDFIO_PUBLIC; + extern size_t pdfioObjGetNumber(pdfio_obj_t *obj) _PDFIO_PUBLIC; + extern const char *pdfioObjGetSubtype(pdfio_obj_t *obj) _PDFIO_PUBLIC; + extern const char *pdfioObjGetType(pdfio_obj_t *obj) _PDFIO_PUBLIC; + extern pdfio_stream_t *pdfioObjOpenStream(pdfio_obj_t *obj, bool decode) _PDFIO_PUBLIC; + + extern bool pdfioPageCopy(pdfio_file_t *pdf, pdfio_obj_t *srcpage) _PDFIO_PUBLIC; + extern size_t pdfioPageGetNumStreams(pdfio_obj_t *page) _PDFIO_PUBLIC; + extern pdfio_stream_t *pdfioPageOpenStream(pdfio_obj_t *page, size_t n, bool decode) _PDFIO_PUBLIC; + + extern bool pdfioStreamClose(pdfio_stream_t *st) _PDFIO_PUBLIC; + extern bool pdfioStreamConsume(pdfio_stream_t *st, size_t bytes) _PDFIO_PUBLIC; + extern bool pdfioStreamGetToken(pdfio_stream_t *st, char *buffer, size_t bufsize) _PDFIO_PUBLIC; + extern ssize_t pdfioStreamPeek(pdfio_stream_t *st, void *buffer, size_t bytes) _PDFIO_PUBLIC; + extern bool pdfioStreamPrintf(pdfio_stream_t *st, const char *format, ...) _PDFIO_PUBLIC _PDFIO_FORMAT(2, 3); + extern bool pdfioStreamPutChar(pdfio_stream_t *st, int ch) _PDFIO_PUBLIC; + extern bool pdfioStreamPuts(pdfio_stream_t *st, const char *s) _PDFIO_PUBLIC; + extern ssize_t pdfioStreamRead(pdfio_stream_t *st, void *buffer, size_t bytes) _PDFIO_PUBLIC; + extern bool pdfioStreamWrite(pdfio_stream_t *st, const void *buffer, size_t bytes) _PDFIO_PUBLIC; + + extern char *pdfioStringCreate(pdfio_file_t *pdf, const char *s) _PDFIO_PUBLIC; + extern char *pdfioStringCreatef(pdfio_file_t *pdf, const char *format, ...) _PDFIO_FORMAT(2, 3) _PDFIO_PUBLIC; + +#ifdef __cplusplus } -# endif // __cplusplus +#endif // __cplusplus #endif // !PDFIO_H diff --git a/test_mod.c b/test_mod.c index 2ef6155..836048c 100644 --- a/test_mod.c +++ b/test_mod.c @@ -17,14 +17,15 @@ int main (int argc, char **argv) pdfio_obj_t *obj_page = pdfioFileGetPage(pdf, 0); pdfio_dict_t *dict_page = pdfioObjGetDict(obj_page); - size_t num_keys = pdfioDictGetNumKeys(dict_page); + size_t num_keys = pdfioDictGetNumPairs(dict_page); printf("Number of keys in this page: %d\n", num_keys); + const char *key; for (unsigned int i = 0; i < num_keys; ++i) { - pdfio_dictKey_t dict_key; - pdfioDictGetKeyByIndex(dict_page, i, &dict_key); - printf("\t%s (%d)\n", dict_key.key, dict_key.type); + key = pdfioDictGetKeyByIndex(dict_page, i); + pdfio_valtype_t type = pdfioDictGetType(dict_page, key); + printf("\t%s (%d)\n", key, type); } pdfioFileClose(pdf);