Annotation of parser3/src/main/pa_uue.C, revision 1.13
1.1 paf 1: /** @file
2: Parser: uuencoding impl.
3:
1.12 misha 4: Copyright(c) 2000-2009 ArtLebedev Group(http://www.artlebedev.com)
1.1 paf 5: Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
6:
7: @todo setrlimit
8: */
1.2 paf 9:
1.13 ! misha 10: static const char * const IDENT_UUE_C="$Date: 2009-09-27 22:07:58 $";
1.1 paf 11:
12: #include "pa_config_includes.h"
13:
14: #include "pa_uue.h"
15:
16: static unsigned char uue_table[64] = {
17: '`', '!', '"', '#', '$', '%', '&', '\'',
18: '(', ')', '*', '+', ',', '-', '.', '/',
19: '0', '1', '2', '3', '4', '5', '6', '7',
20: '8', '9', ':', ';', '<', '=', '>', '?',
21: '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
22: 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
23: 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
24: 'X', 'Y', 'Z', '[', '\\',']', '^', '_'
25: };
26:
1.12 misha 27: const char* pa_uuencode(const String& file_name, const VFile& vfile) {
1.1 paf 28: const unsigned char *in=(const unsigned char *)vfile.value_ptr();
29: size_t in_length=vfile.value_size();
30:
1.12 misha 31: const char* file_name_cstr = file_name.cstr();
32:
33: size_t new_size=((in_length / 3 + 1) * 4);
1.13 ! misha 34: new_size += 2 * new_size / 60/*chars in line + new lines*/ + 2;
1.12 misha 35: new_size += strlen(file_name_cstr) + 11/*header*/ + 6/*footer*/ + 1/*zero terminator*/;
36:
37: const char* result=new(PointerFreeGC) char[new_size];
38: char* optr=(char*)result;
39:
40: //header
41: optr += sprintf(optr, "begin 644 %s\n", file_name_cstr);
42:
43: //body
1.1 paf 44: int count=45;
45: for(const unsigned char *itemp=in; itemp<(in+in_length); itemp+=count) {
46: int index;
47:
48: if((itemp+count)>(in+in_length))
49: count=in_length-(itemp-in);
50:
51: /*
52: * for UU and XX, encode the number of bytes as first character
53: */
54: *optr++ = uue_table[count];
55:
56: for (index=0; index<=count-3; index+=3) {
57: *optr++ = uue_table[itemp[index] >> 2];
58: *optr++ = uue_table[((itemp[index ] & 0x03) << 4) | (itemp[index+1] >> 4)];
59: *optr++ = uue_table[((itemp[index+1] & 0x0f) << 2) | (itemp[index+2] >> 6)];
60: *optr++ = uue_table[ itemp[index+2] & 0x3f];
61: }
62:
63: /*
64: * Special handlitempg for itempcomplete litempes
65: */
66: if (index != count) {
67: if (count - index == 2) {
68: *optr++ = uue_table[itemp[index] >> 2];
69: *optr++ = uue_table[((itemp[index ] & 0x03) << 4) |
70: ( itemp[index+1] >> 4)];
71: *optr++ = uue_table[((itemp[index+1] & 0x0f) << 2)];
72: *optr++ = uue_table[0];
73: }
74: else if (count - index == 1) {
75: *optr++ = uue_table[ itemp[index] >> 2];
76: *optr++ = uue_table[(itemp[index] & 0x03) << 4];
77: *optr++ = uue_table[0];
78: *optr++ = uue_table[0];
79: }
80: }
81: /*
82: * end of line
83: */
84: *optr++ = '\n';
85: }
86:
87: //footer
1.12 misha 88: optr += sprintf(optr, "`\nend\n");
89:
90: *optr = 0;
91:
92: //throw Exception(PARSER_RUNTIME, 0, "%d %d %d", in_length, new_size, (size_t)(optr-result));
93: assert((size_t)(optr-result) < new_size);
94:
95: return result;
96: }
E-mail: