summaryrefslogtreecommitdiff
path: root/src/base64.c
blob: 600fa3950894888de1a22c0579b502f37072620a (plain)
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
 * base64.c - Base 64 encoding/decoding implementation
 * Copyright (C) 2006 Collabora Ltd.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "config.h"
#include "base64.h"

#define DEBUG_FLAG GABBLE_DEBUG_VCARD
#include "debug.h"

#include <ctype.h>
#include <string.h>


/*
|AAAA AABB|BBBB CCCC|CCDD DDDD|

0xFC = 1111 1100
0x03 = 0000 0011
0xF0 = 1111 0000
0x0F = 0000 1111
0xC0 = 1100 0000
0x3F = 0011 1111

3 input bytes = 4 output bytes;
2 input bytes = 2 output bytes;
1 input byte  = 1 output byte.
*/

static const gchar *encoding =
  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

static const guint decoding[256] =
{
  /* ... */
   0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
   0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
   0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
   0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
   0, 0, 0,
  /* + */
  62,
  /* ... */
   0, 0, 0,
  /* / , 0-9 */
  63,
  52, 53, 54, 55, 56, 57, 58, 59, 60, 61,
  /* ... */
   0, 0, 0, 0, 0, 0, 0,
  /* A */
   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,
  /* ... */
   0, 0, 0, 0, 0, 0,
  /* a */
  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
};

#define GET_6_BITS_0(s) (((s)[0] & 0xFC) >> 2)
#define GET_6_BITS_1(s) (((s)[0] & 0x03) << 4) | \
                        (((s)[1] & 0xF0) >> 4)
#define GET_6_BITS_2(s) (((s)[1] & 0x0F) << 2) | \
                        (((s)[2] & 0xC0) >> 6)
#define GET_6_BITS_3(s) (((s)[2] & 0x3F) << 0)

#define GET_BYTE_0(s) (((decoding[(guchar)(s)[0]] & 0x3F) << 2) | \
                       ((decoding[(guchar)(s)[1]] & 0x30) >> 4))
#define GET_BYTE_1(s) (((decoding[(guchar)(s)[1]] & 0x0F) << 4) | \
                       ((decoding[(guchar)(s)[2]] & 0x3C) >> 2))
#define GET_BYTE_2(s) (((decoding[(guchar)(s)[2]] & 0x03) << 6) | \
                       ((decoding[(guchar)(s)[3]] & 0xFF) << 0))

gchar *base64_encode (guint len, const gchar *str, gboolean split_lines)
{
  guint i;
  GString *tmp;

  /* TODO: calculate requisite output string length and allocate that big a
   * GString */
  tmp = g_string_new ("");

  for (i = 0; i < len; i += 3)
    {
      guint c1, c2, c3, c4;

      if (split_lines && i > 0 && (i * 4) % 76 == 0)
          g_string_append_c (tmp, '\n');

      switch (i + 3 - len)
        {
        case 1:
          c1 = encoding[GET_6_BITS_0 (str + i)];
          c2 = encoding[GET_6_BITS_1 (str + i)];
          /* can't use GET_6_BITS_2 because str[i+2] is out of range */
          c3 = encoding[(str[i + 1] & 0x0f) << 2];
          c4 = '=';
          break;
        case 2:
          c1 = encoding[GET_6_BITS_0 (str + i)];
          /* can't use GET_6_BITS_1 because str[i+1] is out of range */
          c2 = encoding[(str[i] & 0x03) << 4];
          c3 = '=';
          c4 = '=';
          break;
        default:
          c1 = encoding[GET_6_BITS_0 (str + i)];
          c2 = encoding[GET_6_BITS_1 (str + i)];
          c3 = encoding[GET_6_BITS_2 (str + i)];
          c4 = encoding[GET_6_BITS_3 (str + i)];
        }

      g_string_append_printf (tmp, "%c%c%c%c", c1, c2, c3, c4);
    }

  return g_string_free (tmp, FALSE);
}

GString *base64_decode (const gchar *str)
{
  guint i;
  GString *tmp;
  char group[4];
  guint filled = 0;

  for (i = 0; str[i]; i++)
    {
      if (str[i] != 'A' &&
          str[i] != '=' &&
          !isspace (str[i]) &&
          decoding[(guchar) str[i]] == 0)
        {
          DEBUG ("bad character %x at byte %u", (guchar)str[i], i);
          return NULL;
        }
    }

  tmp = g_string_new ("");

  for (i = 0; str[i]; i++)
    {
      if (isspace (str[i]))
        continue;

      group[filled++] = str[i];

      if (filled == 4)
        {
          if (group[3] == '=')
            {
              if (group[2] == '=')
                {
                  g_string_append_c (tmp, GET_BYTE_0(group));
                }
              else
                {
                  g_string_append_c (tmp, GET_BYTE_0(group));
                  g_string_append_c (tmp, GET_BYTE_1(group));
                }
             }
           else
            {
              g_string_append_c (tmp, GET_BYTE_0(group));
              g_string_append_c (tmp, GET_BYTE_1(group));
              g_string_append_c (tmp, GET_BYTE_2(group));
            }
          filled = 0;
        }
    }

  if (filled)
    {
      DEBUG ("insufficient padding at end of base64 string:\n%s", str);
      g_string_free (tmp, TRUE);
      return NULL;
    }

  return tmp;
}