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