/src/cairo/src/cairo-deflate-stream.c
Line | Count | Source (jump to first uncovered line) |
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 | | |
39 | | #if CAIRO_HAS_DEFLATE_STREAM |
40 | | |
41 | | #include "cairo-error-private.h" |
42 | | #include "cairo-output-stream-private.h" |
43 | | #include <zlib.h> |
44 | | |
45 | 14.9M | #define BUFFER_SIZE 16384 |
46 | | |
47 | | typedef struct _cairo_deflate_stream { |
48 | | cairo_output_stream_t base; |
49 | | cairo_output_stream_t *output; |
50 | | z_stream zlib_stream; |
51 | | unsigned char input_buf[BUFFER_SIZE]; |
52 | | unsigned char output_buf[BUFFER_SIZE]; |
53 | | } cairo_deflate_stream_t; |
54 | | |
55 | | static void |
56 | | cairo_deflate_stream_deflate (cairo_deflate_stream_t *stream, cairo_bool_t flush) |
57 | 14.4k | { |
58 | 14.4k | int ret; |
59 | 14.4k | cairo_bool_t finished; |
60 | | |
61 | 14.5k | do { |
62 | 14.5k | ret = deflate (&stream->zlib_stream, flush ? Z_FINISH : Z_NO_FLUSH); |
63 | 14.5k | if (flush || stream->zlib_stream.avail_out == 0) |
64 | 10.6k | { |
65 | 10.6k | _cairo_output_stream_write (stream->output, |
66 | 10.6k | stream->output_buf, |
67 | 10.6k | BUFFER_SIZE - stream->zlib_stream.avail_out); |
68 | 10.6k | stream->zlib_stream.next_out = stream->output_buf; |
69 | 10.6k | stream->zlib_stream.avail_out = BUFFER_SIZE; |
70 | 10.6k | } |
71 | | |
72 | 14.5k | finished = TRUE; |
73 | 14.5k | if (stream->zlib_stream.avail_in != 0) |
74 | 33 | finished = FALSE; |
75 | 14.5k | if (flush && ret != Z_STREAM_END) |
76 | 34 | finished = FALSE; |
77 | | |
78 | 14.5k | } while (!finished); |
79 | | |
80 | 14.4k | stream->zlib_stream.next_in = stream->input_buf; |
81 | 14.4k | } |
82 | | |
83 | | static cairo_status_t |
84 | | _cairo_deflate_stream_write (cairo_output_stream_t *base, |
85 | | const unsigned char *data, |
86 | | unsigned int length) |
87 | 7.46M | { |
88 | 7.46M | cairo_deflate_stream_t *stream = (cairo_deflate_stream_t *) base; |
89 | 7.46M | unsigned int count; |
90 | 7.46M | const unsigned char *p = data; |
91 | | |
92 | 14.9M | while (length) { |
93 | 7.47M | count = length; |
94 | 7.47M | if (count > BUFFER_SIZE - stream->zlib_stream.avail_in) |
95 | 3.62k | count = BUFFER_SIZE - stream->zlib_stream.avail_in; |
96 | 7.47M | memcpy (stream->input_buf + stream->zlib_stream.avail_in, p, count); |
97 | 7.47M | p += count; |
98 | 7.47M | stream->zlib_stream.avail_in += count; |
99 | 7.47M | length -= count; |
100 | | |
101 | 7.47M | if (stream->zlib_stream.avail_in == BUFFER_SIZE) |
102 | 3.92k | cairo_deflate_stream_deflate (stream, FALSE); |
103 | 7.47M | } |
104 | | |
105 | 7.46M | return _cairo_output_stream_get_status (stream->output); |
106 | 7.46M | } |
107 | | |
108 | | static cairo_status_t |
109 | | _cairo_deflate_stream_close (cairo_output_stream_t *base) |
110 | 10.5k | { |
111 | 10.5k | cairo_deflate_stream_t *stream = (cairo_deflate_stream_t *) base; |
112 | | |
113 | 10.5k | cairo_deflate_stream_deflate (stream, TRUE); |
114 | 10.5k | deflateEnd (&stream->zlib_stream); |
115 | | |
116 | 10.5k | return _cairo_output_stream_get_status (stream->output); |
117 | 10.5k | } |
118 | | |
119 | | cairo_output_stream_t * |
120 | | _cairo_deflate_stream_create (cairo_output_stream_t *output) |
121 | 10.5k | { |
122 | 10.5k | cairo_deflate_stream_t *stream; |
123 | | |
124 | 10.5k | if (output->status) |
125 | 0 | return _cairo_output_stream_create_in_error (output->status); |
126 | | |
127 | 10.5k | stream = _cairo_calloc (sizeof (cairo_deflate_stream_t)); |
128 | 10.5k | if (unlikely (stream == NULL)) { |
129 | 0 | _cairo_error_throw (CAIRO_STATUS_NO_MEMORY); |
130 | 0 | return (cairo_output_stream_t *) &_cairo_output_stream_nil; |
131 | 0 | } |
132 | | |
133 | 10.5k | _cairo_output_stream_init (&stream->base, |
134 | 10.5k | _cairo_deflate_stream_write, |
135 | 10.5k | NULL, |
136 | 10.5k | _cairo_deflate_stream_close); |
137 | 10.5k | stream->output = output; |
138 | | |
139 | 10.5k | stream->zlib_stream.zalloc = Z_NULL; |
140 | 10.5k | stream->zlib_stream.zfree = Z_NULL; |
141 | 10.5k | stream->zlib_stream.opaque = Z_NULL; |
142 | | |
143 | 10.5k | if (deflateInit (&stream->zlib_stream, Z_DEFAULT_COMPRESSION) != Z_OK) { |
144 | 0 | free (stream); |
145 | 0 | return (cairo_output_stream_t *) &_cairo_output_stream_nil; |
146 | 0 | } |
147 | | |
148 | 10.5k | stream->zlib_stream.next_in = stream->input_buf; |
149 | 10.5k | stream->zlib_stream.avail_in = 0; |
150 | 10.5k | stream->zlib_stream.next_out = stream->output_buf; |
151 | 10.5k | stream->zlib_stream.avail_out = BUFFER_SIZE; |
152 | | |
153 | 10.5k | return &stream->base; |
154 | 10.5k | } |
155 | | |
156 | | #endif /* CAIRO_HAS_DEFLATE_STREAM */ |