1 |
/* |
2 |
* This file is part of uudeview, the simple and friendly multi-part multi- |
3 |
* file uudecoder program (c) 1994-2001 by Frank Pilhofer. The author may |
4 |
* be contacted at fp@fpx.de |
5 |
* |
6 |
* This program is free software; you can redistribute it and/or modify |
7 |
* it under the terms of the GNU General Public License as published by |
8 |
* the Free Software Foundation; either version 2 of the License, or |
9 |
* (at your option) any later version. |
10 |
* |
11 |
* This program is distributed in the hope that it will be useful, |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
* GNU General Public License for more details. |
15 |
*/ |
16 |
|
17 |
/* |
18 |
* These are the functions that are responsible for decoding. The |
19 |
* original idea is from a freeware utility called "uunconc", and |
20 |
* few lines of this code may still bear a remote resemblance to |
21 |
* its code. If you are the author or know him, contact me. |
22 |
* This program could only decode one multi-part, uuencoded file |
23 |
* where the parts were in order. Base64, XX and BinHex decoding, |
24 |
* support for multi-files and part-ordering covered by myself. |
25 |
**/ |
26 |
|
27 |
#ifdef HAVE_CONFIG_H |
28 |
#include "config.h" |
29 |
#endif |
30 |
|
31 |
#ifdef SYSTEM_WINDLL |
32 |
#include <windows.h> |
33 |
#endif |
34 |
#ifdef SYSTEM_OS2 |
35 |
#include <os2.h> |
36 |
#endif |
37 |
|
38 |
#include <stdio.h> |
39 |
#include <ctype.h> |
40 |
|
41 |
#ifdef STDC_HEADERS |
42 |
#include <stdlib.h> |
43 |
#include <string.h> |
44 |
#endif |
45 |
#ifdef HAVE_UNISTD_H |
46 |
#include <unistd.h> |
47 |
#endif |
48 |
#ifdef HAVE_ERRNO_H |
49 |
#include <errno.h> |
50 |
#endif |
51 |
|
52 |
#include <crc32.h> |
53 |
#include <uudeview.h> |
54 |
#include <uuint.h> |
55 |
#include <fptools.h> |
56 |
#include <uustring.h> |
57 |
|
58 |
char * uunconc_id = "$Id$"; |
59 |
|
60 |
/* for braindead systems */ |
61 |
#ifndef SEEK_SET |
62 |
#ifdef L_BEGIN |
63 |
#define SEEK_SET L_BEGIN |
64 |
#else |
65 |
#define SEEK_SET 0 |
66 |
#endif |
67 |
#endif |
68 |
|
69 |
/* |
70 |
* decoder states |
71 |
*/ |
72 |
|
73 |
#define BEGIN (1) |
74 |
#define DATA (2) |
75 |
#define END (3) |
76 |
#define DONE (4) |
77 |
|
78 |
/* |
79 |
* mallocable areas |
80 |
*/ |
81 |
|
82 |
char *uunconc_UUxlat; |
83 |
char *uunconc_UUxlen; |
84 |
char *uunconc_B64xlat; |
85 |
char *uunconc_XXxlat; |
86 |
char *uunconc_BHxlat; |
87 |
char *uunconc_save; |
88 |
|
89 |
/* |
90 |
* decoding translation tables and line length table |
91 |
*/ |
92 |
|
93 |
static int * UUxlen; /* initialized in UUInitConc() */ |
94 |
static int * UUxlat; /* from the malloc'ed areas above */ |
95 |
static int * B64xlat; |
96 |
static int * XXxlat; |
97 |
static int * BHxlat; |
98 |
|
99 |
/* |
100 |
* buffer for decoding |
101 |
*/ |
102 |
|
103 |
static char *save[3]; |
104 |
|
105 |
/* |
106 |
* mallocable areas |
107 |
*/ |
108 |
|
109 |
char *uuncdl_fulline; |
110 |
char *uuncdp_oline; |
111 |
|
112 |
/* |
113 |
* Return information for QuickDecode |
114 |
*/ |
115 |
|
116 |
static int uulboundary; |
117 |
|
118 |
/* |
119 |
* To prevent warnings when using a char as index into an array |
120 |
*/ |
121 |
|
122 |
#define ACAST(s) ((int)(uchar)(s)) |
123 |
|
124 |
/* |
125 |
* Initialize decoding tables |
126 |
*/ |
127 |
|
128 |
void |
129 |
UUInitConc (void) |
130 |
{ |
131 |
int i, j; |
132 |
|
133 |
/* |
134 |
* Update pointers |
135 |
*/ |
136 |
UUxlen = (int *) uunconc_UUxlen; |
137 |
UUxlat = (int *) uunconc_UUxlat; |
138 |
B64xlat = (int *) uunconc_B64xlat; |
139 |
XXxlat = (int *) uunconc_XXxlat; |
140 |
BHxlat = (int *) uunconc_BHxlat; |
141 |
|
142 |
save[0] = uunconc_save; |
143 |
save[1] = uunconc_save + 1200; |
144 |
save[2] = uunconc_save + 2400; |
145 |
|
146 |
/* prepare decoding translation table */ |
147 |
for(i = 0; i < 256; i++) |
148 |
UUxlat[i] = B64xlat[i] = XXxlat[i] = BHxlat[i] = -1; |
149 |
|
150 |
/* |
151 |
* At some time I received a file which used lowercase characters for |
152 |
* uuencoding. This shouldn't be, but let's accept it. Must take special |
153 |
* care that this doesn't break xxdecoding. This is giving me quite a |
154 |
* headache. If this one file hadn't been a Pocahontas picture, I might |
155 |
* have ignored it for good. |
156 |
*/ |
157 |
|
158 |
for (i = ' ', j = 0; i < ' ' + 64; i++, j++) |
159 |
UUxlat[i] /* = UUxlat[i+64] */ = j; |
160 |
for (i = '`', j = 0; i < '`' + 32; i++, j++) |
161 |
UUxlat[i] = j; |
162 |
|
163 |
/* add special cases */ |
164 |
UUxlat['`'] = UUxlat[' ']; |
165 |
UUxlat['~'] = UUxlat['^']; |
166 |
|
167 |
/* prepare line length table */ |
168 |
UUxlen[0] = 1; |
169 |
for(i = 1, j = 5; i <= 61; i += 3, j += 4) |
170 |
UUxlen[i] = UUxlen[i+1] = UUxlen[i+2] = j; |
171 |
|
172 |
/* prepare other tables */ |
173 |
for (i=0; i<64; i++) { |
174 |
B64xlat[ACAST(B64EncodeTable[i])] = i; |
175 |
XXxlat [ACAST(XXEncodeTable [i])] = i; |
176 |
BHxlat [ACAST(BHEncodeTable [i])] = i; |
177 |
} |
178 |
} |
179 |
|
180 |
/* |
181 |
* Workaround for Netscape |
182 |
*/ |
183 |
|
184 |
/* |
185 |
* Determines whether Netscape may have broken up a data line (by |
186 |
* inserting a newline). This only seems to happen after <a in a |
187 |
* href statement |
188 |
*/ |
189 |
|
190 |
int |
191 |
UUBrokenByNetscape (char *string) |
192 |
{ |
193 |
char *ptr; |
194 |
int len; |
195 |
|
196 |
if (string==NULL || (len=strlen(string))<3) |
197 |
return 0; |
198 |
|
199 |
if ((ptr = _FP_stristr (string, "<a href=")) != NULL) { |
200 |
if (_FP_stristr (string, "</a>") > ptr) |
201 |
return 2; |
202 |
} |
203 |
|
204 |
ptr = string + len; |
205 |
|
206 |
if (len<3) return 0; |
207 |
if (*--ptr == ' ') ptr--; |
208 |
ptr--; |
209 |
|
210 |
if (_FP_strnicmp (ptr, "<a", 2) == 0) |
211 |
return 1; |
212 |
|
213 |
return 0; |
214 |
} |
215 |
|
216 |
/* |
217 |
* Try to repair a Netscape-corrupted line of data. |
218 |
* This must only be called on corrupted lines, since non-Netscape |
219 |
* data may even _get_ corrupted by this procedure. |
220 |
* |
221 |
* Some checks are included multiply to speed up the procedure. For |
222 |
* example: (*p1!='<' || strnicmp(p1,"</a>",4)). If the first expression |
223 |
* becomes true, the costly function isn't called :-) |
224 |
* |
225 |
* Since '<', '>', '&' might even be replaced by their html equivalents |
226 |
* in href strings, I'm now using two passes, the first one for & + co, |
227 |
* the second one for hrefs. |
228 |
*/ |
229 |
|
230 |
int |
231 |
UUNetscapeCollapse (char *string) |
232 |
{ |
233 |
char *p1=string, *p2=string; |
234 |
int res = 0; |
235 |
|
236 |
if (string==NULL) |
237 |
return 0; |
238 |
|
239 |
/* |
240 |
* First pass |
241 |
*/ |
242 |
while (*p1) { |
243 |
if (*p1 == '&') { |
244 |
if (_FP_strnicmp (p1, "&", 5) == 0) { p1+=5; *p2++='&'; res=1; } |
245 |
else if (_FP_strnicmp (p1, "<", 4) == 0) { p1+=4; *p2++='<'; res=1; } |
246 |
else if (_FP_strnicmp (p1, ">", 4) == 0) { p1+=4; *p2++='>'; res=1; } |
247 |
else *p2++ = *p1++; |
248 |
res = 1; |
249 |
} |
250 |
else *p2++ = *p1++; |
251 |
} |
252 |
*p2 = '\0'; |
253 |
/* |
254 |
* Second pass |
255 |
*/ |
256 |
p1 = p2 = string; |
257 |
|
258 |
while (*p1) { |
259 |
if (*p1 == '<') { |
260 |
if ((_FP_strnicmp (p1, "<ahref=", 7) == 0 || |
261 |
_FP_strnicmp (p1, "<a href=",8) == 0) && |
262 |
(_FP_strstr (p1, "</a>") != 0 || _FP_strstr (p1, "</A>") != 0)) { |
263 |
while (*p1 && *p1!='>') p1++; |
264 |
if (*p1=='\0' || *(p1+1)!='<') return 0; |
265 |
p1++; |
266 |
while (*p1 && (*p1!='<' || _FP_strnicmp(p1,"</a>",4)!=0)) { |
267 |
*p2++ = *p1++; |
268 |
} |
269 |
if (_FP_strnicmp(p1,"</a>",4) != 0) |
270 |
return 0; |
271 |
p1+=4; |
272 |
res=1; |
273 |
} |
274 |
else |
275 |
*p2++ = *p1++; |
276 |
} |
277 |
else |
278 |
*p2++ = *p1++; |
279 |
} |
280 |
*p2 = '\0'; |
281 |
|
282 |
return res; |
283 |
} |
284 |
|
285 |
/* |
286 |
* The second parameter is 0 if we are still searching for encoded data, |
287 |
* otherwise it indicates the encoding we're using right now. If we're |
288 |
* still in the searching stage, we must be a little more strict in |
289 |
* deciding for or against encoding; there's too much plain text looking |
290 |
* like encoded data :-( |
291 |
*/ |
292 |
|
293 |
int |
294 |
UUValidData (char *ptr, int encoding, int *bhflag) |
295 |
{ |
296 |
int i=0, j, len=0, suspicious=0, flag=0; |
297 |
signed char *s = ptr; |
298 |
|
299 |
if ((s == NULL) || (*s == '\0')) { |
300 |
return 0; /* bad string */ |
301 |
} |
302 |
|
303 |
while (*s) { |
304 |
s++; |
305 |
len++; |
306 |
i++; |
307 |
} |
308 |
|
309 |
if (i == 0) |
310 |
return 0; |
311 |
|
312 |
switch (encoding) { |
313 |
case UU_ENCODED: |
314 |
goto _t_UU; |
315 |
case XX_ENCODED: |
316 |
goto _t_XX; |
317 |
case B64ENCODED: |
318 |
goto _t_B64; |
319 |
case BH_ENCODED: |
320 |
goto _t_Binhex; |
321 |
case YENC_ENCODED: |
322 |
return YENC_ENCODED; |
323 |
} |
324 |
|
325 |
_t_Binhex: /* Binhex Test */ |
326 |
len = i; s = ptr; |
327 |
|
328 |
/* |
329 |
* bhflag notes the state we're in. Within the data, it's 1. If we're |
330 |
* still looking for the initial :, it's 0 |
331 |
*/ |
332 |
if (*bhflag == 0 && *s != ':') { |
333 |
if (encoding==BH_ENCODED) return 0; |
334 |
goto _t_B64; |
335 |
} |
336 |
else if (*bhflag == 0 /* *s == ':' */) { |
337 |
s++; len--; |
338 |
} |
339 |
|
340 |
while (len && BHxlat[ACAST(*s)] != -1) { |
341 |
len--; s++; |
342 |
} |
343 |
|
344 |
/* allow space characters at the end of the line if we are sure */ |
345 |
/* that this is Binhex encoded data or the line was long enough */ |
346 |
|
347 |
flag = (*s == ':') ? 0 : 1; |
348 |
|
349 |
if (*s == ':' && len>0) { |
350 |
s++; len--; |
351 |
} |
352 |
if (((i>=60 && len<=10) || encoding) && *s==' ') { |
353 |
while (len && *s==' ') { |
354 |
s++; len--; |
355 |
} |
356 |
} |
357 |
|
358 |
/* |
359 |
* BinHex data shall have exactly 64 characters (except the last |
360 |
* line). We ignore everything with less than 40 characters to |
361 |
* be flexible |
362 |
*/ |
363 |
|
364 |
if (len != 0 || (flag && i < 40)) { |
365 |
if (encoding==BH_ENCODED) return 0; |
366 |
goto _t_B64; |
367 |
} |
368 |
|
369 |
*bhflag = flag; |
370 |
|
371 |
return BH_ENCODED; |
372 |
|
373 |
_t_B64: /* Base64 Test */ |
374 |
len = i; s = ptr; |
375 |
|
376 |
/* |
377 |
* Face it: there _are_ Base64 lines that are not a multiple of four |
378 |
* in length :-( |
379 |
* |
380 |
* if (len%4) |
381 |
* goto _t_UU; |
382 |
*/ |
383 |
|
384 |
while (len--) { |
385 |
if (*s < 0 || (B64xlat[ACAST(*s)] == -1 && *s != '=')) { |
386 |
/* allow space characters at the end of the line if we are sure */ |
387 |
/* that this is Base64 encoded data or the line was long enough */ |
388 |
if (((i>=60 && len<=10) || encoding) && *s++==' ') { |
389 |
while (*s==' ' && len) s++; |
390 |
if (len==0) return B64ENCODED; |
391 |
} |
392 |
if (encoding==B64ENCODED) return 0; |
393 |
goto _t_UU; |
394 |
} |
395 |
else if (*s == '=') { /* special case at end */ |
396 |
/* if we know this is B64encoded, allow spaces at end of line */ |
397 |
s++; |
398 |
if (*s=='=' && len>=1) { |
399 |
len--; s++; |
400 |
} |
401 |
if (encoding && len && *s==' ') { |
402 |
while (len && *s==' ') { |
403 |
s++; len--; |
404 |
} |
405 |
} |
406 |
if (len != 0) { |
407 |
if (encoding==B64ENCODED) return 0; |
408 |
goto _t_UU; |
409 |
} |
410 |
return B64ENCODED; |
411 |
} |
412 |
s++; |
413 |
} |
414 |
return B64ENCODED; |
415 |
|
416 |
_t_UU: |
417 |
len = i; s = ptr; |
418 |
|
419 |
if (UUxlat[ACAST(*s)] == -1) { /* uutest */ |
420 |
if (encoding==UU_ENCODED) return 0; |
421 |
goto _t_XX; |
422 |
} |
423 |
|
424 |
j = UUxlen[UUxlat[ACAST(*s)]]; |
425 |
|
426 |
if (len-1 == j) /* remove trailing character */ |
427 |
len--; |
428 |
if (len != j) { |
429 |
switch (UUxlat[ACAST(*s)]%3) { |
430 |
case 1: |
431 |
if (j-2 == len) j-=2; |
432 |
break; |
433 |
case 2: |
434 |
if (j-1 == len) j-=1; |
435 |
break; |
436 |
} |
437 |
} |
438 |
|
439 |
/* |
440 |
* some encoders are broken with respect to encoding the last line of |
441 |
* a file and produce extraoneous characters beyond the expected EOL |
442 |
* So were not too picky here about the last line, as long as it's longer |
443 |
* than necessary and shorter than the maximum |
444 |
* this tolerance broke the xxdecoding, because xxencoded data was |
445 |
* detected as being uuencoded :( so don't accept 'h' as first character |
446 |
* also, if the first character is lowercase, don't accept the line to |
447 |
* have space characters. the only encoder I've heard of which uses |
448 |
* lowercase characters at least accepts the special case of encoding |
449 |
* 0 as `. The strchr() shouldn't be too expensive here as it's only |
450 |
* evaluated if the first character is lowercase, which really shouldn't |
451 |
* be in uuencoded text. |
452 |
*/ |
453 |
if (len != j && |
454 |
((ptr[0] == '-' && ptr[1] == '-' && strstr(ptr,"part")!=NULL) || |
455 |
!(*ptr != 'M' && *ptr != 'h' && |
456 |
len > j && len <= UUxlen[UUxlat['M']]))) { |
457 |
if (encoding==UU_ENCODED) return 0; |
458 |
goto _t_XX; /* bad length */ |
459 |
} |
460 |
|
461 |
if (len != j || islower (*ptr)) { |
462 |
/* |
463 |
* if we are not in a 'uuencoded' state, don't allow the line to have |
464 |
* space characters at all. if we know we _are_ decoding uuencoded |
465 |
* data, the rest of the line, beyond the length of encoded data, may |
466 |
* have spaces. |
467 |
*/ |
468 |
if (encoding != UU_ENCODED) |
469 |
if (strchr (ptr, ' ') != NULL) |
470 |
goto _t_XX; |
471 |
|
472 |
/* suspicious = 1; we're careful here REMOVED 0.4.15 __FP__ */ |
473 |
len = j; |
474 |
} |
475 |
|
476 |
while (len--) { |
477 |
if (*s < 0 || UUxlat[ACAST(*s++)] < 0) { |
478 |
if (encoding==UU_ENCODED) return 0; |
479 |
goto _t_XX; /* bad code character */ |
480 |
} |
481 |
if (*s == ' ' && suspicious) { |
482 |
if (encoding==UU_ENCODED) return 0; |
483 |
goto _t_XX; /* this line looks _too_ suspicious */ |
484 |
} |
485 |
} |
486 |
return UU_ENCODED; /* data is valid */ |
487 |
|
488 |
_t_XX: /* XX Test */ |
489 |
len = i; s = ptr; |
490 |
|
491 |
if (XXxlat[ACAST(*s)] == -1) |
492 |
return 0; |
493 |
|
494 |
j = UUxlen[XXxlat[ACAST(*s)]]; /* Same line length table as UUencoding */ |
495 |
|
496 |
if (len-1 == j) /* remove trailing character */ |
497 |
len--; |
498 |
if (len != j) |
499 |
switch (UUxlat[ACAST(*s)]%3) { |
500 |
case 1: |
501 |
if (j-2 == len) j-=2; |
502 |
break; |
503 |
case 2: |
504 |
if (j-1 == len) j-=1; |
505 |
break; |
506 |
} |
507 |
/* |
508 |
* some encoders are broken with respect to encoding the last line of |
509 |
* a file and produce extraoneous characters beyond the expected EOL |
510 |
* So were not too picky here about the last line, as long as it's longer |
511 |
* than necessary and shorter than the maximum |
512 |
*/ |
513 |
if (len != j && !(*ptr != 'h' && len > j && len <= UUxlen[UUxlat['h']])) |
514 |
return 0; /* bad length */ |
515 |
|
516 |
while(len--) { |
517 |
if(*s < 0 || XXxlat[ACAST(*s++)] < 0) { |
518 |
return 0; /* bad code character */ |
519 |
} |
520 |
} |
521 |
return XX_ENCODED; /* data is valid */ |
522 |
} |
523 |
|
524 |
/* |
525 |
* This function may be called upon a line that does not look like |
526 |
* valid encoding on first sight, but might be erroneously encoded |
527 |
* data from Netscape, Lynx or MS Exchange. We might need to read |
528 |
* a new line from the stream, which is why we need the FILE. |
529 |
* Returns the type of encoded data if successful or 0 otherwise. |
530 |
*/ |
531 |
|
532 |
int |
533 |
UURepairData (FILE *datei, char *line, int encoding, int *bhflag) |
534 |
{ |
535 |
int nflag, vflag=0, safety=42; |
536 |
char *ptr; |
537 |
|
538 |
nflag = UUBrokenByNetscape (line); |
539 |
|
540 |
while (vflag == 0 && nflag && safety--) { |
541 |
if (nflag == 1) { /* need next line to repair */ |
542 |
if (strlen (line) > 250) |
543 |
break; |
544 |
ptr = line + strlen (line); |
545 |
if (_FP_fgets (ptr, 299-(ptr-line), datei) == NULL) |
546 |
break; |
547 |
} |
548 |
else { /* don't need next line to repair */ |
549 |
} |
550 |
if (UUNetscapeCollapse (line)) { |
551 |
if ((vflag = UUValidData (line, encoding, bhflag)) == 0) |
552 |
nflag = UUBrokenByNetscape (line); |
553 |
} |
554 |
else |
555 |
nflag = 0; |
556 |
} |
557 |
/* |
558 |
* Sometimes, a line is garbled even without it being split into |
559 |
* the next line. Then we try this in our despair |
560 |
*/ |
561 |
if (vflag == 0) { |
562 |
if (UUNetscapeCollapse (line)) |
563 |
vflag = UUValidData (line, encoding, bhflag); |
564 |
} |
565 |
|
566 |
/* |
567 |
* If this line looks uuencoded, but the line is one character short |
568 |
* of a valid line, it was probably broken by MS Exchange. According |
569 |
* to my test cases, there is at most one space character missing; |
570 |
* there are never two spaces together. |
571 |
* If adding a space character helps making this line uuencoded, do |
572 |
* it! |
573 |
*/ |
574 |
|
575 |
if (vflag == 0) { |
576 |
ptr = line + strlen(line); |
577 |
*ptr++ = ' '; |
578 |
*ptr-- = '\0'; |
579 |
if ((vflag = UUValidData (line, encoding, bhflag)) != UU_ENCODED) { |
580 |
*ptr = '\0'; |
581 |
vflag = 0; |
582 |
} |
583 |
} |
584 |
return vflag; |
585 |
} |
586 |
|
587 |
/* |
588 |
* Decode a single encoded line using method |
589 |
*/ |
590 |
|
591 |
size_t |
592 |
UUDecodeLine (char *s, char *d, int method) |
593 |
{ |
594 |
int i, j, c, cc, count=0, z1, z2, z3, z4; |
595 |
static int leftover=0; |
596 |
int *table; |
597 |
|
598 |
/* |
599 |
* for re-initialization |
600 |
*/ |
601 |
|
602 |
if (s == NULL || d == NULL) { |
603 |
leftover = 0; |
604 |
return 0; |
605 |
} |
606 |
|
607 |
/* |
608 |
* To shut up gcc -Wall |
609 |
*/ |
610 |
z1 = z2 = z3 = z4 = 0; |
611 |
|
612 |
if (method == UU_ENCODED || method == XX_ENCODED) { |
613 |
if (method == UU_ENCODED) |
614 |
table = UUxlat; |
615 |
else |
616 |
table = XXxlat; |
617 |
|
618 |
i = table [ACAST(*s++)]; |
619 |
j = UUxlen[i] - 1; |
620 |
|
621 |
while(j > 0) { |
622 |
c = table[ACAST(*s++)] << 2; |
623 |
cc = table[ACAST(*s++)]; |
624 |
c |= (cc >> 4); |
625 |
|
626 |
if(i-- > 0) |
627 |
d[count++] = c; |
628 |
|
629 |
cc <<= 4; |
630 |
c = table[ACAST(*s++)]; |
631 |
cc |= (c >> 2); |
632 |
|
633 |
if(i-- > 0) |
634 |
d[count++] = cc; |
635 |
|
636 |
c <<= 6; |
637 |
c |= table[ACAST(*s++)]; |
638 |
|
639 |
if(i-- > 0) |
640 |
d[count++] = c; |
641 |
|
642 |
j -= 4; |
643 |
} |
644 |
} |
645 |
else if (method == B64ENCODED) { |
646 |
if (leftover) { |
647 |
strcpy (uuncdl_fulline + leftover, s); |
648 |
|
649 |
leftover = 0; |
650 |
s = uuncdl_fulline; |
651 |
} |
652 |
|
653 |
while ((z1 = B64xlat[ACAST(*s)]) != -1) { |
654 |
if ((z2 = B64xlat[ACAST(*(s+1))]) == -1) break; |
655 |
if ((z3 = B64xlat[ACAST(*(s+2))]) == -1) break; |
656 |
if ((z4 = B64xlat[ACAST(*(s+3))]) == -1) break; |
657 |
|
658 |
d[count++] = (z1 << 2) | (z2 >> 4); |
659 |
d[count++] = (z2 << 4) | (z3 >> 2); |
660 |
d[count++] = (z3 << 6) | (z4); |
661 |
|
662 |
s += 4; |
663 |
} |
664 |
if (z1 != -1 && z2 != -1 && *(s+2) == '=') { |
665 |
d[count++] = (z1 << 2) | (z2 >> 4); |
666 |
s+=2; |
667 |
} |
668 |
else if (z1 != -1 && z2 != -1 && z3 != -1 && *(s+3) == '=') { |
669 |
d[count++] = (z1 << 2) | (z2 >> 4); |
670 |
d[count++] = (z2 << 4) | (z3 >> 2); |
671 |
s+=3; |
672 |
} |
673 |
while (B64xlat[ACAST(*s)] != -1) |
674 |
uuncdl_fulline[leftover++] = *s++; |
675 |
} |
676 |
else if (method == BH_ENCODED) { |
677 |
if (leftover) { |
678 |
strcpy (uuncdl_fulline + leftover, s); |
679 |
|
680 |
leftover = 0; |
681 |
s = uuncdl_fulline; |
682 |
} |
683 |
else if (*s == ':') |
684 |
s++; |
685 |
|
686 |
while ((z1 = BHxlat[ACAST(*s)]) != -1) { |
687 |
if ((z2 = BHxlat[ACAST(*(s+1))]) == -1) break; |
688 |
if ((z3 = BHxlat[ACAST(*(s+2))]) == -1) break; |
689 |
if ((z4 = BHxlat[ACAST(*(s+3))]) == -1) break; |
690 |
|
691 |
d[count++] = (z1 << 2) | (z2 >> 4); |
692 |
d[count++] = (z2 << 4) | (z3 >> 2); |
693 |
d[count++] = (z3 << 6) | (z4); |
694 |
|
695 |
s += 4; |
696 |
} |
697 |
if (z1 != -1 && z2 != -1 && *(s+2) == ':') { |
698 |
d[count++] = (z1 << 2) | (z2 >> 4); |
699 |
s+=2; |
700 |
} |
701 |
else if (z1 != -1 && z2 != -1 && z3 != -1 && *(s+3) == ':') { |
702 |
d[count++] = (z1 << 2) | (z2 >> 4); |
703 |
d[count++] = (z2 << 4) | (z3 >> 2); |
704 |
s+=3; |
705 |
} |
706 |
while (BHxlat[ACAST(*s)] != -1) |
707 |
uuncdl_fulline[leftover++] = *s++; |
708 |
} |
709 |
else if (method == YENC_ENCODED) { |
710 |
while (*s) { |
711 |
if (*s == '=') { |
712 |
if (*++s != '\0') { |
713 |
d[count++] = (char) ((int) *s - 64 - 42); |
714 |
s++; |
715 |
} |
716 |
} |
717 |
else if (*s == '\n' || *s == '\r') { |
718 |
s++; /* ignore */ |
719 |
} |
720 |
else { |
721 |
d[count++] = (char) ((int) *s++ - 42); |
722 |
} |
723 |
} |
724 |
} |
725 |
|
726 |
return count; |
727 |
} |
728 |
|
729 |
/* |
730 |
* ``Decode'' Quoted-Printable text |
731 |
*/ |
732 |
|
733 |
int |
734 |
UUDecodeQP (FILE *datain, FILE *dataout, int *state, |
735 |
long maxpos, int method, int flags, |
736 |
char *boundary) |
737 |
{ |
738 |
char *line=uugen_inbuffer, *p1, *p2; |
739 |
int val; |
740 |
|
741 |
uulboundary = -1; |
742 |
|
743 |
while (!feof (datain) && |
744 |
(ftell(datain)<maxpos || flags&FL_TOEND || |
745 |
(!(flags&FL_PROPER) && uu_fast_scanning))) { |
746 |
if (_FP_fgets (line, 1023, datain) == NULL) |
747 |
break; |
748 |
if (ferror (datain)) { |
749 |
UUMessage (uunconc_id, __LINE__, UUMSG_ERROR, |
750 |
uustring (S_SOURCE_READ_ERR), |
751 |
strerror (uu_errno = errno)); |
752 |
return UURET_IOERR; |
753 |
} |
754 |
line[255] = '\0'; |
755 |
|
756 |
if (boundary && line[0]=='-' && line[1]=='-' && |
757 |
strncmp (line+2, boundary, strlen (boundary)) == 0) { |
758 |
if (line[strlen(boundary)+2]=='-') |
759 |
uulboundary = 1; |
760 |
else |
761 |
uulboundary = 0; |
762 |
return UURET_OK; |
763 |
} |
764 |
|
765 |
if (UUBUSYPOLL(ftell(datain)-progress.foffset,progress.fsize)) { |
766 |
UUMessage (uunconc_id, __LINE__, UUMSG_NOTE, |
767 |
uustring (S_DECODE_CANCEL)); |
768 |
return UURET_CANCEL; |
769 |
} |
770 |
|
771 |
p1 = p2 = line; |
772 |
|
773 |
while (*p2) { |
774 |
while (*p2 && *p2 != '=') |
775 |
p2++; |
776 |
if (*p2 == '\0') |
777 |
break; |
778 |
*p2 = '\0'; |
779 |
fprintf (dataout, "%s", p1); |
780 |
p1 = ++p2; |
781 |
|
782 |
if (isxdigit (*p2) && isxdigit (*(p2+1))) { |
783 |
val = ((isdigit(*p2)) ? (*p2-'0') : (tolower(*p2)-'a'+10)) << 4; |
784 |
val |= ((isdigit(*(p2+1)))?(*(p2+1)-'0') : (tolower(*(p2+1))-'a'+10)); |
785 |
|
786 |
fputc (val, dataout); |
787 |
p2 += 2; |
788 |
p1 = p2; |
789 |
} |
790 |
else if (!*p2) { |
791 |
/* soft line break */ |
792 |
*p2 = '\0'; |
793 |
break; |
794 |
} |
795 |
else { |
796 |
/* huh? */ |
797 |
fputc ('=', dataout); |
798 |
} |
799 |
} |
800 |
|
801 |
/* |
802 |
* If the part ends directly after this line, the data does not end |
803 |
* with a linebreak. Or, as the docs put it, "the CRLF preceding the |
804 |
* encapsulation line is conceptually attached to the boundary. |
805 |
* So if the part ends here, don't print a line break" |
806 |
*/ |
807 |
/* something is broken here now, but it was broken before */ |
808 |
if (!feof (datain) && |
809 |
(ftell(datain)<maxpos || flags&FL_TOEND || |
810 |
(!(flags&FL_PROPER) && uu_fast_scanning))) |
811 |
fprintf (dataout, "%s\n", p1); |
812 |
else |
813 |
fprintf (dataout, "%s", p1); |
814 |
} |
815 |
return UURET_OK; |
816 |
} |
817 |
|
818 |
/* |
819 |
* ``Decode'' plain text. Our job is to properly handle the EOL sequence |
820 |
*/ |
821 |
|
822 |
int |
823 |
UUDecodePT (FILE *datain, FILE *dataout, int *state, |
824 |
long maxpos, int method, int flags, |
825 |
char *boundary) |
826 |
{ |
827 |
char *line=uugen_inbuffer, *ptr; |
828 |
|
829 |
uulboundary = -1; |
830 |
|
831 |
while (!feof (datain) && |
832 |
(ftell(datain)<maxpos || flags&FL_TOEND || |
833 |
(!(flags&FL_PROPER) && uu_fast_scanning))) { |
834 |
if (_FP_fgets (line, 1023, datain) == NULL) |
835 |
break; |
836 |
if (ferror (datain)) { |
837 |
UUMessage (uunconc_id, __LINE__, UUMSG_ERROR, |
838 |
uustring (S_SOURCE_READ_ERR), |
839 |
strerror (uu_errno = errno)); |
840 |
return UURET_IOERR; |
841 |
} |
842 |
line[255] = '\0'; |
843 |
|
844 |
if (boundary && line[0]=='-' && line[1]=='-' && |
845 |
strncmp (line+2, boundary, strlen (boundary)) == 0) { |
846 |
if (line[strlen(boundary)+2]=='-') |
847 |
uulboundary = 1; |
848 |
else |
849 |
uulboundary = 0; |
850 |
return UURET_OK; |
851 |
} |
852 |
|
853 |
if (UUBUSYPOLL(ftell(datain)-progress.foffset,progress.fsize)) { |
854 |
UUMessage (uunconc_id, __LINE__, UUMSG_NOTE, |
855 |
uustring (S_DECODE_CANCEL)); |
856 |
return UURET_CANCEL; |
857 |
} |
858 |
|
859 |
ptr = line + strlen (line); |
860 |
|
861 |
/* |
862 |
* If the part ends directly after this line, the data does not end |
863 |
* with a linebreak. Or, as the docs put it, "the CRLF preceding the |
864 |
* encapsulation line is conceptually attached to the boundary. |
865 |
* So if the part ends here, don't print a line break" |
866 |
*/ |
867 |
if ((ftell(datain)<maxpos || flags&FL_TOEND || flags&FL_PARTIAL || |
868 |
!boundary || (!(flags&FL_PROPER) && uu_fast_scanning))) { |
869 |
*ptr = '\0'; |
870 |
fprintf (dataout, "%s\n", line); |
871 |
} |
872 |
else { |
873 |
*ptr = '\0'; |
874 |
fprintf (dataout, "%s", line); |
875 |
} |
876 |
} |
877 |
return UURET_OK; |
878 |
} |
879 |
|
880 |
/* |
881 |
* Decode a single field using method. For the moment, this supports |
882 |
* Base64 and Quoted Printable only, to support RFC 1522 header decoding. |
883 |
* Quit when seeing the RFC 1522 ?= end marker. |
884 |
*/ |
885 |
|
886 |
int |
887 |
UUDecodeField (char *s, char *d, int method) |
888 |
{ |
889 |
int z1, z2, z3, z4; |
890 |
int count=0; |
891 |
|
892 |
if (method == B64ENCODED) { |
893 |
while ((z1 = B64xlat[ACAST(*s)]) != -1) { |
894 |
if ((z2 = B64xlat[ACAST(*(s+1))]) == -1) break; |
895 |
if ((z3 = B64xlat[ACAST(*(s+2))]) == -1) break; |
896 |
if ((z4 = B64xlat[ACAST(*(s+3))]) == -1) break; |
897 |
|
898 |
d[count++] = (z1 << 2) | (z2 >> 4); |
899 |
d[count++] = (z2 << 4) | (z3 >> 2); |
900 |
d[count++] = (z3 << 6) | (z4); |
901 |
|
902 |
s+=4; |
903 |
} |
904 |
if (z1 != -1 && z2 != -1 && *(s+2) == '=') { |
905 |
d[count++] = (z1 << 2) | (z2 >> 4); |
906 |
s+=2; |
907 |
} |
908 |
else if (z1 != -1 && z2 != -1 && z3 != -1 && *(s+3) == '=') { |
909 |
d[count++] = (z1 << 2) | (z2 >> 4); |
910 |
d[count++] = (z2 << 4) | (z3 >> 2); |
911 |
s+=3; |
912 |
} |
913 |
} |
914 |
else if (method == QP_ENCODED) { |
915 |
while (*s && (*s != '?' || *(s+1) != '=')) { |
916 |
while (*s && *s != '=' && (*s != '?' || *(s+1) != '=')) { |
917 |
d[count++] = *s++; |
918 |
} |
919 |
if (*s == '=') { |
920 |
if (isxdigit (*(s+1)) && isxdigit (*(s+2))) { |
921 |
d[count] = (isdigit (*(s+1)) ? (*(s+1)-'0') : (tolower (*(s+1))-'a'+10)) << 4; |
922 |
d[count] |= (isdigit (*(s+2)) ? (*(s+2)-'0') : (tolower (*(s+2))-'a'+10)); |
923 |
count++; |
924 |
s+=3; |
925 |
} |
926 |
else if (!s[1]) { |
927 |
d[count++] = '\012'; |
928 |
} |
929 |
else { |
930 |
d[count++] = *s++; |
931 |
} |
932 |
} |
933 |
} |
934 |
} |
935 |
else { |
936 |
return -1; |
937 |
} |
938 |
|
939 |
d[count] = '\0'; |
940 |
return count; |
941 |
} |
942 |
|
943 |
int |
944 |
UUDecodePart (FILE *datain, FILE *dataout, int *state, |
945 |
long maxpos, int method, int flags, |
946 |
char *boundary) |
947 |
{ |
948 |
char *line, *oline=uuncdp_oline; |
949 |
int warning=0, vlc=0, lc[2], hadct=0; |
950 |
int tc=0, tf=0, vflag, haddata=0, haddh=0; |
951 |
long yefilesize=0, yepartends=0, yenotlastpart=0; |
952 |
crc32_t yepartcrc=crc32(0L, Z_NULL, 0); |
953 |
static crc32_t yefilecrc=0; |
954 |
static int bhflag=0; |
955 |
size_t count=0; |
956 |
size_t yepartsize=0; |
957 |
char *ptr; |
958 |
|
959 |
if (datain == NULL || dataout == NULL) { |
960 |
yefilecrc = crc32(0L, Z_NULL, 0); |
961 |
bhflag = 0; |
962 |
return UURET_OK; |
963 |
} |
964 |
|
965 |
/* |
966 |
* Use specialized functions for QP_ENCODED and PT_ENCODED plaintext |
967 |
*/ |
968 |
|
969 |
if (method == QP_ENCODED) |
970 |
return UUDecodeQP (datain, dataout, state, maxpos, |
971 |
method, flags, boundary); |
972 |
else if (method == PT_ENCODED) |
973 |
return UUDecodePT (datain, dataout, state, maxpos, |
974 |
method, flags, boundary); |
975 |
|
976 |
lc[0] = lc[1] = 0; |
977 |
vflag = 0; |
978 |
|
979 |
uulboundary = -1; |
980 |
|
981 |
if (method == YENC_ENCODED) { |
982 |
*state = BEGIN; |
983 |
} |
984 |
|
985 |
while (!feof (datain) && *state != DONE && |
986 |
(ftell(datain)<maxpos || flags&FL_TOEND || maxpos==-1 || |
987 |
(!(flags&FL_PROPER) && uu_fast_scanning))) { |
988 |
if (_FP_fgets ((line = uugen_fnbuffer), 1200 - 5, datain) == NULL) |
989 |
break; |
990 |
|
991 |
/* optionally skip .. */ |
992 |
if (*line == '.' && uu_dotdot) |
993 |
line++; |
994 |
|
995 |
if (ferror (datain)) { |
996 |
UUMessage (uunconc_id, __LINE__, UUMSG_ERROR, |
997 |
uustring (S_SOURCE_READ_ERR), |
998 |
strerror (uu_errno = errno)); |
999 |
return UURET_IOERR; |
1000 |
} |
1001 |
|
1002 |
if (!*line) { /* Empty line? */ |
1003 |
if (*state == DATA && |
1004 |
(method == UU_ENCODED || method == XX_ENCODED)) |
1005 |
*state = END; |
1006 |
|
1007 |
/* |
1008 |
* if we had a whole block of valid lines before, we reset our |
1009 |
* 'valid data' flag, tf. Without this 'if', we'd break decoding |
1010 |
* files with interleaved blank lines. The value of 5 is chosen |
1011 |
* quite arbitrarly. |
1012 |
*/ |
1013 |
|
1014 |
if (vlc > 5) |
1015 |
tf = tc = 0; |
1016 |
vlc = 0; |
1017 |
continue; |
1018 |
} |
1019 |
|
1020 |
/* |
1021 |
* Busy Polls |
1022 |
*/ |
1023 |
|
1024 |
if (UUBUSYPOLL(ftell(datain)-progress.foffset,progress.fsize)) { |
1025 |
UUMessage (uunconc_id, __LINE__, UUMSG_NOTE, |
1026 |
uustring (S_DECODE_CANCEL)); |
1027 |
return UURET_CANCEL; |
1028 |
} |
1029 |
|
1030 |
/* |
1031 |
* try to make sense of data |
1032 |
*/ |
1033 |
|
1034 |
line[1200 - 1] = '\0'; /* For Safety of string functions */ |
1035 |
count = 0; |
1036 |
|
1037 |
if (boundary && line[0]=='-' && line[1]=='-' && |
1038 |
strncmp (line+2, boundary, strlen (boundary)) == 0) { |
1039 |
if (line[strlen(boundary)+2]=='-') |
1040 |
uulboundary = 1; |
1041 |
else |
1042 |
uulboundary = 0; |
1043 |
return UURET_OK; |
1044 |
} |
1045 |
|
1046 |
/* |
1047 |
* Use this pseudo-handling only if !FL_PROPER |
1048 |
*/ |
1049 |
|
1050 |
if ((flags&FL_PROPER) == 0) { |
1051 |
if (strncmp (line, "BEGIN", 5) == 0 && |
1052 |
_FP_strstr (line, "CUT HERE") && !tf) { /* I hate these lines */ |
1053 |
tc = tf = vlc = 0; |
1054 |
continue; |
1055 |
} |
1056 |
/* MIME body boundary */ |
1057 |
if (line[0] == '-' && line[1] == '-' && method == B64ENCODED) { |
1058 |
if ((haddata || tc) && (haddh || hadct)) { |
1059 |
*state = DONE; |
1060 |
vlc = 0; |
1061 |
lc[0] = lc[1] = 0; |
1062 |
continue; |
1063 |
} |
1064 |
hadct = 0; |
1065 |
haddh = 1; |
1066 |
continue; |
1067 |
} |
1068 |
if (_FP_strnicmp (line, "Content-Type", 12) == 0) |
1069 |
hadct = 1; |
1070 |
} |
1071 |
|
1072 |
if (*state == BEGIN) { |
1073 |
if ((method == UU_ENCODED || method == XX_ENCODED) && |
1074 |
(strncmp (line, "begin ", 6) == 0 || |
1075 |
_FP_strnicmp (line, "<pre>begin ", 11) == 0)) { /* for LYNX */ |
1076 |
*state = DATA; |
1077 |
continue; |
1078 |
} |
1079 |
else if (method == BH_ENCODED && line[0] == ':') { |
1080 |
if (UUValidData (line, BH_ENCODED, &bhflag) == BH_ENCODED) { |
1081 |
bhflag = 0; |
1082 |
*state = DATA; |
1083 |
} |
1084 |
else |
1085 |
continue; |
1086 |
} |
1087 |
else if (method == YENC_ENCODED && |
1088 |
strncmp (line, "=ybegin ", 8) == 0 && |
1089 |
_FP_strstr (line, " name=") != NULL) { |
1090 |
*state = DATA; |
1091 |
|
1092 |
if ((ptr = _FP_strstr (line, " size=")) != NULL) { |
1093 |
ptr += 6; |
1094 |
yefilesize = atoi (ptr); |
1095 |
} |
1096 |
else { |
1097 |
yefilesize = -1; |
1098 |
} |
1099 |
|
1100 |
if ((ptr =_FP_strstr (line, " part="))) { |
1101 |
int partno = atoi (ptr + 6); |
1102 |
|
1103 |
if ((ptr = _FP_strstr (line, " total="))) |
1104 |
yenotlastpart = atoi (ptr + 7) != partno; |
1105 |
|
1106 |
if (_FP_fgets (line, 1200 - 5, datain) == NULL) { |
1107 |
break; |
1108 |
} |
1109 |
|
1110 |
if ((ptr = _FP_strstr (line, " end=")) == NULL) { |
1111 |
break; |
1112 |
} |
1113 |
|
1114 |
yepartends = atoi (ptr + 5); |
1115 |
} |
1116 |
tf = 1; |
1117 |
continue; |
1118 |
} |
1119 |
else { |
1120 |
continue; |
1121 |
} |
1122 |
|
1123 |
tc = tf = vlc = 0; |
1124 |
lc[0] = lc[1] = 0; |
1125 |
} |
1126 |
else if ((*state == END) && |
1127 |
(method == UU_ENCODED || method == XX_ENCODED)) { |
1128 |
if (strncmp (line, "end", 3) == 0) { |
1129 |
*state = DONE; |
1130 |
break; |
1131 |
} |
1132 |
} |
1133 |
|
1134 |
if (*state == DATA && method == YENC_ENCODED && |
1135 |
strncmp (line, "=yend ", 6) == 0) { |
1136 |
if ((ptr = _FP_strstr (line, " pcrc32=")) != NULL) { |
1137 |
crc32_t pcrc32 = strtoul (ptr + 8, NULL, 16); |
1138 |
if (pcrc32 != yepartcrc) { |
1139 |
UUMessage (uunconc_id, __LINE__, UUMSG_WARNING, |
1140 |
uustring (S_PCRC_MISMATCH), progress.curfile, progress.partno); |
1141 |
} |
1142 |
} |
1143 |
if ((ptr = _FP_strstr (line, " crc32=")) != NULL) |
1144 |
{ |
1145 |
crc32_t fcrc32 = strtoul (ptr + 7, NULL, 16); |
1146 |
if (fcrc32 != yefilecrc) { |
1147 |
UUMessage (uunconc_id, __LINE__, UUMSG_WARNING, |
1148 |
uustring (S_CRC_MISMATCH), progress.curfile); |
1149 |
} |
1150 |
} |
1151 |
if ((ptr = _FP_strstr (line, " size=")) != NULL) |
1152 |
{ |
1153 |
size_t size = atol(ptr + 6); |
1154 |
if (size != yepartsize && yefilesize != -1) { |
1155 |
if (size != yefilesize) |
1156 |
UUMessage (uunconc_id, __LINE__, UUMSG_WARNING, |
1157 |
uustring (S_PSIZE_MISMATCH), progress.curfile, |
1158 |
progress.partno, yepartsize, size); |
1159 |
else |
1160 |
UUMessage (uunconc_id, __LINE__, UUMSG_WARNING, |
1161 |
uustring (S_SIZE_MISMATCH), progress.curfile, |
1162 |
yepartsize, size); |
1163 |
} |
1164 |
} |
1165 |
if (!yenotlastpart && (yepartends == 0 || yepartends >= yefilesize)) { |
1166 |
*state = DONE; |
1167 |
} |
1168 |
break; |
1169 |
} |
1170 |
|
1171 |
if (*state == DATA || *state == END) { |
1172 |
if (method==B64ENCODED && line[0]=='-' && line[1]=='-' && tc) { |
1173 |
break; |
1174 |
} |
1175 |
|
1176 |
if ((vflag = UUValidData (line, (tf)?method:0, &bhflag)) == 0) |
1177 |
vflag = UURepairData (datain, line, (tf)?method:0, &bhflag); |
1178 |
|
1179 |
/* |
1180 |
* correct XX/UUencoded lines that were declared Base64 |
1181 |
*/ |
1182 |
|
1183 |
if ((method == XX_ENCODED || method == UU_ENCODED) && |
1184 |
vflag == B64ENCODED) { |
1185 |
if (UUValidData (line, method, &bhflag) == method) |
1186 |
vflag = method; |
1187 |
} |
1188 |
|
1189 |
if (vflag == method) { |
1190 |
if (tf) { |
1191 |
count = UUDecodeLine (line, oline, method); |
1192 |
if (method == YENC_ENCODED) { |
1193 |
yepartcrc = crc32(yepartcrc, oline, count); |
1194 |
yefilecrc = crc32(yefilecrc, oline, count); |
1195 |
yepartsize += count; |
1196 |
} |
1197 |
vlc++; lc[1]++; |
1198 |
} |
1199 |
else if (tc == 3) { |
1200 |
count = UUDecodeLine (save[0], oline, method); |
1201 |
count += UUDecodeLine (save[1], oline + count, method); |
1202 |
count += UUDecodeLine (save[2], oline + count, method); |
1203 |
count += UUDecodeLine (line, oline + count, method); |
1204 |
tf = 1; |
1205 |
tc = 0; |
1206 |
|
1207 |
/* |
1208 |
* complain if we had one or two invalid lines amidst of |
1209 |
* correctly encoded data. This usually means that the |
1210 |
* file is in error |
1211 |
*/ |
1212 |
|
1213 |
if (lc[1] > 10 && (lc[0] >= 1 && lc[0] <= 2) && !warning) { |
1214 |
UUMessage (uunconc_id, __LINE__, UUMSG_WARNING, |
1215 |
uustring (S_DATA_SUSPICIOUS)); |
1216 |
warning=1; |
1217 |
} |
1218 |
lc[0] = 0; |
1219 |
lc[1] = 3; |
1220 |
} |
1221 |
else { |
1222 |
_FP_strncpy (save[tc++], line, 1200); |
1223 |
} |
1224 |
|
1225 |
if (method == UU_ENCODED) |
1226 |
*state = (line[0] == 'M') ? DATA : END; |
1227 |
else if (method == XX_ENCODED) |
1228 |
*state = (line[0] == 'h') ? DATA : END; |
1229 |
else if (method == B64ENCODED) |
1230 |
*state = (strchr (line, '=') == NULL) ? DATA : DONE; |
1231 |
else if (method == BH_ENCODED) |
1232 |
*state = (!line[0] || strchr(line+1,':')==NULL)?DATA:DONE; |
1233 |
} |
1234 |
else { |
1235 |
vlc = tf = tc = 0; |
1236 |
haddh = 0; |
1237 |
lc[0]++; |
1238 |
} |
1239 |
} |
1240 |
else if (*state != DONE) { |
1241 |
return UURET_NOEND; |
1242 |
} |
1243 |
|
1244 |
if (count) { |
1245 |
if (method == BH_ENCODED) { |
1246 |
if (UUbhwrite (oline, 1, count, dataout) != count) { |
1247 |
UUMessage (uunconc_id, __LINE__, UUMSG_ERROR, |
1248 |
uustring (S_WR_ERR_TEMP), |
1249 |
strerror (uu_errno = errno)); |
1250 |
return UURET_IOERR; |
1251 |
} |
1252 |
} |
1253 |
else if (fwrite (oline, 1, count, dataout) != count) { |
1254 |
UUMessage (uunconc_id, __LINE__, UUMSG_ERROR, |
1255 |
uustring (S_WR_ERR_TEMP), |
1256 |
strerror (uu_errno = errno)); |
1257 |
return UURET_IOERR; |
1258 |
} |
1259 |
haddata++; |
1260 |
count = 0; |
1261 |
} |
1262 |
} |
1263 |
|
1264 |
if (*state == DONE || |
1265 |
(*state == DATA && method == B64ENCODED && |
1266 |
vflag == B64ENCODED && (flags&FL_PROPER || haddh))) { |
1267 |
for (tf=0; tf<tc; tf++) |
1268 |
count += UUDecodeLine (save[tf], oline + count, method); |
1269 |
if (count) { |
1270 |
if (method == BH_ENCODED) { |
1271 |
if (UUbhwrite (oline, 1, count, dataout) != count) { |
1272 |
UUMessage (uunconc_id, __LINE__, UUMSG_ERROR, |
1273 |
uustring (S_WR_ERR_TEMP), |
1274 |
strerror (uu_errno = errno)); |
1275 |
return UURET_IOERR; |
1276 |
} |
1277 |
} |
1278 |
else if (fwrite (oline, 1, count, dataout) != count) { |
1279 |
UUMessage (uunconc_id, __LINE__, UUMSG_ERROR, |
1280 |
uustring (S_WR_ERR_TEMP), |
1281 |
strerror (uu_errno = errno)); |
1282 |
return UURET_IOERR; |
1283 |
} |
1284 |
} |
1285 |
} |
1286 |
return UURET_OK; |
1287 |
} |
1288 |
|
1289 |
/* |
1290 |
* this function decodes the file into a temporary file |
1291 |
*/ |
1292 |
|
1293 |
int |
1294 |
UUDecode (uulist *data) |
1295 |
{ |
1296 |
int state=BEGIN, part=-1, res=0, hb; |
1297 |
unsigned long rsize, dsize, numbytes; |
1298 |
FILE *datain, *dataout; |
1299 |
void *datain_buf, *dataout_buf; |
1300 |
unsigned char r[8]; |
1301 |
char *mode, *ntmp; |
1302 |
uufile *iter; |
1303 |
size_t bytes; |
1304 |
#ifdef HAVE_MKSTEMP |
1305 |
int tmpfd; |
1306 |
const char *tmpprefix = "uuXXXXXX"; |
1307 |
char *tmpdir = NULL; |
1308 |
#endif /* HAVE_MKSTEMP */ |
1309 |
|
1310 |
if (data == NULL || data->thisfile == NULL) |
1311 |
return UURET_ILLVAL; |
1312 |
|
1313 |
if (data->state & UUFILE_TMPFILE) |
1314 |
return UURET_OK; |
1315 |
|
1316 |
if (data->state & UUFILE_NODATA) |
1317 |
return UURET_NODATA; |
1318 |
|
1319 |
if (data->state & UUFILE_NOBEGIN && !uu_desperate) |
1320 |
return UURET_NODATA; |
1321 |
|
1322 |
if (data->uudet == PT_ENCODED) |
1323 |
mode = "wt"; /* open text files in text mode */ |
1324 |
else |
1325 |
mode = "wb"; /* otherwise in binary */ |
1326 |
|
1327 |
#ifdef HAVE_MKSTEMP |
1328 |
if ((getuid()==geteuid()) && (getgid()==getegid())) { |
1329 |
tmpdir=getenv("TMPDIR"); |
1330 |
} |
1331 |
|
1332 |
if (!tmpdir) { |
1333 |
tmpdir = "/tmp"; |
1334 |
} |
1335 |
data->binfile = malloc(strlen(tmpdir)+strlen(tmpprefix)+2); |
1336 |
|
1337 |
if (!data->binfile) { |
1338 |
#else |
1339 |
if ((data->binfile = tempnam (NULL, "uu")) == NULL) { |
1340 |
#endif /* HAVE_MKSTEMP */ |
1341 |
UUMessage (uunconc_id, __LINE__, UUMSG_ERROR, |
1342 |
uustring (S_NO_TEMP_NAME)); |
1343 |
return UURET_NOMEM; |
1344 |
} |
1345 |
|
1346 |
#ifdef HAVE_MKSTEMP |
1347 |
strcpy(data->binfile, tmpdir); |
1348 |
strcat(data->binfile, "/"); |
1349 |
strcat(data->binfile, tmpprefix); |
1350 |
|
1351 |
if ((tmpfd = mkstemp(data->binfile)) == -1 || |
1352 |
(dataout = fdopen(tmpfd, mode)) == NULL) { |
1353 |
#else |
1354 |
if ((dataout = fopen (data->binfile, mode)) == NULL) { |
1355 |
#endif /* HAVE_MKSTEMP */ |
1356 |
/* |
1357 |
* we couldn't create a temporary file. Usually this means that TMP |
1358 |
* and TEMP aren't set |
1359 |
*/ |
1360 |
UUMessage (uunconc_id, __LINE__, UUMSG_ERROR, |
1361 |
uustring (S_WR_ERR_TARGET), |
1362 |
data->binfile, strerror (uu_errno = errno)); |
1363 |
#ifdef HAVE_MKSTEMP |
1364 |
if (tmpfd != -1) { |
1365 |
unlink(data->binfile); |
1366 |
close(tmpfd); |
1367 |
} |
1368 |
#endif /* HAVE_MKSTEMP */ |
1369 |
_FP_free (data->binfile); |
1370 |
data->binfile = NULL; |
1371 |
uu_errno = errno; |
1372 |
return UURET_IOERR; |
1373 |
} |
1374 |
UUSETBUF (dataout, dataout_buf, uu_wbuf); |
1375 |
|
1376 |
/* |
1377 |
* we don't have begin lines in Base64 or plain text files. |
1378 |
*/ |
1379 |
if (data->uudet == B64ENCODED || data->uudet == QP_ENCODED || |
1380 |
data->uudet == PT_ENCODED) |
1381 |
state = DATA; |
1382 |
|
1383 |
/* |
1384 |
* If we know that the file does not have a begin, we simulate |
1385 |
* it in desperate mode |
1386 |
*/ |
1387 |
|
1388 |
if ((data->state & UUFILE_NOBEGIN) && uu_desperate) |
1389 |
state = DATA; |
1390 |
|
1391 |
(void) UUDecodeLine (NULL, NULL, 0); /* init */ |
1392 |
(void) UUbhwrite (NULL, 0, 0, NULL); /* dito */ |
1393 |
(void) UUDecodePart (NULL, NULL, NULL, 0, 0, 0, NULL); /* yep */ |
1394 |
|
1395 |
/* |
1396 |
* initialize progress information |
1397 |
*/ |
1398 |
progress.action = 0; |
1399 |
if (data->filename != NULL) { |
1400 |
_FP_strncpy (progress.curfile, |
1401 |
(strlen(data->filename)>255)? |
1402 |
(data->filename+strlen(data->filename)-255):data->filename, |
1403 |
256); |
1404 |
} |
1405 |
else { |
1406 |
_FP_strncpy (progress.curfile, |
1407 |
(strlen(data->binfile)>255)? |
1408 |
(data->binfile+strlen(data->binfile)-255):data->binfile, |
1409 |
256); |
1410 |
} |
1411 |
progress.partno = 0; |
1412 |
progress.numparts = 0; |
1413 |
progress.fsize = -1; |
1414 |
progress.percent = 0; |
1415 |
progress.action = UUACT_DECODING; |
1416 |
|
1417 |
iter = data->thisfile; |
1418 |
while (iter) { |
1419 |
progress.numparts = (iter->partno)?iter->partno:1; |
1420 |
iter = iter->NEXT; |
1421 |
} |
1422 |
|
1423 |
/* |
1424 |
* let's rock! |
1425 |
*/ |
1426 |
|
1427 |
iter = data->thisfile; |
1428 |
while (iter) { |
1429 |
if (part != -1 && iter->partno != part+1 && !uu_desperate) |
1430 |
break; |
1431 |
else |
1432 |
part = iter->partno; |
1433 |
|
1434 |
if (iter->data->sfname == NULL) { |
1435 |
iter = iter->NEXT; |
1436 |
continue; |
1437 |
} |
1438 |
|
1439 |
/* |
1440 |
* call our FileCallback to retrieve the file |
1441 |
*/ |
1442 |
|
1443 |
if (uu_FileCallback) { |
1444 |
if ((res = (*uu_FileCallback) (uu_FileCBArg, iter->data->sfname, |
1445 |
uugen_fnbuffer, 1)) != UURET_OK) |
1446 |
break; |
1447 |
if ((datain = fopen (uugen_fnbuffer, "rb")) == NULL) { |
1448 |
(*uu_FileCallback) (uu_FileCBArg, iter->data->sfname, |
1449 |
uugen_fnbuffer, 0); |
1450 |
UUMessage (uunconc_id, __LINE__, UUMSG_ERROR, |
1451 |
uustring (S_NOT_OPEN_FILE), |
1452 |
uugen_fnbuffer, strerror (uu_errno = errno)); |
1453 |
res = UURET_IOERR; |
1454 |
break; |
1455 |
} |
1456 |
} |
1457 |
else { |
1458 |
if ((datain = fopen (iter->data->sfname, "rb")) == NULL) { |
1459 |
UUMessage (uunconc_id, __LINE__, UUMSG_ERROR, |
1460 |
uustring (S_NOT_OPEN_FILE), |
1461 |
iter->data->sfname, strerror (uu_errno = errno)); |
1462 |
res = UURET_IOERR; |
1463 |
break; |
1464 |
} |
1465 |
_FP_strncpy (uugen_fnbuffer, iter->data->sfname, 1024); |
1466 |
} |
1467 |
UUSETBUF (datain, datain_buf, uu_rbuf); |
1468 |
|
1469 |
progress.partno = part; |
1470 |
progress.fsize = (iter->data->length)?iter->data->length:-1; |
1471 |
progress.percent = 0; |
1472 |
progress.foffset = iter->data->startpos; |
1473 |
|
1474 |
fseek (datain, iter->data->startpos, SEEK_SET); |
1475 |
res = UUDecodePart (datain, dataout, &state, |
1476 |
iter->data->startpos+iter->data->length, |
1477 |
data->uudet, iter->data->flags, NULL); |
1478 |
fclose (datain); |
1479 |
UUCLRBUF (uu_rbuf, datain_buf); |
1480 |
|
1481 |
if (uu_FileCallback) |
1482 |
(*uu_FileCallback) (uu_FileCBArg, iter->data->sfname, uugen_fnbuffer, 0); |
1483 |
|
1484 |
if (state == DONE || res != UURET_OK) |
1485 |
break; |
1486 |
|
1487 |
iter = iter->NEXT; |
1488 |
} |
1489 |
|
1490 |
if (state == DATA && |
1491 |
(data->uudet == B64ENCODED || data->uudet == QP_ENCODED || |
1492 |
data->uudet == PT_ENCODED)) |
1493 |
state = DONE; /* assume we're done */ |
1494 |
|
1495 |
if (fclose (dataout)) { |
1496 |
UUMessage (uunconc_id, __LINE__, UUMSG_ERROR, |
1497 |
uustring (S_WR_ERR_TEMP), |
1498 |
strerror (uu_errno = errno)); |
1499 |
res = UURET_IOERR; |
1500 |
} |
1501 |
UUCLRBUF (uu_wbuf, dataout_buf); |
1502 |
|
1503 |
if (res != UURET_OK || (state != DONE && !uu_desperate)) { |
1504 |
unlink (data->binfile); |
1505 |
_FP_free (data->binfile); |
1506 |
data->binfile = NULL; |
1507 |
data->state &= ~UUFILE_TMPFILE; |
1508 |
data->state |= UUFILE_ERROR; |
1509 |
|
1510 |
if (res == UURET_OK && state != DONE) |
1511 |
res = UURET_NOEND; |
1512 |
} |
1513 |
else if (res != UURET_OK) { |
1514 |
data->state &= ~UUFILE_DECODED; |
1515 |
data->state |= UUFILE_ERROR | UUFILE_TMPFILE; |
1516 |
} |
1517 |
else { |
1518 |
data->state &= ~UUFILE_ERROR; |
1519 |
data->state |= UUFILE_TMPFILE; |
1520 |
} |
1521 |
|
1522 |
/* |
1523 |
* If this was a BinHex file, we must extract its data or resource fork |
1524 |
*/ |
1525 |
|
1526 |
if (data->uudet == BH_ENCODED && data->binfile) { |
1527 |
#ifdef HAVE_MKSTEMP |
1528 |
ntmp = malloc(strlen(tmpdir)+strlen(tmpprefix)+2); |
1529 |
|
1530 |
if (ntmp == NULL) { |
1531 |
#else |
1532 |
if ((ntmp = tempnam (NULL, "uu")) == NULL) { |
1533 |
#endif /* HAVE_MKSTEMP */ |
1534 |
UUMessage (uunconc_id, __LINE__, UUMSG_ERROR, |
1535 |
uustring (S_NO_TEMP_NAME)); |
1536 |
progress.action = 0; |
1537 |
return UURET_NOMEM; |
1538 |
} |
1539 |
if ((datain = fopen (data->binfile, "rb")) == NULL) { |
1540 |
UUMessage (uunconc_id, __LINE__, UUMSG_ERROR, |
1541 |
uustring (S_NOT_OPEN_FILE), |
1542 |
data->binfile, strerror (uu_errno = errno)); |
1543 |
progress.action = 0; |
1544 |
free (ntmp); |
1545 |
return UURET_IOERR; |
1546 |
} |
1547 |
UUSETBUF (datain, datain_buf, uu_rbuf); |
1548 |
|
1549 |
#ifdef HAVE_MKSTEMP |
1550 |
strcpy(ntmp, tmpdir); |
1551 |
strcat(ntmp, "/"); |
1552 |
strcat(ntmp, tmpprefix); |
1553 |
if ((tmpfd = mkstemp(ntmp)) == -1 || |
1554 |
(dataout = fdopen(tmpfd, "wb")) == NULL) { |
1555 |
#else |
1556 |
if ((dataout = fopen (ntmp, "wb")) == NULL) { |
1557 |
#endif /* HAVE_MKSTEMP */ |
1558 |
UUMessage (uunconc_id, __LINE__, UUMSG_ERROR, |
1559 |
uustring (S_NOT_OPEN_TARGET), |
1560 |
ntmp, strerror (uu_errno = errno)); |
1561 |
progress.action = 0; |
1562 |
fclose (datain); |
1563 |
UUCLRBUF (uu_rbuf, datain_buf); |
1564 |
#ifdef HAVE_MKSTEMP |
1565 |
if (tmpfd != -1) { |
1566 |
unlink(ntmp); |
1567 |
close(tmpfd); |
1568 |
} |
1569 |
#endif /* HAVE_MKSTEMP */ |
1570 |
free (ntmp); |
1571 |
return UURET_IOERR; |
1572 |
} |
1573 |
UUSETBUF (dataout, dataout_buf, uu_wbuf); |
1574 |
|
1575 |
/* |
1576 |
* read fork lengths. remember they're in Motorola format |
1577 |
*/ |
1578 |
r[0] = _FP_fgetc (datain); |
1579 |
hb = (int) r[0] + 22; |
1580 |
fseek (datain, (int) r[0] + 12, SEEK_SET); |
1581 |
fread (r, 1, 8, datain); |
1582 |
|
1583 |
dsize = (((long) 1 << 24) * (long) r[0]) + |
1584 |
(((long) 1 << 16) * (long) r[1]) + |
1585 |
(((long) 1 << 8) * (long) r[2]) + |
1586 |
( (long) r[3]); |
1587 |
rsize = (((long) 1 << 24) * (long) r[4]) + |
1588 |
(((long) 1 << 16) * (long) r[5]) + |
1589 |
(((long) 1 << 8) * (long) r[6]) + |
1590 |
( (long) r[7]); |
1591 |
|
1592 |
UUMessage (uunconc_id, __LINE__, UUMSG_MESSAGE, |
1593 |
uustring (S_BINHEX_SIZES), |
1594 |
dsize, rsize); |
1595 |
|
1596 |
if (dsize == 0) { |
1597 |
fseek (datain, dsize + hb + 2, SEEK_SET); |
1598 |
numbytes = rsize; |
1599 |
} |
1600 |
else if (rsize == 0) { |
1601 |
fseek (datain, hb, SEEK_SET); |
1602 |
numbytes = dsize; |
1603 |
} |
1604 |
else { |
1605 |
/* we should let the user have the choice here */ |
1606 |
UUMessage (uunconc_id, __LINE__, UUMSG_NOTE, |
1607 |
uustring (S_BINHEX_BOTH)); |
1608 |
fseek (datain, hb, SEEK_SET); |
1609 |
numbytes = dsize; |
1610 |
} |
1611 |
|
1612 |
progress.action = 0; |
1613 |
progress.partno = 0; |
1614 |
progress.numparts = 1; |
1615 |
progress.fsize = numbytes ? numbytes : -1; |
1616 |
progress.foffset = hb; |
1617 |
progress.percent = 0; |
1618 |
progress.action = UUACT_COPYING; |
1619 |
|
1620 |
/* |
1621 |
* copy the chosen fork |
1622 |
*/ |
1623 |
|
1624 |
while (!feof (datain) && numbytes) { |
1625 |
if (UUBUSYPOLL(ftell(datain)-progress.foffset,progress.fsize)) { |
1626 |
UUMessage (uunconc_id, __LINE__, UUMSG_NOTE, |
1627 |
uustring (S_DECODE_CANCEL)); |
1628 |
fclose (datain); |
1629 |
UUCLRBUF (uu_rbuf, datain_buf); |
1630 |
fclose (dataout); |
1631 |
UUCLRBUF (uu_wbuf, dataout_buf); |
1632 |
unlink (ntmp); |
1633 |
free (ntmp); |
1634 |
return UURET_CANCEL; |
1635 |
} |
1636 |
|
1637 |
bytes = fread (uugen_inbuffer, 1, |
1638 |
(size_t) ((numbytes>1024)?1024:numbytes), datain); |
1639 |
|
1640 |
if (ferror (datain) || (bytes == 0 && !feof (datain))) { |
1641 |
progress.action = 0; |
1642 |
UUMessage (uunconc_id, __LINE__, UUMSG_ERROR, |
1643 |
uustring (S_SOURCE_READ_ERR), |
1644 |
data->binfile, strerror (uu_errno = errno)); |
1645 |
fclose (datain); |
1646 |
UUCLRBUF (uu_rbuf, datain_buf); |
1647 |
fclose (dataout); |
1648 |
UUCLRBUF (uu_wbuf, dataout_buf); |
1649 |
unlink (ntmp); |
1650 |
free (ntmp); |
1651 |
return UURET_IOERR; |
1652 |
} |
1653 |
if (fwrite (uugen_inbuffer, 1, bytes, dataout) != bytes) { |
1654 |
progress.action = 0; |
1655 |
UUMessage (uunconc_id, __LINE__, UUMSG_ERROR, |
1656 |
uustring (S_WR_ERR_TARGET), |
1657 |
ntmp, strerror (uu_errno = errno)); |
1658 |
fclose (datain); |
1659 |
UUCLRBUF (uu_rbuf, datain_buf); |
1660 |
fclose (dataout); |
1661 |
UUCLRBUF (uu_wbuf, dataout_buf); |
1662 |
unlink (ntmp); |
1663 |
free (ntmp); |
1664 |
return UURET_IOERR; |
1665 |
} |
1666 |
numbytes -= bytes; |
1667 |
} |
1668 |
|
1669 |
if (numbytes) { |
1670 |
UUMessage (uunconc_id, __LINE__, UUMSG_WARNING, |
1671 |
uustring (S_SHORT_BINHEX), |
1672 |
(data->filename)?data->filename: |
1673 |
(data->subfname)?data->subfname:"???", |
1674 |
numbytes); |
1675 |
} |
1676 |
|
1677 |
/* |
1678 |
* replace temp file |
1679 |
*/ |
1680 |
|
1681 |
fclose (datain); |
1682 |
UUCLRBUF (uu_rbuf, datain_buf); |
1683 |
if (fclose (dataout)) { |
1684 |
UUCLRBUF (uu_wbuf, dataout_buf); |
1685 |
UUMessage (uunconc_id, __LINE__, UUMSG_ERROR, |
1686 |
uustring (S_WR_ERR_TARGET), |
1687 |
ntmp, strerror (uu_errno = errno)); |
1688 |
unlink (ntmp); |
1689 |
free (ntmp); |
1690 |
return UURET_IOERR; |
1691 |
} |
1692 |
UUCLRBUF (uu_wbuf, dataout_buf); |
1693 |
|
1694 |
if (unlink (data->binfile)) { |
1695 |
UUMessage (uunconc_id, __LINE__, UUMSG_WARNING, |
1696 |
uustring (S_TMP_NOT_REMOVED), |
1697 |
data->binfile, strerror (uu_errno = errno)); |
1698 |
} |
1699 |
|
1700 |
free (data->binfile); |
1701 |
data->binfile = ntmp; |
1702 |
} |
1703 |
|
1704 |
progress.action = 0; |
1705 |
return res; |
1706 |
} |
1707 |
|
1708 |
/* |
1709 |
* QuickDecode for proper MIME attachments. We expect the pointer to |
1710 |
* be on the first header line. |
1711 |
*/ |
1712 |
|
1713 |
int |
1714 |
UUQuickDecode (FILE *datain, FILE *dataout, char *boundary, long maxpos) |
1715 |
{ |
1716 |
int state=BEGIN, encoding=-1; |
1717 |
headers myenv; |
1718 |
|
1719 |
/* |
1720 |
* Read header and find out about encoding. |
1721 |
*/ |
1722 |
|
1723 |
memset (&myenv, 0, sizeof (headers)); |
1724 |
UUScanHeader (datain, &myenv); |
1725 |
|
1726 |
if (_FP_stristr (myenv.ctenc, "uu") != NULL) |
1727 |
encoding = UU_ENCODED; |
1728 |
else if (_FP_stristr (myenv.ctenc, "xx") != NULL) |
1729 |
encoding = XX_ENCODED; |
1730 |
else if (_FP_stricmp (myenv.ctenc, "base64") == 0) |
1731 |
encoding = B64ENCODED; |
1732 |
else if (_FP_stricmp (myenv.ctenc, "quoted-printable") == 0) |
1733 |
encoding = QP_ENCODED; |
1734 |
else |
1735 |
encoding = PT_ENCODED; |
1736 |
|
1737 |
UUkillheaders (&myenv); |
1738 |
|
1739 |
/* |
1740 |
* okay, so decode this one |
1741 |
*/ |
1742 |
|
1743 |
(void) UUDecodePart (NULL, NULL, NULL, 0, 0, 0, NULL); /* init */ |
1744 |
return UUDecodePart (datain, dataout, &state, maxpos, |
1745 |
encoding, FL_PROPER|FL_TOEND, |
1746 |
boundary); |
1747 |
} |