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
7364
_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
7364
    value = (uint32_t)four_tuple[0] << 24 | four_tuple[1] << 16 | four_tuple[2] << 8 | four_tuple[3];
57
7364
    if (all_zero)
58
7350
	*all_zero = TRUE;
59
44184
    for (i = 0; i < 5; i++) {
60
36820
	digit = value % 85;
61
36820
	if (digit != 0 && all_zero)
62
36390
	    *all_zero = FALSE;
63
36820
	five_tuple[4-i] = digit + 33;
64
36820
	value = value / 85;
65
    }
66
7364
}
67

            
68
static cairo_status_t
69
20
_cairo_base85_stream_write (cairo_output_stream_t *base,
70
			    const unsigned char	  *data,
71
			    unsigned int	   length)
72
{
73
20
    cairo_base85_stream_t *stream = (cairo_base85_stream_t *) base;
74
20
    const unsigned char *ptr = data;
75
    unsigned char five_tuple[5];
76
    cairo_bool_t is_zero;
77

            
78
29448
    while (length) {
79
29428
	stream->four_tuple[stream->pending++] = *ptr++;
80
29428
	length--;
81
29428
	if (stream->pending == 4) {
82
7350
	    _expand_four_tuple_to_five (stream->four_tuple, five_tuple, &is_zero);
83
7350
	    if (is_zero)
84
		_cairo_output_stream_write (stream->output, "z", 1);
85
	    else
86
7350
		_cairo_output_stream_write (stream->output, five_tuple, 5);
87
7350
	    stream->pending = 0;
88
	}
89
    }
90

            
91
20
    return _cairo_output_stream_get_status (stream->output);
92
}
93

            
94
static cairo_status_t
95
20
_cairo_base85_stream_close (cairo_output_stream_t *base)
96
{
97
20
    cairo_base85_stream_t *stream = (cairo_base85_stream_t *) base;
98
    unsigned char five_tuple[5];
99

            
100
20
    if (stream->pending) {
101
14
	memset (stream->four_tuple + stream->pending, 0, 4 - stream->pending);
102
14
	_expand_four_tuple_to_five (stream->four_tuple, five_tuple, NULL);
103
14
	_cairo_output_stream_write (stream->output, five_tuple, stream->pending + 1);
104
    }
105

            
106
20
    return _cairo_output_stream_get_status (stream->output);
107
}
108

            
109
cairo_output_stream_t *
110
20
_cairo_base85_stream_create (cairo_output_stream_t *output)
111
{
112
    cairo_base85_stream_t *stream;
113

            
114
20
    if (output->status)
115
	return _cairo_output_stream_create_in_error (output->status);
116

            
117
20
    stream = _cairo_calloc (sizeof (cairo_base85_stream_t));
118
20
    if (unlikely (stream == NULL)) {
119
	_cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
120
	return (cairo_output_stream_t *) &_cairo_output_stream_nil;
121
    }
122

            
123
20
    _cairo_output_stream_init (&stream->base,
124
			       _cairo_base85_stream_write,
125
			       NULL,
126
			       _cairo_base85_stream_close);
127
20
    stream->output = output;
128
20
    stream->pending = 0;
129

            
130
20
    return &stream->base;
131
}