File: base64.c

package info (click to toggle)
dspam 3.10.1+dfsg-11
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 6,656 kB
  • sloc: ansic: 26,034; sh: 12,546; perl: 5,469; makefile: 690; sql: 379
file content (207 lines) | stat: -rw-r--r-- 4,364 bytes parent folder | download
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
197
198
199
200
201
202
203
204
205
206
207
/* $Id: base64.c,v 1.99 2011/06/28 00:13:48 sbajic Exp $ */

/*
 DSPAM
 COPYRIGHT (C) 2002-2011 DSPAM PROJECT

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU Affero General Public License as
 published by the Free Software Foundation, either version 3 of the
 License, or (at your option) any later version.

 This program 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 Affero General Public License for more details.

 You should have received a copy of the GNU Affero General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/

/*
 * base64.c - base64 encoding/decoding routines
 *
 */

#ifdef HAVE_CONFIG_H
#include <auto-config.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "error.h"
#include "base64.h"

char *
base64decode (const char *buf)
{
#ifdef VERBOSE
  LOGDEBUG ("decoding Base64 encoded buffer");
#endif
  unsigned char alphabet[64] =
    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  static char first_time = 1,inalphabet[256], decoder[256];
  int bits, c, char_count;
  int pos = 0, dpos = 0;
  char *decoded;

  decoded = malloc ((strlen (buf) * 2) + 2);
  if (decoded == NULL)
    return NULL;
  decoded[0] = 0;

  if(first_time) {
    int i;
    for (i = (sizeof alphabet) - 1; i >= 0; i--)
    {
      inalphabet[alphabet[i]] = 1;
      decoder[alphabet[i]] = i;
    }
    first_time = 0;
  }
  char_count = 0;
  bits = 0;
  while (buf[pos] != 0)
  {
    c = buf[pos];
    if (c == '=')
      break;
    if (c > 255 || !inalphabet[c])
    {
      pos++;
      continue;
    }
    bits += decoder[c];
    char_count++;
    if (char_count == 4)
    {
      decoded[dpos] = (bits >> 16);
      decoded[dpos + 1] = ((bits >> 8) & 0xff);
      decoded[dpos + 2] = (bits & 0xff);
      decoded[dpos + 3] = 0;
      dpos += 3;
      bits = 0;
      char_count = 0;
    }
    else
    {
      bits <<= 6;
    }
    pos++;
  }
  c = buf[pos];
  if (c == 0)
  {
    if (char_count)
    {
      LOGDEBUG ("base64 encoding incomplete: at least %d bits truncated",
                ((4 - char_count) * 6));
    }
  }
  else
  {                             /* c == '=' */
    switch (char_count)
    {
    case 1:
      LOGDEBUG ("base64 encoding incomplete: at least 2 bits missing");
      break;
    case 2:
      decoded[dpos] = (bits >> 10);
      decoded[dpos + 1] = 0;
      break;
    case 3:
      decoded[dpos] = (bits >> 16);
      decoded[dpos + 1] = ((bits >> 8) & 0xff);
      decoded[dpos + 2] = 0;
      break;
    }
  }
  if (strlen(decoded) > 0 && decoded[strlen(decoded)-1] != '\n')
    strcat(decoded, "\n");
  return decoded;
}

char *
base64encode (const char *buf)
{
#ifdef VERBOSE
  LOGDEBUG ("encoding buffer to Base64");
#endif
  unsigned char alphabet[64] =
    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  int cols, bits, c, char_count;
  char *out;
  long rpos = 0, wpos = 0;

  out = malloc (strlen (buf) * 2);
  if (out == NULL)
    return NULL;

  out[0] = 0;

  char_count = 0;
  bits = 0;
  cols = 0;
  c = buf[rpos];
  while (c != 0)
  {
    bits += c;
    char_count++;
    if (char_count == 3)
    {
      out[wpos] = (alphabet[bits >> 18]);
      out[wpos + 1] = (alphabet[(bits >> 12) & 0x3f]);
      out[wpos + 2] = (alphabet[(bits >> 6) & 0x3f]);
      out[wpos + 3] = (alphabet[bits & 0x3f]);
      wpos += 4;
      cols += 4;
      if (cols == 72)
      {
        out[wpos] = '\n';
        wpos++;
        cols = 0;
      }
      out[wpos] = 0;
      bits = 0;
      char_count = 0;
    }
    else
    {
      bits <<= 8;
    }
    rpos++;
    c = buf[rpos];
  }
  if (char_count != 0)
  {
    bits <<= 16 - (8 * char_count);
    out[wpos] = (alphabet[bits >> 18]);
    out[wpos + 1] = (alphabet[(bits >> 12) & 0x3f]);
    wpos += 2;
    if (char_count == 1)
    {
      out[wpos] = '=';
      out[wpos + 1] = '=';
    }
    else
    {
      out[wpos] = (alphabet[(bits >> 6) & 0x3f]);
      out[wpos + 1] = '=';
    }
    wpos += 2;
    if (cols > 0)
    {
      out[wpos] = '\n';
      wpos++;
    }
    out[wpos] = 0;
  }

  if (out[strlen (out) - 1] != '\n')
    strcat (out, "\n");

  return out;
}