~reviczky/luatex/texlive-bin-git

« back to all changes in this revision

Viewing changes to libs/cairo/cairo-1.12.16/src/cairo-base85-stream.c

  • Committer: Adam Reviczky
  • Date: 2015-04-26 22:40:47 UTC
  • Revision ID: adam.reviczky@kclalumni.net-20150426224047-i2p26n3wqphupq6z
TeX Live 2015 import (rev. 37052)

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
_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
 
    value = four_tuple[0] << 24 | four_tuple[1] << 16 | four_tuple[2] << 8 | four_tuple[3];
57
 
    if (all_zero)
58
 
        *all_zero = TRUE;
59
 
    for (i = 0; i < 5; i++) {
60
 
        digit = value % 85;
61
 
        if (digit != 0 && all_zero)
62
 
            *all_zero = FALSE;
63
 
        five_tuple[4-i] = digit + 33;
64
 
        value = value / 85;
65
 
    }
66
 
}
67
 
 
68
 
static cairo_status_t
69
 
_cairo_base85_stream_write (cairo_output_stream_t *base,
70
 
                            const unsigned char   *data,
71
 
                            unsigned int           length)
72
 
{
73
 
    cairo_base85_stream_t *stream = (cairo_base85_stream_t *) base;
74
 
    const unsigned char *ptr = data;
75
 
    unsigned char five_tuple[5];
76
 
    cairo_bool_t is_zero;
77
 
 
78
 
    while (length) {
79
 
        stream->four_tuple[stream->pending++] = *ptr++;
80
 
        length--;
81
 
        if (stream->pending == 4) {
82
 
            _expand_four_tuple_to_five (stream->four_tuple, five_tuple, &is_zero);
83
 
            if (is_zero)
84
 
                _cairo_output_stream_write (stream->output, "z", 1);
85
 
            else
86
 
                _cairo_output_stream_write (stream->output, five_tuple, 5);
87
 
            stream->pending = 0;
88
 
        }
89
 
    }
90
 
 
91
 
    return _cairo_output_stream_get_status (stream->output);
92
 
}
93
 
 
94
 
static cairo_status_t
95
 
_cairo_base85_stream_close (cairo_output_stream_t *base)
96
 
{
97
 
    cairo_base85_stream_t *stream = (cairo_base85_stream_t *) base;
98
 
    unsigned char five_tuple[5];
99
 
 
100
 
    if (stream->pending) {
101
 
        memset (stream->four_tuple + stream->pending, 0, 4 - stream->pending);
102
 
        _expand_four_tuple_to_five (stream->four_tuple, five_tuple, NULL);
103
 
        _cairo_output_stream_write (stream->output, five_tuple, stream->pending + 1);
104
 
    }
105
 
 
106
 
    return _cairo_output_stream_get_status (stream->output);
107
 
}
108
 
 
109
 
cairo_output_stream_t *
110
 
_cairo_base85_stream_create (cairo_output_stream_t *output)
111
 
{
112
 
    cairo_base85_stream_t *stream;
113
 
 
114
 
    if (output->status)
115
 
        return _cairo_output_stream_create_in_error (output->status);
116
 
 
117
 
    stream = malloc (sizeof (cairo_base85_stream_t));
118
 
    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
 
    _cairo_output_stream_init (&stream->base,
124
 
                               _cairo_base85_stream_write,
125
 
                               NULL,
126
 
                               _cairo_base85_stream_close);
127
 
    stream->output = output;
128
 
    stream->pending = 0;
129
 
 
130
 
    return &stream->base;
131
 
}