- reverting escaped commit
1 | /* |
2 | * The contents of this file are subject to the AOLserver Public License |
3 | * Version 1.1 (the "License"); you may not use this file except in |
4 | * compliance with the License. You may obtain a copy of the License at |
5 | * http://aolserver.com/. |
6 | * |
7 | * Software distributed under the License is distributed on an "AS IS" |
8 | * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See |
9 | * the License for the specific language governing rights and limitations |
10 | * under the License. |
11 | * |
12 | * The Original Code is AOLserver Code and related documentation |
13 | * distributed by AOL. |
14 | * |
15 | * The Initial Developer of the Original Code is America Online, |
16 | * Inc. Portions created by AOL are Copyright (C) 1999 America Online, |
17 | * Inc. All Rights Reserved. |
18 | * |
19 | * Alternatively, the contents of this file may be used under the terms |
20 | * of the GNU General Public License (the "GPL"), in which case the |
21 | * provisions of GPL are applicable instead of those above. If you wish |
22 | * to allow use of your version of this file only under the terms of the |
23 | * GPL and not to allow others to use your version of this file under the |
24 | * License, indicate your decision by deleting the provisions above and |
25 | * replace them with the notice and other provisions required by the GPL. |
26 | * If you do not delete the provisions above, a recipient may use your |
27 | * version of this file under either the License or the GPL. |
28 | */ |
29 | |
30 | /* |
31 | * uuencode.c -- |
32 | * |
33 | * Uuencoding and decoding routines which map 8-bit binary bytes |
34 | * into 6-bit ascii characters. |
35 | * |
36 | */ |
37 | |
38 | static const char *RCSID = "@(#) $Header: /cvsroot-fuse/aolserver/aolserver/nsd/uuencode.c,v 1.4 2011/07/05 18:37:47 gneumann Exp $, compiled: " __DATE__ " " __TIME__; |
39 | |
40 | #include "nsd.h" |
41 | |
42 | /* |
43 | * The following array specify the output ascii character for each |
44 | * of the 64 6-bit characters. |
45 | */ |
46 | |
47 | static char six2pr[64] = { |
48 | 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', |
49 | 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', |
50 | 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', |
51 | 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', |
52 | '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' |
53 | }; |
54 | |
55 | /* |
56 | * The following array maps all 256 8-bit ascii characters to |
57 | * either the corresponding 6-bit value or -1 for invalid character. |
58 | */ |
59 | |
60 | static int pr2six[256] = { |
61 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
62 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
63 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, |
64 | 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, |
65 | -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, |
66 | 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, |
67 | -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, |
68 | 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1, |
69 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
70 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
71 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
72 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
73 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
74 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
75 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
76 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 |
77 | }; |
78 | |
79 | #define ENC(c) (six2pr[(c)]) |
80 | #define DEC(c) ((unsigned char) pr2six[(int)(c)]) |
81 | |
82 | |
83 | /* |
84 | *---------------------------------------------------------------------- |
85 | * |
86 | * <a href="/cvs/aolserver/aolserver/nsd/uuencode.c#A_Ns_HtuuEncode">Ns_HtuuEncode</a> -- |
87 | * |
88 | * Encode a string. |
89 | * |
90 | * Results: |
91 | * Number of bytes placed in output. |
92 | * |
93 | * Side effects: |
94 | * Encoded characters are placed in output which must be |
95 | * large enough for the result, i.e., (1 + (len * 4) / 3) |
96 | * bytes. |
97 | * |
98 | *---------------------------------------------------------------------- |
99 | */ |
100 | |
101 | int |
102 | <a href="/cvs/aolserver/aolserver/nsd/uuencode.c#A_Ns_HtuuEncode">Ns_HtuuEncode</a>(unsigned char *input, unsigned int len, char *output) |
103 | { |
104 | register unsigned char *p, *q; |
105 | register int n; |
106 | |
107 | /* |
108 | * Convert every three input bytes into four output |
109 | * characters. |
110 | */ |
111 | |
112 | p = input; |
113 | q = output; |
114 | for (n = len / 3; n > 0; --n) { |
115 | *q++ = ENC(p[0] >> 2); |
116 | *q++ = ENC(((p[0] << 4) & 060) | ((p[1] >> 4) & 017)); |
117 | *q++ = ENC(((p[1] << 2) & 074) | ((p[2] >> 6) & 03)); |
118 | *q++ = ENC(p[2] & 077); |
119 | p += 3; |
120 | } |
121 | |
122 | /* |
123 | * Convert and pad any remaining bytes. |
124 | */ |
125 | |
126 | n = len % 3; |
127 | if (n > 0) { |
128 | *q++ = ENC(p[0] >> 2); |
129 | if (n == 1) { |
130 | *q++ = ENC((p[0] << 4) & 060); |
131 | *q++ = '='; |
132 | } else { |
133 | *q++ = ENC(((p[0] << 4) & 060) | ((p[1] >> 4) & 017)); |
134 | *q++ = ENC((p[1] << 2) & 074); |
135 | } |
136 | *q++ = '='; |
137 | } |
138 | *q = '\0'; |
139 | return (q - (unsigned char *) output); |
140 | } |
141 | |
142 | |
143 | /* |
144 | *---------------------------------------------------------------------- |
145 | * |
146 | * <a href="/cvs/aolserver/aolserver/nsd/uuencode.c#A_Ns_HtuuDecode">Ns_HtuuDecode</a> -- |
147 | * |
148 | * Decode a string. |
149 | * |
150 | * Results: |
151 | * Number of binary bytes decoded. |
152 | * |
153 | * Side effects: |
154 | * Decoded characters are placed in output which must be |
155 | * large enough for the result, i.e., (3 + (len * 3) / 4) |
156 | * bytes. |
157 | * |
158 | *---------------------------------------------------------------------- |
159 | */ |
160 | |
161 | int |
162 | <a href="/cvs/aolserver/aolserver/nsd/uuencode.c#A_Ns_HtuuDecode">Ns_HtuuDecode</a>(char *input, unsigned char *output, int outputlen) |
163 | { |
164 | register unsigned char *p, *q; |
165 | register int len, n; |
166 | |
167 | |
168 | /* |
169 | * Skip leading space, if any. |
170 | */ |
171 | |
172 | while (*input == ' ' || *input == '\t') { |
173 | ++input; |
174 | } |
175 | |
176 | /* |
177 | * Determine the maximum length of output bytes. |
178 | */ |
179 | |
180 | p = input; |
181 | while (pr2six[(int)(*p)] >= 0) { |
182 | ++p; |
183 | } |
184 | len = p - (unsigned char *) input; |
185 | |
186 | /* |
187 | * Decode every four input bytes. |
188 | */ |
189 | |
190 | p = input; |
191 | q = output; |
192 | for (n = len / 4; n > 0; --n) { |
193 | *q++ = DEC(p[0]) << 2 | DEC(p[1]) >> 4; |
194 | *q++ = DEC(p[1]) << 4 | DEC(p[2]) >> 2; |
195 | *q++ = DEC(p[2]) << 6 | DEC(p[3]); |
196 | p += 4; |
197 | } |
198 | |
199 | /* |
200 | * Decode remaining 2 or 3 bytes. |
201 | */ |
202 | |
203 | n = len % 4; |
204 | if (n > 1) { |
205 | *q++ = DEC(p[0]) << 2 | DEC(p[1]) >> 4; |
206 | } |
207 | if (n > 2) { |
208 | *q++ = DEC(p[1]) << 4 | DEC(p[2]) >> 2; |
209 | } |
210 | if ((q - output) < outputlen) { |
211 | *q = '\0'; |
212 | } |
213 | return (q - output); |
214 | } |
Copyright © 2010 Geeknet, Inc. All rights reserved. Terms of Use