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
4 * Copyright © 2005 Red Hat, Inc
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 * You should have received a copy of the MPL along with this library
18 * in the file COPYING-MPL-1.1
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/
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.
29 * The Original Code is the cairo graphics library.
31 * The Initial Developer of the Original Code is Red Hat, Inc.
34 * Kristian Høgsberg <krh@redhat.com>
38 #include "cairo-output-stream-private.h"
40 typedef struct _cairo_base85_stream
{
41 cairo_output_stream_t base
;
42 cairo_output_stream_t
*output
;
43 unsigned char four_tuple
[4];
45 } cairo_base85_stream_t
;
48 _expand_four_tuple_to_five (unsigned char four_tuple
[4],
49 unsigned char five_tuple
[5],
50 cairo_bool_t
*all_zero
)
55 value
= four_tuple
[0] << 24 | four_tuple
[1] << 16 | four_tuple
[2] << 8 | four_tuple
[3];
58 for (i
= 0; i
< 5; i
++) {
60 if (digit
!= 0 && all_zero
)
62 five_tuple
[4-i
] = digit
+ 33;
68 _cairo_base85_stream_write (cairo_output_stream_t
*base
,
69 const unsigned char *data
,
72 cairo_base85_stream_t
*stream
= (cairo_base85_stream_t
*) base
;
73 const unsigned char *ptr
= data
;
74 unsigned char five_tuple
[5];
78 stream
->four_tuple
[stream
->pending
++] = *ptr
++;
80 if (stream
->pending
== 4) {
81 _expand_four_tuple_to_five (stream
->four_tuple
, five_tuple
, &is_zero
);
83 _cairo_output_stream_write (stream
->output
, "z", 1);
85 _cairo_output_stream_write (stream
->output
, five_tuple
, 5);
90 return _cairo_output_stream_get_status (stream
->output
);
94 _cairo_base85_stream_close (cairo_output_stream_t
*base
)
96 cairo_base85_stream_t
*stream
= (cairo_base85_stream_t
*) base
;
97 unsigned char five_tuple
[5];
99 if (stream
->pending
) {
100 memset (stream
->four_tuple
+ stream
->pending
, 0, 4 - stream
->pending
);
101 _expand_four_tuple_to_five (stream
->four_tuple
, five_tuple
, NULL
);
102 _cairo_output_stream_write (stream
->output
, five_tuple
, stream
->pending
+ 1);
105 /* Mark end of base85 data */
106 _cairo_output_stream_printf (stream
->output
, "~>");
108 return _cairo_output_stream_get_status (stream
->output
);
111 cairo_output_stream_t
*
112 _cairo_base85_stream_create (cairo_output_stream_t
*output
)
114 cairo_base85_stream_t
*stream
;
117 return _cairo_output_stream_create_in_error (output
->status
);
119 stream
= malloc (sizeof (cairo_base85_stream_t
));
120 if (stream
== NULL
) {
121 _cairo_error_throw (CAIRO_STATUS_NO_MEMORY
);
122 return (cairo_output_stream_t
*) &_cairo_output_stream_nil
;
125 _cairo_output_stream_init (&stream
->base
,
126 _cairo_base85_stream_write
,
127 _cairo_base85_stream_close
);
128 stream
->output
= output
;
131 return &stream
->base
;