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