1/*
2 * << Haru Free PDF Library >> -- hpdf_objects.c
3 *
4 * URL: http://libharu.org
5 *
6 * Copyright (c) 1999-2006 Takeshi Kanno <takeshi_kanno@est.hi-ho.ne.jp>
7 * Copyright (c) 2007-2009 Antony Dovgal <tony@daylessday.org>
8 *
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.
15 *
16 */
17
18#include "hpdf_conf.h"
19#include "hpdf_utils.h"
20#include "hpdf_objects.h"
21
22void
23HPDF_Obj_Free (HPDF_MMgr mmgr,
24 void *obj)
25{
26 HPDF_Obj_Header *header;
27
28 HPDF_PTRACE((" HPDF_Obj_Free\n"));
29
30 if (!obj)
31 return;
32
33 header = (HPDF_Obj_Header *)obj;
34
35 if (!(header->obj_id & HPDF_OTYPE_INDIRECT))
36 HPDF_Obj_ForceFree (mmgr, obj);
37}
38
39
40void
41HPDF_Obj_ForceFree (HPDF_MMgr mmgr,
42 void *obj)
43{
44 HPDF_Obj_Header *header;
45
46 HPDF_PTRACE((" HPDF_Obj_ForceFree\n"));
47
48 if (!obj)
49 return;
50
51 header = (HPDF_Obj_Header *)obj;
52
53 HPDF_PTRACE((" HPDF_Obj_ForceFree obj=0x%08X obj_id=0x%08X "
54 "obj_class=0x%08X\n",
55 (HPDF_UINT)obj, (HPDF_UINT)(header->obj_id),
56 (HPDF_UINT)(header->obj_class)));
57
58 switch (header->obj_class & HPDF_OCLASS_ANY) {
59 case HPDF_OCLASS_STRING:
60 HPDF_String_Free (obj);
61 break;
62 case HPDF_OCLASS_BINARY:
63 HPDF_Binary_Free (obj);
64 break;
65 case HPDF_OCLASS_ARRAY:
66 HPDF_Array_Free (obj);
67 break;
68 case HPDF_OCLASS_DICT:
69 HPDF_Dict_Free (obj);
70 break;
71 default:
72 HPDF_FreeMem (mmgr, obj);
73 }
74}
75
76HPDF_STATUS
77HPDF_Obj_Write (void *obj,
78 HPDF_Stream stream,
79 HPDF_Encrypt e)
80{
81 HPDF_Obj_Header *header = (HPDF_Obj_Header *)obj;
82
83 HPDF_PTRACE((" HPDF_Obj_Write\n"));
84
85 if (header->obj_id & HPDF_OTYPE_HIDDEN) {
86 HPDF_PTRACE(("#HPDF_Obj_Write obj=0x%08X skipped\n", (HPDF_UINT)obj));
87 return HPDF_OK;
88 }
89
90 if (header->obj_class == HPDF_OCLASS_PROXY) {
91 char buf[HPDF_SHORT_BUF_SIZ];
92 char *pbuf = buf;
93 char *eptr = buf + HPDF_SHORT_BUF_SIZ - 1;
94 HPDF_Proxy p = obj;
95
96 header = (HPDF_Obj_Header*)p->obj;
97
98 pbuf = HPDF_IToA (pbuf, header->obj_id & 0x00FFFFFF, eptr);
99 *pbuf++ = ' ';
100 pbuf = HPDF_IToA (pbuf, header->gen_no, eptr);
101 HPDF_StrCpy(pbuf, " R", eptr);
102
103 return HPDF_Stream_WriteStr(stream, buf);
104 }
105
106 return HPDF_Obj_WriteValue(obj, stream, e);
107}
108
109HPDF_STATUS
110HPDF_Obj_WriteValue (void *obj,
111 HPDF_Stream stream,
112 HPDF_Encrypt e)
113{
114 HPDF_Obj_Header *header;
115 HPDF_STATUS ret;
116
117 HPDF_PTRACE((" HPDF_Obj_WriteValue\n"));
118
119 header = (HPDF_Obj_Header *)obj;
120
121 HPDF_PTRACE((" HPDF_Obj_WriteValue obj=0x%08X obj_class=0x%04X\n",
122 (HPDF_UINT)obj, (HPDF_UINT)header->obj_class));
123
124 switch (header->obj_class & HPDF_OCLASS_ANY) {
125 case HPDF_OCLASS_NAME:
126 ret = HPDF_Name_Write (obj, stream);
127 break;
128 case HPDF_OCLASS_NUMBER:
129 ret = HPDF_Number_Write (obj, stream);
130 break;
131 case HPDF_OCLASS_REAL:
132 ret = HPDF_Real_Write (obj, stream);
133 break;
134 case HPDF_OCLASS_STRING:
135 ret = HPDF_String_Write (obj, stream, e);
136 break;
137 case HPDF_OCLASS_BINARY:
138 ret = HPDF_Binary_Write (obj, stream, e);
139 break;
140 case HPDF_OCLASS_ARRAY:
141 ret = HPDF_Array_Write (obj, stream, e);
142 break;
143 case HPDF_OCLASS_DICT:
144 ret = HPDF_Dict_Write (obj, stream, e);
145 break;
146 case HPDF_OCLASS_BOOLEAN:
147 ret = HPDF_Boolean_Write (obj, stream);
148 break;
149 case HPDF_OCLASS_NULL:
150 ret = HPDF_Stream_WriteStr (stream, "null");
151 break;
152 default:
153 ret = HPDF_ERR_UNKNOWN_CLASS;
154 }
155
156 return ret;
157}
158
159HPDF_Proxy
160HPDF_Proxy_New (HPDF_MMgr mmgr,
161 void *obj)
162{
163 HPDF_Proxy p = HPDF_GetMem (mmgr, sizeof(HPDF_Proxy_Rec));
164
165 HPDF_PTRACE((" HPDF_Proxy_New\n"));
166
167 if (p) {
168 HPDF_MemSet (&p->header, 0, sizeof(HPDF_Obj_Header));
169 p->header.obj_class = HPDF_OCLASS_PROXY;
170 p->obj = obj;
171 }
172
173 return p;
174}
175
176