Annotation of parser3/src/main/pa_uue.C, revision 1.14

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.14    ! misha      10: static const char * const IDENT_UUE_C="$Date: 2009-09-27 23:40:07 $";
1.1       paf        11: 
                     12: #include "pa_config_includes.h"
                     13: 
                     14: #include "pa_uue.h"
1.14    ! misha      15: #include "pa_memory.h"
1.1       paf        16: 
                     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) {
        !            29:        size_t new_size=((in_size / 3 + 1) * 4);
1.13      misha      30:        new_size += 2 * new_size / 60/*chars in line + new lines*/ + 2;
1.14    ! misha      31:        new_size += strlen(file_name) + 11/*header*/ + 6/*footer*/ + 1/*zero terminator*/;
1.12      misha      32: 
                     33:        const char* result=new(PointerFreeGC) char[new_size];
                     34:        char* optr=(char*)result;
                     35: 
                     36:        //header
1.14    ! misha      37:        optr += sprintf(optr, "begin 644 %s\n", file_name);
1.12      misha      38: 
                     39:        //body
1.1       paf        40:        int count=45;
1.14    ! misha      41:        for(const unsigned char *itemp=in; itemp<(in+in_size); itemp+=count) {
1.1       paf        42:                int index;      
                     43: 
1.14    ! misha      44:                if((itemp+count)>(in+in_size)) 
        !            45:                        count=in_size-(itemp-in);
1.1       paf        46: 
                     47:                /*
                     48:                * for UU and XX, encode the number of bytes as first character
                     49:                */
                     50:                *optr++ = uue_table[count];
                     51:                
                     52:                for (index=0; index<=count-3; index+=3) {
                     53:                        *optr++ = uue_table[itemp[index] >> 2];
                     54:                        *optr++ = uue_table[((itemp[index  ] & 0x03) << 4) | (itemp[index+1] >> 4)];
                     55:                        *optr++ = uue_table[((itemp[index+1] & 0x0f) << 2) | (itemp[index+2] >> 6)];
                     56:                        *optr++ = uue_table[  itemp[index+2] & 0x3f];
                     57:                }
                     58:                
                     59:                /*
                     60:                * Special handlitempg for itempcomplete litempes
                     61:                */
                     62:                if (index != count) {
                     63:                        if (count - index == 2) {
                     64:                                *optr++ = uue_table[itemp[index] >> 2];
                     65:                                *optr++ = uue_table[((itemp[index  ] & 0x03) << 4) | 
                     66:                                        ( itemp[index+1] >> 4)];
                     67:                                *optr++ = uue_table[((itemp[index+1] & 0x0f) << 2)];
                     68:                                *optr++ = uue_table[0];
                     69:                        }
                     70:                        else if (count - index == 1) {
                     71:                                *optr++ = uue_table[ itemp[index] >> 2];
                     72:                                *optr++ = uue_table[(itemp[index] & 0x03) << 4];
                     73:                                *optr++ = uue_table[0];
                     74:                                *optr++ = uue_table[0];
                     75:                        }
                     76:                }
                     77:                /*
                     78:                * end of line
                     79:                */
                     80:                *optr++ = '\n'; 
                     81:        }
                     82:        
                     83:        //footer
1.12      misha      84:        optr += sprintf(optr, "`\nend\n");
                     85: 
                     86:        *optr = 0;
                     87: 
1.14    ! misha      88:        //throw Exception(PARSER_RUNTIME, 0, "%d %d %d", in_size, new_size, (size_t)(optr-result));
1.12      misha      89:        assert((size_t)(optr-result) < new_size);
                     90: 
                     91:        return result;
1.14    ! misha      92: }

E-mail: