~ubuntu-branches/ubuntu/quantal/cups-filters/quantal-proposed

« back to all changes in this revision

Viewing changes to filter/pdftopdf/P2POutputStream.cxx

  • Committer: Package Import Robot
  • Author(s): Till Kamppeter
  • Date: 2012-08-20 14:53:42 UTC
  • mfrom: (2.1.2 experimental)
  • Revision ID: package-import@ubuntu.com-20120820145342-bddzpwqv0klmt84d
Tags: 1.0.22-1
* New upstream release
   - pdftopdf filter replaced by new QPDF-based filter from Tobias
     Hoffmann's Google Summer of Code project. The former Poppler-based
     pdftopdf duplicated a lot of Poppler's code. The old filter is
     still in the package as pdftopdf.old with source code in
     filter/pdftopdf.old. It will be removed in a later release.
   - bannertopdf: Page duplication routine fixed.
   - bannertopdf: Fixed invalid output of a direct stream object.
   - Added most recent contributors to AUTHORS and COPYING files.
* debian/control: Added build dependency on libqpdf-dev.
* debian/copyright: Updated for the addition of the new pdftopdf filter.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 
3
 
Copyright (c) 2006-2007, BBR Inc.  All rights reserved.
4
 
 
5
 
Permission is hereby granted, free of charge, to any person obtaining
6
 
a copy of this software and associated documentation files (the
7
 
"Software"), to deal in the Software without restriction, including
8
 
without limitation the rights to use, copy, modify, merge, publish,
9
 
distribute, sublicense, and/or sell copies of the Software, and to
10
 
permit persons to whom the Software is furnished to do so, subject to
11
 
the following conditions:
12
 
 
13
 
The above copyright notice and this permission notice shall be included
14
 
in all copies or substantial portions of the Software.
15
 
 
16
 
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
 
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
 
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
 
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
 
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
 
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
 
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
 
 
24
 
*/
25
 
/*
26
 
 P2POutputStream.cc
27
 
 pdftopdf stream for output
28
 
*/
29
 
 
30
 
#include <config.h>
31
 
#include "goo/gmem.h"
32
 
#include "P2POutputStream.h"
33
 
#include <stdarg.h>
34
 
#include <string.h>
35
 
#include "P2PError.h"
36
 
 
37
 
#define LINEBUFSIZE 1024
38
 
 
39
 
 
40
 
/* Constructor */
41
 
P2POutputStream::P2POutputStream(FILE *fpA)
42
 
{
43
 
  fp = fpA;
44
 
  position = 0;
45
 
  deflating = gFalse;
46
 
}
47
 
 
48
 
P2POutputStream::~P2POutputStream()
49
 
{
50
 
}
51
 
 
52
 
int P2POutputStream::puts(const char *str)
53
 
{
54
 
  return write(str,strlen(str));
55
 
}
56
 
 
57
 
int P2POutputStream::putchar(char c)
58
 
{
59
 
  int r;
60
 
 
61
 
  if ((r = write(&c,1)) < 0) return r;
62
 
  return c & 0xff;
63
 
}
64
 
 
65
 
int P2POutputStream::printf(const char *format,...)
66
 
{
67
 
  char linebuf[LINEBUFSIZE];
68
 
  va_list ap;
69
 
  int r;
70
 
 
71
 
  va_start(ap,format);
72
 
  r = vsnprintf(linebuf,LINEBUFSIZE,format,ap);
73
 
  va_end(ap);
74
 
  if (r > 0) {
75
 
    if (puts(linebuf) < 0) {
76
 
      return EOF;
77
 
    }
78
 
  }
79
 
  return r;
80
 
}
81
 
 
82
 
int P2POutputStream::write(const void *buf, int n)
83
 
{
84
 
  int r;
85
 
  if (n <= 0) return 0;
86
 
#ifdef HAVE_LIBZ
87
 
  char outputBuf[LINEBUFSIZE*2];
88
 
 
89
 
  if (deflating) {
90
 
    zstream.next_in = const_cast<Bytef *>(static_cast<const Bytef *>(buf));
91
 
    zstream.avail_in = n;
92
 
    do {
93
 
      int outputLen;
94
 
 
95
 
      zstream.next_out = reinterpret_cast<Bytef *>(outputBuf);
96
 
      zstream.avail_out = LINEBUFSIZE*2;
97
 
      if ((r = deflate(&zstream,0)) != Z_OK) {
98
 
        p2pError(-1,const_cast<char *>("ZLIB:deflate error"));
99
 
        return r;
100
 
      }
101
 
      /* Z_OK */
102
 
      /* output data */
103
 
      outputLen = LINEBUFSIZE*2-zstream.avail_out;
104
 
      if ((r = fwrite(outputBuf,1,outputLen,fp)) != outputLen) {
105
 
        p2pError(-1,const_cast<char *>("P2POutputStream: write  error"));
106
 
        break;
107
 
      }
108
 
      position += r;
109
 
    } while (zstream.avail_in > 0);
110
 
    return n;
111
 
  } else {
112
 
#endif
113
 
    if ((r = fwrite(buf,1,n,fp)) != n) {
114
 
      p2pError(-1,const_cast<char *>("P2POutputStream: write  error"));
115
 
      return r;
116
 
    }
117
 
    position += r;
118
 
    return r;
119
 
#ifdef HAVE_LIBZ
120
 
  }
121
 
#endif
122
 
}
123
 
 
124
 
/* define alloc functions */
125
 
namespace {
126
 
  voidpf gmalloc_zalloc(voidpf opaque, uInt items, uInt size)
127
 
  {
128
 
    return gmalloc(items*size);
129
 
  }
130
 
  void gmalloc_zfree(voidpf opqaue, voidpf address)
131
 
  {
132
 
    gfree(address);
133
 
  }
134
 
};
135
 
 
136
 
void P2POutputStream::startDeflate()
137
 
{
138
 
#ifdef HAVE_LIBZ
139
 
  zstream.zalloc = gmalloc_zalloc;
140
 
  zstream.zfree = gmalloc_zfree;
141
 
  zstream.opaque = 0;
142
 
 
143
 
  if (deflateInit(&zstream,Z_DEFAULT_COMPRESSION) != Z_OK) {
144
 
    p2pError(-1,const_cast<char *>("ZLIB:deflateInit error"));
145
 
    return;
146
 
  }
147
 
  deflating = gTrue;
148
 
#endif
149
 
}
150
 
 
151
 
void P2POutputStream::endDeflate()
152
 
{
153
 
#ifdef HAVE_LIBZ
154
 
  char outputBuf[LINEBUFSIZE*2];
155
 
  int r;
156
 
 
157
 
  if (!deflating) return;
158
 
  do {
159
 
    int n;
160
 
    int outputLen;
161
 
 
162
 
    zstream.next_out = reinterpret_cast<Bytef *>(outputBuf);
163
 
    zstream.avail_out = LINEBUFSIZE*2;
164
 
    zstream.next_in = 0;
165
 
    zstream.avail_in = 0;
166
 
    if ((r = deflate(&zstream,Z_FINISH)) != Z_STREAM_END && r != Z_OK) {
167
 
      p2pError(-1,const_cast<char *>("ZLIB:deflate error"));
168
 
      break;
169
 
    }
170
 
    /* Z_OK or Z_STREAM_END */
171
 
    /* output data */
172
 
    outputLen = LINEBUFSIZE*2-zstream.avail_out;
173
 
    if ((n = fwrite(outputBuf,1,outputLen,fp)) != outputLen) {
174
 
      p2pError(-1,const_cast<char *>("P2POutputStream: write  error"));
175
 
      break;
176
 
    }
177
 
    position += n;
178
 
  } while (r != Z_STREAM_END);
179
 
  if (deflateEnd(&zstream) != Z_OK) {
180
 
      p2pError(-1,const_cast<char *>("ZLIB:deflateEnd error"));
181
 
  }
182
 
  deflating = gFalse;
183
 
#endif
184
 
}