]> git.ipfire.org Git - thirdparty/squid.git/blame - lib/uudecode.c
Boilerplate: update copyright blurbs on lib/snmplib
[thirdparty/squid.git] / lib / uudecode.c
CommitLineData
f7f3304a 1#include "squid.h"
25f98340 2#include "uudecode.h"
e81957b7 3
e81957b7 4/* aaaack but it's fast and const should make it shared text page. */
26ac0430 5const int pr2six[256] = {
b8d8561b 6 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
7 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63,
8 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64, 64, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
9 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 64, 64, 64, 64, 64, 64, 26, 27,
10 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,
11 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
12 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
13 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
14 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
15 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
16 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64
e81957b7 17};
18
b8d8561b 19char *
0ee4272b 20uudecode(const char *bufcoded)
b8d8561b 21{
e81957b7 22 int nbytesdecoded;
0ee4272b 23 const unsigned char *bufin;
2a8a7464 24 char *bufplain;
25 unsigned char *bufout;
26 int nprbytes;
b8d8561b 27
e81957b7 28 /* Strip leading whitespace. */
b8d8561b 29
30 while (*bufcoded == ' ' || *bufcoded == '\t')
26ac0430 31 bufcoded++;
b8d8561b 32
e81957b7 33 /* Figure out how many characters are in the input buffer.
34 * Allocate this many from the per-transaction pool for the result.
35 */
0ee4272b 36 bufin = (const unsigned char *) bufcoded;
b8d8561b 37 while (pr2six[*(bufin++)] <= 63);
b001e822 38 nprbytes = (const char *) bufin - bufcoded - 1;
b8d8561b 39 nbytesdecoded = ((nprbytes + 3) / 4) * 3;
e81957b7 40
41 bufplain = xmalloc(nbytesdecoded + 1);
b8d8561b 42 bufout = (unsigned char *) bufplain;
0ee4272b 43 bufin = (const unsigned char *) bufcoded;
b8d8561b 44
e81957b7 45 while (nprbytes > 0) {
26ac0430
AJ
46 *(bufout++) =
47 (unsigned char) (pr2six[*bufin] << 2 | pr2six[bufin[1]] >> 4);
48 *(bufout++) =
49 (unsigned char) (pr2six[bufin[1]] << 4 | pr2six[bufin[2]] >> 2);
50 *(bufout++) =
51 (unsigned char) (pr2six[bufin[2]] << 6 | pr2six[bufin[3]]);
52 bufin += 4;
53 nprbytes -= 4;
e81957b7 54 }
b8d8561b 55
56 if (nprbytes & 03) {
26ac0430
AJ
57 if (pr2six[bufin[-2]] > 63)
58 nbytesdecoded -= 2;
59 else
60 nbytesdecoded -= 1;
e81957b7 61 }
62 bufplain[nbytesdecoded] = '\0';
63 return bufplain;
64}