]> git.ipfire.org Git - thirdparty/squid.git/blame - lib/uudecode.c
Docs: Copyright updates for 2018 (#114)
[thirdparty/squid.git] / lib / uudecode.c
CommitLineData
0545caaa 1/*
5b74111a 2 * Copyright (C) 1996-2018 The Squid Software Foundation and contributors
0545caaa
AJ
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
f7f3304a 9#include "squid.h"
25f98340 10#include "uudecode.h"
e81957b7 11
e81957b7 12/* aaaack but it's fast and const should make it shared text page. */
26ac0430 13const int pr2six[256] = {
b8d8561b 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, 62, 64, 64, 64, 63,
16 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,
17 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 64, 64, 64, 64, 64, 64, 26, 27,
18 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,
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, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
21 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
22 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
23 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
24 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64
e81957b7 25};
26
b8d8561b 27char *
0ee4272b 28uudecode(const char *bufcoded)
b8d8561b 29{
e81957b7 30 int nbytesdecoded;
0ee4272b 31 const unsigned char *bufin;
2a8a7464 32 char *bufplain;
33 unsigned char *bufout;
34 int nprbytes;
b8d8561b 35
e81957b7 36 /* Strip leading whitespace. */
b8d8561b 37
38 while (*bufcoded == ' ' || *bufcoded == '\t')
26ac0430 39 bufcoded++;
b8d8561b 40
e81957b7 41 /* Figure out how many characters are in the input buffer.
42 * Allocate this many from the per-transaction pool for the result.
43 */
0ee4272b 44 bufin = (const unsigned char *) bufcoded;
b8d8561b 45 while (pr2six[*(bufin++)] <= 63);
b001e822 46 nprbytes = (const char *) bufin - bufcoded - 1;
b8d8561b 47 nbytesdecoded = ((nprbytes + 3) / 4) * 3;
e81957b7 48
49 bufplain = xmalloc(nbytesdecoded + 1);
b8d8561b 50 bufout = (unsigned char *) bufplain;
0ee4272b 51 bufin = (const unsigned char *) bufcoded;
b8d8561b 52
e81957b7 53 while (nprbytes > 0) {
26ac0430
AJ
54 *(bufout++) =
55 (unsigned char) (pr2six[*bufin] << 2 | pr2six[bufin[1]] >> 4);
56 *(bufout++) =
57 (unsigned char) (pr2six[bufin[1]] << 4 | pr2six[bufin[2]] >> 2);
58 *(bufout++) =
59 (unsigned char) (pr2six[bufin[2]] << 6 | pr2six[bufin[3]]);
60 bufin += 4;
61 nprbytes -= 4;
e81957b7 62 }
b8d8561b 63
64 if (nprbytes & 03) {
26ac0430
AJ
65 if (pr2six[bufin[-2]] > 63)
66 nbytesdecoded -= 2;
67 else
68 nbytesdecoded -= 1;
e81957b7 69 }
70 bufplain[nbytesdecoded] = '\0';
71 return bufplain;
72}
f53969cc 73