1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
/*---------------------------------------------------------------------------*
| PDFlib - A library for generating PDF on the fly |
+---------------------------------------------------------------------------+
| Copyright (c) 1997-2006 Thomas Merz and PDFlib GmbH. All rights reserved. |
+---------------------------------------------------------------------------+
| |
| This software is subject to the PDFlib license. It is NOT in the |
| public domain. Extended versions and commercial licenses are |
| available, please check http://www.pdflib.com. |
| |
*---------------------------------------------------------------------------*/
/* $Id: p_filter.c,v 1.2 2009/10/20 18:14:16 scuri Exp $
*
* Compressed and uncompressed stream output
*
*/
#include "p_intern.h"
/* methods for constructing a data source from a file */
#define FILE_BUFSIZE 1024
void
pdf_data_source_file_init(PDF *p, PDF_data_source *src)
{
pdc_file *fp;
src->buffer_length = FILE_BUFSIZE;
src->buffer_start = (pdc_byte *)
pdc_malloc(p->pdc, src->buffer_length, "pdf_data_source_file_init");
fp = pdc_fsearch_fopen(p->pdc, (const char *) src->private_data, NULL,
"embedded ", PDC_FILE_BINARY);
if (fp == NULL)
pdc_error(p->pdc, -1, 0, 0, 0, 0);
if (src->offset)
pdc_fseek(fp, src->offset, SEEK_SET);
src->private_data = (void *) fp;
src->total = (long) 0;
}
pdc_bool
pdf_data_source_file_fill(PDF *p, PDF_data_source *src)
{
size_t bytes_needed;
(void) p; /* avoid compiler warning "unreferenced parameter" */
if (src->length != (long) 0 && src->total + FILE_BUFSIZE > src->length)
bytes_needed = (size_t) (src->length - src->total);
else
bytes_needed = FILE_BUFSIZE;
src->next_byte = src->buffer_start;
src->bytes_available = pdc_fread(src->buffer_start, 1,
bytes_needed, (pdc_file *) (src->private_data));
src->total += (long) src->bytes_available;
if (src->bytes_available == 0)
return pdc_false;
else
return pdc_true;
}
void
pdf_data_source_file_terminate(PDF *p, PDF_data_source *src)
{
pdc_free(p->pdc, (void *) src->buffer_start);
pdc_fclose((pdc_file *) src->private_data);
if (src->length != (long) 0 && src->total != src->length)
pdc_error(p->pdc, PDC_E_IO_READ, "?", 0, 0, 0);
}
/* methods for constructing a data source from a memory buffer */
int
pdf_data_source_buf_fill(PDF *p, PDF_data_source *src)
{
(void) p; /* avoid compiler warning "unreferenced parameter" */
if (src->next_byte == NULL) {
src->next_byte = src->buffer_start;
src->bytes_available = src->buffer_length;
return pdc_true;
}
return pdc_false;
}
/* copy the complete contents of src to a stream */
void
pdf_copy_stream(PDF *p, PDF_data_source *src, pdc_bool compress)
{
int oldcompresslevel = pdc_get_compresslevel(p->out);
if (!compress)
pdc_set_compresslevel(p->out, 0);
if (src->init)
src->init(p, src);
pdc_begin_pdfstream(p->out);
while (src->fill(p, src))
pdc_write(p->out, src->next_byte, src->bytes_available);
pdc_end_pdfstream(p->out);
if (src->terminate)
src->terminate(p, src);
if (!compress)
pdc_set_compresslevel(p->out, oldcompresslevel);
}
|