Path: blob/master/libs/mpg123/src/libmpg123/stringbuf.c
4394 views
/*1stringbuf: mimicking a bit of C++ to more safely handle strings23copyright 2006-20 by the mpg123 project4- free software under the terms of the LGPL 2.15see COPYING and AUTHORS files in distribution or http://mpg123.org6initially written by Thomas Orgis7*/89#include "mpg123lib_intern.h"10#include "config.h"11#include "mpg123.h"12#include "../compat/compat.h"13#include <string.h>14#include "../common/debug.h"1516mpg123_string* attribute_align_arg mpg123_new_string(const char *val)17{18mpg123_string *sb = malloc(sizeof(mpg123_string));19if(!sb)20return NULL;21mpg123_init_string(sb);22mpg123_set_string(sb, val);23return sb;24}2526void attribute_align_arg mpg123_delete_string(mpg123_string* sb)27{28if(!sb)29return;30mpg123_free_string(sb);31free(sb);32}3334void attribute_align_arg mpg123_init_string(mpg123_string* sb)35{36/* Handing in NULL here is a fatal mistake and rightfully so. */37sb->p = NULL;38sb->size = 0;39sb->fill = 0;40}4142void attribute_align_arg mpg123_free_string(mpg123_string* sb)43{44if(!sb)45return;46if(sb->p != NULL) free(sb->p);47mpg123_init_string(sb);48}4950int attribute_align_arg mpg123_grow_string(mpg123_string* sb, size_t new)51{52if(!sb)53return 0;54if(sb->size < new) return mpg123_resize_string(sb, new);55else return 1;56}5758int attribute_align_arg mpg123_resize_string(mpg123_string* sb, size_t new)59{60if(!sb)61return 0;62debug3("resizing string pointer %p from %lu to %lu", (void*) sb->p, (unsigned long)sb->size, (unsigned long)new);63if(new == 0)64{65if(sb->size && sb->p != NULL) free(sb->p);66mpg123_init_string(sb);67return 1;68}69if(sb->size != new)70{71char* t;72debug("really!");73t = (char*) INT123_safe_realloc(sb->p, new*sizeof(char));74debug1("INT123_safe_realloc returned %p", (void*) t);75if(t != NULL)76{77sb->p = t;78sb->size = new;79if(sb->size < sb->fill)80{81// Cut short the existing data, properly.82sb->fill = sb->size;83sb->p[sb->fill-1] = 0;84}85return 1;86}87else return 0;88}89else return 1; /* success */90}9192int attribute_align_arg mpg123_copy_string(mpg123_string* from, mpg123_string* to)93{94size_t fill;95char *text;9697debug2("called copy_string with %p -> %p", (void*)from, (void*)to);98if(to == NULL)99return 0;100if(from == NULL)101{102fill = 0;103text = NULL;104}105else106{107fill = from->fill;108text = from->p;109}110111if(mpg123_resize_string(to, fill))112{113if(fill) /* Avoid memcpy(NULL, NULL, 0) */114memcpy(to->p, text, fill);115to->fill = fill;116return 1;117}118else return 0;119}120121int attribute_align_arg mpg123_move_string(mpg123_string *from, mpg123_string *to)122{123if(to)124mpg123_free_string(to);125else126mpg123_free_string(from);127if(from && to)128*to = *from;129if(from)130mpg123_init_string(from);131return (from && to) ? 1 : 0;132}133134int attribute_align_arg mpg123_add_string(mpg123_string* sb, const char* stuff)135{136debug1("adding %s", stuff);137return mpg123_add_substring(sb, stuff, 0, stuff ? strlen(stuff) : 0);138}139140int attribute_align_arg mpg123_add_substring(mpg123_string *sb, const char *stuff, size_t from, size_t count)141{142debug("adding a substring");143if(!sb || !stuff)144return 0;145if(sb->fill) /* includes zero byte... */146{147if( (SIZE_MAX - sb->fill >= count) /* Avoid overflow. */148&& (sb->size >= sb->fill+count || mpg123_grow_string(sb, sb->fill+count)) )149{150memcpy(sb->p+sb->fill-1, stuff+from, count);151sb->fill += count;152sb->p[sb->fill-1] = 0; /* Terminate! */153}154else return 0;155}156else157{158if( count < SIZE_MAX && mpg123_grow_string(sb, count+1) )159{160memcpy(sb->p, stuff+from, count);161sb->fill = count+1;162sb->p[sb->fill-1] = 0; /* Terminate! */163}164else return 0;165}166return 1;167}168169int attribute_align_arg mpg123_set_substring(mpg123_string* sb, const char* stuff, size_t from, size_t count)170{171if(!sb)172return 0;173sb->fill = 0;174return mpg123_add_substring(sb, stuff, from, count);175}176177int attribute_align_arg mpg123_set_string(mpg123_string* sb, const char* stuff)178{179if(!sb)180return 0;181sb->fill = 0;182return mpg123_add_string(sb, stuff);183}184185size_t attribute_align_arg mpg123_strlen(mpg123_string *sb, int utf8)186{187size_t i;188size_t bytelen;189190/* Notions of empty string. If there's only a single character, it has to be the trailing zero, and if the first is the trailing zero anyway, we got empty. */191if(!sb || sb->fill < 2 || sb->p[0] == 0) return 0;192193/* Find the first non-null character from the back.194We already established that the first character is non-null195That at fill-2 has to be null, though. */196for(i=sb->fill-2; i>0; --i)197if(sb->p[i] != 0) break;198199/* For simple byte strings, we are done now. */200bytelen = i+1;201202if(!utf8) return bytelen;203else204{205/* Work out the actual count of UTF8 bytes.206This employs no particular encoding error checking. */207size_t len = 0;208for(i=0; i<bytelen; ++i)209{210/* Every byte that is not a continuation byte ( 0xc0 == 10xx xxxx ) stands for a character. */211if((sb->p[i] & 0xc0) != 0x80) len++;212}213return len;214}215}216217int attribute_align_arg mpg123_chomp_string(mpg123_string *sb)218{219if(!sb || !sb->fill) return 0;220221/* Ensure that it is zero-terminated. */222char *c = sb->p+sb->fill-1;223*c = 0;224for(; c >= sb->p; --c)225{226/* Stop at the first proper character. */227if(*c && *c != '\r' && *c != '\n') break;228else *c = 0;229}230// We at least got a trailing zero.231sb->fill = (size_t)(c - sb->p + 1) + 1;232233return 1;234}235236int attribute_align_arg mpg123_same_string(mpg123_string *a, mpg123_string *b)237{238if(!a || !b)239return 0;240if(a->fill != b->fill)241return 0;242if(memcmp(a->p, b->p, a->fill))243return 0;244return 1;245}246247248