jabberd2  2.3.2
base64.c
Go to the documentation of this file.
1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2  * contributor license agreements. See the NOTICE file distributed with
3  * this work for additional information regarding copyright ownership.
4  * The ASF licenses this file to You under the Apache License, Version 2.0
5  * (the "License"); you may not use this file except in compliance with
6  * the License. You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 /* base64 encoder/decoder. Originally part of main/util.c
18  * but moved here so that support/ab and apr_sha1.c could
19  * use it. This meant removing the apr_palloc()s and adding
20  * ugly 'len' functions, which is quite a nasty cost.
21  */
22 
23 #include "util.h"
24 
25 /* aaaack but it's fast and const should make it shared text page. */
26 static const unsigned char pr2six[256] =
27 {
28  /* ASCII table */
29  64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
30  64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
31  64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63,
32  52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64,
33  64, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
34  15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 64, 64, 64, 64, 64,
35  64, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
36  41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 64, 64, 64, 64, 64,
37  64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
38  64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
39  64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
40  64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
41  64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
42  64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
43  64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
44  64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64
45 };
46 
47 int apr_base64_decode_len(const char *bufcoded, int buflen)
48 {
49  int nbytesdecoded;
50  register const unsigned char *bufin;
51  register int nprbytes;
52 
53  bufin = (const unsigned char *) bufcoded;
54  while (pr2six[*(bufin++)] <= 63 && buflen-- > 0);
55 
56  nprbytes = (bufin - (const unsigned char *) bufcoded) - 1;
57  nbytesdecoded = (((int)nprbytes + 3) / 4) * 3;
58 
59  return nbytesdecoded + 1;
60 }
61 
62 int apr_base64_decode_binary(unsigned char *bufplain, const char *bufcoded, int buflen);
63 
64 int apr_base64_decode(char *bufplain, const char *bufcoded, int buflen)
65 {
66  int len;
67 
68  len = apr_base64_decode_binary((unsigned char *) bufplain, bufcoded, buflen);
69  bufplain[len] = '\0';
70  return len;
71 }
72 
73 int apr_base64_decode_binary(unsigned char *bufplain,
74  const char *bufcoded, int buflen)
75 {
76  int nbytesdecoded;
77  register const unsigned char *bufin;
78  register unsigned char *bufout;
79  register int nprbytes;
80 
81  bufin = (const unsigned char *) bufcoded;
82  while (pr2six[*(bufin++)] <= 63 && buflen-- > 0);
83  nprbytes = (bufin - (const unsigned char *) bufcoded) - 1;
84  nbytesdecoded = (((int)nprbytes + 3) / 4) * 3;
85 
86  bufout = (unsigned char *) bufplain;
87  bufin = (const unsigned char *) bufcoded;
88 
89  while (nprbytes > 4) {
90  *(bufout++) =
91  (unsigned char) (pr2six[*bufin] << 2 | pr2six[bufin[1]] >> 4);
92  *(bufout++) =
93  (unsigned char) (pr2six[bufin[1]] << 4 | pr2six[bufin[2]] >> 2);
94  *(bufout++) =
95  (unsigned char) (pr2six[bufin[2]] << 6 | pr2six[bufin[3]]);
96  bufin += 4;
97  nprbytes -= 4;
98  }
99 
100  /* Note: (nprbytes == 1) would be an error, so just ingore that case */
101  if (nprbytes > 1) {
102  *(bufout++) =
103  (unsigned char) (pr2six[*bufin] << 2 | pr2six[bufin[1]] >> 4);
104  }
105  if (nprbytes > 2) {
106  *(bufout++) =
107  (unsigned char) (pr2six[bufin[1]] << 4 | pr2six[bufin[2]] >> 2);
108  }
109  if (nprbytes > 3) {
110  *(bufout++) =
111  (unsigned char) (pr2six[bufin[2]] << 6 | pr2six[bufin[3]]);
112  }
113 
114  nbytesdecoded -= (4 - (int)nprbytes) & 3;
115  return nbytesdecoded;
116 }
117 
118 static const char basis_64[] =
119  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
120 
122 {
123  return ((len + 2) / 3 * 4) + 1;
124 }
125 
126 int apr_base64_encode_binary(char *encoded, const unsigned char *string, int len);
127 
128 int apr_base64_encode(char *encoded, const char *string, int len)
129 {
130  return apr_base64_encode_binary(encoded, (const unsigned char *) string, len);
131 }
132 
133 int apr_base64_encode_binary(char *encoded,
134  const unsigned char *string, int len)
135 {
136  int i;
137  char *p;
138 
139  p = encoded;
140  for (i = 0; i < len - 2; i += 3) {
141  *p++ = basis_64[(string[i] >> 2) & 0x3F];
142  *p++ = basis_64[((string[i] & 0x3) << 4) |
143  ((int) (string[i + 1] & 0xF0) >> 4)];
144  *p++ = basis_64[((string[i + 1] & 0xF) << 2) |
145  ((int) (string[i + 2] & 0xC0) >> 6)];
146  *p++ = basis_64[string[i + 2] & 0x3F];
147  }
148  if (i < len) {
149  *p++ = basis_64[(string[i] >> 2) & 0x3F];
150  if (i == (len - 1)) {
151  *p++ = basis_64[((string[i] & 0x3) << 4)];
152  *p++ = '=';
153  }
154  else {
155  *p++ = basis_64[((string[i] & 0x3) << 4) |
156  ((int) (string[i + 1] & 0xF0) >> 4)];
157  *p++ = basis_64[((string[i + 1] & 0xF) << 2)];
158  }
159  *p++ = '=';
160  }
161 
162  *p++ = '\0';
163  return (int)(p - encoded);
164 }
165 
166 /* convenience functions for j2 */
167 char *b64_encode(char *buf, int len) {
168  int elen;
169  char *out;
170 
171  if(len == 0)
172  len = strlen(buf);
173 
174  elen = apr_base64_encode_len(len);
175  out = (char *) malloc(sizeof(char) * (elen + 1));
176 
177  apr_base64_encode(out, buf, len);
178 
179  return out;
180 }
181 
182 char *b64_decode(char *buf) {
183  int elen;
184  char *out;
185 
186  elen = apr_base64_decode_len(buf, -1);
187  out = (char *) malloc(sizeof(char) * (elen + 1));
188 
189  apr_base64_decode(out, buf, -1);
190 
191  return out;
192 }
int apr_base64_encode_binary(char *encoded, const unsigned char *string, int len)
Definition: base64.c:133
char * b64_encode(char *buf, int len)
Definition: base64.c:167
int apr_base64_decode(char *bufplain, const char *bufcoded, int buflen)
Definition: base64.c:64
int apr_base64_decode_len(const char *bufcoded, int buflen)
Definition: base64.c:47
int apr_base64_encode(char *encoded, const char *string, int len)
Definition: base64.c:128
static const unsigned char pr2six[256]
Definition: base64.c:26
char * b64_decode(char *buf)
Definition: base64.c:182
int apr_base64_encode_len(int len)
Definition: base64.c:121
int apr_base64_decode_binary(unsigned char *bufplain, const char *bufcoded, int buflen)
Definition: base64.c:73
static const char basis_64[]
Definition: base64.c:118