2 * << Haru Free PDF Library >> -- hpdf_dict.c
4 * URL: http://libharu.org
6 * Copyright (c) 1999-2006 Takeshi Kanno <takeshi_kanno@est.hi-ho.ne.jp>
7 * Copyright (c) 2007-2009 Antony Dovgal <tony@daylessday.org>
9 * Permission to use, copy, modify, distribute and sell this software
10 * and its documentation for any purpose is hereby granted without fee,
11 * provided that the above copyright notice appear in all copies and
12 * that both that copyright notice and this permission notice appear
13 * in supporting documentation.
14 * It is provided "as is" without express or implied warranty.
18 #include "hpdf_conf.h"
19 #include "hpdf_utils.h"
20 #include "hpdf_objects.h"
23 GetElement (HPDF_Dict dict,
26 /*--------------------------------------------------------------------------*/
29 HPDF_Dict_New (HPDF_MMgr mmgr)
33 obj = (HPDF_Dict)HPDF_GetMem (mmgr, sizeof(HPDF_Dict_Rec));
35 HPDF_MemSet (obj, 0, sizeof(HPDF_Dict_Rec));
36 obj->header.obj_class = HPDF_OCLASS_DICT;
38 obj->error = mmgr->error;
39 obj->list = HPDF_List_New (mmgr, HPDF_DEF_ITEMS_PER_BLOCK);
40 obj->filter = HPDF_STREAM_FILTER_NONE;
42 HPDF_FreeMem (mmgr, obj);
52 HPDF_DictStream_New (HPDF_MMgr mmgr,
59 obj = HPDF_Dict_New (mmgr);
63 /* only stream object is added to xref automatically */
64 ret += HPDF_Xref_Add (xref, obj);
68 length = HPDF_Number_New (mmgr, 0);
72 ret = HPDF_Xref_Add (xref, length);
76 ret = HPDF_Dict_Add (obj, "Length", length);
80 obj->stream = HPDF_MemStream_New (mmgr, HPDF_STREAM_BUF_SIZ);
89 HPDF_Dict_Free (HPDF_Dict dict)
99 for (i = 0; i < dict->list->count; i++) {
100 HPDF_DictElement element =
101 (HPDF_DictElement)HPDF_List_ItemAt (dict->list, i);
104 HPDF_Obj_Free (dict->mmgr, element->value);
105 HPDF_FreeMem (dict->mmgr, element);
110 HPDF_Stream_Free (dict->stream);
112 HPDF_List_Free (dict->list);
114 dict->header.obj_class = 0;
116 HPDF_FreeMem (dict->mmgr, dict);
120 HPDF_Dict_Add_FilterParams(HPDF_Dict dict, HPDF_Dict filterParam)
122 HPDF_Array paramArray;
123 /* prepare params object */
124 paramArray = HPDF_Dict_GetItem (dict, "DecodeParms",
126 if(paramArray==NULL) {
127 paramArray = HPDF_Array_New (dict->mmgr);
129 return HPDF_Error_GetCode (dict->error);
132 HPDF_Dict_Add(dict, "DecodeParms", paramArray);
134 HPDF_Array_Add(paramArray, filterParam);
140 HPDF_Dict_Write (HPDF_Dict dict,
147 ret = HPDF_Stream_WriteStr (stream, "<<\012");
151 if (dict->before_write_fn) {
152 if ((ret = dict->before_write_fn (dict)) != HPDF_OK)
156 /* encrypt-dict must not be encrypted. */
157 if (dict->header.obj_class == (HPDF_OCLASS_DICT | HPDF_OSUBCLASS_ENCRYPT))
161 /* set filter element */
162 if (dict->filter == HPDF_STREAM_FILTER_NONE)
163 HPDF_Dict_RemoveElement (dict, "Filter");
165 HPDF_Array array = HPDF_Dict_GetItem (dict, "Filter",
169 array = HPDF_Array_New (dict->mmgr);
171 return HPDF_Error_GetCode (dict->error);
173 ret = HPDF_Dict_Add (dict, "Filter", array);
178 HPDF_Array_Clear (array);
180 #ifndef LIBHPDF_HAVE_NOZLIB
181 if (dict->filter & HPDF_STREAM_FILTER_FLATE_DECODE)
182 HPDF_Array_AddName (array, "FlateDecode");
183 #endif /* LIBHPDF_HAVE_NOZLIB */
185 if (dict->filter & HPDF_STREAM_FILTER_DCT_DECODE)
186 HPDF_Array_AddName (array, "DCTDecode");
188 if(dict->filter & HPDF_STREAM_FILTER_CCITT_DECODE)
189 HPDF_Array_AddName (array, "CCITTFaxDecode");
191 if(dict->filterParams!=NULL)
193 HPDF_Dict_Add_FilterParams(dict, dict->filterParams);
198 for (i = 0; i < dict->list->count; i++) {
199 HPDF_DictElement element =
200 (HPDF_DictElement)HPDF_List_ItemAt (dict->list, i);
201 HPDF_Obj_Header *header = (HPDF_Obj_Header *)(element->value);
204 return HPDF_SetError (dict->error, HPDF_INVALID_OBJECT, 0);
206 if (header->obj_id & HPDF_OTYPE_HIDDEN) {
207 HPDF_PTRACE((" HPDF_Dict_Write obj=%p skipped obj_id=0x%08X\n",
208 element->value, (HPDF_UINT)header->obj_id));
210 ret = HPDF_Stream_WriteEscapeName (stream, element->key);
214 ret = HPDF_Stream_WriteChar (stream, ' ');
218 ret = HPDF_Obj_Write (element->value, stream, e);
222 ret = HPDF_Stream_WriteStr (stream, "\012");
228 if (dict->write_fn) {
229 if ((ret = dict->write_fn (dict, stream)) != HPDF_OK)
233 if ((ret = HPDF_Stream_WriteStr (stream, ">>")) != HPDF_OK)
240 /* get "length" element */
241 length = (HPDF_Number)HPDF_Dict_GetItem (dict, "Length",
244 return HPDF_SetError (dict->error,
245 HPDF_DICT_STREAM_LENGTH_NOT_FOUND, 0);
247 /* "length" element must be indirect-object */
248 if (!(length->header.obj_id & HPDF_OTYPE_INDIRECT)) {
249 return HPDF_SetError (dict->error, HPDF_DICT_ITEM_UNEXPECTED_TYPE,
253 if ((ret = HPDF_Stream_WriteStr (stream, "\012stream\015\012")) /* Acrobat 8.15 requires both \r and \n here */
257 strptr = stream->size;
260 HPDF_Encrypt_Reset (e);
262 if ((ret = HPDF_Stream_WriteToStream (dict->stream, stream,
263 dict->filter, e)) != HPDF_OK)
266 HPDF_Number_SetValue (length, stream->size - strptr);
268 ret = HPDF_Stream_WriteStr (stream, "\012endstream");
271 /* 2006.08.13 add. */
272 if (dict->after_write_fn) {
273 if ((ret = dict->after_write_fn (dict)) != HPDF_OK)
281 HPDF_Dict_Add (HPDF_Dict dict,
285 HPDF_Obj_Header *header;
286 HPDF_STATUS ret = HPDF_OK;
287 HPDF_DictElement element;
290 if (HPDF_Error_GetCode (dict->error) == HPDF_OK)
291 return HPDF_SetError (dict->error, HPDF_INVALID_OBJECT, 0);
293 return HPDF_INVALID_OBJECT;
296 header = (HPDF_Obj_Header *)obj;
298 if (header->obj_id & HPDF_OTYPE_DIRECT)
299 return HPDF_SetError (dict->error, HPDF_INVALID_OBJECT, 0);
302 HPDF_Obj_Free (dict->mmgr, obj);
303 return HPDF_SetError (dict->error, HPDF_INVALID_OBJECT, 0);
306 if (dict->list->count >= HPDF_LIMIT_MAX_DICT_ELEMENT) {
307 HPDF_PTRACE((" HPDF_Dict_Add exceed limitatin of dict count(%d)\n",
308 HPDF_LIMIT_MAX_DICT_ELEMENT));
310 HPDF_Obj_Free (dict->mmgr, obj);
311 return HPDF_SetError (dict->error, HPDF_DICT_COUNT_ERR, 0);
314 /* check whether there is an object which has same name */
315 element = GetElement (dict, key);
318 HPDF_Obj_Free (dict->mmgr, element->value);
319 element->value = NULL;
321 element = (HPDF_DictElement)HPDF_GetMem (dict->mmgr,
322 sizeof(HPDF_DictElement_Rec));
325 /* cannot create element object */
326 if (!(header->obj_id & HPDF_OTYPE_INDIRECT))
327 HPDF_Obj_Free (dict->mmgr, obj);
329 return HPDF_Error_GetCode (dict->error);
332 HPDF_StrCpy (element->key, key, element->key +
333 HPDF_LIMIT_MAX_NAME_LEN + 1);
334 element->value = NULL;
336 ret = HPDF_List_Add (dict->list, element);
337 if (ret != HPDF_OK) {
338 if (!(header->obj_id & HPDF_OTYPE_INDIRECT))
339 HPDF_Obj_Free (dict->mmgr, obj);
341 HPDF_FreeMem (dict->mmgr, element);
343 return HPDF_Error_GetCode (dict->error);
347 if (header->obj_id & HPDF_OTYPE_INDIRECT) {
348 HPDF_Proxy proxy = HPDF_Proxy_New (dict->mmgr, obj);
351 return HPDF_Error_GetCode (dict->error);
353 element->value = proxy;
354 proxy->header.obj_id |= HPDF_OTYPE_DIRECT;
356 element->value = obj;
357 header->obj_id |= HPDF_OTYPE_DIRECT;
365 HPDF_Dict_AddName (HPDF_Dict dict,
369 HPDF_Name name = HPDF_Name_New (dict->mmgr, value);
371 return HPDF_Error_GetCode (dict->error);
373 return HPDF_Dict_Add (dict, key, name);
378 HPDF_Dict_AddNumber (HPDF_Dict dict,
382 HPDF_Number number = HPDF_Number_New (dict->mmgr, value);
385 return HPDF_Error_GetCode (dict->error);
387 return HPDF_Dict_Add (dict, key, number);
392 HPDF_Dict_AddReal (HPDF_Dict dict,
396 HPDF_Real real = HPDF_Real_New (dict->mmgr, value);
399 return HPDF_Error_GetCode (dict->error);
401 return HPDF_Dict_Add (dict, key, real);
406 HPDF_Dict_AddBoolean (HPDF_Dict dict,
410 HPDF_Boolean obj = HPDF_Boolean_New (dict->mmgr, value);
413 return HPDF_Error_GetCode (dict->error);
415 return HPDF_Dict_Add (dict, key, obj);
420 HPDF_Dict_GetItem (HPDF_Dict dict,
422 HPDF_UINT16 obj_class)
424 HPDF_DictElement element = GetElement (dict, key);
427 if (element && HPDF_StrCmp(key, element->key) == 0) {
428 HPDF_Obj_Header *header = (HPDF_Obj_Header *)element->value;
430 if (header->obj_class == HPDF_OCLASS_PROXY) {
431 HPDF_Proxy p = element->value;
432 header = (HPDF_Obj_Header *)p->obj;
435 obj = element->value;
437 if ((header->obj_class & HPDF_OCLASS_ANY) != obj_class) {
438 HPDF_PTRACE((" HPDF_Dict_GetItem dict=%p key=%s obj_class=0x%08X\n",
439 dict, key, (HPDF_UINT)header->obj_class));
440 HPDF_SetError (dict->error, HPDF_DICT_ITEM_UNEXPECTED_TYPE, 0);
453 GetElement (HPDF_Dict dict,
458 for (i = 0; i < dict->list->count; i++) {
459 HPDF_DictElement element =
460 (HPDF_DictElement)HPDF_List_ItemAt (dict->list, i);
462 if (HPDF_StrCmp (key, element->key) == 0)
471 HPDF_Dict_RemoveElement (HPDF_Dict dict,
476 for (i = 0; i < dict->list->count; i++) {
477 HPDF_DictElement element =
478 (HPDF_DictElement)HPDF_List_ItemAt (dict->list, i);
480 if (HPDF_StrCmp (key, element->key) == 0) {
481 HPDF_List_Remove (dict->list, element);
483 HPDF_Obj_Free (dict->mmgr, element->value);
484 HPDF_FreeMem (dict->mmgr, element);
490 return HPDF_DICT_ITEM_NOT_FOUND;
494 HPDF_Dict_GetKeyByObj (HPDF_Dict dict,
499 for (i = 0; i < dict->list->count; i++) {
500 HPDF_Obj_Header *header;
501 HPDF_DictElement element =
502 (HPDF_DictElement)HPDF_List_ItemAt (dict->list, i);
504 header = (HPDF_Obj_Header *)(element->value);
505 if (header->obj_class == HPDF_OCLASS_PROXY) {
506 HPDF_Proxy p = element->value;
511 if (element->value == obj)