--- parser3/src/main/pa_common.C 2001/11/14 13:23:50 1.91 +++ parser3/src/main/pa_common.C 2026/09/30 16:44:20 1.348 @@ -1,503 +1,619 @@ /** @file Parser: commonly functions. - Copyright(c) 2001 ArtLebedev Group(http://www.artlebedev.com) - Author: Alexander Petrosyan (http://paf.design.ru) - - $Id: pa_common.C,v 1.91 2001/11/14 13:23:50 paf Exp $ + Copyright (c) 2000-2026 Art. Lebedev Studio (https://www.artlebedev.com) + Authors: Konstantin Morshnev , Alexandr Petrosian */ #include "pa_common.h" -#include "pa_types.h" #include "pa_exception.h" -#include "pa_pool.h" +#include "pa_inline_hash.h" #include "pa_globals.h" -#include "pa_value.h" -#include "pa_hash.h" -#include "pa_string.h" +#include "pa_charsets.h" +#include "pa_request_charsets.h" +#include "pa_file.h" +#include "pa_stack.h" -#ifndef _O_TEXT -# define _O_TEXT 0 -#endif -#ifndef _O_BINARY -# define _O_BINARY 0 -#endif -#ifndef O_TRUNC -# define O_TRUNC 0 -#endif +#include "pa_idna.h" +#include "pa_convert_utf.h" + +volatile const char * IDENT_PA_COMMON_C="$Id: pa_common.C,v 1.348 2026/09/30 16:44:20 moko Exp $" IDENT_PA_COMMON_H IDENT_PA_HASH_H IDENT_PA_INLINE_HASH_H IDENT_PA_ARRAY_H IDENT_PA_STACK_H; + +// defines for globals + +#define FILE_STATUS_NAME "status" + +// globals + +const String file_status_name(FILE_STATUS_NAME); + +String sql_bind_name(SQL_BIND_NAME); +String sql_limit_name(PA_SQL_LIMIT_NAME); +String sql_offset_name(PA_SQL_OFFSET_NAME); +String sql_default_name(SQL_DEFAULT_NAME); +String sql_distinct_name(SQL_DISTINCT_NAME); +String sql_value_type_name(SQL_VALUE_TYPE_NAME); -static char *strnchr(char *buf, size_t size, char c) { - for(; size-->0; buf++) { - if(*buf==c) - return buf; +bool capitalized(const char* s){ + bool upper=true; + for(const char* c=s; *c; c++){ + if(*c != (upper ? toupper((unsigned char)*c) : tolower((unsigned char)*c))) + return false; + upper=strchr("-_ ", *c) != 0; } + return true; +} - return 0; +const char* capitalize(const char* s){ + if(!s || capitalized(s)) + return s; + + char* result=pa_strdup(s); + if(result){ + bool upper=true; + for(char* c=result; *c; c++){ + *c=upper ? (char)toupper((unsigned char)*c) : (char)tolower((unsigned char)*c); + upper=strchr("-_ ", *c) != 0; + } + } + return (const char*)result; } -void fix_line_breaks(char *buf, size_t& size) { +char *str_lower(const char *s, size_t length){ + char *result=pa_strdup(s, length); + for(char* c=result; *c; c++) + *c=(char)tolower((unsigned char)*c); + return result; +} + +char *str_upper(const char *s, size_t length){ + char *result=pa_strdup(s, 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=buf+size; - char *dest=buf; + const char* const eob=str+length; + char* dest=str; // fix DOS: \r\n -> \n // fix Macintosh: \r -> \n - char *bol=buf; - while(char *eol=strnchr(bol, eob -bol, '\r')) { + char* bol=str; + while(char* eol=(char*)memchr(bol, '\r', eob -bol)) { size_t len=eol-bol; if(dest!=bol) - memcpy(dest, bol, len); + memmove(dest, bol, len); dest+=len; - *dest++='\n'; + *dest++='\n'; - if(&eol[1]=0 && - stat(fname, &finfo)==0) { - /*if(exclusive) - flock(f, LOCK_EX);*/ - size_t max_size=limit?min(offset+limit, finfo.st_size)-offset:finfo.st_size; - int read_size; - if(!max_size) { // eof - if(as_text) { - data=pool.malloc(1); - *(char*)data=0; - } else - data=0; - read_size=0; - } else { - data=pool.malloc(max_size+(as_text?1:0), 3); - if(offset) - lseek(f, offset, SEEK_SET); - read_size=read(f, data, max_size); - } - /*if(exclusive) - flock(f, LOCK_UN);*/ - close(f); - if(!max_size) // eof - return true; - - if(read_size<0 || read_size>max_size) - throw Exception(0, 0, - &file_spec, - "read failed: actually read %d bytes count not in [0..%lu] valid range", - read_size, (unsigned long)max_size); //never - - data_size=read_size; - if(as_text) { - fix_line_breaks((char *)data, data_size); - // note: after fixing - ((char*&)data)[data_size]=0; - } - return true; - } - if(fail_on_read_problem) - throw Exception(0, 0, - &file_spec, - "read failed: %s (%d), actual filename '%s'", - strerror(errno), errno, fname); - return false; -} - -static void create_dir_for_file(const String& file_spec) { - size_t pos_after=1; - int pos_before; - while((pos_before=file_spec.pos("/", 1, pos_after))>=0) { - mkdir(file_spec.mid(0, pos_before).cstr(String::UL_FILE_SPEC), 0775); - pos_after=pos_before+1; - } + memmove(dest, bol, eob-bol); + str[length]=0; // terminating } -void file_write(Pool& pool, - const String& file_spec, - const void *data, size_t size, - bool as_text, - bool do_append/*, - bool exclusive*/) { - const char *fname=file_spec.cstr(String::UL_FILE_SPEC); - int f; - if(access(fname, W_OK)!=0) // no - create_dir_for_file(file_spec); - - if((f=open(fname, - O_CREAT|O_RDWR - |(as_text?_O_TEXT:_O_BINARY) - |(do_append?O_APPEND:O_TRUNC), 0666))>=0) { - /*if(exclusive) - flock(f, LOCK_EX);*/ - - if(size) write(f, data, size); -#if O_TRUNC==0 - ftruncate(f, size); -#endif - /*if(exclusive) - flock(f, LOCK_UN);*/ - close(f); - } else - throw Exception(0, 0, - &file_spec, - "write failed: %s (%d), actual filename '%s'", - strerror(errno), errno, fname); -} - -// throws nothing! [this is required in file_move & file_delete] -static void rmdir(const String& file_spec, size_t pos_after) { - int pos_before; - if((pos_before=file_spec.pos("/", 1, pos_after))>=0) - rmdir(file_spec, pos_before+1); - - rmdir(file_spec.mid(0, pos_after-1/* / */).cstr(String::UL_FILE_SPEC)); -} -void file_delete(Pool& pool, const String& file_spec) { - const char *fname=file_spec.cstr(String::UL_FILE_SPEC); - if(unlink(fname)!=0) - throw Exception(0, 0, - &file_spec, - "unlink failed: %s (%d), actual filename '%s'", - strerror(errno), errno, fname); - - rmdir(file_spec, 1); -} -void file_move(Pool& pool, const String& old_spec, const String& new_spec) { - const char *old_spec_cstr=old_spec.cstr(String::UL_FILE_SPEC); - const char *new_spec_cstr=new_spec.cstr(String::UL_FILE_SPEC); - - create_dir_for_file(new_spec); - - if(rename(old_spec_cstr, new_spec_cstr)!=0) - throw Exception(0, 0, - &old_spec, - "rename failed: %s (%d), actual filename '%s' to '%s'", - strerror(errno), errno, old_spec_cstr, new_spec_cstr); - - rmdir(old_spec, 1); -} - - -static bool entry_readable(const String& file_spec, bool need_dir) { - const char *fname=file_spec.cstr(String::UL_FILE_SPEC); - struct stat finfo; - if(access(fname, R_OK)==0 && stat(fname, &finfo)==0) { - bool is_dir=finfo.st_mode&S_IFDIR != 0; - return is_dir==need_dir; - } - return false; -} -bool file_readable(const String& file_spec) { - return entry_readable(file_spec, false); -} -bool dir_readable(const String& file_spec) { - return entry_readable(file_spec, true); -} -String *file_readable(const String& path, const String& name) { - String *result=new(path.pool()) String(path); - *result << "/"; - *result << name; - return file_readable(*result)?result:0; -} -bool file_executable(const String& file_spec) { - return access(file_spec.cstr(String::UL_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) { - Pool& pool=file_spec.pool(); - const char *fname=file_spec.cstr(String::UL_FILE_SPEC); - struct stat finfo; - if(stat(fname, &finfo)!=0) - if(fail_on_read_problem) - throw Exception(0, 0, - &file_spec, - "getting file size failed: %s (%d), real filename '%s'", - strerror(errno), errno, fname); - else - return false; - rsize=finfo.st_size; - ratime=finfo.st_atime; - rmtime=finfo.st_mtime; - rctime=finfo.st_ctime; - return true; -} +/** + scans for @a delim[default \n] in @a *row_ref, + @return piece of line before it or end of string, if no @a delim found + assigns @a *row_ref to point right after delimiter if there were one + or to zero if no @a delim were found. +*/ -char *getrow(char **row_ref, char delim) { - char *result=*row_ref; - if(result) { - *row_ref=strchr(result, delim); +char* getrow(char* *row_ref, char delim) { + char* result=*row_ref; + if(result) { + *row_ref=strchr(result, delim); if(*row_ref) *((*row_ref)++)=0; else if(!*result) return 0; - } - return result; + } + return result; } -char *lsplit(char *string, char delim) { - if(string) { - char *v=strchr(string, delim); +const char* strrpbrk(const char* string, const char* chars) { + if(string) { + const char* pos=string+strlen(string); + while(pos!=string) { + if(strchr(chars, *--pos)) + return pos; + } + } + return NULL; +} + +char* lsplit(char* string, char delim) { + if(string) { + char* v=strchr(string, delim); if(v) { *v=0; return v+1; } - } - return 0; + } + return 0; } -char *lsplit(char **string_ref, char delim) { - char *result=*string_ref; - char *next=lsplit(*string_ref, delim); - *string_ref=next; - return result; +char* lsplit(char* *string_ref, char delim) { + char* result=*string_ref; + char* next=lsplit(*string_ref, delim); + *string_ref=next; + return result; } -char *rsplit(char *string, char delim) { - if(string) { - char *v=strrchr(string, delim); +char* rsplit(char* string, char delim) { + if(string) { + char* v=strrchr(string, delim); if(v) { *v=0; return v+1; } - } - return NULL; + } + return NULL; } -/// @todo less stupid type detection -char *format(Pool& pool, double value, char *fmt) { - char *result=(char *)pool.malloc(MAX_NUMBER, 4); - if(fmt) - if(strpbrk(fmt, "diouxX")) - if(strpbrk(fmt, "ouxX")) - snprintf(result, MAX_NUMBER, fmt, (uint)value ); - else - snprintf(result, MAX_NUMBER, fmt, (int)value ); - else - snprintf(result, MAX_NUMBER, fmt, value); - else - snprintf(result, MAX_NUMBER, "%d", (int)value); - +char* rsplit(char* string, const char* delims) { + if(char* v=strrpbrk(string, delims)) { + *v=0; + return v+1; + } + return NULL; +} + +void pa_strncpy(char *dst, const char *src, size_t dst_size){ + size_t left = dst_size; + + if (left != 0 && src) { + while (--left != 0) { + if ((*dst++ = *src++) == '\0') + return; + } + } + if (dst_size != 0) + *dst = '\0'; +} + +#define STRCAT_STEP(str, len) if(str) { memcpy(ptr, str, len); ptr += len; } + +char *pa_strcat(const char *a, const char *b, const char *c) { + size_t len_a = a ? strlen(a) : 0; + size_t len_b = b ? strlen(b) : 0; + size_t len_c = c ? strlen(c) : 0; + char *result=new(PointerFreeGC) char[len_a + len_b + len_c +1/*0*/]; + char *ptr=result; + STRCAT_STEP(a, len_a); + STRCAT_STEP(b, len_b); + STRCAT_STEP(c, len_c); + *ptr='\0'; return result; } + size_t stdout_write(const void *buf, size_t size) { #ifdef WIN32 + size_t to_write = size; do{ - int chunk_written=fwrite(buf, 1, min(8*0x400, size), stdout); + int chunk_written=fwrite(buf, 1, min((size_t)8*0x400, size), stdout); if(chunk_written<=0) break; size-=chunk_written; buf=((const char*)buf)+chunk_written; } while(size>0); - return size; + fflush(stdout); + return to_write-size; #else - return fwrite(buf, 1, size, stdout); + size_t result=fwrite(buf, 1, size, stdout); + fflush(stdout); + return result; #endif } -char *unescape_chars(Pool& pool, const char *cp, int len) { - char *s=(char *)pool.malloc(len + 1, 5); - enum EscapeState { - EscapeRest, - EscapeFirst, - EscapeSecond - } escapeState=EscapeRest; - int escapedValue=0; +enum EscapeState { + EscapeRest, + EscapeFirst, + EscapeSecond, + EscapeUnicode +}; + +// @todo prescan for reduce required size (unescaped sting in 1 byte charset requires less memory usually) +char* unescape_chars(const char* cp, int len, Charset* charset, bool js){ + char* s=new(PointerFreeGC) char[len+1]; // must be enough (%uXXXX==6 bytes, max utf-8 char length==6 bytes) + XMLByte* dst=(XMLByte *)s; + EscapeState escapeState=EscapeRest; + uint escapedValue=0; int srcPos=0; - int dstPos=0; - while(srcPos < len) { - int ch=cp[srcPos]; - switch(escapeState) { - case EscapeRest: - if(ch=='%') { - escapeState=EscapeFirst; - } else if(ch=='+') { - s[dstPos++]=' '; - } else { - s[dstPos++]=ch; + short int jsCnt=0; + while(srcPosstore_Char(dst, (XMLCh)escapedValue, '?'); + escapeState=EscapeRest; + } + } else { + // not full unicode value + escapeState=EscapeRest; + } + break; } - break; - case EscapeFirst: - escapedValue=hex_value[ch] << 4; - escapeState=EscapeSecond; - break; - case EscapeSecond: - escapedValue +=hex_value[ch]; - s[dstPos++]=escapedValue; - escapeState=EscapeRest; - break; } + srcPos++; } - s[dstPos]=0; + + *dst=0; // zero-termination return s; } -/// used by attributed_meaning_to_string / append_attribute_subattribute -struct Attributed_meaning_info { - String *header; // header line being constructed - String::Untaint_lang lang; // language in which to append to that line -}; -static void append_attribute_subattribute(const Hash::Key& akey, Hash::Val *avalue, - void *info) { - if(akey==VALUE_NAME) - return; - - Attributed_meaning_info& ami=*static_cast(info); - - // ...; charset=windows1251 - *ami.header << "; "; - ami.header->append(akey, ami.lang); - *ami.header << "="; - ami.header->append(static_cast(avalue)->as_string(), ami.lang); -} -const String& attributed_meaning_to_string(Value& meaning, - String::Untaint_lang lang) { - String &result=*new(meaning.pool()) String(meaning.pool()); - if(Hash *hash=meaning.get_hash(0)) { - // $value(value) $subattribute(subattribute value) - if(Value *value=static_cast(hash->get(*value_name))) - result.append(value->as_string(), lang, true); - - Attributed_meaning_info attributed_meaning_info={ - &result, - lang - }; - hash->for_each(append_attribute_subattribute, &attributed_meaning_info); - } else // result value - result.append(meaning.as_string(), lang, true); +char *search_stop(char*& current, char cstop_at) { + // sanity check + if(!current) + return 0; + // skip leading WS + while(*current==' ' || *current=='\t') + current++; + if(!*current) + return current=0; + + char *result=current; + if(char *pstop_at=strchr(current, cstop_at)) { + *pstop_at=0; + current=pstop_at+1; + } else + current=0; return result; } -#ifdef WIN32 -void back_slashes_to_slashes(char *s) { - if(s) - for(; *s; s++) - if(*s=='\\') - *s='/'; -} -/* -void slashes_to_back_slashes(char *s) { - if(s) - for(; *s; s++) - if(*s=='/') - *s='\\'; +char* backslashes_to_slashes(const char* path) { + if(!path) + return 0; + char* result=pa_strdup(path); + for(char* s=result; *s; s++) + if(*s=='\\') + *s='/'; + return result; } -*/ -#endif -bool StrEqNc(const char *s1, const char *s2, bool strict) { - while(true) { - if(!(*s1)) { - if(!(*s2)) - return true; - else - return !strict; - } else if(!(*s2)) - return !strict; - if(isalpha(*s1)) { - if(tolower(*s1) !=tolower(*s2)) - return false; - } else if((*s1) !=(*s2)) - return false; - s1++; - s2++; +const String& backslashes_to_slashes(const String& path) { + size_t pos=path.pos('\\'); + if(pos==STRING_NOT_FOUND) + return path; + String& result=*new String; + size_t from=0; + do { + result << path.mid(from, pos) << "/"; + from=pos+1; + } while((pos=path.pos('\\', from))!=STRING_NOT_FOUND); + result << path.mid(from, path.length()); + return result; +} + +size_t strpos(const char *str, const char *substr) { + const char *p = strstr(str, substr); + return (p==0)?STRING_NOT_FOUND:p-str; +} + +size_t remove_crlf(char* start, char* end) { + char* from=start; + char* to=start; + bool skip=false; + while(from < end){ + switch(*from){ + case '\n': + case '\r': + case '\t': + case ' ': + if(!skip){ + *to=' '; + to++; + skip=true; + } + break; + default: + if(from != to) + *to=*from; + to++; + skip=false; + } + from++; } + return to-start; } -static bool isLeap(int year) { - return !( - (year % 4) || ((year % 400) && !(year % 100)) - ); -} - -int getMonthDays(int year, int month) { - int monthDays[]={ - 31, - isLeap(year) ? 29 : 28, - 31, - 30, - 31, - 30, - 31, - 31, - 30, - 31, - 30, - 31 - }; - return monthDays[month]; -} - -void remove_crlf(char *start, char *end) { - for(char *p=start; phh*/+1/*for zero-teminator*/]; + unsigned char *src=bytes; + unsigned char *end=bytes+size; + char *dest=bytes_hex; + + const char *hex=upcase? hex_digits : "0123456789abcdef"; + + for(; src 0; j--) + { + if(dwCrc & 1) + dwCrc = (dwCrc >> 1) ^ dwPolynomial; + else + dwCrc >>= 1; + } + crc32Table[i] = dwCrc; + } + } +} + +inline void CalcCrc32(const unsigned char byte, unsigned long &crc32) +{ + crc32 = ((crc32) >> 8) ^ crc32Table[(byte) ^ ((crc32) & 0x000000FF)]; +} + + +unsigned long pa_crc32(const char *in, size_t in_size){ + unsigned long crc32=0xFFFFFFFF; + + InitCrc32Table(); + for(size_t i = 0; i(context); + if(finfo.st_size) { + InitCrc32Table(); + int nCount=0; + do { + unsigned char buffer[FILE_BUFFER_SIZE]; + nCount = file_block_read(f, buffer, sizeof(buffer)); + for(int i = 0; i < nCount; i++) CalcCrc32(buffer[i], crc32); + } while(nCount > 0); + } +} + +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; +} + +// content-type: xxx; charset=WE-NEED-THIS +// content-type: xxx; charset="WE-NEED-THIS" +// content-type: xxx; charset="WE-NEED-THIS"; +Charset* detect_charset(const char* content_type){ + if(content_type){ + char* CONTENT_TYPE=str_upper(content_type); + + if(const char* begin=strstr(CONTENT_TYPE, "CHARSET=")){ + begin+=8; // skip "CHARSET=" + char* end=0; + if(*begin && (*begin=='"' || *begin =='\'')){ + char quote=*begin; + begin++; + end=(char*)strchr(begin, quote); + } + if(!end) + end=(char*)strchr(begin, ';'); + + if(end) + *end=0; // terminator + + return *begin ? &pa_charsets.get_direct(begin) : 0; } + } + return 0; +} + +const UTF16* pa_utf16_encode(const char* in, Charset& source_charset){ + if(!in) + return 0; + + String::C sIn(in,strlen(in)); + + UTF16* utf16=(UTF16*)pa_malloc_atomic(sIn.length*2+2); + UTF16* utf16_end=utf16; + + if(!source_charset.isUTF8()) + sIn=Charset::transcode(sIn, source_charset, pa_UTF8_charset); + + int status=pa_convertUTF8toUTF16((const UTF8**)&sIn.str, (const UTF8*)(sIn.str+sIn.length), &utf16_end, utf16+sIn.length, strictConversion); + if(status != conversionOK) + throw Exception("utf-16 encode", new String(in), "utf-16 conversion failed (%d)", status); + + *utf16_end=0; + + return utf16; +} + +const char* pa_utf16_decode(const UTF16* in, Charset& asked_charset){ + if(!in) + return 0; + + const UTF16* utf16_start=in; + const UTF16* utf16_end; + + for(utf16_end=in; *utf16_end; utf16_end++); + + char *result = (char *)pa_malloc_atomic((utf16_end-in)*6+1); + char *result_end = result; + + int status=pa_convertUTF16toUTF8(&utf16_start, utf16_end, (UTF8**)&result_end, (UTF8*)(result+(utf16_end-in)*6), strictConversion); + + if(status != conversionOK) + throw Exception("utf-16 decode", 0, "utf conversion failed (%d)", status); + + *result_end='\0'; + + if(asked_charset.isUTF8()) + return result; + + return Charset::transcode(result, pa_UTF8_charset, asked_charset).cstr(); +} + +static bool is_latin(const char *in){ + for(; *in; in++){ + if ((unsigned char)(*in) > 0x7F) + return false; + } + return true; +} + +#define MAX_IDNA_LENGTH 256 + +const char *pa_idna_encode(const char *in, Charset& source_charset){ + if(!in || is_latin(in)) + return in; + + uint32_t utf32[MAX_IDNA_LENGTH]; + uint32_t *utf32_end=utf32; + + String::C sIn(in,strlen(in)); + + if(!source_charset.isUTF8()) + 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) + throw Exception("idna encode", new String(in), "utf conversion failed (%d)", status); + + *utf32_end=0; + + char *result = (char *)pa_malloc(MAX_IDNA_LENGTH); + status=pa_idna_to_ascii_4z(utf32, result, MAX_IDNA_LENGTH, 0); + if(status != IDNA_SUCCESS) + throw Exception("idna encode", new String(in), "encode failed: %s", pa_idna_strerror(status)); + + return result; } +const char *pa_idna_decode(const char *in, Charset &asked_charset){ + if(!in || !(*in)) + return in; + + uint32_t utf32[MAX_IDNA_LENGTH]; + const uint32_t *utf32_start=utf32; + uint32_t *utf32_end; + + int status=pa_idna_to_unicode_4z(in, utf32, MAX_IDNA_LENGTH, 0); + if(status != IDNA_SUCCESS) + throw Exception("idna decode", new String(in), "decode failed: %s", pa_idna_strerror(status)); + + for(utf32_end=utf32; *utf32_end; utf32_end++); + + char *result = (char *)pa_malloc(MAX_IDNA_LENGTH); + char *result_end = result; + + status=pa_convertUTF32toUTF8(&utf32_start, utf32_end, (UTF8**)&result_end, (UTF8*)(result+MAX_IDNA_LENGTH-1), strictConversion); + if(status != conversionOK) + throw Exception("idna decode", new String(in), "utf conversion failed (%d)", status); + + *result_end='\0'; + + if(!asked_charset.isUTF8()) + result = (char *)Charset::transcode(result, pa_UTF8_charset, asked_charset).cstr(); + + return result; +} /// must be last in this file #undef vsnprintf -int __vsnprintf(char *b, size_t s, const char *f, va_list l) { +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 + // 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; -#if _MSC_VER - /* - win32: - mk:@MSITStore:C:\Program%20Files\Microsoft%20Visual%20Studio\MSDN\2001APR\1033\vccore.chm::/html/_crt__vsnprintf.2c_._vsnwprintf.htm - if the number of bytes to write exceeds buffer, then count bytes are written and –1 is returned - */ - r=_vsnprintf(b, s, f, l); + // 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); + r=vsnprintf(b, s, f, l); /* - solaris: - man vsnprintf + solaris: man vsnprintf - The snprintf() function returns the number of characters + 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 @@ -506,17 +622,17 @@ int __vsnprintf(char *b, size_t s, const if(r<0) r=0; - else if(r>s) + else if((size_t)r>s) r=s; #endif b[r]=0; return r; } -int __snprintf(char *b, size_t s, const char *f, ...) { +int pa_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); + va_start(l, f); + int r=pa_vsnprintf(b, s, f, l); + va_end(l); return r; }