--- parser3/src/main/pa_common.C 2016/07/21 18:30:10 1.289 +++ parser3/src/main/pa_common.C 2016/12/24 23:02:13 1.297 @@ -50,7 +50,7 @@ #define pa_mkdir(path, mode) mkdir(path, mode) #endif -volatile const char * IDENT_PA_COMMON_C="$Id: pa_common.C,v 1.289 2016/07/21 18:30:10 moko Exp $" IDENT_PA_COMMON_H IDENT_PA_HASH_H IDENT_PA_ARRAY_H IDENT_PA_STACK_H; +volatile const char * IDENT_PA_COMMON_C="$Id: pa_common.C,v 1.297 2016/12/24 23:02:13 moko Exp $" IDENT_PA_COMMON_H IDENT_PA_HASH_H IDENT_PA_ARRAY_H IDENT_PA_STACK_H; // some maybe-undefined constants @@ -156,9 +156,9 @@ File_read_result file_read(Request_chars Charset* asked_charset=0; if(params) if(Value* vcharset_name=params->get(PA_CHARSET_NAME)) - asked_charset=&::charsets.get(vcharset_name->as_string().change_case(charsets.source(), String::CC_UPPER)); + asked_charset=&pa_charsets.get(vcharset_name->as_string()); - asked_charset=::charsets.checkBOM(result.str, result.length, asked_charset); + asked_charset=pa_charsets.checkBOM(result.str, result.length, asked_charset); if(result.length && transcode_text_result && asked_charset){ // length must be checked because transcode returns CONST string in case length==0, which contradicts hacking few lines below String::C body=String::C(result.str, result.length); @@ -493,12 +493,7 @@ bool file_executable(const String& file_ return access(file_spec.taint_cstr(String::L_FILE_SPEC), X_OK)==0; } -bool file_stat(const String& file_spec, - size_t& rsize, - time_t& ratime, - time_t& rmtime, - time_t& rctime, - bool fail_on_read_problem) { +bool file_stat(const String& file_spec, uint64_t& rsize, time_t& ratime, time_t& rmtime, time_t& rctime, bool fail_on_read_problem) { const char* fname=file_spec.taint_cstr(String::L_FILE_SPEC); struct stat finfo; if(stat(fname, &finfo)!=0) { @@ -543,6 +538,20 @@ const char* capitalize(const char* s){ return (const char*)result; } +char *str_lower(const char *s, size_t helper_length){ + char *result=pa_strdup(s, helper_length); + for(char* c=result; *c; c++) + *c=(char)tolower((unsigned char)*c); + return result; +} + +char *str_upper(const char *s, size_t helper_length){ + char *result=pa_strdup(s, helper_length); + for(char* c=result; *c; c++) + *c=(char)toupper((unsigned char)*c); + return result; +} + void fix_line_breaks(char *str, size_t& length) { //_asm int 3; const char* const eob=str+length; @@ -881,56 +890,6 @@ const char* hex_string(unsigned char* by return bytes_hex; } -/// must be last in this file -#undef vsnprintf -int __vsnprintf(char* b, size_t s, const char* f, va_list l) { - if(!s) - return 0; - - int r; - // note: on win32 & maybe somewhere else - // vsnprintf do not writes terminating 0 in 'buffer full' case, reducing - // http://stackoverflow.com/questions/2915672/snprintf-and-visual-studio-2010 - --s; - - // clients do not check for negative 's', feature: ignore such prints - if((ssize_t)s<0) - return 0; - -#ifdef _MSC_VER - // win32: if the number of bytes to write exceeds buffer, then count bytes are written and -1 is returned - r=_vsnprintf(b, s, f, l); - if(r<0) - r=s; -#else - r=vsnprintf(b, s, f, l); - /* - solaris: man vsnprintf - - The snprintf() function returns the number of characters - formatted, that is, the number of characters that would have - been written to the buffer if it were large enough. If the - value of n is 0 on a call to snprintf(), an unspecified - value less than 1 is returned. - */ - - if(r<0) - r=0; - else if((size_t)r>s) - r=s; -#endif - b[r]=0; - return r; -} - -int __snprintf(char* b, size_t s, const char* f, ...) { - va_list l; - va_start(l, f); - int r=__vsnprintf(b, s, f, l); - va_end(l); - return r; -} - /* mime64 functions are from libgmime[http://spruce.sourceforge.net/gmime/] lib */ /* * Authors: Michael Zucchi @@ -1287,7 +1246,7 @@ inline void CalcCrc32(const unsigned cha } -const unsigned long pa_crc32(const char *in, size_t in_size){ +unsigned long pa_crc32(const char *in, size_t in_size){ unsigned long crc32=0xFFFFFFFF; InitCrc32Table(); @@ -1310,7 +1269,7 @@ static void file_crc32_file_action(struc } } -const unsigned long pa_crc32(const String& file_spec){ +unsigned long pa_crc32(const String& file_spec){ unsigned long crc32=0xFFFFFFFF; file_read_action_under_lock(file_spec, "crc32", file_crc32_file_action, &crc32); return ~crc32; @@ -1321,10 +1280,7 @@ const unsigned long pa_crc32(const Strin // content-type: xxx; charset="WE-NEED-THIS"; Charset* detect_charset(const char* content_type){ if(content_type){ - char* CONTENT_TYPE=pa_strdup(content_type); - - for(char *p=CONTENT_TYPE; *p; p++) - *p=(char)toupper((unsigned char)*p); + char* CONTENT_TYPE=str_upper(content_type); if(const char* begin=strstr(CONTENT_TYPE, "CHARSET=")){ begin+=8; // skip "CHARSET=" @@ -1340,7 +1296,7 @@ Charset* detect_charset(const char* cont if(end) *end=0; // terminator - return *begin?&charsets.get(begin):0; + return *begin ? &pa_charsets.get_direct(begin) : 0; } } return 0; @@ -1367,7 +1323,7 @@ const char *pa_idna_encode(const char *i String::C sIn(in,strlen(in)); if(!source_charset.isUTF8()) - sIn=Charset::transcode(sIn, source_charset, UTF8_charset); + sIn=Charset::transcode(sIn, source_charset, pa_UTF8_charset); int status=pa_convertUTF8toUTF32((const UTF8**)&sIn.str, (const UTF8*)(sIn.str+sIn.length), &utf32_end, utf32+MAX_IDNA_LENGTH-1, strictConversion); if(status != conversionOK) @@ -1408,7 +1364,57 @@ const char *pa_idna_decode(const char *i *result_end='\0'; if(!asked_charset.isUTF8()) - result = (char *)Charset::transcode(result, UTF8_charset, asked_charset).cstr(); + result = (char *)Charset::transcode(result, pa_UTF8_charset, asked_charset).cstr(); return result; } +/// must be last in this file +#undef vsnprintf +int pa_vsnprintf(char* b, size_t s, const char* f, va_list l) { + if(!s) + return 0; + + int r; + // note: on win32 & maybe somewhere else + // vsnprintf do not writes terminating 0 in 'buffer full' case, reducing + // http://stackoverflow.com/questions/2915672/snprintf-and-visual-studio-2010 + --s; + + // clients do not check for negative 's', feature: ignore such prints + if((ssize_t)s<0) + return 0; + +#ifdef _MSC_VER + // win32: if the number of bytes to write exceeds buffer, then count bytes are written and -1 is returned + r=_vsnprintf(b, s, f, l); + if(r<0) + r=s; +#else + r=vsnprintf(b, s, f, l); + /* + solaris: man vsnprintf + + The snprintf() function returns the number of characters + formatted, that is, the number of characters that would have + been written to the buffer if it were large enough. If the + value of n is 0 on a call to snprintf(), an unspecified + value less than 1 is returned. + */ + + if(r<0) + r=0; + else if((size_t)r>s) + r=s; +#endif + b[r]=0; + return r; +} + +int pa_snprintf(char* b, size_t s, const char* f, ...) { + va_list l; + va_start(l, f); + int r=pa_vsnprintf(b, s, f, l); + va_end(l); + return r; +} +