Compare commits

..

1 Commits

3 changed files with 518 additions and 431 deletions

View File

@ -9,23 +9,27 @@
#include "pdfio-private.h"
//
// Local functions...
//
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
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...
@ -48,6 +52,7 @@ void _pdfioDictClear(pdfio_dict_t *dict, // I - Dictionary
}
}
//
// 'pdfioDictCopy()' - Copy a dictionary to a PDF file.
//
@ -62,6 +67,7 @@ pdfioDictCopy(pdfio_file_t *pdf, // I - PDF file
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);
// Create the new dictionary...
@ -113,6 +119,7 @@ pdfioDictCopy(pdfio_file_t *pdf, // I - PDF file
return (ndict);
}
//
// 'pdfioDictCreate()' - Create a dictionary to hold key/value pairs.
//
@ -122,6 +129,7 @@ pdfioDictCreate(pdfio_file_t *pdf) // I - PDF file
{
pdfio_dict_t *dict; // New dictionary
if (!pdf)
return (NULL);
@ -149,6 +157,7 @@ pdfioDictCreate(pdfio_file_t *pdf) // I - PDF file
return (dict);
}
//
// '_pdfioDictDecrypt()' - Decrypt the values in a dictionary.
//
@ -162,6 +171,7 @@ _pdfioDictDecrypt(pdfio_file_t *pdf, // I - PDF file
size_t i; // Looping var
_pdfio_pair_t *pair; // Current 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))
@ -171,16 +181,19 @@ _pdfioDictDecrypt(pdfio_file_t *pdf, // I - PDF file
return (true);
}
//
// '_pdfioDictDebug()' - Dump a dictionary to stderr.
//
void _pdfioDictDebug(pdfio_dict_t *dict, // I - Dictionary
void
_pdfioDictDebug(pdfio_dict_t *dict, // I - Dictionary
FILE *fp) // I - Output file
{
size_t i; // Looping var
_pdfio_pair_t *pair; // Current pair
for (i = dict->num_pairs, pair = dict->pairs; i > 0; i --, pair ++)
{
fprintf(fp, "/%s", pair->key);
@ -188,11 +201,13 @@ void _pdfioDictDebug(pdfio_dict_t *dict, // I - Dictionary
}
}
//
// '_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)
{
@ -211,6 +226,7 @@ void _pdfioDictDelete(pdfio_dict_t *dict) // I - Dictionary
free(dict);
}
//
// 'pdfioDictGetArray()' - Get a key array value from a dictionary.
//
@ -221,12 +237,14 @@ pdfioDictGetArray(pdfio_dict_t *dict, // I - Dictionary
{
_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.
//
@ -238,6 +256,7 @@ pdfioDictGetBinary(pdfio_dict_t *dict, // I - Dictionary
{
_pdfio_value_t *value = _pdfioDictGetValue(dict, key);
if (!length)
return (NULL);
@ -258,6 +277,7 @@ pdfioDictGetBinary(pdfio_dict_t *dict, // I - Dictionary
}
}
//
// 'pdfioDictGetBoolean()' - Get a key boolean value from a dictionary.
//
@ -268,12 +288,14 @@ pdfioDictGetBoolean(pdfio_dict_t *dict, // I - Dictionary
{
_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.
//
@ -284,12 +306,14 @@ pdfioDictGetDate(pdfio_dict_t *dict, // I - Dictionary
{
_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.
//
@ -300,12 +324,14 @@ pdfioDictGetDict(pdfio_dict_t *dict, // I - Dictionary
{
_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.
//
@ -316,12 +342,14 @@ pdfioDictGetName(pdfio_dict_t *dict, // I - Dictionary
{
_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.
//
@ -332,12 +360,14 @@ pdfioDictGetNumber(pdfio_dict_t *dict, // I - Dictionary
{
_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.
//
@ -348,12 +378,14 @@ pdfioDictGetObj(pdfio_dict_t *dict, // I - Dictionary
{
_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.
//
@ -365,6 +397,7 @@ pdfioDictGetRect(pdfio_dict_t *dict, // I - Dictionary
{
_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);
@ -380,6 +413,7 @@ pdfioDictGetRect(pdfio_dict_t *dict, // I - Dictionary
}
}
//
// 'pdfioDictGetString()' - Get a key string value from a dictionary.
//
@ -390,12 +424,14 @@ pdfioDictGetString(pdfio_dict_t *dict, // I - Dictionary
{
_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.
//
@ -406,9 +442,11 @@ pdfioDictGetType(pdfio_dict_t *dict, // I - Dictionary
{
_pdfio_value_t *value = _pdfioDictGetValue(dict, key);
return (value ? value->type : PDFIO_VALTYPE_NONE);
}
//
// '_pdfioDictGetValue()' - Get a key value from a dictionary.
//
@ -420,6 +458,7 @@ _pdfioDictGetValue(pdfio_dict_t *dict, // I - Dictionary
_pdfio_pair_t temp, // Search key
*match; // Matching key pair
PDFIO_DEBUG("_pdfioDictGetValue(dict=%p, key=\"%s\")\n", dict, key);
if (!dict || !dict->num_pairs || !key)
@ -444,6 +483,7 @@ _pdfioDictGetValue(pdfio_dict_t *dict, // I - Dictionary
}
}
//
// 'pdfioDictIterateKeys()' - Iterate the keys in a dictionary.
//
@ -463,7 +503,8 @@ _pdfioDictGetValue(pdfio_dict_t *dict, // I - Dictionary
// have been iterated.
//
void pdfioDictIterateKeys(
void
pdfioDictIterateKeys(
pdfio_dict_t *dict, // I - Dictionary
pdfio_dict_cb_t cb, // I - Callback function
void *cb_data) // I - Callback data
@ -471,6 +512,7 @@ void pdfioDictIterateKeys(
size_t i; // Looping var
_pdfio_pair_t *pair; // Current pair
// Range check input...
if (!dict || !cb)
return;
@ -487,11 +529,9 @@ void pdfioDictIterateKeys(
//
size_t // O - Number of keys
pdfioDictGetNumPairs(pdfio_dict_t *dict) // I - Dictionary
pdfioDictGetNumKeys(pdfio_dict_t *dict) // I - Dictionary
{
if (dict)
return (dict->num_pairs);
return (0);
}
//
@ -499,13 +539,20 @@ pdfioDictGetNumPairs(pdfio_dict_t *dict) // I - Dictionary
//
// Alternative form to enumerate the keys of a dictionary
const char * // O - Value
bool // O - Value
pdfioDictGetKeyByIndex(pdfio_dict_t *dict, // I - Dictionary
size_t index) // I - Index
size_t index, // I - Index
pdfio_dictKey_t *key) // I - struct pointer
{
if (index < dict->num_pairs)
return (dict->pairs[index].key);
return (NULL);
{
if (key){
key->key = dict->pairs[index].key;
key->type = (pdfio_valtype_t **)(dict->pairs[index].value.type);
return (true);
}
}
return (false);
}
//
@ -524,6 +571,7 @@ _pdfioDictRead(pdfio_file_t *pdf, // I - PDF file
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);
// Create a dictionary and start reading...
@ -570,6 +618,7 @@ _pdfioDictRead(pdfio_file_t *pdf, // I - PDF file
return (NULL);
}
//
// 'pdfioDictSetArray()' - Set a key array in a dictionary.
//
@ -581,6 +630,7 @@ pdfioDictSetArray(pdfio_dict_t *dict, // I - Dictionary
{
_pdfio_value_t temp; // New value
// Range check input...
if (!dict || !key || !value)
return (false);
@ -592,10 +642,12 @@ pdfioDictSetArray(pdfio_dict_t *dict, // I - Dictionary
return (_pdfioDictSetValue(dict, key, &temp));
}
//
// 'pdfioDictSetBinary()' - Set a key binary string in a dictionary.
//
bool // O - `true` on success, `false` on failure
pdfioDictSetBinary(
pdfio_dict_t *dict, // I - Dictionary
@ -605,6 +657,7 @@ pdfioDictSetBinary(
{
_pdfio_value_t temp; // New value
// Range check input...
if (!dict || !key || !value || !valuelen)
return (false);
@ -627,6 +680,7 @@ pdfioDictSetBinary(
return (true);
}
//
// 'pdfioDictSetBoolean()' - Set a key boolean in a dictionary.
//
@ -638,6 +692,7 @@ pdfioDictSetBoolean(pdfio_dict_t *dict, // I - Dictionary
{
_pdfio_value_t temp; // New value
// Range check input...
if (!dict || !key)
return (false);
@ -649,6 +704,7 @@ pdfioDictSetBoolean(pdfio_dict_t *dict, // I - Dictionary
return (_pdfioDictSetValue(dict, key, &temp));
}
//
// 'pdfioDictSetDate()' - Set a date value in a dictionary.
//
@ -660,6 +716,7 @@ pdfioDictSetDate(pdfio_dict_t *dict, // I - Dictionary
{
_pdfio_value_t temp; // New value
// Range check input...
if (!dict || !key)
return (false);
@ -671,6 +728,7 @@ pdfioDictSetDate(pdfio_dict_t *dict, // I - Dictionary
return (_pdfioDictSetValue(dict, key, &temp));
}
//
// 'pdfioDictSetDict()' - Set a key dictionary in a dictionary.
//
@ -682,6 +740,7 @@ pdfioDictSetDict(pdfio_dict_t *dict, // I - Dictionary
{
_pdfio_value_t temp; // New value
// Range check input...
if (!dict || !key || !value)
return (false);
@ -693,6 +752,7 @@ pdfioDictSetDict(pdfio_dict_t *dict, // I - Dictionary
return (_pdfioDictSetValue(dict, key, &temp));
}
//
// 'pdfioDictSetName()' - Set a key name in a dictionary.
//
@ -704,6 +764,7 @@ pdfioDictSetName(pdfio_dict_t *dict, // I - Dictionary
{
_pdfio_value_t temp; // New value
// Range check input...
if (!dict || !key || !value)
return (false);
@ -715,6 +776,7 @@ pdfioDictSetName(pdfio_dict_t *dict, // I - Dictionary
return (_pdfioDictSetValue(dict, key, &temp));
}
//
// 'pdfioDictSetNull()' - Set a key null in a dictionary.
//
@ -725,6 +787,7 @@ pdfioDictSetNull(pdfio_dict_t *dict, // I - Dictionary
{
_pdfio_value_t temp; // New value
// Range check input...
if (!dict || !key)
return (false);
@ -735,6 +798,7 @@ pdfioDictSetNull(pdfio_dict_t *dict, // I - Dictionary
return (_pdfioDictSetValue(dict, key, &temp));
}
//
// 'pdfioDictSetNumber()' - Set a key number in a dictionary.
//
@ -746,6 +810,7 @@ pdfioDictSetNumber(pdfio_dict_t *dict, // I - Dictionary
{
_pdfio_value_t temp; // New value
// Range check input...
if (!dict || !key)
return (false);
@ -757,6 +822,7 @@ pdfioDictSetNumber(pdfio_dict_t *dict, // I - Dictionary
return (_pdfioDictSetValue(dict, key, &temp));
}
//
// 'pdfioDictSetObj()' - Set a key indirect object reference in a dictionary.
//
@ -768,6 +834,7 @@ pdfioDictSetObj(pdfio_dict_t *dict, // I - Dictionary
{
_pdfio_value_t temp; // New value
// Range check input...
if (!dict || !key || !value)
return (false);
@ -780,6 +847,7 @@ pdfioDictSetObj(pdfio_dict_t *dict, // I - Dictionary
return (_pdfioDictSetValue(dict, key, &temp));
}
//
// 'pdfioDictSetRect()' - Set a key rectangle in a dictionary.
//
@ -791,6 +859,7 @@ pdfioDictSetRect(pdfio_dict_t *dict, // I - Dictionary
{
_pdfio_value_t temp; // New value
// Range check input...
if (!dict || !key || !value)
return (false);
@ -807,6 +876,7 @@ pdfioDictSetRect(pdfio_dict_t *dict, // I - Dictionary
return (_pdfioDictSetValue(dict, key, &temp));
}
//
// 'pdfioDictSetString()' - Set a key literal string in a dictionary.
//
@ -818,6 +888,7 @@ pdfioDictSetString(pdfio_dict_t *dict, // I - Dictionary
{
_pdfio_value_t temp; // New value
// Range check input...
if (!dict || !key || !value)
return (false);
@ -829,6 +900,7 @@ pdfioDictSetString(pdfio_dict_t *dict, // I - Dictionary
return (_pdfioDictSetValue(dict, key, &temp));
}
//
// 'pdfioDictSetStringf()' - Set a key formatted string in a dictionary.
//
@ -843,6 +915,7 @@ pdfioDictSetStringf(
char buffer[8192]; // String buffer
va_list ap; // Argument list
// Range check input...
if (!dict || !key || !format)
return (false);
@ -855,6 +928,7 @@ pdfioDictSetStringf(
return (pdfioDictSetString(dict, key, buffer));
}
//
// '_pdfioDictSetValue()' - Set a key value in a dictionary.
//
@ -867,6 +941,7 @@ _pdfioDictSetValue(
{
_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...
@ -923,6 +998,7 @@ _pdfioDictSetValue(
return (true);
}
//
// '_pdfioDictWrite()' - Write a dictionary to a PDF file.
//
@ -936,6 +1012,7 @@ _pdfioDictWrite(pdfio_dict_t *dict, // I - Dictionary
size_t i; // Looping var
_pdfio_pair_t *pair; // Current key/value pair
if (length)
*length = 0;
@ -964,6 +1041,7 @@ _pdfioDictWrite(pdfio_dict_t *dict, // I - Dictionary
return (_pdfioFilePuts(pdf, ">>"));
}
//
// 'compare_pairs()' - Compare the keys for two pairs.
//

18
pdfio.h
View File

@ -15,16 +15,17 @@
# include <sys/types.h>
# include <time.h>
# ifdef __cplusplus
extern "C"
{
extern "C" {
# endif // __cplusplus
//
// Version number...
//
# define PDFIO_VERSION "1.2.1"
//
// Visibility and other annotations...
//
@ -39,6 +40,7 @@ extern "C"
# define _PDFIO_DEPRECATED
# endif // __has_extension || __GNUC__
//
// Types and constants...
//
@ -121,6 +123,13 @@ extern "C"
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;
//
// Functions...
@ -162,8 +171,8 @@ extern "C"
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 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;
@ -242,6 +251,7 @@ extern "C"
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

View File

@ -17,15 +17,14 @@ 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 = pdfioDictGetNumPairs(dict_page);
size_t num_keys = pdfioDictGetNumKeys(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)
{
key = pdfioDictGetKeyByIndex(dict_page, i);
pdfio_valtype_t type = pdfioDictGetType(dict_page, key);
printf("\t%s (%d)\n", key, type);
pdfio_dictKey_t dict_key;
pdfioDictGetKeyByIndex(dict_page, i, &dict_key);
printf("\t%s (%d)\n", dict_key.key, dict_key.type);
}
pdfioFileClose(pdf);