hpdf 2.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (101) hide show
  1. data/History +57 -0
  2. data/License +25 -0
  3. data/README +165 -0
  4. data/examples/arc_demo.rb +82 -0
  5. data/examples/demo.rb +92 -0
  6. data/examples/encryption.rb +41 -0
  7. data/examples/ext_gstater_demo.rb +171 -0
  8. data/examples/font_demo.rb +67 -0
  9. data/examples/jpfont_demo.rb +122 -0
  10. data/examples/line_demo.rb +301 -0
  11. data/examples/slide_show_demo.rb +139 -0
  12. data/examples/text_demo2.rb +189 -0
  13. data/examples/ttfont_demo.rb +106 -0
  14. data/ext/extconf.rb +6 -0
  15. data/ext/hpdf.c +3779 -0
  16. data/ext/hpdf_annotation.c +415 -0
  17. data/ext/hpdf_array.c +344 -0
  18. data/ext/hpdf_binary.c +117 -0
  19. data/ext/hpdf_boolean.c +47 -0
  20. data/ext/hpdf_catalog.c +354 -0
  21. data/ext/hpdf_destination.c +339 -0
  22. data/ext/hpdf_dict.c +488 -0
  23. data/ext/hpdf_doc.c +2020 -0
  24. data/ext/hpdf_doc_png.c +137 -0
  25. data/ext/hpdf_encoder.c +2991 -0
  26. data/ext/hpdf_encoder_cns.c +36359 -0
  27. data/ext/hpdf_encoder_cnt.c +15307 -0
  28. data/ext/hpdf_encoder_jp.c +16125 -0
  29. data/ext/hpdf_encoder_kr.c +27978 -0
  30. data/ext/hpdf_encrypt.c +632 -0
  31. data/ext/hpdf_encryptdict.c +240 -0
  32. data/ext/hpdf_error.c +114 -0
  33. data/ext/hpdf_ext_gstate.c +150 -0
  34. data/ext/hpdf_font.c +229 -0
  35. data/ext/hpdf_font_cid.c +1030 -0
  36. data/ext/hpdf_font_tt.c +406 -0
  37. data/ext/hpdf_font_type1.c +391 -0
  38. data/ext/hpdf_fontdef.c +56 -0
  39. data/ext/hpdf_fontdef_base14.c +4506 -0
  40. data/ext/hpdf_fontdef_cid.c +194 -0
  41. data/ext/hpdf_fontdef_cns.c +471 -0
  42. data/ext/hpdf_fontdef_cnt.c +250 -0
  43. data/ext/hpdf_fontdef_jp.c +1904 -0
  44. data/ext/hpdf_fontdef_kr.c +1572 -0
  45. data/ext/hpdf_fontdef_tt.c +2230 -0
  46. data/ext/hpdf_fontdef_type1.c +524 -0
  47. data/ext/hpdf_gstate.c +119 -0
  48. data/ext/hpdf_image.c +568 -0
  49. data/ext/hpdf_image_png.c +424 -0
  50. data/ext/hpdf_info.c +164 -0
  51. data/ext/hpdf_list.c +333 -0
  52. data/ext/hpdf_mmgr.c +243 -0
  53. data/ext/hpdf_name.c +71 -0
  54. data/ext/hpdf_null.c +31 -0
  55. data/ext/hpdf_number.c +49 -0
  56. data/ext/hpdf_objects.c +172 -0
  57. data/ext/hpdf_outline.c +329 -0
  58. data/ext/hpdf_page_label.c +74 -0
  59. data/ext/hpdf_page_operator.c +2764 -0
  60. data/ext/hpdf_pages.c +1508 -0
  61. data/ext/hpdf_real.c +61 -0
  62. data/ext/hpdf_streams.c +1435 -0
  63. data/ext/hpdf_string.c +189 -0
  64. data/ext/hpdf_utils.c +438 -0
  65. data/ext/hpdf_xref.c +348 -0
  66. data/ext/include/hpdf.h +1227 -0
  67. data/ext/include/hpdf_annotation.h +74 -0
  68. data/ext/include/hpdf_catalog.h +81 -0
  69. data/ext/include/hpdf_conf.h +76 -0
  70. data/ext/include/hpdf_consts.h +549 -0
  71. data/ext/include/hpdf_destination.h +41 -0
  72. data/ext/include/hpdf_doc.h +159 -0
  73. data/ext/include/hpdf_encoder.h +311 -0
  74. data/ext/include/hpdf_encrypt.h +156 -0
  75. data/ext/include/hpdf_encryptdict.h +66 -0
  76. data/ext/include/hpdf_error.h +201 -0
  77. data/ext/include/hpdf_ext_gstate.h +38 -0
  78. data/ext/include/hpdf_font.h +112 -0
  79. data/ext/include/hpdf_fontdef.h +403 -0
  80. data/ext/include/hpdf_gstate.h +80 -0
  81. data/ext/include/hpdf_image.h +72 -0
  82. data/ext/include/hpdf_info.h +48 -0
  83. data/ext/include/hpdf_list.h +85 -0
  84. data/ext/include/hpdf_mmgr.h +82 -0
  85. data/ext/include/hpdf_objects.h +587 -0
  86. data/ext/include/hpdf_outline.h +74 -0
  87. data/ext/include/hpdf_page_label.h +35 -0
  88. data/ext/include/hpdf_pages.h +128 -0
  89. data/ext/include/hpdf_streams.h +276 -0
  90. data/ext/include/hpdf_types.h +488 -0
  91. data/ext/include/hpdf_utils.h +161 -0
  92. data/tests/arc_demo.rb +82 -0
  93. data/tests/demo.rb +91 -0
  94. data/tests/encryption.rb +41 -0
  95. data/tests/ext_gstater_demo.rb +171 -0
  96. data/tests/font_demo.rb +67 -0
  97. data/tests/line_demo.rb +301 -0
  98. data/tests/slide_show_demo.rb +139 -0
  99. data/tests/test_all_examples.rb +31 -0
  100. data/tests/text_demo2.rb +189 -0
  101. metadata +147 -0
@@ -0,0 +1,61 @@
1
+ /*
2
+ * << Haru Free PDF Library 2.0.0 >> -- hpdf_real.c
3
+ *
4
+ * Copyright (c) 1999-2006 Takeshi Kanno <takeshi_kanno@est.hi-ho.ne.jp>
5
+ *
6
+ * Permission to use, copy, modify, distribute and sell this software
7
+ * and its documentation for any purpose is hereby granted without fee,
8
+ * provided that the above copyright notice appear in all copies and
9
+ * that both that copyright notice and this permission notice appear
10
+ * in supporting documentation.
11
+ * It is provided "as is" without express or implied warranty.
12
+ *
13
+ */
14
+
15
+ #include "hpdf_conf.h"
16
+ #include "hpdf_utils.h"
17
+ #include "hpdf_objects.h"
18
+
19
+
20
+ HPDF_Real
21
+ HPDF_Real_New (HPDF_MMgr mmgr,
22
+ HPDF_REAL value)
23
+ {
24
+ HPDF_Real obj = HPDF_GetMem (mmgr, sizeof(HPDF_Real_Rec));
25
+
26
+ if (obj) {
27
+ HPDF_MemSet (&obj->header, 0, sizeof(HPDF_Obj_Header));
28
+ obj->header.obj_class = HPDF_OCLASS_REAL;
29
+ obj->error = mmgr->error;
30
+ HPDF_Real_SetValue (obj, value);
31
+ }
32
+
33
+ return obj;
34
+ }
35
+
36
+
37
+ HPDF_STATUS
38
+ HPDF_Real_Write (HPDF_Real obj,
39
+ HPDF_Stream stream)
40
+ {
41
+ return HPDF_Stream_WriteReal (stream, obj->value);
42
+ }
43
+
44
+
45
+ HPDF_STATUS
46
+ HPDF_Real_SetValue (HPDF_Real obj,
47
+ HPDF_REAL value)
48
+ {
49
+ HPDF_STATUS ret = HPDF_OK;
50
+
51
+ if (value > HPDF_LIMIT_MAX_REAL)
52
+ return HPDF_SetError (obj->error, HPDF_REAL_OUT_OF_RANGE, 0);
53
+
54
+ if (value < HPDF_LIMIT_MIN_REAL)
55
+ return HPDF_SetError (obj->error, HPDF_REAL_OUT_OF_RANGE, 0);
56
+
57
+ obj->value =value;
58
+
59
+ return ret;
60
+ }
61
+
@@ -0,0 +1,1435 @@
1
+ /*
2
+ * << Haru Free PDF Library 2.0.3 >> -- hpdf_streams.h
3
+ *
4
+ * Copyright (c) 1999-2006 Takeshi Kanno <takeshi_kanno@est.hi-ho.ne.jp>
5
+ *
6
+ * Permission to use, copy, modify, distribute and sell this software
7
+ * and its documentation for any purpose is hereby granted without fee,
8
+ * provided that the above copyright notice appear in all copies and
9
+ * that both that copyright notice and this permission notice appear
10
+ * in supporting documentation.
11
+ * It is provided "as is" without express or implied warranty.
12
+ *
13
+ * 2005.12.20 Created.
14
+ *
15
+ */
16
+
17
+ #include <errno.h>
18
+
19
+ #ifndef HPDF_NOZLIB
20
+ #include <zlib.h>
21
+ #include <zconf.h>
22
+ #endif /* HPDF_NOZLIB */
23
+
24
+ #include "hpdf_conf.h"
25
+ #include "hpdf_consts.h"
26
+ #include "hpdf_utils.h"
27
+ #include "hpdf_streams.h"
28
+
29
+ HPDF_STATUS
30
+ HPDF_MemStream_WriteFunc (HPDF_Stream stream,
31
+ const HPDF_BYTE *ptr,
32
+ HPDF_UINT siz);
33
+
34
+
35
+ HPDF_STATUS
36
+ HPDF_MemStream_SeekFunc (HPDF_Stream stream,
37
+ HPDF_INT pos,
38
+ HPDF_WhenceMode mode);
39
+
40
+
41
+ HPDF_STATUS
42
+ HPDF_MemStream_ReadFunc (HPDF_Stream stream,
43
+ HPDF_BYTE *buf,
44
+ HPDF_UINT *size);
45
+
46
+
47
+ HPDF_INT32
48
+ HPDF_MemStream_TellFunc (HPDF_Stream stream);
49
+
50
+
51
+ HPDF_UINT32
52
+ HPDF_MemStream_SizeFunc (HPDF_Stream stream);
53
+
54
+
55
+ void
56
+ HPDF_MemStream_FreeFunc (HPDF_Stream stream);
57
+
58
+ HPDF_STATUS
59
+ HPDF_MemStream_InWrite (HPDF_Stream stream,
60
+ const HPDF_BYTE **ptr,
61
+ HPDF_UINT *count);
62
+
63
+ HPDF_STATUS
64
+ HPDF_Stream_WriteToStreamWithDeflate (HPDF_Stream src,
65
+ HPDF_Stream dst,
66
+ HPDF_Encrypt e);
67
+
68
+
69
+ HPDF_STATUS
70
+ HPDF_FileReader_ReadFunc (HPDF_Stream stream,
71
+ HPDF_BYTE *ptr,
72
+ HPDF_UINT *siz);
73
+
74
+
75
+ HPDF_STATUS
76
+ HPDF_FileReader_SeekFunc (HPDF_Stream stream,
77
+ HPDF_INT pos,
78
+ HPDF_WhenceMode mode);
79
+
80
+
81
+ HPDF_INT32
82
+ HPDF_FileStream_TellFunc (HPDF_Stream stream);
83
+
84
+
85
+ HPDF_UINT32
86
+ HPDF_FileStream_SizeFunc (HPDF_Stream stream);
87
+
88
+
89
+ HPDF_STATUS
90
+ HPDF_FileWriter_WriteFunc (HPDF_Stream stream,
91
+ const HPDF_BYTE *ptr,
92
+ HPDF_UINT siz);
93
+
94
+ void
95
+ HPDF_FileStream_FreeFunc (HPDF_Stream stream);
96
+
97
+
98
+
99
+ /*
100
+ * HPDF_Stream_Read
101
+ *
102
+ * stream : Pointer to a HPDF_Stream object.
103
+ * ptr : Pointer to a buffer to copy read data.
104
+ * size : Pointer to a variable which indecates buffer size.
105
+ *
106
+ * HPDF_Stream_read returns HPDF_OK when success. On failer, it returns
107
+ * error-code returned by reading function of this stream.
108
+ *
109
+ */
110
+
111
+ HPDF_STATUS
112
+ HPDF_Stream_Read (HPDF_Stream stream,
113
+ HPDF_BYTE *ptr,
114
+ HPDF_UINT *size)
115
+ {
116
+ if (!(stream->read_fn))
117
+ return HPDF_SetError (stream->error, HPDF_INVALID_OPERATION, 0);
118
+
119
+ /*
120
+ if (HPDF_Error_GetCode(stream->error) != HPDF_NOERROR)
121
+ return HPDF_THIS_FUNC_WAS_SKIPPED;
122
+ */
123
+
124
+ return stream->read_fn(stream, ptr, size);
125
+ }
126
+
127
+
128
+ /*
129
+ * HPDF_Stream_ReadLn
130
+ *
131
+ * stream : Pointer to a HPDF_Stream object.
132
+ * s : Pointer to a buffer to copy read data.
133
+ * size : buffer-size of s.
134
+ *
135
+ * Read from stream until the buffer is exhausted or line-feed charactor is
136
+ * read.
137
+ *
138
+ */
139
+ HPDF_STATUS
140
+ HPDF_Stream_ReadLn (HPDF_Stream stream,
141
+ char *s,
142
+ HPDF_UINT *size)
143
+ {
144
+ char buf[HPDF_STREAM_BUF_SIZ];
145
+ HPDF_UINT r_size = *size;
146
+ HPDF_UINT read_size = HPDF_STREAM_BUF_SIZ;
147
+
148
+ HPDF_PTRACE((" HPDF_Stream_ReadLn\n"));
149
+
150
+ if (!stream || !s || *size == 0)
151
+ return HPDF_SetError (stream->error, HPDF_INVALID_PARAMETER, 0);
152
+
153
+ if (!(stream->seek_fn) || !(stream->read_fn))
154
+ return HPDF_SetError (stream->error, HPDF_INVALID_OPERATION, 0);
155
+
156
+ if (r_size < HPDF_STREAM_BUF_SIZ)
157
+ read_size = r_size;
158
+
159
+ *size = 0;
160
+
161
+ while (r_size > 1) {
162
+ char *pbuf = buf;
163
+ HPDF_STATUS ret = HPDF_Stream_Read (stream, buf, &read_size);
164
+
165
+ if (ret != HPDF_OK && read_size == 0)
166
+ return ret;
167
+
168
+ r_size -= read_size;
169
+
170
+ while (read_size > 0) {
171
+ if (*pbuf == 0x0A || *pbuf == 0x0D) {
172
+ *s = 0;
173
+ read_size--;
174
+
175
+ /* handling CR-LF marker */
176
+ if (*pbuf == 0x0D || read_size > 1) {
177
+ pbuf++;
178
+
179
+ if (*pbuf == 0x0A)
180
+ read_size--;
181
+ }
182
+
183
+ if (read_size > 0)
184
+ return HPDF_Stream_Seek (stream, 0 - read_size,
185
+ HPDF_SEEK_CUR);
186
+ else
187
+ return HPDF_OK;
188
+ }
189
+
190
+ *s++ = *pbuf++;
191
+ read_size--;
192
+ (*size)++;
193
+ }
194
+
195
+ if (r_size < HPDF_STREAM_BUF_SIZ)
196
+ read_size = r_size;
197
+ else
198
+ read_size = HPDF_STREAM_BUF_SIZ;
199
+
200
+ if (ret == HPDF_STREAM_EOF)
201
+ return HPDF_STREAM_EOF;
202
+ }
203
+
204
+ *s = 0;
205
+
206
+ return HPDF_STREAM_READLN_CONTINUE;
207
+ }
208
+
209
+
210
+ /*
211
+ * HPDF_Stream_Write
212
+ *
213
+ * stream : Pointer to a HPDF_Stream object.
214
+ * ptr : Pointer to a buffer to write.
215
+ * siz : The size of buffer to write.
216
+ *
217
+ * HPDF_Stream_Write returns HPDF_OK when success. On failer, it returns
218
+ * error-code returned by writing function of this stream.
219
+ *
220
+ */
221
+ HPDF_STATUS
222
+ HPDF_Stream_Write (HPDF_Stream stream,
223
+ const HPDF_BYTE *ptr,
224
+ HPDF_UINT size)
225
+ {
226
+ HPDF_STATUS ret;
227
+
228
+ HPDF_PTRACE((" HPDF_Stream_Write\n"));
229
+
230
+ if (!(stream->write_fn))
231
+ return HPDF_SetError(stream->error, HPDF_INVALID_OPERATION, 0);
232
+
233
+ /*
234
+ if (HPDF_Error_GetCode(stream->error) != HPDF_NOERROR)
235
+ return HPDF_THIS_FUNC_WAS_SKIPPED;
236
+ */
237
+
238
+ ret = stream->write_fn(stream, ptr, size);
239
+
240
+ if (ret != HPDF_OK)
241
+ return ret;
242
+
243
+ stream->size += size;
244
+
245
+ return HPDF_OK;
246
+ }
247
+
248
+
249
+ HPDF_STATUS
250
+ HPDF_Stream_WriteChar (HPDF_Stream stream,
251
+ char value)
252
+ {
253
+ return HPDF_Stream_Write(stream, &value, sizeof(char));
254
+ }
255
+
256
+
257
+ HPDF_STATUS
258
+ HPDF_Stream_WriteStr (HPDF_Stream stream,
259
+ const char *value)
260
+ {
261
+ HPDF_UINT len = HPDF_StrLen(value, -1);
262
+
263
+ return HPDF_Stream_Write(stream, value, len);
264
+ }
265
+
266
+ HPDF_STATUS
267
+ HPDF_Stream_WriteUChar (HPDF_Stream stream,
268
+ HPDF_BYTE value)
269
+ {
270
+ return HPDF_Stream_Write(stream, &value, sizeof(HPDF_BYTE));
271
+ }
272
+
273
+ HPDF_STATUS
274
+ HPDF_Stream_WriteInt (HPDF_Stream stream,
275
+ HPDF_INT value)
276
+ {
277
+ char buf[HPDF_INT_LEN + 1];
278
+
279
+ char* p = HPDF_IToA(buf, value, buf + HPDF_INT_LEN);
280
+
281
+ return HPDF_Stream_Write(stream, buf, (HPDF_UINT)(p - buf));
282
+ }
283
+
284
+ HPDF_STATUS
285
+ HPDF_Stream_WriteUInt (HPDF_Stream stream,
286
+ HPDF_UINT value)
287
+ {
288
+ return HPDF_Stream_WriteInt(stream, (HPDF_INT)value);
289
+ }
290
+
291
+ HPDF_STATUS
292
+ HPDF_Stream_WriteReal (HPDF_Stream stream,
293
+ HPDF_REAL value)
294
+ {
295
+ char buf[HPDF_REAL_LEN + 1];
296
+
297
+ char* p = HPDF_FToA(buf, value, buf + HPDF_REAL_LEN);
298
+
299
+ return HPDF_Stream_Write(stream, buf, (HPDF_UINT)(p - buf));
300
+ }
301
+
302
+ void
303
+ HPDF_Stream_Free (HPDF_Stream stream)
304
+ {
305
+ if (!stream)
306
+ return;
307
+
308
+ if (stream->free_fn)
309
+ stream->free_fn(stream);
310
+
311
+ stream->sig_bytes = 0;
312
+
313
+ HPDF_FreeMem(stream->mmgr, stream);
314
+ }
315
+
316
+ HPDF_STATUS
317
+ HPDF_Stream_Seek (HPDF_Stream stream,
318
+ HPDF_INT pos,
319
+ HPDF_WhenceMode mode)
320
+ {
321
+ HPDF_PTRACE((" HPDF_Stream_Seek\n"));
322
+
323
+ if (!(stream->seek_fn))
324
+ return HPDF_SetError (stream->error, HPDF_INVALID_OPERATION, 0);
325
+
326
+ if (HPDF_Error_GetCode(stream->error) != 0)
327
+ return HPDF_THIS_FUNC_WAS_SKIPPED;
328
+
329
+ return stream->seek_fn(stream, pos, mode);
330
+ }
331
+
332
+
333
+ HPDF_INT32
334
+ HPDF_Stream_Tell (HPDF_Stream stream)
335
+ {
336
+ HPDF_PTRACE((" HPDF_Stream_Tell\n"));
337
+
338
+ if (!(stream->tell_fn))
339
+ return HPDF_SetError (stream->error, HPDF_INVALID_OPERATION, 0);
340
+
341
+ if (HPDF_Error_GetCode(stream->error) != 0)
342
+ return HPDF_THIS_FUNC_WAS_SKIPPED;
343
+
344
+ return stream->tell_fn(stream);
345
+ }
346
+
347
+
348
+ HPDF_UINT32
349
+ HPDF_Stream_Size (HPDF_Stream stream)
350
+ {
351
+ HPDF_PTRACE((" HPDF_Stream_Tell\n"));
352
+
353
+ if (stream->write_fn)
354
+ return stream->size;
355
+
356
+ if (!(stream->size_fn)) {
357
+ HPDF_SetError (stream->error, HPDF_INVALID_OPERATION, 0);
358
+ return 0;
359
+ }
360
+
361
+ if (HPDF_Error_GetCode(stream->error) != 0)
362
+ return 0;
363
+
364
+ return stream->size_fn(stream);
365
+ }
366
+
367
+
368
+ HPDF_STATUS
369
+ HPDF_Stream_WriteEscapeName (HPDF_Stream stream,
370
+ const char *value)
371
+ {
372
+ char tmp_char[HPDF_LIMIT_MAX_NAME_LEN * 3 + 2];
373
+ HPDF_UINT len;
374
+ HPDF_INT i;
375
+ const HPDF_BYTE* pos1;
376
+ char* pos2;
377
+
378
+ HPDF_PTRACE((" HPDF_Stream_WriteEscapeName\n"));
379
+
380
+ len = HPDF_StrLen (value, HPDF_LIMIT_MAX_NAME_LEN);
381
+ pos1 = (HPDF_BYTE*)value;
382
+ pos2 = tmp_char;
383
+
384
+ *pos2++ = '/';
385
+ for (i = 0; i < (HPDF_INT32)len; i++) {
386
+ HPDF_BYTE c = *pos1++;
387
+ if (HPDF_NEEDS_ESCAPE(c)) {
388
+ *pos2++ = '#';
389
+ *pos2 = (char)(c >> 4);
390
+ if (*pos2 <= 9)
391
+ *pos2 += 0x30;
392
+ else
393
+ *pos2 += 0x41 - 10;
394
+ pos2++;
395
+
396
+ *pos2 = (char)(c & 0x0f);
397
+ if (*pos2 <= 9)
398
+ *pos2 += 0x30;
399
+ else
400
+ *pos2 += 0x41 - 10;
401
+ pos2++;
402
+ } else
403
+ *pos2++ = c;
404
+ }
405
+ *pos2 = 0;
406
+
407
+ return HPDF_Stream_Write (stream, tmp_char, HPDF_StrLen(tmp_char, -1));
408
+ }
409
+
410
+ HPDF_STATUS
411
+ HPDF_Stream_WriteEscapeText2 (HPDF_Stream stream,
412
+ const char *text,
413
+ HPDF_UINT len)
414
+ {
415
+ char buf[HPDF_TEXT_DEFAULT_LEN];
416
+ HPDF_UINT idx = 0;
417
+ HPDF_INT i;
418
+ const char* p = text;
419
+ HPDF_STATUS ret;
420
+
421
+ HPDF_PTRACE((" HPDF_Stream_WriteEscapeText2\n"));
422
+
423
+ if (!len)
424
+ return HPDF_OK;
425
+
426
+ buf[idx++] = '(';
427
+
428
+ for (i = 0; i < len; i++) {
429
+ HPDF_BYTE c = (HPDF_BYTE)*p++;
430
+ if (HPDF_NEEDS_ESCAPE(c)) {
431
+ buf[idx++] = '\\';
432
+
433
+ buf[idx] = c >> 6;
434
+ buf[idx] += 0x30;
435
+ idx++;
436
+ buf[idx] = (c & 0x38) >> 3;
437
+ buf[idx] += 0x30;
438
+ idx++;
439
+ buf[idx] = (c & 0x07);
440
+ buf[idx] += 0x30;
441
+ idx++;
442
+ }
443
+ else
444
+ buf[idx++] = c;
445
+
446
+ if (idx > HPDF_TEXT_DEFAULT_LEN - 4) {
447
+ ret = HPDF_Stream_Write (stream, buf, idx);
448
+ if (ret != HPDF_OK)
449
+ return ret;
450
+ idx = 0;
451
+ }
452
+ }
453
+ buf[idx++] = ')';
454
+
455
+ ret = HPDF_Stream_Write (stream, buf, idx);
456
+
457
+ return ret;
458
+ }
459
+
460
+ HPDF_STATUS
461
+ HPDF_Stream_WriteEscapeText (HPDF_Stream stream,
462
+ const char *text)
463
+ {
464
+ HPDF_UINT len;
465
+
466
+ HPDF_PTRACE((" HPDF_Stream_WriteEscapeText\n"));
467
+
468
+ len = (text == NULL) ? 0 : HPDF_StrLen (text, HPDF_LIMIT_MAX_STRING_LEN);
469
+
470
+ return HPDF_Stream_WriteEscapeText2(stream, text, len);
471
+ }
472
+
473
+ HPDF_STATUS
474
+ HPDF_Stream_WriteBinary (HPDF_Stream stream,
475
+ const HPDF_BYTE *data,
476
+ HPDF_UINT len,
477
+ HPDF_Encrypt e)
478
+ {
479
+ char buf[HPDF_TEXT_DEFAULT_LEN];
480
+ HPDF_BYTE ebuf[HPDF_TEXT_DEFAULT_LEN];
481
+ HPDF_BYTE *pbuf = NULL;
482
+ HPDF_BOOL flg = HPDF_FALSE;
483
+ HPDF_UINT idx = 0;
484
+ HPDF_UINT i;
485
+ const HPDF_BYTE* p;
486
+ HPDF_STATUS ret = HPDF_OK;
487
+
488
+ HPDF_PTRACE((" HPDF_Stream_WriteBinary\n"));
489
+
490
+ if (e) {
491
+ if (len <= HPDF_TEXT_DEFAULT_LEN)
492
+ pbuf = ebuf;
493
+ else {
494
+ pbuf = (HPDF_BYTE *)HPDF_GetMem (stream->mmgr, len);
495
+ flg = HPDF_TRUE;
496
+ }
497
+
498
+ HPDF_Encrypt_CryptBuf (e, data, pbuf, len);
499
+ p = pbuf;
500
+ } else {
501
+ p = data;
502
+ }
503
+
504
+ for (i = 0; i < len; i++, p++) {
505
+ char c = *p >> 4;
506
+
507
+ if (c <= 9)
508
+ c += 0x30;
509
+ else
510
+ c += 0x41 - 10;
511
+ buf[idx++] = c;
512
+
513
+ c = *p & 0x0f;
514
+ if (c <= 9)
515
+ c += 0x30;
516
+ else
517
+ c += 0x41 - 10;
518
+ buf[idx++] = c;
519
+
520
+ if (idx > HPDF_TEXT_DEFAULT_LEN - 2) {
521
+ ret = HPDF_Stream_Write (stream, buf, idx);
522
+ if (ret != HPDF_OK) {
523
+ if (flg)
524
+ HPDF_FreeMem (stream->mmgr, pbuf);
525
+ return ret;
526
+ }
527
+ idx = 0;
528
+ }
529
+ }
530
+
531
+ if (idx > 0) {
532
+ ret = HPDF_Stream_Write (stream, buf, idx);
533
+ }
534
+
535
+ if (flg)
536
+ HPDF_FreeMem (stream->mmgr, pbuf);
537
+
538
+ return ret;
539
+ }
540
+
541
+
542
+ HPDF_STATUS
543
+ HPDF_Stream_WriteToStreamWithDeflate (HPDF_Stream src,
544
+ HPDF_Stream dst,
545
+ HPDF_Encrypt e)
546
+ {
547
+ #ifndef HPDF_NOZLIB
548
+
549
+ #define DEFLATE_BUF_SIZ ((HPDF_INT)(HPDF_STREAM_BUF_SIZ * 1.1) + 13)
550
+
551
+ HPDF_STATUS ret;
552
+ HPDF_BOOL flg;
553
+
554
+ z_stream strm;
555
+ Bytef inbuf[HPDF_STREAM_BUF_SIZ];
556
+ Bytef otbuf[DEFLATE_BUF_SIZ];
557
+ HPDF_BYTE ebuf[DEFLATE_BUF_SIZ];
558
+
559
+ HPDF_PTRACE((" HPDF_Stream_WriteToStreamWithDeflate\n"));
560
+
561
+ /* initialize input stream */
562
+ ret = HPDF_Stream_Seek (src, 0, HPDF_SEEK_SET);
563
+ if (ret != HPDF_OK)
564
+ return ret;
565
+
566
+ /* initialize decompression stream. */
567
+ HPDF_MemSet(&strm, 0x00, sizeof(z_stream));
568
+ strm.next_out = otbuf;
569
+ strm.avail_out = DEFLATE_BUF_SIZ;
570
+
571
+ ret = deflateInit_(&strm, Z_DEFAULT_COMPRESSION, ZLIB_VERSION,
572
+ sizeof(z_stream));
573
+ if (ret != Z_OK)
574
+ return HPDF_SetError (src->error, HPDF_ZLIB_ERROR, ret);
575
+
576
+ strm.next_in = inbuf;
577
+ strm.avail_in = 0;
578
+
579
+ flg = HPDF_FALSE;
580
+ for (;;) {
581
+ HPDF_UINT size = HPDF_STREAM_BUF_SIZ;
582
+
583
+ ret = HPDF_Stream_Read (src, inbuf, &size);
584
+
585
+ strm.next_in = inbuf;
586
+ strm.avail_in = size;
587
+
588
+ if (ret != HPDF_OK) {
589
+ if (ret == HPDF_STREAM_EOF) {
590
+ flg = HPDF_TRUE;
591
+ if (size == 0)
592
+ break;
593
+ } else {
594
+ deflateEnd(&strm);
595
+ return ret;
596
+ }
597
+ }
598
+
599
+ while (strm.avail_in > 0) {
600
+ ret = deflate(&strm, Z_NO_FLUSH);
601
+ if (ret != Z_OK && ret != Z_STREAM_END) {
602
+ deflateEnd(&strm);
603
+ return HPDF_SetError (src->error, HPDF_ZLIB_ERROR, ret);
604
+ }
605
+
606
+ if (strm.avail_out == 0) {
607
+ if (e) {
608
+ HPDF_Encrypt_CryptBuf (e, otbuf, ebuf, DEFLATE_BUF_SIZ);
609
+ ret = HPDF_Stream_Write(dst, ebuf, DEFLATE_BUF_SIZ);
610
+ } else
611
+ ret = HPDF_Stream_Write (dst, otbuf, DEFLATE_BUF_SIZ);
612
+
613
+ if (ret != HPDF_OK) {
614
+ deflateEnd(&strm);
615
+ return HPDF_SetError (src->error, HPDF_ZLIB_ERROR, ret);
616
+ }
617
+
618
+ strm.next_out = otbuf;
619
+ strm.avail_out = DEFLATE_BUF_SIZ;
620
+ }
621
+ }
622
+
623
+ if (flg)
624
+ break;
625
+ }
626
+
627
+ flg = HPDF_FALSE;
628
+ for (;;) {
629
+ ret = deflate(&strm, Z_FINISH);
630
+ if (ret != Z_OK && ret != Z_STREAM_END) {
631
+ deflateEnd(&strm);
632
+ return HPDF_SetError (src->error, HPDF_ZLIB_ERROR, ret);
633
+ }
634
+
635
+ if (ret == Z_STREAM_END)
636
+ flg = HPDF_TRUE;
637
+
638
+ if (strm.avail_out < DEFLATE_BUF_SIZ) {
639
+ HPDF_UINT osize = DEFLATE_BUF_SIZ - strm.avail_out;
640
+ if (e) {
641
+ HPDF_Encrypt_CryptBuf (e, otbuf, ebuf, osize);
642
+ ret = HPDF_Stream_Write(dst, ebuf, osize);
643
+ } else
644
+ ret = HPDF_Stream_Write (dst, otbuf, osize);
645
+
646
+ if (ret != HPDF_OK) {
647
+ deflateEnd(&strm);
648
+ return HPDF_SetError (src->error, HPDF_ZLIB_ERROR, ret);
649
+ }
650
+
651
+ strm.next_out = otbuf;
652
+ strm.avail_out = DEFLATE_BUF_SIZ;
653
+ }
654
+
655
+ if (flg)
656
+ break;
657
+ }
658
+
659
+ deflateEnd(&strm);
660
+ return HPDF_OK;
661
+ #else /* HPDF_NOZLIB */
662
+ return HPDF_UNSUPPORTED_FUNC;
663
+ #endif /* HPDF_NOZLIB */
664
+ }
665
+
666
+ HPDF_STATUS
667
+ HPDF_Stream_WriteToStream (HPDF_Stream src,
668
+ HPDF_Stream dst,
669
+ HPDF_UINT filter,
670
+ HPDF_Encrypt e)
671
+ {
672
+ HPDF_STATUS ret;
673
+ HPDF_BYTE buf[HPDF_STREAM_BUF_SIZ];
674
+ HPDF_BYTE ebuf[HPDF_STREAM_BUF_SIZ];
675
+ HPDF_BOOL flg;
676
+
677
+ HPDF_PTRACE((" HPDF_Stream_WriteToStream\n"));
678
+
679
+ if (!dst || !(dst->write_fn)) {
680
+ HPDF_SetError (src->error, HPDF_INVALID_OBJECT, 0);
681
+ return HPDF_INVALID_OBJECT;
682
+ }
683
+
684
+ if (HPDF_Error_GetCode (src->error) != HPDF_NOERROR ||
685
+ HPDF_Error_GetCode (dst->error) != HPDF_NOERROR)
686
+ return HPDF_THIS_FUNC_WAS_SKIPPED;
687
+
688
+ #ifndef HPDF_NOZLIB
689
+ if (filter & HPDF_STREAM_FILTER_FLATE_DECODE)
690
+ return HPDF_Stream_WriteToStreamWithDeflate (src, dst, e);
691
+ #endif /* HPDF_NOZLIB */
692
+
693
+ /* initialize input stream */
694
+ if (HPDF_Stream_Size (src) == 0)
695
+ return HPDF_OK;
696
+
697
+ ret = HPDF_Stream_Seek (src, 0, HPDF_SEEK_SET);
698
+ if (ret != HPDF_OK)
699
+ return ret;
700
+
701
+ flg = HPDF_FALSE;
702
+ for (;;) {
703
+ HPDF_UINT size = HPDF_STREAM_BUF_SIZ;
704
+
705
+ ret = HPDF_Stream_Read (src, buf, &size);
706
+
707
+ if (ret != HPDF_OK) {
708
+ if (ret == HPDF_STREAM_EOF) {
709
+ flg = HPDF_TRUE;
710
+ if (size == 0)
711
+ break;
712
+ } else {
713
+ return ret;
714
+ }
715
+ }
716
+
717
+ if (e) {
718
+ HPDF_Encrypt_CryptBuf (e, buf, ebuf, size);
719
+ ret = HPDF_Stream_Write(dst, ebuf, size);
720
+ } else {
721
+ ret = HPDF_Stream_Write(dst, buf, size);
722
+ }
723
+
724
+ if (ret != HPDF_OK)
725
+ return ret;
726
+
727
+ if (flg)
728
+ break;
729
+ }
730
+
731
+ return HPDF_OK;
732
+ }
733
+
734
+ HPDF_Stream
735
+ HPDF_FileReader_New (HPDF_MMgr mmgr,
736
+ const char *fname)
737
+ {
738
+ HPDF_Stream stream;
739
+ HPDF_FILEP fp = HPDF_FOPEN (fname, "rb");
740
+
741
+ HPDF_PTRACE((" HPDF_FileReader_New\n"));
742
+
743
+ if (!fp) {
744
+ HPDF_SetError (mmgr->error, HPDF_FILE_OPEN_ERROR, errno);
745
+ return NULL;
746
+ }
747
+
748
+ stream = (HPDF_Stream)HPDF_GetMem(mmgr, sizeof(HPDF_Stream_Rec));
749
+
750
+ if (stream) {
751
+ HPDF_MemSet(stream, 0, sizeof(HPDF_Stream_Rec));
752
+ stream->sig_bytes = HPDF_STREAM_SIG_BYTES;
753
+ stream->type = HPDF_STREAM_FILE;
754
+ stream->error = mmgr->error;
755
+ stream->mmgr = mmgr;
756
+ stream->read_fn = HPDF_FileReader_ReadFunc;
757
+ stream->seek_fn = HPDF_FileReader_SeekFunc;
758
+ stream->tell_fn = HPDF_FileStream_TellFunc;
759
+ stream->size_fn = HPDF_FileStream_SizeFunc;
760
+ stream->free_fn = HPDF_FileStream_FreeFunc;
761
+ stream->attr = fp;
762
+ }
763
+
764
+ return stream;
765
+ }
766
+
767
+ /*
768
+ * HPDF_FileReader_ReadFunc
769
+ *
770
+ * Reading data function for HPDF_FileReader.
771
+ *
772
+ */
773
+
774
+ HPDF_STATUS
775
+ HPDF_FileReader_ReadFunc (HPDF_Stream stream,
776
+ HPDF_BYTE *ptr,
777
+ HPDF_UINT *siz)
778
+ {
779
+ HPDF_FILEP fp = (HPDF_FILEP)stream->attr;
780
+ HPDF_UINT rsiz;
781
+
782
+ HPDF_PTRACE((" HPDF_FileReader_ReadFunc\n"));
783
+
784
+ HPDF_MemSet(ptr, 0, *siz);
785
+ rsiz = HPDF_FREAD(ptr, 1, *siz, fp);
786
+
787
+ if (rsiz != *siz) {
788
+ if (HPDF_FEOF(fp)) {
789
+
790
+ *siz = rsiz;
791
+
792
+ return HPDF_STREAM_EOF;
793
+ }
794
+
795
+ return HPDF_SetError (stream->error, HPDF_FILE_IO_ERROR, HPDF_FERROR(fp));
796
+ }
797
+
798
+ return HPDF_OK;
799
+ }
800
+
801
+ /*
802
+ * HPDF_FileReader_SeekFunc
803
+ *
804
+ * Seeking data function for HPDF_FileReader.
805
+ *
806
+ * stream : Pointer to a HPDF_Stream object.
807
+ * pos : New position of stream object.
808
+ * HPDF_whence_mode : Seeking mode describing below.
809
+ * HPDF_SEEK_SET : Absolute file position
810
+ * HPDF_SEEK_CUR : Relative to the current file position
811
+ * HPDF_SEEK_END : Relative to the current end of file.
812
+ *
813
+ * HPDF_FileReader_seek_fn returns HPDF_OK when successful. On failer
814
+ * the result is HPDF_FILE_IO_ERROR and HPDF_Error_GetCode2() returns the
815
+ * error which returned by file seeking function of platform.
816
+ *
817
+ */
818
+
819
+ HPDF_STATUS
820
+ HPDF_FileReader_SeekFunc (HPDF_Stream stream,
821
+ HPDF_INT pos,
822
+ HPDF_WhenceMode mode)
823
+ {
824
+ HPDF_FILEP fp = (HPDF_FILEP)stream->attr;
825
+ HPDF_INT whence;
826
+
827
+ HPDF_PTRACE((" HPDF_FileReader_SeekFunc\n"));
828
+
829
+ switch (mode) {
830
+ case HPDF_SEEK_CUR:
831
+ whence = SEEK_CUR;
832
+ break;
833
+ case HPDF_SEEK_END:
834
+ whence = SEEK_END;
835
+ break;
836
+ default:
837
+ whence = SEEK_SET;
838
+ }
839
+
840
+ if (HPDF_FSEEK (fp, pos, whence) != 0) {
841
+ return HPDF_SetError (stream->error, HPDF_FILE_IO_ERROR, HPDF_FERROR(fp));
842
+ }
843
+
844
+ return HPDF_OK;
845
+ }
846
+
847
+
848
+ HPDF_INT32
849
+ HPDF_FileStream_TellFunc (HPDF_Stream stream)
850
+ {
851
+ HPDF_INT32 ret;
852
+ HPDF_FILEP fp = (HPDF_FILEP)stream->attr;
853
+
854
+ HPDF_PTRACE((" HPDF_FileReader_TellFunc\n"));
855
+
856
+ if ((ret = HPDF_FTELL (fp)) < 0) {
857
+ return HPDF_SetError (stream->error, HPDF_FILE_IO_ERROR,
858
+ HPDF_FERROR(fp));
859
+ }
860
+
861
+ return ret;
862
+ }
863
+
864
+
865
+ HPDF_UINT32
866
+ HPDF_FileStream_SizeFunc (HPDF_Stream stream)
867
+ {
868
+ HPDF_INT size;
869
+ HPDF_INT ptr;
870
+ HPDF_FILEP fp = (HPDF_FILEP)stream->attr;
871
+
872
+ HPDF_PTRACE((" HPDF_FileReader_SizeFunc\n"));
873
+
874
+ /* save current file-pointer */
875
+ if ((ptr = HPDF_FTELL (fp)) < 0) {
876
+ HPDF_SetError (stream->error, HPDF_FILE_IO_ERROR,
877
+ HPDF_FERROR(fp));
878
+ return 0;
879
+ }
880
+
881
+ /* move file-pointer to the end of the file */
882
+ if (HPDF_FSEEK (fp, 0, SEEK_END) < 0) {
883
+ HPDF_SetError (stream->error, HPDF_FILE_IO_ERROR,
884
+ HPDF_FERROR(fp));
885
+ return 0;
886
+ }
887
+
888
+ /* get the pointer of the end of the file */
889
+ if ((size = HPDF_FTELL (fp)) < 0) {
890
+ HPDF_SetError (stream->error, HPDF_FILE_IO_ERROR,
891
+ HPDF_FERROR(fp));
892
+ return 0;
893
+ }
894
+
895
+ /* restore current file-pointer */
896
+ if (HPDF_FSEEK (fp, ptr, SEEK_SET) < 0) {
897
+ HPDF_SetError (stream->error, HPDF_FILE_IO_ERROR,
898
+ HPDF_FERROR(fp));
899
+ return 0;
900
+ }
901
+
902
+ return (HPDF_UINT32)size;
903
+ }
904
+
905
+
906
+ HPDF_Stream
907
+ HPDF_FileWriter_New (HPDF_MMgr mmgr,
908
+ const char *fname)
909
+ {
910
+ HPDF_Stream stream;
911
+ HPDF_FILEP fp = HPDF_FOPEN (fname, "wb");
912
+
913
+ HPDF_PTRACE((" HPDF_FileWriter_New\n"));
914
+
915
+ if (!fp) {
916
+ HPDF_SetError (mmgr->error, HPDF_FILE_OPEN_ERROR, errno);
917
+ return NULL;
918
+ }
919
+
920
+ stream = (HPDF_Stream)HPDF_GetMem (mmgr, sizeof(HPDF_Stream_Rec));
921
+
922
+ if (stream) {
923
+ HPDF_MemSet (stream, 0, sizeof(HPDF_Stream_Rec));
924
+ stream->sig_bytes = HPDF_STREAM_SIG_BYTES;
925
+ stream->error = mmgr->error;
926
+ stream->mmgr = mmgr;
927
+ stream->write_fn = HPDF_FileWriter_WriteFunc;
928
+ stream->free_fn = HPDF_FileStream_FreeFunc;
929
+ stream->tell_fn = HPDF_FileStream_TellFunc;
930
+ stream->attr = fp;
931
+ stream->type = HPDF_STREAM_FILE;
932
+ }
933
+
934
+ return stream;
935
+ }
936
+
937
+ HPDF_STATUS
938
+ HPDF_FileWriter_WriteFunc (HPDF_Stream stream,
939
+ const HPDF_BYTE *ptr,
940
+ HPDF_UINT siz)
941
+ {
942
+ HPDF_FILEP fp;
943
+ HPDF_UINT ret;
944
+
945
+ HPDF_PTRACE((" HPDF_FileWriter_WriteFunc\n"));
946
+
947
+ fp = (HPDF_FILEP)stream->attr;
948
+ ret = HPDF_FWRITE (ptr, 1, siz, fp);
949
+
950
+ if (ret != siz) {
951
+ return HPDF_SetError (stream->error, HPDF_FILE_IO_ERROR, HPDF_FERROR(fp));
952
+ }
953
+
954
+ return HPDF_OK;
955
+ }
956
+
957
+
958
+ void
959
+ HPDF_FileStream_FreeFunc (HPDF_Stream stream)
960
+ {
961
+ HPDF_FILEP fp;
962
+
963
+ HPDF_PTRACE((" HPDF_FileStream_FreeFunc\n"));
964
+
965
+ fp = (HPDF_FILEP)stream->attr;
966
+
967
+ if (fp)
968
+ HPDF_FCLOSE(fp);
969
+
970
+ stream->attr = NULL;
971
+ }
972
+
973
+ HPDF_STATUS
974
+ HPDF_MemStream_InWrite (HPDF_Stream stream,
975
+ const HPDF_BYTE **ptr,
976
+ HPDF_UINT *count)
977
+ {
978
+ HPDF_MemStreamAttr attr = (HPDF_MemStreamAttr)stream->attr;
979
+ HPDF_UINT rsize = attr->buf_siz - attr->w_pos;
980
+
981
+ HPDF_PTRACE((" HPDF_MemStream_InWrite\n"));
982
+
983
+ if (*count <= 0)
984
+ return HPDF_OK;
985
+
986
+ if (rsize >= *count) {
987
+ HPDF_MemCpy (attr->w_ptr, *ptr, *count);
988
+ attr->w_ptr += *count;
989
+ attr->w_pos += *count;
990
+ *count = 0;
991
+ } else {
992
+ if (rsize > 0) {
993
+ HPDF_MemCpy (attr->w_ptr, *ptr, rsize);
994
+ *ptr += rsize;
995
+ *count -= rsize;
996
+ }
997
+ attr->w_ptr = (HPDF_BYTE*)HPDF_GetMem (stream->mmgr, attr->buf_siz);
998
+
999
+ if (attr->w_ptr == NULL)
1000
+ return HPDF_Error_GetCode (stream->error);
1001
+
1002
+ if (HPDF_List_Add (attr->buf, attr->w_ptr) != HPDF_OK) {
1003
+ HPDF_FreeMem (stream->mmgr, attr->w_ptr);
1004
+ attr->w_ptr = NULL;
1005
+
1006
+ return HPDF_Error_GetCode (stream->error);
1007
+ }
1008
+ attr->w_pos = 0;
1009
+ }
1010
+ return HPDF_OK;
1011
+ }
1012
+
1013
+
1014
+ HPDF_STATUS
1015
+ HPDF_MemStream_WriteFunc (HPDF_Stream stream,
1016
+ const HPDF_BYTE *ptr,
1017
+ HPDF_UINT siz)
1018
+ {
1019
+ HPDF_UINT wsiz = siz;
1020
+
1021
+ HPDF_PTRACE((" HPDF_MemStream_WriteFunc\n"));
1022
+
1023
+ if (HPDF_Error_GetCode (stream->error) != 0)
1024
+ return HPDF_THIS_FUNC_WAS_SKIPPED;
1025
+
1026
+ while (wsiz > 0) {
1027
+ HPDF_STATUS ret = HPDF_MemStream_InWrite (stream, &ptr, &wsiz);
1028
+ if (ret != HPDF_OK)
1029
+ return ret;
1030
+ }
1031
+
1032
+ return HPDF_OK;
1033
+ }
1034
+
1035
+
1036
+ HPDF_INT32
1037
+ HPDF_MemStream_TellFunc (HPDF_Stream stream)
1038
+ {
1039
+ HPDF_INT32 ret;
1040
+ HPDF_MemStreamAttr attr = (HPDF_MemStreamAttr)stream->attr;
1041
+
1042
+ HPDF_PTRACE((" HPDF_MemStream_TellFunc\n"));
1043
+
1044
+ ret = attr->r_ptr_idx * attr->buf_siz;
1045
+ ret += attr->r_pos;
1046
+
1047
+ return ret;
1048
+ }
1049
+
1050
+
1051
+ HPDF_UINT32
1052
+ HPDF_MemStream_SizeFunc (HPDF_Stream stream)
1053
+ {
1054
+ HPDF_PTRACE((" HPDF_MemStream_SizeFunc\n"));
1055
+
1056
+ return stream->size;
1057
+ }
1058
+
1059
+ HPDF_STATUS
1060
+ HPDF_MemStream_SeekFunc (HPDF_Stream stream,
1061
+ HPDF_INT pos,
1062
+ HPDF_WhenceMode mode)
1063
+ {
1064
+ HPDF_MemStreamAttr attr = (HPDF_MemStreamAttr)stream->attr;
1065
+
1066
+ HPDF_PTRACE((" HPDF_MemStream_SeekFunc\n"));
1067
+
1068
+ if (mode == HPDF_SEEK_CUR) {
1069
+ pos += (attr->r_ptr_idx * attr->buf_siz);
1070
+ pos += attr->r_pos;
1071
+ } else if (mode == HPDF_SEEK_END)
1072
+ pos = stream->size - pos;
1073
+
1074
+ if (pos > stream->size || stream->size == 0)
1075
+ return HPDF_SetError (stream->error, HPDF_STREAM_EOF, 0);
1076
+
1077
+ attr->r_ptr_idx = pos / attr->buf_siz;
1078
+ attr->r_pos = pos % attr->buf_siz;
1079
+ attr->r_ptr = (HPDF_BYTE*)HPDF_List_ItemAt (attr->buf, attr->r_ptr_idx);
1080
+ if (attr->r_ptr == NULL) {
1081
+ HPDF_SetError (stream->error, HPDF_INVALID_OBJECT, 0);
1082
+ return HPDF_INVALID_OBJECT;
1083
+ } else
1084
+ attr->r_ptr += attr->r_pos;
1085
+
1086
+ return HPDF_OK;
1087
+ }
1088
+
1089
+
1090
+ HPDF_BYTE*
1091
+ HPDF_MemStream_GetBufPtr (HPDF_Stream stream,
1092
+ HPDF_UINT index,
1093
+ HPDF_UINT *length)
1094
+ {
1095
+ HPDF_BYTE *ret;
1096
+ HPDF_MemStreamAttr attr;
1097
+
1098
+ HPDF_PTRACE((" HPDF_MemStream_GetBufPtr\n"));
1099
+
1100
+ if (stream->type != HPDF_STREAM_MEMORY) {
1101
+ HPDF_SetError (stream->error, HPDF_INVALID_OBJECT, 0);
1102
+ return NULL;
1103
+ }
1104
+
1105
+ attr = (HPDF_MemStreamAttr)stream->attr;
1106
+
1107
+ ret = (HPDF_BYTE *)HPDF_List_ItemAt (attr->buf, index);
1108
+ if (ret == NULL) {
1109
+ HPDF_SetError (stream->error, HPDF_INVALID_PARAMETER, 0);
1110
+ *length = 0;
1111
+ return NULL;
1112
+ }
1113
+
1114
+ *length = (attr->buf->count - 1 == index) ? attr->w_pos : attr->buf_siz;
1115
+ return ret;
1116
+ }
1117
+
1118
+
1119
+ void
1120
+ HPDF_MemStream_FreeData (HPDF_Stream stream)
1121
+ {
1122
+ HPDF_MemStreamAttr attr;
1123
+ HPDF_UINT i;
1124
+
1125
+ HPDF_PTRACE((" HPDF_MemStream_FreeData\n"));
1126
+
1127
+ if (!stream || stream->type != HPDF_STREAM_MEMORY)
1128
+ return;
1129
+
1130
+ attr = (HPDF_MemStreamAttr)stream->attr;
1131
+
1132
+ for (i = 0; i < attr->buf->count; i++)
1133
+ HPDF_FreeMem (stream->mmgr, HPDF_List_ItemAt (attr->buf, i));
1134
+
1135
+ HPDF_List_Clear(attr->buf);
1136
+
1137
+ stream->size = 0;
1138
+ attr->w_pos = attr->buf_siz;
1139
+ attr->w_ptr = NULL;
1140
+ attr->r_ptr_idx = 0;
1141
+ attr->r_pos = 0;
1142
+ }
1143
+
1144
+ void
1145
+ HPDF_MemStream_FreeFunc (HPDF_Stream stream)
1146
+ {
1147
+ HPDF_MemStreamAttr attr;
1148
+
1149
+ HPDF_PTRACE((" HPDF_MemStream_FreeFunc\n"));
1150
+
1151
+ attr = (HPDF_MemStreamAttr)stream->attr;
1152
+ HPDF_MemStream_FreeData (stream);
1153
+ HPDF_List_Free (attr->buf);
1154
+ HPDF_FreeMem (stream->mmgr, attr);
1155
+ stream->attr = NULL;
1156
+ }
1157
+
1158
+ HPDF_Stream
1159
+ HPDF_MemStream_New (HPDF_MMgr mmgr,
1160
+ HPDF_UINT buf_siz)
1161
+ {
1162
+ HPDF_Stream stream;
1163
+
1164
+ HPDF_PTRACE((" HPDF_MemStream_New\n"));
1165
+
1166
+ // Create new HPDF_Stream object.
1167
+ stream = (HPDF_Stream)HPDF_GetMem (mmgr, sizeof(HPDF_Stream_Rec));
1168
+
1169
+ if (stream) {
1170
+ // Create attribute struct.
1171
+ HPDF_MemStreamAttr attr = (HPDF_MemStreamAttr)HPDF_GetMem (mmgr,
1172
+ sizeof(HPDF_MemStreamAttr_Rec));
1173
+
1174
+ if (!attr) {
1175
+ HPDF_FreeMem (mmgr, stream);
1176
+ return NULL;
1177
+ }
1178
+
1179
+ HPDF_MemSet (stream, 0, sizeof(HPDF_Stream_Rec));
1180
+ HPDF_MemSet (attr, 0, sizeof(HPDF_MemStreamAttr_Rec));
1181
+
1182
+ attr->buf = HPDF_List_New (mmgr, HPDF_DEF_ITEMS_PER_BLOCK);
1183
+ if (!attr->buf) {
1184
+ HPDF_FreeMem (mmgr, stream);
1185
+ HPDF_FreeMem (mmgr, attr);
1186
+ return NULL;
1187
+ }
1188
+
1189
+ stream->sig_bytes = HPDF_STREAM_SIG_BYTES;
1190
+ stream->type = HPDF_STREAM_MEMORY;
1191
+ stream->error = mmgr->error;
1192
+ stream->mmgr = mmgr;
1193
+ stream->attr = attr;
1194
+ attr->buf_siz = (buf_siz > 0) ? buf_siz : HPDF_STREAM_BUF_SIZ;
1195
+ attr->w_pos = attr->buf_siz;
1196
+
1197
+ stream->write_fn = HPDF_MemStream_WriteFunc;
1198
+ stream->read_fn = HPDF_MemStream_ReadFunc;
1199
+ stream->seek_fn = HPDF_MemStream_SeekFunc;
1200
+ stream->tell_fn = HPDF_MemStream_TellFunc;
1201
+ stream->size_fn = HPDF_MemStream_SizeFunc;
1202
+ stream->free_fn = HPDF_MemStream_FreeFunc;
1203
+ }
1204
+
1205
+ return stream;
1206
+ }
1207
+
1208
+ HPDF_UINT
1209
+ HPDF_MemStream_GetBufSize (HPDF_Stream stream)
1210
+ {
1211
+ HPDF_MemStreamAttr attr;
1212
+
1213
+ HPDF_PTRACE((" HPDF_MemStream_GetBufSize\n"));
1214
+
1215
+ if (!stream || stream->type != HPDF_STREAM_MEMORY)
1216
+ return 0;
1217
+
1218
+ attr = (HPDF_MemStreamAttr)stream->attr;
1219
+ return attr->buf_siz;
1220
+ }
1221
+
1222
+ HPDF_UINT
1223
+ HPDF_MemStream_GetBufCount (HPDF_Stream stream)
1224
+ {
1225
+ HPDF_MemStreamAttr attr;
1226
+
1227
+ HPDF_PTRACE((" HPDF_MemStream_GetBufCount\n"));
1228
+
1229
+ if (!stream || stream->type != HPDF_STREAM_MEMORY)
1230
+ return 0;
1231
+
1232
+ attr = (HPDF_MemStreamAttr)stream->attr;
1233
+ return attr->buf->count;
1234
+ }
1235
+
1236
+ HPDF_STATUS
1237
+ HPDF_MemStream_ReadFunc (HPDF_Stream stream,
1238
+ HPDF_BYTE *buf,
1239
+ HPDF_UINT *size)
1240
+ {
1241
+ HPDF_MemStreamAttr attr = (HPDF_MemStreamAttr)stream->attr;
1242
+ HPDF_UINT buf_size;
1243
+ HPDF_UINT rlen = *size;
1244
+
1245
+ HPDF_PTRACE((" HPDF_MemStream_ReadFunc\n"));
1246
+
1247
+ *size = 0;
1248
+
1249
+ while (rlen > 0) {
1250
+ HPDF_UINT tmp_len;
1251
+
1252
+ if (attr->buf->count == 0)
1253
+ return HPDF_STREAM_EOF;
1254
+
1255
+ if (attr->buf->count - 1 > attr->r_ptr_idx)
1256
+ tmp_len = attr->buf_siz - attr->r_pos;
1257
+ else if (attr->buf->count - 1 == attr->r_ptr_idx)
1258
+ tmp_len = attr->w_pos - attr->r_pos;
1259
+ else
1260
+ return HPDF_STREAM_EOF;
1261
+
1262
+ if (!attr->r_ptr)
1263
+ attr->r_ptr = (HPDF_BYTE*)HPDF_List_ItemAt (attr->buf,
1264
+ attr->r_ptr_idx);
1265
+
1266
+ if (tmp_len >= rlen) {
1267
+ HPDF_MemCpy (buf, attr->r_ptr, rlen);
1268
+ attr->r_pos += rlen;
1269
+ *size += rlen;
1270
+ attr->r_ptr += rlen;
1271
+ return HPDF_OK;
1272
+ } else {
1273
+ buf = HPDF_MemCpy (buf, attr->r_ptr, tmp_len);
1274
+ rlen -= tmp_len;
1275
+ *size += tmp_len;
1276
+
1277
+ if (attr->r_ptr_idx == attr->buf->count - 1) {
1278
+ attr->r_ptr += tmp_len;
1279
+ attr->r_pos += tmp_len;
1280
+ return HPDF_STREAM_EOF;
1281
+ }
1282
+
1283
+ attr->r_ptr_idx++;
1284
+ attr->r_pos = 0;
1285
+ attr->r_ptr = HPDF_MemStream_GetBufPtr (stream, attr->r_ptr_idx,
1286
+ &buf_size);
1287
+ }
1288
+ }
1289
+
1290
+ return HPDF_OK;
1291
+ }
1292
+
1293
+
1294
+ HPDF_STATUS
1295
+ HPDF_MemStream_Rewrite (HPDF_Stream stream,
1296
+ HPDF_BYTE *buf,
1297
+ HPDF_UINT size)
1298
+ {
1299
+ HPDF_MemStreamAttr attr = (HPDF_MemStreamAttr)stream->attr;
1300
+ HPDF_UINT buf_size;
1301
+ HPDF_UINT rlen = size;
1302
+
1303
+ HPDF_PTRACE((" HPDF_MemStream_Rewrite\n"));
1304
+
1305
+ while (rlen > 0) {
1306
+ HPDF_UINT tmp_len;
1307
+
1308
+ if (attr->buf->count <= attr->r_ptr_idx) {
1309
+ HPDF_STATUS ret = HPDF_MemStream_WriteFunc (stream, buf, rlen);
1310
+ attr->r_ptr_idx = attr->buf->count;
1311
+ attr->r_pos = attr->w_pos;
1312
+ attr->r_ptr = attr->w_ptr;
1313
+ return ret;
1314
+ } else if (attr->buf->count == attr->r_ptr_idx)
1315
+ tmp_len = attr->w_pos - attr->r_pos;
1316
+ else
1317
+ tmp_len = attr->buf_siz - attr->r_pos;
1318
+
1319
+ if (tmp_len >= rlen) {
1320
+ HPDF_MemCpy(attr->r_ptr, buf, rlen);
1321
+ attr->r_pos += rlen;
1322
+ attr->r_ptr += rlen;
1323
+ return HPDF_OK;
1324
+ } else {
1325
+ HPDF_MemCpy(attr->r_ptr, buf, tmp_len);
1326
+ buf += tmp_len;
1327
+ rlen -= tmp_len;
1328
+ attr->r_ptr_idx++;
1329
+
1330
+ if (attr->buf->count > attr->r_ptr_idx) {
1331
+ attr->r_pos = 0;
1332
+ attr->r_ptr = HPDF_MemStream_GetBufPtr (stream, attr->r_ptr_idx,
1333
+ &buf_size);
1334
+ }
1335
+ }
1336
+ }
1337
+ return HPDF_OK;
1338
+ }
1339
+
1340
+ /*
1341
+ * HPDF_CallbackReader_new
1342
+ *
1343
+ * Constractor for HPDF_CallbackReader.
1344
+ *
1345
+ * mmgr : Pointer to a HPDF_MMgr object.
1346
+ * read_fn : Pointer to a user function for reading data.
1347
+ * seek_fn : Pointer to a user function for seeking data.
1348
+ * data : Pointer to a data which defined by user.
1349
+ *
1350
+ * return: If success, It returns pointer to new HPDF_Stream object,
1351
+ * otherwise, it returns NULL.
1352
+ *
1353
+ */
1354
+
1355
+
1356
+ HPDF_Stream
1357
+ HPDF_CallbackReader_New (HPDF_MMgr mmgr,
1358
+ HPDF_Stream_Read_Func read_fn,
1359
+ HPDF_Stream_Seek_Func seek_fn,
1360
+ HPDF_Stream_Tell_Func tell_fn,
1361
+ HPDF_Stream_Size_Func size_fn,
1362
+ void* data)
1363
+ {
1364
+ HPDF_Stream stream;
1365
+
1366
+ HPDF_PTRACE((" HPDF_CallbackReader_New\n"));
1367
+
1368
+ stream = (HPDF_Stream)HPDF_GetMem (mmgr, sizeof(HPDF_Stream_Rec));
1369
+
1370
+ if (stream) {
1371
+ HPDF_MemSet (stream, 0, sizeof(HPDF_Stream_Rec));
1372
+ stream->sig_bytes = HPDF_STREAM_SIG_BYTES;
1373
+ stream->error = mmgr->error;
1374
+ stream->mmgr = mmgr;
1375
+ stream->read_fn = read_fn;
1376
+ stream->seek_fn = seek_fn;
1377
+ stream->tell_fn = tell_fn;
1378
+ stream->size_fn = size_fn;
1379
+ stream->attr = data;
1380
+ stream->type = HPDF_STREAM_CALLBACK;
1381
+ }
1382
+
1383
+ return stream;
1384
+ }
1385
+
1386
+ /*
1387
+ * HPDF_CallbackWriter_new
1388
+ *
1389
+ * Constractor for HPDF_CallbackWriter.
1390
+ *
1391
+ * mmgr : Pointer to a HPDF_MMgr object.
1392
+ * read_fn : Pointer to a user function for writing data.
1393
+ * data : Pointer to a data which defined by user.
1394
+ *
1395
+ * return: If success, It returns pointer to new HPDF_Stream object,
1396
+ * otherwise, it returns NULL.
1397
+ *
1398
+ */
1399
+
1400
+
1401
+ HPDF_Stream
1402
+ HPDF_CallbackWriter_New (HPDF_MMgr mmgr,
1403
+ HPDF_Stream_Write_Func write_fn,
1404
+ void* data)
1405
+ {
1406
+ HPDF_Stream stream;
1407
+
1408
+ HPDF_PTRACE((" HPDF_CallbackWriter_New\n"));
1409
+
1410
+ stream = (HPDF_Stream)HPDF_GetMem (mmgr, sizeof(HPDF_Stream_Rec));
1411
+
1412
+ if (stream) {
1413
+ HPDF_MemSet (stream, 0, sizeof(HPDF_Stream_Rec));
1414
+ stream->sig_bytes = HPDF_STREAM_SIG_BYTES;
1415
+ stream->error = mmgr->error;
1416
+ stream->mmgr = mmgr;
1417
+ stream->write_fn = write_fn;
1418
+ stream->attr = data;
1419
+ stream->type = HPDF_STREAM_CALLBACK;
1420
+ }
1421
+
1422
+ return stream;
1423
+ }
1424
+
1425
+
1426
+
1427
+ HPDF_STATUS
1428
+ HPDF_Stream_Validate (HPDF_Stream stream)
1429
+ {
1430
+ if (!stream || stream->sig_bytes != HPDF_STREAM_SIG_BYTES)
1431
+ return HPDF_FALSE;
1432
+ else
1433
+ return HPDF_TRUE;
1434
+ }
1435
+