--- parser3/src/main/pa_common.C 2019/11/22 22:37:30 1.308 +++ parser3/src/main/pa_common.C 2023/11/16 00:40:42 1.328 @@ -1,8 +1,8 @@ /** @file Parser: commonly functions. - Copyright (c) 2000-2017 Art. Lebedev Studio (http://www.artlebedev.com) - Author: Alexandr Petrosian (http://paf.design.ru) + Copyright (c) 2000-2023 Art. Lebedev Studio (http://www.artlebedev.com) + Authors: Konstantin Morshnev , Alexandr Petrosian */ #include "pa_common.h" @@ -29,7 +29,7 @@ #define pa_mkdir(path, mode) mkdir(path, mode) #endif -volatile const char * IDENT_PA_COMMON_C="$Id: pa_common.C,v 1.308 2019/11/22 22:37:30 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.328 2023/11/16 00:40:42 moko Exp $" IDENT_PA_COMMON_H IDENT_PA_HASH_H IDENT_PA_ARRAY_H IDENT_PA_STACK_H; // some maybe-undefined constants @@ -91,16 +91,6 @@ FILE *pa_fopen(const char *pathname, con #endif -char* file_read_text(Request_charsets& charsets, const String& file_spec, bool fail_on_read_problem) { - File_read_result file=file_read(charsets, file_spec, true, 0, fail_on_read_problem, 0, 0, 0, true); - return file.success?file.str:0; -} - -char* file_load_text(Request& r, const String& file_spec, bool fail_on_read_problem, HashStringValue* params, bool transcode_result) { - File_read_result file=file_load(r, file_spec, true, params, fail_on_read_problem, 0, 0, 0, transcode_result); - return file.success?file.str:0; -} - /// these options were handled but not checked elsewhere, now check them int pa_get_valid_file_options_count(HashStringValue& options) { int result=0; @@ -120,62 +110,68 @@ int pa_get_valid_file_options_count(Hash #ifndef DOXYGEN struct File_read_action_info { char **data; size_t *data_size; - char* buf; size_t offset; size_t count; + char* buf; uint64_t offset; size_t limit; }; #endif static void file_read_action(struct stat& finfo, int f, const String& file_spec, void *context) { - File_read_action_info& info=*static_cast(context); - size_t to_read_size=info.count; - if(!to_read_size) - to_read_size=check_file_size(finfo.st_size, file_spec); + File_read_action_info& info = *static_cast(context); + size_t to_read_size = check_file_size(info.limit && info.limit < (size_t)finfo.st_size ? info.limit : (size_t)finfo.st_size, &file_spec); if(to_read_size) { if(info.offset) - lseek(f, info.offset, SEEK_SET); - *info.data=info.buf ? info.buf : (char *)pa_malloc_atomic(to_read_size+1); - ssize_t result=read(f, *info.data, to_read_size); + pa_lseek(f, info.offset, SEEK_SET); // seek never fails as POSIX allows the file offset to be set beyond the EOF + *info.data = info.buf ? info.buf : (char *)pa_malloc_atomic(to_read_size+1); + ssize_t result = read(f, *info.data, to_read_size); if(result<0) throw Exception("file.read", &file_spec, "read failed: %s (%d)", strerror(errno), errno); - *info.data_size=result; + *info.data_size = result; } else { // empty file // for both, text and binary: for text we need that terminator, for binary we need nonzero pointer to be able to save such files - *info.data=(char *)pa_malloc_atomic(1); - *(char*)(*info.data)=0; - *info.data_size=0; + *info.data = (char *)pa_malloc_atomic(1); + *(char*)(*info.data) = 0; + *info.data_size = 0; return; } } +File_read_result file_read_binary(const String& file_spec, bool fail_on_read_problem, char* buf, uint64_t offset, size_t limit) { + File_read_result result = {false, 0, 0, 0}; + File_read_action_info info = {&result.str, &result.length, buf, offset, limit}; + + result.success = file_read_action_under_lock(file_spec, "read", file_read_action, &info, 0, fail_on_read_problem); + return result; +} + File_read_result file_read(Request_charsets& charsets, const String& file_spec, - bool as_text, HashStringValue *params, + bool as_text, HashStringValue *options, bool fail_on_read_problem, - char* buf, size_t offset, size_t count, bool transcode_text_result) { - File_read_result result={false, 0, 0, 0}; - if(params){ - int valid_options=pa_get_valid_file_options_count(*params); - if(valid_options!=params->count()) + size_t offset = 0, size_t limit = 0, bool transcode_text_result = true) { + File_read_result result = {false, 0, 0, 0}; + if(options){ + int valid_options = pa_get_valid_file_options_count(*options); + if(valid_options != options->count()) throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION); } - File_read_action_info info={&result.str, &result.length, buf, offset, count}; + File_read_action_info info = {&result.str, &result.length, 0, offset, limit}; - result.success=file_read_action_under_lock(file_spec, "read", file_read_action, &info, as_text, fail_on_read_problem); + result.success = file_read_action_under_lock(file_spec, "read", file_read_action, &info, as_text, fail_on_read_problem); if(as_text){ if(result.success){ - Charset* asked_charset=0; - if(params) - if(Value* vcharset_name=params->get(PA_CHARSET_NAME)) - asked_charset=&pa_charsets.get(vcharset_name->as_string()); + Charset* asked_charset = 0; + if(options) + if(Value* vcharset_name = options->get(PA_CHARSET_NAME)) + asked_charset = &pa_charsets.get(vcharset_name->as_string()); - asked_charset=pa_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); + String::C body = String::C(result.str, result.length); body=Charset::transcode(body, *asked_charset, charsets.source()); - result.str=const_cast(body.str); // hacking a little - result.length=body.length; + result.str = const_cast(body.str); // hacking a little + result.length = body.length; } } if(result.length) @@ -186,27 +182,43 @@ File_read_result file_read(Request_chars } File_read_result file_load(Request& r, const String& file_spec, - bool as_text, HashStringValue *params, + bool as_text, HashStringValue *options, bool fail_on_read_problem, - char* buf, size_t offset, size_t count, bool transcode_text_result) { + bool transcode_text_result) { + + size_t offset = 0; + size_t limit = 0; + + if(options){ + if(Value *voffset = (Value *)options->get(sql_offset_name)) + offset = r.process(*voffset).as_int(); + if(Value *vlimit = (Value *)options->get(sql_limit_name)) + limit = r.process(*vlimit).as_int(); + // no check on options count here + } - File_read_result result={false, 0, 0, 0}; if(file_spec.starts_with("http://")) { - if(offset || count) + if(offset || limit) throw Exception(PARSER_RUNTIME, 0, "offset and load options are not supported for HTTP:// file load"); // fail on read problem - File_read_http_result http=pa_internal_file_read_http(r, file_spec, as_text, params, transcode_text_result); - result.success=true; - result.str=http.str; - result.length=http.length; - result.headers=http.headers; + File_read_http_result http = pa_internal_file_read_http(r, file_spec, as_text, options, transcode_text_result); + + File_read_result result = {true, http.str, http.length, http.headers}; + return result; } else - result = file_read(r.charsets, file_spec, as_text, params, fail_on_read_problem, buf, offset, count, transcode_text_result); + return file_read(r.charsets, file_spec, as_text, options, fail_on_read_problem, offset, limit, transcode_text_result); +} - return result; +char* file_read_text(Request_charsets& charsets, const String& file_spec, bool fail_on_read_problem) { + File_read_result file = file_read(charsets, file_spec, true, 0, fail_on_read_problem); + return file.success ? file.str : 0; } +char* file_load_text(Request& r, const String& file_spec, bool fail_on_read_problem, HashStringValue* options, bool transcode_result) { + File_read_result file = file_load(r, file_spec, true, options, fail_on_read_problem, transcode_result); + return file.success ? file.str : 0; +} #ifdef PA_SAFE_MODE void check_safe_mode(struct stat finfo, const String& file_spec, const char* fname) { @@ -274,11 +286,13 @@ bool file_read_action_under_lock(const S } void create_dir_for_file(const String& file_spec) { - size_t pos_after=1; - size_t pos_before; - while((pos_before=file_spec.pos('/', pos_after))!=STRING_NOT_FOUND) { - pa_mkdir(file_spec.mid(0, pos_before).taint_cstr(String::L_FILE_SPEC), 0775); - pos_after=pos_before+1; + const char *str=file_spec.taint_cstr(String::L_FILE_SPEC); + if(str[0]){ + const char *pos=str+1; + while((pos=strchr(pos,'/')) && pos[1]) { // to avoid trailing /, see #1166 + pa_mkdir(pa_strdup(str,pos-str), 0775); + pos++; + } } } @@ -393,7 +407,19 @@ static size_t get_dir(char* fname, size_ return pos; } -static bool entry_readable(char* fname, bool need_dir) { +bool entry_exists(const char* fname, struct stat *afinfo) { + struct stat lfinfo; + bool result=pa_stat(fname, &lfinfo)==0; + if(afinfo) + *afinfo=lfinfo; + return result; +} + +bool entry_exists(const String& file_spec) { + return entry_exists(file_spec.taint_cstr(String::L_FILE_SPEC), 0); +} + +static bool entry_ifdir(char *fname, bool need_dir) { if(need_dir){ size_t size=strlen(fname); while(size) { @@ -406,15 +432,15 @@ static bool entry_readable(char* fname, } struct stat finfo; - if(access(fname, R_OK)==0 && entry_exists(fname, &finfo)) { + if(entry_exists(fname, &finfo)) { bool is_dir=(finfo.st_mode&S_IFDIR) != 0; return is_dir==need_dir; } return false; } -static bool entry_readable(const String& file_spec, bool need_dir) { - return entry_readable(file_spec.taint_cstrm(String::L_FILE_SPEC), need_dir); +static bool entry_ifdir(const String& file_spec, bool need_dir) { + return entry_ifdir(file_spec.taint_cstrm(String::L_FILE_SPEC), need_dir); } // throws nothing! [this is required in file_move & file_delete] @@ -423,7 +449,7 @@ static void rmdir(const String& file_spe size_t length=strlen(dir_spec); while( (length=get_dir(dir_spec, length)) && (length > pos_after) ){ #ifdef _MSC_VER - if(!entry_readable(dir_spec, true)) + if(!entry_ifdir(dir_spec, true)) break; DWORD attrs=GetFileAttributes(dir_spec); if( @@ -469,25 +495,12 @@ void file_move(const String& old_spec, c } -bool entry_exists(const char* fname, struct stat *afinfo) { - struct stat lfinfo; - bool result=pa_stat(fname, &lfinfo)==0; - if(afinfo) - *afinfo=lfinfo; - return result; -} - -bool entry_exists(const String& file_spec) { - const char* fname=file_spec.taint_cstr(String::L_FILE_SPEC); - return entry_exists(fname, 0); -} - bool file_exist(const String& file_spec) { - return entry_readable(file_spec, false); + return entry_ifdir(file_spec, false); } bool dir_exists(const String& file_spec) { - return entry_readable(file_spec, true); + return entry_ifdir(file_spec, true); } const String* file_exist(const String& path, const String& name) { @@ -518,9 +531,9 @@ bool file_stat(const String& file_spec, return true; } -size_t check_file_size(uint64_t size, const String& file_spec){ - if(size > pa_file_size_limit) - throw Exception(PARSER_RUNTIME, &file_spec, "content size of %.15g bytes exceeds the limit (%.15g bytes)", (double)size, (double)pa_file_size_limit); +size_t check_file_size(uint64_t size, const String* file_spec){ + if(size > (uint64_t)pa_file_size_limit) + throw Exception(PARSER_RUNTIME, file_spec, "content size of %.15g bytes exceeds the limit (%.15g bytes)", (double)size, (double)pa_file_size_limit); return (size_t)size; } @@ -553,15 +566,15 @@ 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); +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 helper_length){ - char *result=pa_strdup(s, helper_length); +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; @@ -638,9 +651,46 @@ char* rsplit(char* string, char delim) { return v+1; } } - return NULL; + return NULL; +} + +void pa_strncpy(char *dst, const char *src, size_t n){ + size_t left = n; + + if (left != 0 && src) { + while (--left != 0) { + if ((*dst++ = *src++) == '\0') + return; + } + } + if (n != 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; } +const char *pa_filename(const char *path){ + if(!path) + return NULL; + for(const char *c = path + strlen(path) - 1; c >= path; c--) { + if(*c == '/' || *c == '\\') + return c+1; + } + return path; +} // format: %[flags][width][.precision]type http://msdn.microsoft.com/ru-ru/library/56e442dc(en-us,VS.80).aspx // flags: '-', '+', ' ', '#', '0' http://msdn.microsoft.com/ru-ru/library/8aky45ct(en-us,VS.80).aspx @@ -715,12 +765,14 @@ const char* format(double value, const c case FormatDouble: size=snprintf(local_buf, sizeof(local_buf), fmt, value); break; + case FormatUInt: + if(value >= 0){ // on Apple M1 (uint) is 0 + size=snprintf(local_buf, sizeof(local_buf), fmt, (uint)value); + break; + } case FormatInt: size=snprintf(local_buf, sizeof(local_buf), fmt, (int)value); break; - case FormatUInt: - size=snprintf(local_buf, sizeof(local_buf), fmt, (uint)value); - break; case FormatInvalid: throw Exception(PARSER_RUNTIME, 0, "Incorrect format string '%s' was specified.", fmt); } @@ -738,16 +790,19 @@ size_t stdout_write(const void *buf, siz #ifdef WIN32 size_t to_write = size; do{ - int chunk_written=fwrite(buf, 1, min((size_t)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); + } while(size>0); + 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 } @@ -859,7 +914,7 @@ size_t strpos(const char *str, const cha return (p==0)?STRING_NOT_FOUND:p-str; } -int remove_crlf(char* start, char* end) { +size_t remove_crlf(char* start, char* end) { char* from=start; char* to=start; bool skip=false; @@ -905,8 +960,8 @@ const char* hex_string(unsigned char* by return bytes_hex; } -int file_block_read(const int f, unsigned char* buffer, const size_t size){ - int nCount = read(f, buffer, size); +ssize_t file_block_read(const int f, void* buffer, const size_t size){ + ssize_t nCount = read(f, buffer, size); if (nCount < 0) throw Exception("file.read", 0, "read failed: %s (%d)", strerror(errno), errno); return nCount;