Line data Source code
1 : /* -*- Mode: c; c-basic-offset: 4; indent-tabs-mode: t; tab-width: 8; -*- */
2 : /* cairo - a vector graphics library with display and print output
3 : *
4 : * Copyright © 2006 Adrian Johnson
5 : *
6 : * This library is free software; you can redistribute it and/or
7 : * modify it either under the terms of the GNU Lesser General Public
8 : * License version 2.1 as published by the Free Software Foundation
9 : * (the "LGPL") or, at your option, under the terms of the Mozilla
10 : * Public License Version 1.1 (the "MPL"). If you do not alter this
11 : * notice, a recipient may use your version of this file under either
12 : * the MPL or the LGPL.
13 : *
14 : * You should have received a copy of the LGPL along with this library
15 : * in the file COPYING-LGPL-2.1; if not, write to the Free Software
16 : * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
17 : * You should have received a copy of the MPL along with this library
18 : * in the file COPYING-MPL-1.1
19 : *
20 : * The contents of this file are subject to the Mozilla Public License
21 : * Version 1.1 (the "License"); you may not use this file except in
22 : * compliance with the License. You may obtain a copy of the License at
23 : * http://www.mozilla.org/MPL/
24 : *
25 : * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
26 : * OF ANY KIND, either express or implied. See the LGPL or the MPL for
27 : * the specific language governing rights and limitations.
28 : *
29 : * The Original Code is the cairo graphics library.
30 : *
31 : * The Initial Developer of the Original Code is Adrian Johnson.
32 : *
33 : * Author(s):
34 : * Adrian Johnson <ajohnson@redneon.com>
35 : */
36 :
37 : #include "cairoint.h"
38 : #include "cairo-error-private.h"
39 : #include "cairo-output-stream-private.h"
40 : #include <zlib.h>
41 :
42 : #define BUFFER_SIZE 16384
43 :
44 : typedef struct _cairo_deflate_stream {
45 : cairo_output_stream_t base;
46 : cairo_output_stream_t *output;
47 : z_stream zlib_stream;
48 : unsigned char input_buf[BUFFER_SIZE];
49 : unsigned char output_buf[BUFFER_SIZE];
50 : } cairo_deflate_stream_t;
51 :
52 : static void
53 0 : cairo_deflate_stream_deflate (cairo_deflate_stream_t *stream, cairo_bool_t flush)
54 : {
55 : int ret;
56 : cairo_bool_t finished;
57 :
58 : do {
59 0 : ret = deflate (&stream->zlib_stream, flush ? Z_FINISH : Z_NO_FLUSH);
60 0 : if (flush || stream->zlib_stream.avail_out == 0)
61 : {
62 0 : _cairo_output_stream_write (stream->output,
63 0 : stream->output_buf,
64 0 : BUFFER_SIZE - stream->zlib_stream.avail_out);
65 0 : stream->zlib_stream.next_out = stream->output_buf;
66 0 : stream->zlib_stream.avail_out = BUFFER_SIZE;
67 : }
68 :
69 0 : finished = TRUE;
70 0 : if (stream->zlib_stream.avail_in != 0)
71 0 : finished = FALSE;
72 0 : if (flush && ret != Z_STREAM_END)
73 0 : finished = FALSE;
74 :
75 0 : } while (!finished);
76 :
77 0 : stream->zlib_stream.next_in = stream->input_buf;
78 0 : }
79 :
80 : static cairo_status_t
81 0 : _cairo_deflate_stream_write (cairo_output_stream_t *base,
82 : const unsigned char *data,
83 : unsigned int length)
84 : {
85 0 : cairo_deflate_stream_t *stream = (cairo_deflate_stream_t *) base;
86 : unsigned int count;
87 0 : const unsigned char *p = data;
88 :
89 0 : while (length) {
90 0 : count = length;
91 0 : if (count > BUFFER_SIZE - stream->zlib_stream.avail_in)
92 0 : count = BUFFER_SIZE - stream->zlib_stream.avail_in;
93 0 : memcpy (stream->input_buf + stream->zlib_stream.avail_in, p, count);
94 0 : p += count;
95 0 : stream->zlib_stream.avail_in += count;
96 0 : length -= count;
97 :
98 0 : if (stream->zlib_stream.avail_in == BUFFER_SIZE)
99 0 : cairo_deflate_stream_deflate (stream, FALSE);
100 : }
101 :
102 0 : return _cairo_output_stream_get_status (stream->output);
103 : }
104 :
105 : static cairo_status_t
106 0 : _cairo_deflate_stream_close (cairo_output_stream_t *base)
107 : {
108 0 : cairo_deflate_stream_t *stream = (cairo_deflate_stream_t *) base;
109 :
110 0 : cairo_deflate_stream_deflate (stream, TRUE);
111 0 : deflateEnd (&stream->zlib_stream);
112 :
113 0 : return _cairo_output_stream_get_status (stream->output);
114 : }
115 :
116 : cairo_output_stream_t *
117 0 : _cairo_deflate_stream_create (cairo_output_stream_t *output)
118 : {
119 : cairo_deflate_stream_t *stream;
120 :
121 0 : if (output->status)
122 0 : return _cairo_output_stream_create_in_error (output->status);
123 :
124 0 : stream = malloc (sizeof (cairo_deflate_stream_t));
125 0 : if (unlikely (stream == NULL)) {
126 0 : _cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
127 0 : return (cairo_output_stream_t *) &_cairo_output_stream_nil;
128 : }
129 :
130 0 : _cairo_output_stream_init (&stream->base,
131 : _cairo_deflate_stream_write,
132 : NULL,
133 : _cairo_deflate_stream_close);
134 0 : stream->output = output;
135 :
136 0 : stream->zlib_stream.zalloc = Z_NULL;
137 0 : stream->zlib_stream.zfree = Z_NULL;
138 0 : stream->zlib_stream.opaque = Z_NULL;
139 :
140 0 : if (deflateInit (&stream->zlib_stream, Z_DEFAULT_COMPRESSION) != Z_OK) {
141 0 : free (stream);
142 0 : return (cairo_output_stream_t *) &_cairo_output_stream_nil;
143 : }
144 :
145 0 : stream->zlib_stream.next_in = stream->input_buf;
146 0 : stream->zlib_stream.avail_in = 0;
147 0 : stream->zlib_stream.next_out = stream->output_buf;
148 0 : stream->zlib_stream.avail_out = BUFFER_SIZE;
149 :
150 0 : return &stream->base;
151 : }
|