1
/* $Id: base64.c 3553 2011-05-05 06:14:19Z nanang $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
#include <pjlib-util/base64.h>
21
#include <pj/assert.h>
27
const char base64_char[] = {
28
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
29
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
30
'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
31
'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
32
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
33
'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7',
37
static int base256_char(char c)
39
if (c >= 'A' && c <= 'Z')
41
else if (c >= 'a' && c <= 'z')
42
return (c - 'a' + 26);
43
else if (c >= '0' && c <= '9')
44
return (c - '0' + 52);
50
/* It *may* happen on bad input, so this is not a good idea.
51
* pj_assert(!"Should not happen as '=' should have been filtered");
58
static void base256to64(pj_uint8_t c1, pj_uint8_t c2, pj_uint8_t c3,
59
int padding, char *output)
61
*output++ = base64_char[c1>>2];
62
*output++ = base64_char[((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4)];
65
*output++ = base64_char[((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6)];
66
*output = base64_char[c3 & 0x3F];
69
*output++ = base64_char[((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6)];
81
PJ_DEF(pj_status_t) pj_base64_encode(const pj_uint8_t *input, int in_len,
82
char *output, int *out_len)
84
const pj_uint8_t *pi = input;
85
pj_uint8_t c1, c2, c3;
89
PJ_ASSERT_RETURN(input && output && out_len, PJ_EINVAL);
90
PJ_ASSERT_RETURN(*out_len >= PJ_BASE256_TO_BASE64_LEN(in_len),
98
base256to64(c1, 0, 0, 2, po);
106
base256to64(c1, c2, 0, 1, po);
112
base256to64(c1, c2, c3, 0, po);
119
*out_len = po - output;
124
PJ_DEF(pj_status_t) pj_base64_decode(const pj_str_t *input,
125
pj_uint8_t *out, int *out_len)
127
const char *buf = input->ptr;
128
int len = input->slen;
132
PJ_ASSERT_RETURN(input && out && out_len, PJ_EINVAL);
134
while (buf[len-1] == '=' && len)
137
PJ_ASSERT_RETURN(*out_len >= PJ_BASE64_TO_BASE256_LEN(len),
140
for (i=0, j=0; i<len; ) {
141
/* Fill up c, silently ignoring invalid characters */
142
for (k=0; k<4 && i<len; ++k) {
144
c[k] = base256_char(buf[i++]);
145
} while (c[k]==INV && i<len);
150
out[j++] = (pj_uint8_t)((c[0]<<2) | ((c[1] & 0x30)>>4));
152
out[j++] = (pj_uint8_t)
153
(((c[1] & 0x0F)<<4) | ((c[2] & 0x3C)>>2));
159
out[j++] = (pj_uint8_t)((c[0]<<2) | ((c[1] & 0x30)>>4));
160
out[j++] = (pj_uint8_t)(((c[1] & 0x0F)<<4) | ((c[2] & 0x3C)>>2));
161
out[j++] = (pj_uint8_t)(((c[2] & 0x03)<<6) | (c[3] & 0x3F));
164
pj_assert(j < *out_len);