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 © 2005 Red Hat, Inc
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 Red Hat, Inc.
32 : *
33 : * Author(s):
34 : * Kristian Høgsberg <krh@redhat.com>
35 : */
36 :
37 : #include "cairoint.h"
38 : #include "cairo-error-private.h"
39 : #include "cairo-output-stream-private.h"
40 :
41 : typedef struct _cairo_base85_stream {
42 : cairo_output_stream_t base;
43 : cairo_output_stream_t *output;
44 : unsigned char four_tuple[4];
45 : int pending;
46 : } cairo_base85_stream_t;
47 :
48 : static void
49 0 : _expand_four_tuple_to_five (unsigned char four_tuple[4],
50 : unsigned char five_tuple[5],
51 : cairo_bool_t *all_zero)
52 : {
53 : uint32_t value;
54 : int digit, i;
55 :
56 0 : value = four_tuple[0] << 24 | four_tuple[1] << 16 | four_tuple[2] << 8 | four_tuple[3];
57 0 : if (all_zero)
58 0 : *all_zero = TRUE;
59 0 : for (i = 0; i < 5; i++) {
60 0 : digit = value % 85;
61 0 : if (digit != 0 && all_zero)
62 0 : *all_zero = FALSE;
63 0 : five_tuple[4-i] = digit + 33;
64 0 : value = value / 85;
65 : }
66 0 : }
67 :
68 : static cairo_status_t
69 0 : _cairo_base85_stream_write (cairo_output_stream_t *base,
70 : const unsigned char *data,
71 : unsigned int length)
72 : {
73 0 : cairo_base85_stream_t *stream = (cairo_base85_stream_t *) base;
74 0 : const unsigned char *ptr = data;
75 : unsigned char five_tuple[5];
76 : cairo_bool_t is_zero;
77 :
78 0 : while (length) {
79 0 : stream->four_tuple[stream->pending++] = *ptr++;
80 0 : length--;
81 0 : if (stream->pending == 4) {
82 0 : _expand_four_tuple_to_five (stream->four_tuple, five_tuple, &is_zero);
83 0 : if (is_zero)
84 0 : _cairo_output_stream_write (stream->output, "z", 1);
85 : else
86 0 : _cairo_output_stream_write (stream->output, five_tuple, 5);
87 0 : stream->pending = 0;
88 : }
89 : }
90 :
91 0 : return _cairo_output_stream_get_status (stream->output);
92 : }
93 :
94 : static cairo_status_t
95 0 : _cairo_base85_stream_close (cairo_output_stream_t *base)
96 : {
97 0 : cairo_base85_stream_t *stream = (cairo_base85_stream_t *) base;
98 : unsigned char five_tuple[5];
99 :
100 0 : if (stream->pending) {
101 0 : memset (stream->four_tuple + stream->pending, 0, 4 - stream->pending);
102 0 : _expand_four_tuple_to_five (stream->four_tuple, five_tuple, NULL);
103 0 : _cairo_output_stream_write (stream->output, five_tuple, stream->pending + 1);
104 : }
105 :
106 0 : return _cairo_output_stream_get_status (stream->output);
107 : }
108 :
109 : cairo_output_stream_t *
110 0 : _cairo_base85_stream_create (cairo_output_stream_t *output)
111 : {
112 : cairo_base85_stream_t *stream;
113 :
114 0 : if (output->status)
115 0 : return _cairo_output_stream_create_in_error (output->status);
116 :
117 0 : stream = malloc (sizeof (cairo_base85_stream_t));
118 0 : if (unlikely (stream == NULL)) {
119 0 : _cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
120 0 : return (cairo_output_stream_t *) &_cairo_output_stream_nil;
121 : }
122 :
123 0 : _cairo_output_stream_init (&stream->base,
124 : _cairo_base85_stream_write,
125 : NULL,
126 : _cairo_base85_stream_close);
127 0 : stream->output = output;
128 0 : stream->pending = 0;
129 :
130 0 : return &stream->base;
131 : }
|