static rc_t SaveToFile( struct KFile * f, const VNamelist * nl, const char * delim ) { uint32_t count; rc_t rc = VNameListCount ( nl, &count ); if ( rc == 0 && count > 0 ) { uint32_t idx; uint64_t pos = 0; for ( idx = 0; idx < count && rc == 0; ++idx ) { const char * s; rc = VNameListGet ( nl, idx, &s ); if ( rc == 0 && s != NULL ) { size_t num_writ; rc = KFileWriteAll ( f, pos, s, string_size ( s ), &num_writ ); if ( rc == 0 ) { pos += num_writ; rc = KFileWriteAll ( f, pos, delim, string_size ( delim ), &num_writ ); if ( rc == 0 ) pos += num_writ; } } } if ( rc == 0 ) rc = KFileSetSize ( f, pos ); } return rc; }
static bool pacbio_is_schema_dflt( const char * schema ) { size_t asize = string_size ( schema ); size_t bsize = string_size ( DFLT_SCHEMA ); uint32_t max_chars = ( asize > bsize ) ? asize : bsize; return ( string_cmp ( schema, asize, DFLT_SCHEMA, bsize, max_chars ) == 0 ); }
/* ----------------------------------------------------------------------------- */ static bool has_col( const VTable * tab, const char * colname ) { bool res = false; struct KNamelist * columns; rc_t rc = VTableListReadableColumns( tab, &columns ); if ( rc == 0 ) { uint32_t count; rc = KNamelistCount( columns, &count ); if ( rc == 0 && count > 0 ) { uint32_t idx; size_t colname_size = string_size( colname ); for ( idx = 0; idx < count && rc == 0 && !res; ++idx ) { const char * a_name; rc = KNamelistGet ( columns, idx, &a_name ); if ( rc == 0 ) { int cmp; size_t a_name_size = string_size( a_name ); uint32_t max_chars = ( uint32_t )colname_size; if ( a_name_size > max_chars ) max_chars = ( uint32_t )a_name_size; cmp = strcase_cmp ( colname, colname_size, a_name, a_name_size, max_chars ); res = ( cmp == 0 ); } } } KNamelistRelease( columns ); } return res; }
static int64_t CC sort_cmp (const void ** l, const void ** r, void * data) { /* KDirectory * d; */ uint64_t lz, rz; rc_t rc; /* d = data; */ /* lz = l; */ /* rz = r; */ rc = KDirectoryFileSize (data, &lz, *l); if (rc == 0) { rc = KDirectoryFileSize (data, &rz, *r); if (rc == 0) { int64_t zdiff; zdiff = lz - rz; if (zdiff != 0) return (zdiff < 0) ? -1 : 1; else { size_t lsz = string_size (*l); size_t rsz = string_size (*r); return string_cmp (*l, lsz, *r, rsz, lsz+1); } } } return 0; /* dunno just leave it... */ }
rc_t vds_append_str( p_dump_str s, const char *s1 ) { rc_t rc = 0; if ( ( s == NULL )||( s1 == NULL ) ) { rc = RC( rcVDB, rcNoTarg, rcInserting, rcParam, rcNull ); } else { size_t append_len = string_size( s1 ); if ( append_len > 0 ) { if ( ( s->str_limit > 0 )&&( s->str_len >= s->str_limit ) ) { s->truncated = true; } else { rc = vds_inc_buffer( s, append_len ); if ( rc == 0 ) { size_t l = string_size( s->buf ); string_copy( s->buf + l, s->buf_size - l, s1, append_len ); rc = vds_truncate( s ); /* adjusts str_len */ } } } } return rc; }
bool namelist_contains( const KNamelist *names, const char * a_name ) { bool res = false; uint32_t count; rc_t rc = KNamelistCount( names, &count ); if ( rc == 0 && count > 0 ) { uint32_t idx; size_t a_name_len = string_size( a_name ); for ( idx = 0; idx < count && rc == 0 && !res; ++idx ) { const char * s; rc = KNamelistGet( names, idx, &s ); if ( rc == 0 && s != NULL ) { size_t s_len = string_size( s ); size_t max_len = a_name_len > s_len ? a_name_len : s_len; int cmp = string_cmp( a_name, a_name_len, s, s_len, max_len ); if ( cmp == 0 ) res = true; } } } return res; }
/** * Processes the command-line options, filling in the members of the Options * structure corresponding to the switches */ int options_parse(Options *self, int argc, char *argv[]) { int arg = 1; while (arg < argc) { String s = string_from_c(argv[arg]); /* Debug: */ if (!strcmp(argv[arg], "-d") || !strcmp(argv[arg], "--debug")) { self->debug = 1; /* Output filename: */ } else if (!strcmp(argv[arg], "-o")) { ++arg; if (argc <= arg) return 0; self->name_out = string_from_c(argv[arg]); /* Output filename, smushed: */ } else if (2 == string_match(s, string_from_k("-o"))) { self->name_out = string(s.p + 2, s.end); /* Input filename: */ } else { if (string_size(self->name_in)) return 0; self->name_in = s; } ++arg; } if (!string_size(self->name_in)) return 0; return 1; }
/**************************************************************************************** splits an argument example: "/path/file=grp1" into path = "/path/file" and attribute = "grp1" or example: "/path/file" into path = "/path/file" and attribute = NULL ****************************************************************************************/ static rc_t split_argument( const char *argument, char ** path, char ** attribute, char delim ) { if ( argument == NULL || path == NULL || attribute == NULL ) return RC( rcApp, rcNoTarg, rcConstructing, rcParam, rcNull ); else { char * delim_ptr = string_chr ( argument, string_size ( argument ), delim ); if ( delim_ptr == NULL ) { *path = string_dup_measure( argument, NULL ); *attribute = NULL; } else { size_t len = string_size( argument ); size_t len1 = ( delim_ptr - argument ); *path = string_dup ( argument, len1 ); if ( delim_ptr < argument + len - 1 ) *attribute = string_dup ( delim_ptr + 1, len - ( len1 + 1 ) ); else *attribute = NULL; } } return 0; }
LIB_EXPORT rc_t CC XFS_StrEndsWith ( const char * Str, const char * End ) { uint32_t StrLen, EndLen; if ( Str == NULL || End == NULL ) { return false; } StrLen = string_len ( Str, string_size ( Str ) ); EndLen = string_len ( End, string_size ( End ) ); if ( StrLen >= EndLen && EndLen > 0 ) { return string_cmp ( Str + ( StrLen - EndLen ), EndLen, End, EndLen, EndLen ) == 0; } return false; } /* XFS_StrEndsWith () */
static int64_t DictionaryEntryFind ( const void *p_a, const BSTNode *p_b ) { const char * a = ( const char * ) p_a; DictionaryEntry* b = ( DictionaryEntry * ) p_b; size_t a_path_size = string_size ( a ); return string_cmp ( a, a_path_size, b -> path, string_size ( b -> path ), ( uint32_t ) a_path_size ); }
static int64_t DictionaryEntryCompare ( const BSTNode *p_a, const BSTNode *p_b ) { DictionaryEntry* a = ( DictionaryEntry * ) p_a; DictionaryEntry* b = ( DictionaryEntry * ) p_b; size_t a_path_size = string_size ( a -> path ); return string_cmp ( a -> path, a_path_size, b -> path, string_size ( b -> path ), ( uint32_t ) a_path_size ); }
app_action_t application_function_flash_receive(string_t *src, string_t *dst) { unsigned int chunk_offset, chunk_length; if(string_size(&flash_sector_buffer) < SPI_FLASH_SEC_SIZE) { string_format(dst, "ERROR flash-receive: flash sector buffer too small: %d\n", string_size(&flash_sector_buffer)); return(app_action_error); } if(parse_uint(1, src, &chunk_offset, 0, ' ') != parse_ok) { string_append(dst, "ERROR flash-receive: chunk offset required\n"); return(app_action_error); } if(parse_uint(2, src, &chunk_length, 0, ' ') != parse_ok) { string_append(dst, "ERROR flash-receive: chunk chunk_length required\n"); return(app_action_error); } if((chunk_length == 0) || ((chunk_offset % chunk_length) != 0)) { string_append(dst, "ERROR: flash-receive: chunk offset should be divisible by chunk size"); return(app_action_error); } if((chunk_length == 0) || ((SPI_FLASH_SEC_SIZE % chunk_length) != 0)) { string_append(dst, "ERROR: flash-receive: chunk length should be divisible by flash sector size"); return(app_action_error); } if((chunk_offset + chunk_length) > SPI_FLASH_SEC_SIZE) { string_format(dst, "ERROR flash-receive: chunk_length(%u) + chunk_offset(%u) > sector size(%d)\n", chunk_length, chunk_offset, SPI_FLASH_SEC_SIZE); return(app_action_error); } if((flash_sector_buffer_use != fsb_free) && (flash_sector_buffer_use != fsb_config_cache) && (flash_sector_buffer_use != fsb_ota)) { string_format(dst, "ERROR: flash_send: sector buffer in use: %u\n", flash_sector_buffer_use); return(app_action_error); } flash_sector_buffer_use = fsb_ota; string_format(dst, "OK flash-receive: sending bytes: %u, from offset: %u, data: @", chunk_length, chunk_offset); string_splice(dst, -1, &flash_sector_buffer, chunk_offset, chunk_length); string_append(dst, "\n"); if((chunk_offset + chunk_length) >= SPI_FLASH_SEC_SIZE) flash_sector_buffer_use = fsb_free; return(app_action_normal); }
static rc_t ConfigRepoSet(DLList* repos, KConfig * kfg, const char* kfgPath, const char *dflt, uint8_t type) { const KConfigNode *node; rc_t rc = KConfigOpenNodeRead ( kfg, & node, kfgPath ); if ( rc == 0 ) { KNamelist* children; rc = KConfigNodeListChild ( node, &children ); if ( rc == 0 ) { uint32_t count; rc = KNamelistCount ( children, &count ); if ( rc == 0 ) { uint32_t i; for (i = 0; i < count; ++i) { const char* name; rc = KNamelistGet ( children, i, &name ); if ( rc == 0 ) { #define BufSize 4096 char buf[ BufSize ]; size_t bSize = string_size(kfgPath); string_copy(buf, BufSize, kfgPath, bSize); if (bSize + string_size(name) < sizeof(buf)) { NCBIRepository* repo; string_copy(buf + bSize, sizeof(buf) - bSize, name, string_size(name) + 1); rc = ConfigRepo( kfg, dflt, buf, buf, type, &repo ); DLListPushTail( repos, (DLNode*) repo ); } #undef BufSize } else { rc = RC ( rcSRA, rcMgr, rcConstructing, rcString, rcExcessive ); } if ( rc != 0 ) { break; } } } KNamelistRelease ( children ); } KConfigNodeRelease ( node ); } if (GetRCState(rc) == rcNotFound) { return 0; } return rc; }
/** * Concatenates one string onto another. The new string is silently null- * terminated, and can be passed to C functions. */ String string_cat(Pool *pool, String s1, String s2) { size_t size = string_size(s1) + string_size(s2); char *out = (char*)pool_alloc(pool, size + 1, 1); memcpy(out, s1.p, string_size(s1)); memcpy(out + string_size(s1), s2.p, string_size(s2)); out[size] = 0; return string(out, out + size); }
/* * walks through the column-definitions and if a column * has the to_copy-flag set, and it's name is in the * given list of column-names the to copy-flag is cleared * does not require an open cursor. */ rc_t col_defs_exclude_these_columns( col_defs* defs, const char * prefix, const char * column_names ) { rc_t rc = 0; const KNamelist *names; if ( defs == NULL ) return RC( rcExe, rcNoTarg, rcResolving, rcSelf, rcNull ); /* it is OK if we have not column-names to exclude */ if ( column_names == NULL ) return 0; if ( column_names[0] == 0 ) return 0; rc = nlt_make_namelist_from_string( &names, column_names ); DISP_RC( rc, "col_defs_parse_string:nlt_make_namelist_from_string() failed" ); if ( rc == 0 ) { uint32_t idx, len = VectorLength( &(defs->cols) ); size_t prefix_len = 0; if ( prefix != 0 ) prefix_len = string_size( prefix ) + 2; for ( idx = 0; idx < len; ++idx ) { p_col_def col = (p_col_def) VectorGet ( &(defs->cols), idx ); if ( col != NULL ) { if ( col->requested ) { if ( nlt_is_name_in_namelist( names, col->name ) ) col->requested = false; else { if ( prefix != NULL ) { size_t len1 = string_size ( col->name ) + prefix_len; char * s = malloc( len1 ); if ( s != NULL ) { size_t num_writ; rc_t rc1 = string_printf ( s, len1, &num_writ, "%s:%s", prefix, col->name ); if ( rc1 == 0 ) { if ( nlt_is_name_in_namelist( names, s ) ) col->requested = false; } free( s ); } } } } } } KNamelistRelease( names ); } return rc; }
static int cmp_pchar( const char * a, const char * b ) { int res = 0; if ( ( a != NULL )&&( b != NULL ) ) { size_t len_a = string_size( a ); size_t len_b = string_size( b ); res = string_cmp ( a, len_a, b, len_b, ( len_a < len_b ) ? len_b : len_a ); } return res; }
static seq_id_node * make_seq_id_node( const char * seq_id, const char * name, INSDC_coord_len seq_len ) { seq_id_node *res = calloc( sizeof *res, 1 ); if ( res != NULL ) { res->name = string_dup( name, string_size( name ) ); res->seq_id = string_dup( seq_id, string_size( seq_id ) ); res->seq_len = seq_len; } return res; }
/** * Initialize string container with an exist sub string container. */ void string_init_copy_substring(string_t* pstr_dest, const string_t* cpstr_src, size_t t_pos, size_t t_len) { assert(pstr_dest != NULL); assert(cpstr_src != NULL); assert(t_pos < string_size(cpstr_src)); basic_string_init_copy_substring(pstr_dest, cpstr_src, t_pos, t_len); if(t_len != NPOS && t_pos + t_len <= string_size(cpstr_src)) { basic_string_push_back(pstr_dest, '\0'); } }
static bool string_ends_in( const char * str, const char * end ) { bool res = false; if ( str != NULL && end != NULL ) { uint32_t l_str = string_len( str, string_size( str ) ); uint32_t l_end = string_len( end, string_size( end ) ); if ( l_str >= l_end && l_end > 0 ) { const char * p = str + ( l_str - l_end ); res = ( string_cmp ( p, l_end, end, l_end, l_end ) == 0 ); } } return res; }
app_action_t application_function_flash_read(string_t *src, string_t *dst) { unsigned int address, sector; SHA_CTX sha_context; unsigned char sha_result[SHA_DIGEST_LENGTH]; string_new(, sha_string, SHA_DIGEST_LENGTH * 2 + 2); if(string_size(&flash_sector_buffer) < SPI_FLASH_SEC_SIZE) { string_format(dst, "ERROR flash-read: flash sector buffer too small: %d\n", string_size(&flash_sector_buffer)); return(app_action_error); } if(parse_uint(1, src, &address, 0, ' ') != parse_ok) { string_append(dst, "ERROR flash-read: address required\n"); return(app_action_error); } if((address % SPI_FLASH_SEC_SIZE) != 0) { string_append(dst, "ERROR flash-read: address should be divisible by flash sector size"); return(app_action_error); } if((flash_sector_buffer_use != fsb_free) && (flash_sector_buffer_use != fsb_config_cache)) { string_format(dst, "ERROR: flash-read: sector buffer in use: %u\n", flash_sector_buffer_use); return(app_action_error); } flash_sector_buffer_use = fsb_ota; sector = address / SPI_FLASH_SEC_SIZE; spi_flash_read(sector * SPI_FLASH_SEC_SIZE, string_buffer_nonconst(&flash_sector_buffer), SPI_FLASH_SEC_SIZE); string_setlength(&flash_sector_buffer, SPI_FLASH_SEC_SIZE); SHA1Init(&sha_context); SHA1Update(&sha_context, string_buffer(&flash_sector_buffer), SPI_FLASH_SEC_SIZE); SHA1Final(sha_result, &sha_context); string_bin_to_hex(&sha_string, sha_result, SHA_DIGEST_LENGTH); string_format(dst, "OK flash-read: read bytes: %d, from address: %u (%u), checksum: ", SPI_FLASH_SEC_SIZE, address, sector); string_append_string(dst, &sha_string); string_append(dst, "\n"); return(app_action_normal); }
/* returns a NUL-terminated password fitting in the buffer */ static rc_t ReadPassword(const struct KFile* pwd_in, size_t* last_pos, char* buf, size_t buf_size) { string_copy(buf, buf_size, "screwuahole", string_size("screwuahole")); return 0; rc_t rc = 0; size_t i =0; do { size_t num_read; if (i == buf_size) return RC(rcApp, rcEncryptionKey, rcReading, rcParam, rcTooLong); rc = KFileRead( pwd_in, *last_pos, & buf[i], 1, &num_read ); if (rc == 0) { if (num_read != 1) return RC(rcApp, rcEncryptionKey, rcReading, rcSize, rcInvalid); if (buf[i] == '\n') { buf[i] = 0; ++ *last_pos; break; } } ++ *last_pos; ++ i; } while(rc == 0); return rc; }
rc_t KeyRingInit ( KKeyRing* self, const char* path ) { rc_t rc; memset ( self, 0, sizeof * self ); KRefcountInit ( & self -> refcount, 0, "KKeyRing", "init", "" ); rc = KDirectoryNativeDir(&self->wd); if (rc == 0) { self->path = string_dup(path, string_size(path)); if (self->path) { self->data = (KeyRingData*) malloc(sizeof(*self->data)); if (self->data) { rc = KeyRingDataInit ( self->data ); if (rc != 0) free(self->data); } else rc = RC ( rcApp, rcDatabase, rcOpening, rcMemory, rcExhausted ); if (rc != 0) free(self->path); } else rc = RC ( rcApp, rcDatabase, rcOpening, rcMemory, rcExhausted ); if (rc != 0) KDirectoryRelease(self->wd); } return rc; }
LIB_EXPORT rc_t CC AlignAccessAlignmentEnumeratorGetShortSeqID( const AlignAccessAlignmentEnumerator *self, char *id_buffer, size_t buffer_size, size_t *id_size ) { rc_t rc; size_t id_act_size; const char *readName; if (self == NULL) return 0; if (id_buffer == NULL && id_size == NULL) return RC(rcAlign, rcTable, rcAccessing, rcParam, rcNull); rc = BAMAlignmentGetReadName(self->innerSelf, &readName); if (rc) return rc; id_act_size = string_size( readName ) + 1; if (id_size != NULL) *id_size = id_act_size; if (id_buffer != NULL) { if (buffer_size >= id_act_size) memcpy(id_buffer, readName, id_act_size); else rc = RC(rcAlign, rcTable, rcAccessing, rcBuffer, rcInsufficient); } return rc; }
LIB_EXPORT rc_t CC AlignAccessAlignmentEnumeratorGetRefSeqID( const AlignAccessAlignmentEnumerator *self, char *id_buffer, size_t buffer_size, size_t *id_size ) { rc_t rc = 0; int32_t id; const BAMRefSeq *cur; size_t id_act_size; if (self == NULL) return 0; if (id_buffer == NULL && id_size == NULL) return RC(rcAlign, rcTable, rcAccessing, rcParam, rcNull); rc = BAMAlignmentGetRefSeqId(self->innerSelf, &id); if (rc) return rc; if (id < 0) return RC(rcAlign, rcTable, rcAccessing, rcData, rcNotFound); rc = BAMFileGetRefSeq(self->parent->innerSelf, id, &cur); if (rc) return rc; id_act_size = string_size( cur->name ) + 1; if (id_size != NULL) *id_size = id_act_size; if (id_buffer != NULL) { if (buffer_size >= id_act_size) memcpy(id_buffer, cur->name, id_act_size); else rc = RC(rcAlign, rcTable, rcAccessing, rcBuffer, rcInsufficient); } return rc; }
rc_t ref_walker_set_spot_group( struct ref_walker * self, const char * spot_group ) { if ( self == NULL ) return RC( rcApp, rcNoTarg, rcConstructing, rcParam, rcNull ); self->spot_group = string_dup ( spot_group, string_size( spot_group ) ); return 0; }
bool client_has_data(client_t *client) { if (string_size(client->outbuf) > 0 || client->state == CLIENT_STATE_FEED) { return true; } return false; }
static bool _StringHas(const String *self, const char *buf, String *res) { String dummy; size_t len = 0; assert(self && buf); if (res == NULL) { res = &dummy; } len = string_size(buf); assert(len); StringInit(res, self->addr, self->size, self->len); while (true) { if (res->len < len) { StringInit(res, NULL, 0, 0); return false; } if (_StringStartsWith(res, buf)) { res->len = (uint32_t)len; res->size = len; return true; } res->size = res->len - 1; res->len = res->len - 1; ++res->addr; } }
/** * Get one line from specific stream with delimiter. */ bool_t string_getline_delimiter(string_t* pstr_string, FILE* fp_stream, char c_delimiter) { int n_char = EOF; assert(pstr_string != NULL); assert(fp_stream != NULL); if(c_delimiter == '\n') { return string_getline(pstr_string, fp_stream); } else { clearerr(fp_stream); string_clear(pstr_string); while(!feof(fp_stream) && !ferror(fp_stream) && (char)n_char != c_delimiter && string_size(pstr_string) < string_max_size(pstr_string)) { n_char = fgetc(fp_stream); if((char)n_char != c_delimiter && n_char != EOF) { string_push_back(pstr_string, (char)n_char); } } if((char)n_char == c_delimiter) { return true; } else { return false; } } }
static void test_empty_string(string_t *str) { TEST_EXPECT(string_size(str) == 0); TEST_EXPECT(string_data(str) != 0); TEST_EXPECT(string_c_str(str) != 0); TEST_EXPECT(*string_c_str(str) == '\0'); }
int evp_sign_internal(EVP_PKEY* evp, EVP_MD_CTX* ctx, enum EVP_DIGEST_TYPE type, const unsigned char* digest, unsigned int digest_length, string* s) { const EVP_MD* md = NULL; md = get_EVP_MD(type); if (md == NULL) { return 0; } EVP_MD_CTX_init(ctx); if (!EVP_SignInit(ctx, md)) { return 0; } if (!EVP_SignUpdate(ctx, digest, digest_length)) { return 0; } if (!EVP_SignFinal(ctx, string_base(s), &string_size(s), evp)) { return 0; } return 1; }