Tizen 2.0 Release
[framework/graphics/cairo.git] / src / cairo-base64-stream.c
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-2007 Emmanuel Pacaud <emmanuel.pacaud@free.fr>
5  * Copyright © 2009 Chris Wilson
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it either under the terms of the GNU Lesser General Public
9  * License version 2.1 as published by the Free Software Foundation
10  * (the "LGPL") or, at your option, under the terms of the Mozilla
11  * Public License Version 1.1 (the "MPL"). If you do not alter this
12  * notice, a recipient may use your version of this file under either
13  * the MPL or the LGPL.
14  *
15  * You should have received a copy of the LGPL along with this library
16  * in the file COPYING-LGPL-2.1; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
18  * You should have received a copy of the MPL along with this library
19  * in the file COPYING-MPL-1.1
20  *
21  * The contents of this file are subject to the Mozilla Public License
22  * Version 1.1 (the "License"); you may not use this file except in
23  * compliance with the License. You may obtain a copy of the License at
24  * http://www.mozilla.org/MPL/
25  *
26  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
27  * OF ANY KIND, either express or implied. See the LGPL or the MPL for
28  * the specific language governing rights and limitations.
29  *
30  * The Original Code is the cairo graphics library.
31  *
32  * The Initial Developer of the Original Code is Red Hat, Inc.
33  *
34  * Author(s):
35  *      Kristian Høgsberg <krh@redhat.com>
36  *      Chris Wilson <chris@chris-wilson.co.uk>
37  */
38
39 #include "cairoint.h"
40 #include "cairo-error-private.h"
41 #include "cairo-output-stream-private.h"
42
43 typedef struct _cairo_base64_stream {
44     cairo_output_stream_t base;
45     cairo_output_stream_t *output;
46     unsigned int in_mem;
47     unsigned int trailing;
48     unsigned char src[3];
49 } cairo_base64_stream_t;
50
51 static char const base64_table[64] =
52 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
53
54 static cairo_status_t
55 _cairo_base64_stream_write (cairo_output_stream_t *base,
56                             const unsigned char   *data,
57                             unsigned int           length)
58 {
59     cairo_base64_stream_t * stream = (cairo_base64_stream_t *) base;
60     unsigned char *src = stream->src;
61     unsigned int i;
62
63     if (stream->in_mem + length < 3) {
64         for (i = 0; i < length; i++) {
65             src[i + stream->in_mem] = *data++;
66         }
67         stream->in_mem += length;
68         return CAIRO_STATUS_SUCCESS;
69     }
70
71     do {
72         unsigned char dst[4];
73
74         for (i = stream->in_mem; i < 3; i++) {
75             src[i] = *data++;
76             length--;
77         }
78         stream->in_mem = 0;
79
80         dst[0] = base64_table[src[0] >> 2];
81         dst[1] = base64_table[(src[0] & 0x03) << 4 | src[1] >> 4];
82         dst[2] = base64_table[(src[1] & 0x0f) << 2 | src[2] >> 6];
83         dst[3] = base64_table[src[2] & 0xfc >> 2];
84         /* Special case for the last missing bits */
85         switch (stream->trailing) {
86             case 2:
87                 dst[2] = '=';
88             case 1:
89                 dst[3] = '=';
90             default:
91                 break;
92         }
93         _cairo_output_stream_write (stream->output, dst, 4);
94     } while (length >= 3);
95
96     for (i = 0; i < length; i++) {
97         src[i] = *data++;
98     }
99     stream->in_mem = length;
100
101     return _cairo_output_stream_get_status (stream->output);
102 }
103
104 static cairo_status_t
105 _cairo_base64_stream_close (cairo_output_stream_t *base)
106 {
107     cairo_base64_stream_t *stream = (cairo_base64_stream_t *) base;
108     cairo_status_t status = CAIRO_STATUS_SUCCESS;
109
110     if (stream->in_mem > 0) {
111         memset (stream->src + stream->in_mem, 0, 3 - stream->in_mem);
112         stream->trailing = 3 - stream->in_mem;
113         stream->in_mem = 3;
114         status = _cairo_base64_stream_write (base, NULL, 0);
115     }
116
117     return status;
118 }
119
120 cairo_output_stream_t *
121 _cairo_base64_stream_create (cairo_output_stream_t *output)
122 {
123     cairo_base64_stream_t *stream;
124
125     if (output->status)
126         return _cairo_output_stream_create_in_error (output->status);
127
128     stream = malloc (sizeof (cairo_base64_stream_t));
129     if (unlikely (stream == NULL)) {
130         _cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
131         return (cairo_output_stream_t *) &_cairo_output_stream_nil;
132     }
133
134     _cairo_output_stream_init (&stream->base,
135                                _cairo_base64_stream_write,
136                                NULL,
137                                _cairo_base64_stream_close);
138
139     stream->output = output;
140     stream->in_mem = 0;
141     stream->trailing = 0;
142
143     return &stream->base;
144 }