--- parser3/src/main/pa_uue.C 2005/08/09 08:14:52 1.9 +++ parser3/src/main/pa_uue.C 2023/09/26 20:49:10 1.20 @@ -1,17 +1,18 @@ /** @file Parser: uuencoding impl. - Copyright(c) 2000,2001-2005 ArtLebedev Group(http://www.artlebedev.com) - Author: Alexandr Petrosian (http://paf.design.ru) + Copyright (c) 2001-2023 Art. Lebedev Studio (http://www.artlebedev.com) + Authors: Konstantin Morshnev , Alexandr Petrosian @todo setrlimit */ -static const char * const IDENT_UUE_C="$Date: 2005/08/09 08:14:52 $"; - #include "pa_config_includes.h" #include "pa_uue.h" +#include "pa_memory.h" + +volatile const char * IDENT_PA_UUE_C="$Id: pa_uue.C,v 1.20 2023/09/26 20:49:10 moko Exp $" IDENT_PA_UUE_H; static unsigned char uue_table[64] = { '`', '!', '"', '#', '$', '%', '&', '\'', @@ -23,25 +24,27 @@ static unsigned char uue_table[64] = { 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', '\\',']', '^', '_' }; -void pa_uuencode(String& result, const String& file_name, const VFile& vfile) { + +const char* pa_uuencode(const unsigned char* in, size_t in_size, const char* file_name) { + int count=45; + + size_t new_size = ((in_size / 3 + 1) * 4); + new_size += 2 * new_size / (count / 3 * 4) /*chars in line + new lines*/ + 2; + new_size += strlen(file_name) + 11/*header*/ + 6/*footer*/ + 1/*zero terminator*/; + + const char* result=new(PointerFreeGC) char[new_size]; + char* optr=(char*)result; + //header - result << "content-transfer-encoding: x-uuencode\n" << "\n"; - result << "begin 644 " << file_name << "\n"; + optr += sprintf(optr, "begin 644 %s\n", file_name); //body - const unsigned char *in=(const unsigned char *)vfile.value_ptr(); - size_t in_length=vfile.value_size(); - - int count=45; - for(const unsigned char *itemp=in; itemp<(in+in_length); itemp+=count) { + for(const unsigned char *itemp=in; itemp<(in+in_size); itemp+=count) { int index; - if((itemp+count)>(in+in_length)) - count=in_length-(itemp-in); + if((itemp+count)>(in+in_size)) + count=in_size-(itemp-in); - char *buf=new(PointerFreeGC) char[MAX_STRING]; - char *optr=buf; - /* * for UU and XX, encode the number of bytes as first character */ @@ -75,12 +78,14 @@ void pa_uuencode(String& result, const S /* * end of line */ - *optr++ = '\n'; - *optr = 0; - result << buf; + *optr++ = '\n'; } //footer - result<< "`\n" - "end\n"; + optr += sprintf(optr, "`\nend\n"); + + //throw Exception(PARSER_RUNTIME, 0, "%d %d %d", in_size, new_size, (size_t)(optr-result)); + assert((size_t)(optr-result) < new_size); + + return result; }