~ubuntu-branches/ubuntu/jaunty/gimp/jaunty-security

« back to all changes in this revision

Viewing changes to plug-ins/metadata/base64.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2007-05-02 16:33:03 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20070502163303-bvzhjzbpw8qglc4y
Tags: 2.3.16-1ubuntu1
* Resynchronized with Debian, remaining Ubuntu changes:
  - debian/rules: i18n magic.
* debian/control.in:
  - Maintainer: Ubuntu Core Developers <ubuntu-devel@lists.ubuntu.com>
* debian/patches/02_help-message.patch,
  debian/patches/03_gimp.desktop.in.in.patch,
  debian/patches/10_dont_show_wizard.patch: updated.
* debian/patches/04_composite-signedness.patch,
  debian/patches/05_add-letter-spacing.patch: dropped, used upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* base64.h - encode and decode base64 encoding according to RFC 2045
 
2
 *
 
3
 * Copyright (C) 2005, Raphaël Quinet <raphael@gimp.org>
 
4
 *
 
5
 * This library is free software; you can redistribute it and/or
 
6
 * modify it under the terms of the GNU Lesser General Public
 
7
 * License as published by the Free Software Foundation; either
 
8
 * version 2 of the License, or (at your option) any later version.
 
9
 *
 
10
 * This library is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
13
 * Lesser General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU Lesser General Public
 
16
 * License along with this library; if not, write to the
 
17
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
18
 * Boston, MA 02111-1307, USA.
 
19
 */
 
20
 
 
21
/* Yet another implementation of base64 encoding and decoding.  I
 
22
 * ended up writing this because most of the implementations that I
 
23
 * found required a null-terminated buffer, some of the others did not
 
24
 * ignore whitespace (especially those written for HTTP usage) and the
 
25
 * rest were not compatible with the LGPL (some were GPL, not LGPL).
 
26
 * Or at least I haven't been able to find LGPL implementations.
 
27
 * Writing this according to RFC 2045 did not take long anyway.
 
28
 */
 
29
 
 
30
#ifndef WITHOUT_GIMP
 
31
#  include "config.h"
 
32
#  include <string.h>
 
33
#  include "base64.h"
 
34
#  include "libgimp/stdplugins-intl.h"
 
35
#else
 
36
#  include <string.h>
 
37
#  include "base64.h"
 
38
#  define _(String) (String)
 
39
#  define N_(String) (String)
 
40
#endif
 
41
 
 
42
#if HAVE_STDIO_H
 
43
#include <stdio.h>
 
44
#endif
 
45
#if HAVE_STDLIB_H
 
46
#include <stdlib.h>
 
47
#endif
 
48
 
 
49
static const gchar base64_code[] =
 
50
  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
 
51
static const gint base64_6bits[256] =
 
52
{
 
53
  -2, -3, -3, -3, -3, -3, -3, -3, -3, -1, -1, -1, -1, -1, -3, -3,
 
54
  -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3,
 
55
  -1, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, 62, -3, -3, -3, 63,
 
56
  52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -3, -3, -3, -2, -3, -3,
 
57
  -3,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
 
58
  15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -3, -3, -3, -3, -3,
 
59
  -3, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
 
60
  41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -3, -3, -3, -3, -3,
 
61
  -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3,
 
62
  -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3,
 
63
  -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3,
 
64
  -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3,
 
65
  -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3,
 
66
  -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3,
 
67
  -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3,
 
68
  -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3
 
69
}; /* -1: skip whitespace, -2: end of input, -3: error */
 
70
 
 
71
/**
 
72
 * base64_decode:
 
73
 * @src_b64: input buffer containing base64-encoded data
 
74
 * @src_size: input buffer size (in bytes) or -1 if @src_b64 is nul-terminated
 
75
 * @dest: buffer in which the decoded data should be stored
 
76
 * @dest_size: size of the destination buffer
 
77
 * @ignore_errors: if #TRUE, skip all invalid characters (no data validation)
 
78
 *
 
79
 * Read base64-encoded data from the input buffer @src_b64 and write
 
80
 * the decoded data into @dest.
 
81
 *
 
82
 * The base64 encoding uses 4 bytes for every 3 bytes of input, so
 
83
 * @dest_size should be at least 3/4 of @src_size (or less if the
 
84
 * input contains whitespace characters).  The base64 encoding has no
 
85
 * reliable EOF marker, so this can cause additional data following
 
86
 * the base64-encoded block to be misinterpreted if @src_size is not
 
87
 * specified correctly.  The decoder will stop at the first nul byte
 
88
 * or at the first '=' (padding byte) so you should ensure that one of
 
89
 * these is present if you supply -1 for @src_size.  For more details
 
90
 * about the base64 encoding, see RFC 2045, chapter 6.8.
 
91
 *
 
92
 * Returns: the number of bytes stored in @dest, or -1 if invalid data was found.
 
93
 */
 
94
gssize
 
95
base64_decode (const gchar *src_b64,
 
96
               gsize        src_size,
 
97
               gchar       *dest,
 
98
               gsize        dest_size,
 
99
               gboolean     ignore_errors)
 
100
{
 
101
  gint32 decoded;
 
102
  gssize i;
 
103
  gint   n;
 
104
  gint   bits;
 
105
 
 
106
  g_return_val_if_fail (src_b64 != NULL, -1);
 
107
  g_return_val_if_fail (dest != NULL, -1);
 
108
  decoded = 0;
 
109
  n = 0;
 
110
  bits = 0;
 
111
  for (i = 0; (src_size != 0) && (i + 3 <= dest_size); src_b64++, src_size--)
 
112
    {
 
113
      bits = base64_6bits[(int) *src_b64 & 0xff];
 
114
      if (bits < 0)
 
115
        {
 
116
          if (bits == -2)
 
117
            break;
 
118
          else if ((bits == -3) && !ignore_errors)
 
119
            return -1;
 
120
          else
 
121
            continue;
 
122
        }
 
123
      decoded <<= 6;
 
124
      decoded += bits;
 
125
      if (++n >= 4)
 
126
        {
 
127
          /* we have decoded 4 source chars => 24 bits of output (3 chars) */
 
128
          dest[i++] = decoded >> 16;
 
129
          dest[i++] = (decoded >> 8) & 0xff;
 
130
          dest[i++] = decoded & 0xff;
 
131
          decoded = 0;
 
132
          n = 0;
 
133
        }
 
134
    }
 
135
  if ((n == 3) && (i + 2 <= dest_size))
 
136
    {
 
137
      /* 3 source chars (+ 1 padding "=") => 16 bits of output (2 chars) */
 
138
      dest[i++] = decoded >> 10;
 
139
      dest[i++] = (decoded >> 2) & 0xff;
 
140
    }
 
141
  else if ((n == 2) && (i + 1 <= dest_size))
 
142
    {
 
143
      /* 2 source chars (+ 2 padding "=") => 8 bits of output (1 char) */
 
144
      dest[i++] = decoded >> 4;
 
145
    }
 
146
  if (i < dest_size)
 
147
    dest[i] = 0;
 
148
  return i;
 
149
}
 
150
 
 
151
/**
 
152
 * base64_encode:
 
153
 * @src: input buffer
 
154
 * @src_size: input buffer size (in bytes) or -1 if @src is nul-terminated
 
155
 * @dest_b64: buffer in which the base64 encoded data should be stored
 
156
 * @dest_size: size of the destination buffer
 
157
 * @columns: if > 0, add line breaks in the output after this many columns
 
158
 *
 
159
 * Read binary data from the input buffer @src and write
 
160
 * base64-encoded data into @dest_b64.
 
161
 *
 
162
 * Since the base64 encoding uses 4 bytes for every 3 bytes of input,
 
163
 * @dest_size should be at least 4/3 of @src_size, plus optional line
 
164
 * breaks if @columns > 0 and up to two padding bytes at the end.  For
 
165
 * more details about the base64 encoding, see RFC 2045, chapter 6.8.
 
166
 * Note that RFC 2045 recommends setting @columns to 76.
 
167
 *
 
168
 * Returns: the number of bytes stored in @dest.
 
169
 */
 
170
gssize
 
171
base64_encode (const gchar *src,
 
172
               gsize        src_size,
 
173
               gchar       *dest_b64,
 
174
               gsize        dest_size,
 
175
               gint         columns)
 
176
{
 
177
  guint32 bits;
 
178
  gssize  i;
 
179
  gint    n;
 
180
  gint    c;
 
181
 
 
182
  g_return_val_if_fail (src != NULL, -1);
 
183
  g_return_val_if_fail (dest_b64 != NULL, -1);
 
184
 
 
185
  n = 0;
 
186
  bits = 0;
 
187
  c = 0;
 
188
  for (i = 0; (src_size != 0) && (i + 4 <= dest_size); src++, src_size--)
 
189
    {
 
190
      bits += *(guchar *)src;
 
191
      if (++n == 3)
 
192
        {
 
193
          dest_b64[i++] = base64_code[(bits >> 18) & 0x3f];
 
194
          dest_b64[i++] = base64_code[(bits >> 12) & 0x3f];
 
195
          dest_b64[i++] = base64_code[(bits >> 6)  & 0x3f];
 
196
          dest_b64[i++] = base64_code[bits         & 0x3f];
 
197
          bits = 0;
 
198
          n = 0;
 
199
          if (columns > 0)
 
200
            {
 
201
              c += 4;
 
202
              if ((c >= columns) && (i < dest_size))
 
203
                {
 
204
                  dest_b64[i++] = '\n';
 
205
                  c = 0;
 
206
                }
 
207
            }
 
208
        }
 
209
      else
 
210
        {
 
211
          bits <<= 8;
 
212
        }
 
213
    }
 
214
  if ((n != 0) && (i + 4 <= dest_size))
 
215
    {
 
216
      if (n == 1)
 
217
        {
 
218
          dest_b64[i++] = base64_code[(bits >> 10) & 0x3f];
 
219
          dest_b64[i++] = base64_code[(bits >> 4)  & 0x3f];
 
220
          dest_b64[i++] = '=';
 
221
          dest_b64[i++] = '=';
 
222
        }
 
223
      else
 
224
        {
 
225
          dest_b64[i++] = base64_code[(bits >> 18) & 0x3f];
 
226
          dest_b64[i++] = base64_code[(bits >> 12) & 0x3f];
 
227
          dest_b64[i++] = base64_code[(bits >> 6)  & 0x3f];
 
228
          dest_b64[i++] = '=';
 
229
        }
 
230
    }
 
231
  if ((columns > 0) && ((c != 0) || (n != 0)) && (i + 1 < dest_size))
 
232
    dest_b64[i++] = '\n';
 
233
  if (i < dest_size)
 
234
    dest_b64[i] = 0;
 
235
  return i;
 
236
}