/****f* FUNC/simplestring_addn * NAME * simplestring_addn * SYNOPSIS * void simplestring_addn(simplestring* string, const char* add, int add_len) * FUNCTION * copies n characters from source to target string * INPUTS * target - target string * source - source string * add_len - number of characters to copy * RESULT * void * NOTES * SEE ALSO * simplestring_add () * SOURCE */ void simplestring_addn(simplestring* target, const char* source, int add_len) { if(target && source) { if(!target->str) { simplestring_init_str(target); } if(target->len + add_len + 1 > target->size) { /* newsize is current length + new length */ int newsize = target->len + add_len + 1; int incr = target->size * 2; /* align to SIMPLESTRING_INCR increments */ newsize = newsize - (newsize % incr) + incr; target->str = (char*)realloc(target->str, newsize); target->size = target->str ? newsize : 0; } if(target->str) { if(add_len) { memcpy(target->str + target->len, source, add_len); } target->len += add_len; target->str[target->len] = 0; /* null terminate */ } } }
/****f* FUNC/simplestring_addn * NAME * simplestring_addn * SYNOPSIS * void simplestring_addn(simplestring* string, const char* add, int add_len) * FUNCTION * copies n characters from source to target string * INPUTS * target - target string * source - source string * add_len - number of characters to copy * RESULT * void * NOTES * SEE ALSO * simplestring_add () * SOURCE */ void simplestring_addn(simplestring* target, const char* source, size_t add_len) { size_t newsize = target->size, incr = 0; if(target && source) { if(!target->str) { simplestring_init_str(target); } if((INT_MAX - add_len) < target->len || (INT_MAX - add_len - 1) < target->len) { /* check for overflows, if there's a potential overflow do nothing */ return; } if(target->len + add_len + 1 > target->size) { /* newsize is current length + new length */ newsize = target->len + add_len + 1; incr = target->size * 2; /* align to SIMPLESTRING_INCR increments */ if (incr) { newsize = newsize - (newsize % incr) + incr; } if(newsize < (target->len + add_len + 1)) { /* some kind of overflow happened */ return; } target->str = (char*)erealloc(target->str, newsize); target->size = target->str ? newsize : 0; } if(target->str) { if(add_len) { memcpy(target->str + target->len, source, add_len); } target->len += add_len; target->str[target->len] = 0; /* null terminate */ } } }