blob: b28e7527273e6df10672186bbea44a6934af5cb1 [file] [log] [blame]
Tristan Matthews0a329cc2013-07-17 13:20:14 -04001/* $Id$ */
2/*
3 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
5 *
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.
10 *
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.
15 *
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
19 */
20#include <pjlib-util/base64.h>
21#include <pj/assert.h>
22#include <pj/errno.h>
23
24#define INV -1
25#define PADDING '='
26
27const 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',
34 '8', '9', '+', '/'
35};
36
37static int base256_char(char c)
38{
39 if (c >= 'A' && c <= 'Z')
40 return (c - 'A');
41 else if (c >= 'a' && c <= 'z')
42 return (c - 'a' + 26);
43 else if (c >= '0' && c <= '9')
44 return (c - '0' + 52);
45 else if (c == '+')
46 return (62);
47 else if (c == '/')
48 return (63);
49 else {
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");
52 */
53 return INV;
54 }
55}
56
57
58static void base256to64(pj_uint8_t c1, pj_uint8_t c2, pj_uint8_t c3,
59 int padding, char *output)
60{
61 *output++ = base64_char[c1>>2];
62 *output++ = base64_char[((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4)];
63 switch (padding) {
64 case 0:
65 *output++ = base64_char[((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6)];
66 *output = base64_char[c3 & 0x3F];
67 break;
68 case 1:
69 *output++ = base64_char[((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6)];
70 *output = PADDING;
71 break;
72 case 2:
73 default:
74 *output++ = PADDING;
75 *output = PADDING;
76 break;
77 }
78}
79
80
81PJ_DEF(pj_status_t) pj_base64_encode(const pj_uint8_t *input, int in_len,
82 char *output, int *out_len)
83{
84 const pj_uint8_t *pi = input;
85 pj_uint8_t c1, c2, c3;
86 int i = 0;
87 char *po = output;
88
89 PJ_ASSERT_RETURN(input && output && out_len, PJ_EINVAL);
90 PJ_ASSERT_RETURN(*out_len >= PJ_BASE256_TO_BASE64_LEN(in_len),
91 PJ_ETOOSMALL);
92
93 while (i < in_len) {
94 c1 = *pi++;
95 ++i;
96
97 if (i == in_len) {
98 base256to64(c1, 0, 0, 2, po);
99 po += 4;
100 break;
101 } else {
102 c2 = *pi++;
103 ++i;
104
105 if (i == in_len) {
106 base256to64(c1, c2, 0, 1, po);
107 po += 4;
108 break;
109 } else {
110 c3 = *pi++;
111 ++i;
112 base256to64(c1, c2, c3, 0, po);
113 }
114 }
115
116 po += 4;
117 }
118
119 *out_len = (int)(po - output);
120 return PJ_SUCCESS;
121}
122
123
124PJ_DEF(pj_status_t) pj_base64_decode(const pj_str_t *input,
125 pj_uint8_t *out, int *out_len)
126{
127 const char *buf = input->ptr;
128 int len = (int)input->slen;
129 int i, j, k;
130 int c[4];
131
132 PJ_ASSERT_RETURN(input && out && out_len, PJ_EINVAL);
133
134 while (buf[len-1] == '=' && len)
135 --len;
136
137 PJ_ASSERT_RETURN(*out_len >= PJ_BASE64_TO_BASE256_LEN(len),
138 PJ_ETOOSMALL);
139
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) {
143 do {
144 c[k] = base256_char(buf[i++]);
145 } while (c[k]==INV && i<len);
146 }
147
148 if (k<4) {
149 if (k > 1) {
150 out[j++] = (pj_uint8_t)((c[0]<<2) | ((c[1] & 0x30)>>4));
151 if (k > 2) {
152 out[j++] = (pj_uint8_t)
153 (((c[1] & 0x0F)<<4) | ((c[2] & 0x3C)>>2));
154 }
155 }
156 break;
157 }
158
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));
162 }
163
164 pj_assert(j < *out_len);
165 *out_len = j;
166
167 return PJ_SUCCESS;
168}
169
170