GitLab repository storage has been migrated to hashed layout. Please contact Infrastructure team if you notice any issues with repositories or hooks.

gbase64.c 10.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
/* gbase64.c - Base64 encoding/decoding
 *
 *  Copyright (C) 2006 Alexander Larsson <alexl@redhat.com>
 *  Copyright (C) 2000-2003 Ximian Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * This is based on code in camel, written by:
 *    Michael Zucchi <notzed@ximian.com>
 *    Jeffrey Stedfast <fejj@ximian.com>
 */

#include "config.h"

#include <string.h>

#include "gbase64.h"
#include "glib.h"
#include "glibintl.h"

#include "galias.h"

static const char base64_alphabet[] =
	"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

/**
 * g_base64_encode_step:
41 42
 * @in: the binary data to encode
 * @len: the length of @in
43 44 45 46 47 48
 * @break_lines: whether to break long lines
 * @out: pointer to destination buffer
 * @state: Saved state between steps, initialize to 0
 * @save: Saved state between steps, initialize to 0
 *
 * Incrementally encode a sequence of binary data into it's Base-64 stringified
49 50
 * representation. By calling this function multiple times you can convert 
 * data in chunks to avoid having to have the full encoded data in memory.
51
 *
52 53
 * When all of the data has been converted you must call 
 * g_base64_encode_close() to flush the saved state.
54 55 56 57 58 59 60
 *
 * The output buffer must be large enough to fit all the data that will
 * be written to it. Due to the way base64 encodes you will need
 * at least: @len * 4 / 3 + 6 bytes. If you enable line-breaking you will
 * need at least: @len * 4 / 3 + @len * 4 / (3 * 72) + 7 bytes.
 *
 * @break_lines is typically used when putting base64-encoded data in emails.
61 62
 * It breaks the lines at 72 columns instead of putting all of the text on 
 * the same line. This avoids problems with long lines in the email system.
63 64 65 66 67 68
 *
 * Return value: The number of bytes of output that was written
 *
 * Since: 2.12
 */
gsize
Matthias Clasen's avatar
Matthias Clasen committed
69 70 71 72 73 74
g_base64_encode_step (const guchar *in, 
		      gsize         len, 
		      gboolean      break_lines, 
		      gchar        *out, 
		      gint         *state, 
		      gint         *save)
75 76 77 78
{
  char *outptr;
  const guchar *inptr;
  
79 80 81 82 83
  g_return_val_if_fail (in != NULL, 0);
  g_return_val_if_fail (out != NULL, 0);
  g_return_val_if_fail (state != NULL, 0);
  g_return_val_if_fail (save != NULL, 0);

84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
  if (len <= 0)
    return 0;
  
  inptr = in;
  outptr = out;
  
  if (len + ((char *) save) [0] > 2)
    {
      const guchar *inend = in+len-2;
      int c1, c2, c3;
      int already;
      
      already = *state;
      
      switch (((char *) save) [0])
Matthias Clasen's avatar
Matthias Clasen committed
99 100 101 102 103 104 105 106
	{	
	case 1:	
	  c1 = ((unsigned char *) save) [1]; 
          goto skip1;
	case 2:	
          c1 = ((unsigned char *) save) [1];
	  c2 = ((unsigned char *) save) [2]; 
          goto skip2;
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
	}
      
      /* 
       * yes, we jump into the loop, no i'm not going to change it, 
       * it's beautiful! 
       */
      while (inptr < inend)
	{
	  c1 = *inptr++;
	skip1:
	  c2 = *inptr++;
	skip2:
	  c3 = *inptr++;
	  *outptr++ = base64_alphabet [ c1 >> 2 ];
	  *outptr++ = base64_alphabet [ c2 >> 4 | 
					((c1&0x3) << 4) ];
	  *outptr++ = base64_alphabet [ ((c2 &0x0f) << 2) | 
					(c3 >> 6) ];
	  *outptr++ = base64_alphabet [ c3 & 0x3f ];
	  /* this is a bit ugly ... */
Matthias Clasen's avatar
Matthias Clasen committed
127
	  if (break_lines && (++already) >= 19)
128
	    {
Matthias Clasen's avatar
Matthias Clasen committed
129
	      *outptr++ = '\n';
130 131 132 133 134
	      already = 0;
	    }
	}
      
      ((char *)save)[0] = 0;
Matthias Clasen's avatar
Matthias Clasen committed
135
      len = 2 - (inptr - inend);
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
      *state = already;
    }
  
  if (len>0)
    {
      char *saveout;
      
      /* points to the slot for the next char to save */
      saveout = & (((char *)save)[1]) + ((char *)save)[0];
      
      /* len can only be 0 1 or 2 */
      switch(len)
	{
	case 2:	*saveout++ = *inptr++;
	case 1:	*saveout++ = *inptr++;
	}
Matthias Clasen's avatar
Matthias Clasen committed
152
      ((char *)save)[0] += len;
153 154
    }
  
Matthias Clasen's avatar
Matthias Clasen committed
155
  return outptr - out;
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
}

/**
 * g_base64_encode_close:
 * @break_lines: whether to break long lines
 * @out: pointer to destination buffer
 * @state: Saved state from g_base64_encode_step()
 * @save: Saved state from g_base64_encode_step()
 *
 * Flush the status from a sequence of calls to g_base64_encode_step().
 *
 * Return value: The number of bytes of output that was written
 *
 * Since: 2.12
 */
gsize
Matthias Clasen's avatar
Matthias Clasen committed
172 173 174 175
g_base64_encode_close (gboolean  break_lines,
		       gchar    *out, 
		       gint     *state, 
		       gint     *save)
176 177 178 179
{
  int c1, c2;
  char *outptr = out;

180 181 182 183
  g_return_val_if_fail (out != NULL, 0);
  g_return_val_if_fail (state != NULL, 0);
  g_return_val_if_fail (save != NULL, 0);

184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
  c1 = ((unsigned char *) save) [1];
  c2 = ((unsigned char *) save) [2];
  
  switch (((char *) save) [0])
    {
    case 2:
      outptr [2] = base64_alphabet[ ( (c2 &0x0f) << 2 ) ];
      g_assert (outptr [2] != 0);
      goto skip;
    case 1:
      outptr[2] = '=';
    skip:
      outptr [0] = base64_alphabet [ c1 >> 2 ];
      outptr [1] = base64_alphabet [ c2 >> 4 | ( (c1&0x3) << 4 )];
      outptr [3] = '=';
      outptr += 4;
      break;
    }
  if (break_lines)
    *outptr++ = '\n';
  
  *save = 0;
  *state = 0;
  
Matthias Clasen's avatar
Matthias Clasen committed
208
  return outptr - out;
209 210 211 212
}

/**
 * g_base64_encode:
213 214
 * @data: the binary data to encode
 * @len: the length of @data
215
 *
216
 * Encode a sequence of binary data into its Base-64 stringified
217 218 219
 * representation.
 *
 * Return value: a newly allocated, zero-terminated Base-64 encoded
220 221
 *               string representing @data. The returned string must 
 *               be freed with g_free().
222 223 224
 *
 * Since: 2.12
 */
Matthias Clasen's avatar
Matthias Clasen committed
225 226 227
gchar *
g_base64_encode (const guchar *data, 
                 gsize         len)
228
{
Matthias Clasen's avatar
Matthias Clasen committed
229 230 231
  gchar *out;
  gint state = 0, outlen;
  gint save = 0;
232

233
  g_return_val_if_fail (data != NULL, NULL);
234
  g_return_val_if_fail (len > 0, NULL);
235

236 237 238
  /* We can use a smaller limit here, since we know the saved state is 0 */
  out = g_malloc (len * 4 / 3 + 4);
  outlen = g_base64_encode_step (data, len, FALSE, out, &state, &save);
239
  outlen += g_base64_encode_close (FALSE, out + outlen, &state, &save);
240
  out[outlen] = '\0';
241

Matthias Clasen's avatar
Matthias Clasen committed
242
  return (gchar *) out;
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
}

static const unsigned char mime_base64_rank[256] = {
  255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
  255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
  255,255,255,255,255,255,255,255,255,255,255, 62,255,255,255, 63,
   52, 53, 54, 55, 56, 57, 58, 59, 60, 61,255,255,255,  0,255,255,
  255,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
   15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,255,255,255,255,255,
  255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
   41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,255,255,255,255,255,
  255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
  255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
  255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
  255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
  255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
  255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
  255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
  255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
};

/**
Matthias Clasen's avatar
Matthias Clasen committed
265
 * g_base64_decode_step: 
266 267 268 269 270 271
 * @in: binary input data
 * @len: max length of @in data to decode
 * @out: output buffer
 * @state: Saved state between steps, initialize to 0
 * @save: Saved state between steps, initialize to 0
 *
272 273 274
 * Incrementally decode a sequence of binary data from its Base-64 stringified
 * representation. By calling this function multiple times you can convert 
 * data in chunks to avoid having to have the full encoded data in memory.
275 276 277 278 279 280 281 282 283 284
 *
 * The output buffer must be large enough to fit all the data that will
 * be written to it. Since base64 encodes 3 bytes in 4 chars you need
 * at least: @len * 3 / 4 bytes.
 * 
 * Return value: The number of bytes of output that was written
 *
 * Since: 2.12
 **/
gsize
Matthias Clasen's avatar
Matthias Clasen committed
285 286 287 288 289
g_base64_decode_step (const gchar  *in, 
		      gsize         len, 
		      guchar       *out, 
		      gint         *state, 
		      guint        *save)
290 291 292 293
{
  const guchar *inptr;
  guchar *outptr;
  const guchar *inend;
Alexander Larsson's avatar
Alexander Larsson committed
294 295
  guchar c, rank;
  guchar last[2];
296 297
  unsigned int v;
  int i;
298 299 300 301 302 303 304 305

  g_return_val_if_fail (in != NULL, 0);
  g_return_val_if_fail (out != NULL, 0);
  g_return_val_if_fail (state != NULL, 0);
  g_return_val_if_fail (save != NULL, 0);

  if (len <= 0)
    return 0;
306 307 308 309 310 311 312 313
  
  inend = (const guchar *)in+len;
  outptr = out;
  
  /* convert 4 base64 bytes to 3 normal bytes */
  v=*save;
  i=*state;
  inptr = (const guchar *)in;
Alexander Larsson's avatar
Alexander Larsson committed
314
  last[0] = last[1] = 0;
315 316
  while (inptr < inend)
    {
Alexander Larsson's avatar
Alexander Larsson committed
317 318 319
      c = *inptr++;
      rank = mime_base64_rank [c];
      if (rank != 0xff)
320
	{
Alexander Larsson's avatar
Alexander Larsson committed
321 322 323
	  last[1] = last[0];
	  last[0] = c;
	  v = (v<<6) | rank;
324 325 326 327
	  i++;
	  if (i==4)
	    {
	      *outptr++ = v>>16;
Alexander Larsson's avatar
Alexander Larsson committed
328 329 330 331
	      if (last[1] != '=')
		*outptr++ = v>>8;
	      if (last[0] != '=')
		*outptr++ = v;
332 333 334 335 336 337 338 339 340 341 342 343 344
	      i=0;
	    }
	}
    }
  
  *save = v;
  *state = i;
  
  return outptr - out;
}

/**
 * g_base64_decode:
345 346
 * @text: zero-terminated string with base64 text to decode
 * @out_len: The length of the decoded data is written here
347 348 349
 *
 * Decode a sequence of Base-64 encoded text into binary data
 *
350
 * Return value: a newly allocated buffer containing the binary data
351 352
 *               that @text represents. The returned buffer must
 *               be freed with g_free().
353 354 355 356
 *
 * Since: 2.12
 */
guchar *
Matthias Clasen's avatar
Matthias Clasen committed
357 358
g_base64_decode (const gchar *text,
		 gsize       *out_len)
359 360
{
  guchar *ret;
361
  gint input_length, state = 0;
362 363
  guint save = 0;
  
364 365 366
  g_return_val_if_fail (text != NULL, NULL);
  g_return_val_if_fail (out_len != NULL, NULL);

367 368 369 370 371
  input_length = strlen (text);

  g_return_val_if_fail (input_length > 1, NULL);

  ret = g_malloc0 (input_length * 3 / 4);
372
  
373
  *out_len = g_base64_decode_step (text, input_length, ret, &state, &save);
374 375 376 377 378 379
  
  return ret; 
}

#define __G_BASE64_C__
#include "galiasdef.c"