| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385 |
- #include "precompile.h"
- #include <float.h>
- #include <ctype.h>
- #include <stdarg.h>
- #include <assert.h>
- #include <string.h>
- #include "strutil.h"
- #include "memutil.h"
- #include <winpr/wtypes.h>
- #include <winpr/string.h>
- #pragma warning(disable : 4311)
- TOOLKIT_API const char *memstr(const char *buf, int n, const char *str)
- {
- const char *p, *e;
- size_t len;
- assert(buf);
- assert(str);
- len = strlen(str);
- for (p = buf, e = buf+n-len; p <= e; ++p) {
- if (memcmp(p, str, len) == 0)
- return p;
- }
- return NULL;
- }
- TOOLKIT_API char *memstr1(char *buf, int n, const char *str)
- {
- char *p, *e;
- size_t len;
- assert(buf);
- assert(str);
- len = strlen(str);
- for (p = buf, e = buf+n-len; p <= e; ++p) {
- if (memcmp(p, str, len) == 0)
- return p;
- }
- return NULL;
- }
- TOOLKIT_API const char *memrstr(const char *buf, int n, const char *str)
- {
- const char *p, *e;
- size_t len;
- assert(buf);
- assert(str);
- len = strlen(str);
- for (p = buf, e = buf+n-len; e >= p; e--) {
- if (memcmp(e, str, len) == 0)
- return e;
- }
- return NULL;
- }
- TOOLKIT_API const char *memistr(const char *buf, int n, const char *str)
- {
- const char *p, *e;
- size_t len;
- assert(buf);
- assert(str);
- len = strlen(str);
- for (p = buf, e = buf+n-len; p <= e; ++p) {
- if (_memicmp(p, str, len) == 0)
- return p;
- }
- return NULL;
- }
- TOOLKIT_API const char *memristr(const char *buf, int n, const char *str)
- {
- const char *p, *e;
- size_t len;
- assert(buf);
- assert(str);
- len = strlen(str);
- for (p = buf, e = buf+n-len; e >= p; e--) {
- if (_memicmp(e, str, len) == 0)
- return e;
- }
- return NULL;
- }
- #ifdef _WIN32
- /* Naive implementation of memmem() */
- TOOLKIT_API void *memmem(const void *haystack, size_t haystacklen,
- const void *needle, size_t needlelen)
- {
- size_t i;
- char const *hs = haystack;
- if (needlelen == 0)
- return (void *)haystack;
- if (needlelen > haystacklen || haystack == NULL || needle == NULL)
- return NULL;
- for (i = 0; i <= haystacklen - needlelen; i++) {
- if (memcmp(hs + i, needle, needlelen) == 0)
- return (void *)(hs + i);
- }
- return NULL;
- }
- /* Naive implementation of strcasestr() */
- TOOLKIT_API char *strcasestr(const char *haystack,
- const char *needle)
- {
- unsigned char lcn, ucn;
- unsigned i;
- if (haystack == NULL || needle == NULL)
- return NULL;
- lcn = ucn = needle[0];
- if (isupper(lcn))
- lcn = tolower(lcn);
- else if (islower(ucn))
- ucn = toupper(ucn);
- if (lcn == 0)
- return (char *)haystack;
- while (haystack[0] != 0) {
- if (lcn == haystack[0] || ucn == haystack[0]) {
- for (i = 1; ; i++) {
- char n = needle[i], h = haystack[i];
- if (n == 0)
- return (char *)haystack;
- if (h == 0)
- return NULL;
- if (isupper(n)) n = tolower(n);
- if (isupper(h)) h = tolower(h);
- if (n != h)
- break;
- }
- }
- haystack++;
- }
- return NULL; /* Not found */
- }
- #endif //_WIN32
- TOOLKIT_API char *strltrim(char *s, const char *trims)
- {
- char *k = s;
- char *p;
- while (strchr(trims, *k))
- ++k;
- if (k != s)
- for (p = s; *p++ = *k++;);
- return s;
- }
- TOOLKIT_API char *strrtrim(char *s, const char *trims)
- {
- size_t n = strlen(s);
- while (n && strchr(trims, s[n-1]))
- n--;
- s[n] = 0;
- return s;
- }
- TOOLKIT_API char *strtrim(char *s, const char *trims)
- {
- return strltrim(strrtrim(s, trims), trims);
- }
- TOOLKIT_API char *strnormws(char *s)
- {
- char *in = s, *out = s;
- int ch;
- while (isspace((ch = *in++)))
- ;
- if (ch != '\0')
- for (;;) {
- *out++ = ch;
- ch = *in++;
- if (ch == '\0')
- break;
- if (isspace(ch)) {
- while(isspace((ch = *in++)))
- ;
- if (ch == '\0')
- break;
- *out++ = ' ';
- }
- }
- *out = '\0';
- return s;
- }
- #define ONES_WORD 0x01010101
- #define EIGHTS_WORD 0x80808080
- #ifdef _WIN32
- #define word_has_nullbyte(w) (((w) - 0x01010101) & ~(w) & EIGHTS_WORD)
- TOOLKIT_API char *stpcpy(char *dst, const char *src)
- {
- const int *p = (const int *)src;
- int *q = (int*)dst;
- const char *sp;
- char *sq;
- if (!((int)p&(sizeof(int)-1)) && !((int)q&(sizeof(int)-1))) {
- int x = *p;
- while (!word_has_nullbyte(x)) {
- *q++ = x;
- x = *++p;
- }
- }
- sp = (const char*)p;
- sq = (char*)q;
- while (*sq = *sp++)
- sq++;
- return sq;
- }
- TOOLKIT_API char* strsep(char** stringp, const char* delim)
- {
- char* str, * end;
- str = *stringp + strspn(*stringp, delim);
- if (*str == '\0') {
- end = str;
- str = NULL;
- }
- else {
- end = str + strcspn(str, delim);
- if (*end != '\0')
- *end++ = '\0';
- }
- *stringp = end;
- return str;
- }
- TOOLKIT_API char* strtok_r(char* str, const char* delim, char** tracker)
- {
- if (str != NULL)
- *tracker = str;
- return strsep(tracker, delim);
- }
- #endif //_WIN32
- TOOLKIT_API char * strreplace(const char *src, const char *old, const char *news)
- {
- # define SUBS_TOP 32
- const char *subs[SUBS_TOP], *p, *sub_start, *src_without_old;
- char *dest, *ret;
- size_t old_len, new_len, dest_size, tmp;
- int i;
- old_len = strlen(old);
- new_len = strlen(news);
- /* Find substrings and compute size of result */
- dest_size = 1;
- src_without_old = src;
- for (i = 0, p = src ;; p = sub_start + old_len) {
- sub_start = strstr(p, old);
- if (i < SUBS_TOP)
- subs[i++] = sub_start;
- if (sub_start == NULL)
- break;
- dest_size += new_len;
- if (dest_size < new_len) {
- return NULL;
- }
- src_without_old += old_len;
- }
- tmp = (p - src_without_old) + strlen(p);
- dest_size += tmp;
- if (dest_size < tmp) {
- return NULL;
- }
- /* Make result string */
- ret = malloc(dest_size);
- if (ret) {
- dest = ret;
- for (i = 0;; ) {
- p = (i < SUBS_TOP ? subs[i++] : strstr(src, old));
- if (p == NULL)
- break;
- tmp = p - src;
- memcpy(dest, src, tmp);
- src += tmp + old_len;
- dest = stpcpy(dest + tmp, news);
- }
- strcpy(dest, src);
- }
- return ret;
- }
- TOOLKIT_API char **strsplit(const char *s, const char *delim)
- {
- const char *sp;
- char *p, **ret, **rp;
- size_t i;
- /* skip to first substring */
- s += strspn(s, delim);
- /* allocate array */
- for (i = 1, sp = s; *sp != '\0'; i++) {
- sp += strcspn(sp, delim);
- sp += strspn(sp, delim);
- }
- ret = rp = malloc(i * sizeof(char *));
- if (ret != NULL) {
- while (*s != '\0') {
- /* found new substring */
- i = strcspn(s, delim);
- *rp++ = p = malloc(i + 1);
- if (p == NULL) {
- strfreev(ret);
- return NULL;
- }
- memcpy(p, s, i);
- p[i] = '\0';
- s += i;
- s += strspn(s, delim);
- }
- *rp = NULL;
- }
- return ret;
- }
- TOOLKIT_API void strfreev(char **strings)
- {
- if (strings) {
- char **p;
- for (p = strings; *p != NULL; ++p)
- free(*p);
- free(strings);
- }
- }
- TOOLKIT_API char *strsub(char *dest,char *src,size_t offset,size_t len)
- {
- *dest = '\0';
- if (!memchr(src, '\0', offset))
- strncat(dest, src + offset, len);
- return dest;
- }
- TOOLKIT_API char *strleft(char *dest, char *src, size_t len)
- {
- *dest = '\0';
- strncat(dest, src, len);
- return dest;
- }
- TOOLKIT_API char *strright(char *dest, char *src, size_t len)
- {
- size_t src_len;
- src_len = strlen(src);
- if(src_len > len)
- src += src_len - len;
- return strcpy(dest, src);
- }
- static char * TOOLKIT_CC __strallocv(int stack, const char *arg1, va_list arg_list)
- {
- size_t len;
- const char *arg;
- char *ret, *end;
- int bad;
- va_list ap;
- /* compute length of result string */
- len = 1;
- bad = 0;
- //TODO: need to test its validity
- #ifdef _WIN32
- /*gcc compile error: assignment to expression with array type*/
- ap = arg_list;
- #else
- va_copy(ap, arg_list);
- #endif
- for (arg = arg1; arg != (char *)0; arg = va_arg(ap, char *)) {
- size_t arglen = strlen(arg);
- len += arglen;
- if(len < arglen) {
- /* string length too large for size_t */
- bad = 1;
- break;
- }
- }
- if(bad) {
- return NULL;
- }
- /* create result string */
- if (!stack)
- ret = malloc(len);
- else
- ret = _alloca(len);
- if(ret != NULL) {
- end = ret;
- *end = '\0';
- #ifdef _WIN32
- /*gcc compile error: assignment to expression with array type*/
- ap = arg_list;
- #else
- va_copy(ap, arg_list);
- #endif
- for (arg = arg1; arg != (char *)0; arg = va_arg(ap, char *))
- end = stpcpy(end, arg);
- }
- return ret;
- }
- TOOLKIT_API char * TOOLKIT_CC stralloc(const char *arg1, ...)
- {
- char *r;
- va_list ap;
- va_start(ap, arg1);
- r = __strallocv(0, arg1, ap);
- va_end(ap);
- return r;
- }
- TOOLKIT_API char * TOOLKIT_CC stralloca(const char *arg1, ...)
- {
- char *r;
- va_list ap;
- va_start(ap, arg1);
- r = __strallocv(1, arg1, ap);
- va_end(ap);
- return r;
- }
- TOOLKIT_API void *memdup(const void *buf, int len)
- {
- void *ret;
- if (buf) {
- ret = malloc(len);
- if (ret) {
- memcpy(ret, buf, len);
- }
- } else {
- ret = NULL;
- }
- return ret;
- }
- TOOLKIT_API void *memdupa(const void *buf, int len)
- {
- void *ret;
- if (buf) {
- ret = _alloca(len);
- if (ret) {
- memcpy(ret, buf, len);
- }
- } else {
- ret = NULL;
- }
- return ret;
- }
- #ifdef _WIN32
- TOOLKIT_API char *strdupa(const char * s)
- {
- size_t size;
- char *p;
- assert(s);
- size = strlen(s) + 1;
- if (size == 0)
- p = NULL;
- else if ((p = _alloca(size)) != NULL)
- memcpy(p, s, size);
- return p;
- }
- TOOLKIT_API char *strndup(const char *str, int n)
- {
- char *new_str;
- if (str) {
- new_str = (char*)malloc(n+1);
- strncpy(new_str, str, n);
- new_str[n] = '\0';
- } else {
- new_str = NULL;
- }
- return new_str;
- }
- #endif //_WIN32
- TOOLKIT_API char* strnfill(int length, int fill_char)
- {
- char *str;
- str = (char*)malloc(length + 1);
- memset (str, fill_char, length);
- str[length] = '\0';
- return str;
- }
- TOOLKIT_API int strcmp0 (const char *str1,const char *str2)
- {
- if (!str1)
- return -(str1 != str2);
- if (!str2)
- return str1 != str2;
- return strcmp (str1, str2);
- }
- TOOLKIT_API char* TOOLKIT_CC strdup_printf(const char *format, ...)
- {
- char *buffer;
- va_list args;
- va_start(args, format);
- buffer = strdup_vprintf(format, args);
- va_end(args);
- return buffer;
- }
- static char *vasnprintf (char *resultbuf, size_t *lengthp, const char *format, va_list args);
- TOOLKIT_API char* TOOLKIT_CC strdup_vprintf(const char *format, va_list args)
- {
- size_t length;
- return vasnprintf (NULL, &length, format, args);
- }
- TOOLKIT_API size_t strlcpy(char *dest, const char *src, size_t size)
- {
- const char *start;
- start = src;
- if (size) {
- while (--size && *src)
- *dest++ = *src++;
- *dest = '\0';
- }
- while (*src)
- ++src;
- return src - start;
- }
- TOOLKIT_API size_t strlcat(char * dest, const char *src, size_t size)
- {
- size_t len;
- char *end;
- for (end = dest; *end; ++end) ;
- len = end - dest;
- return len + strlcpy(end, src, size > len ? size - len : 0);
- }
- TOOLKIT_API char** strdupv (char **str_array)
- {
- if (str_array)
- {
- int i;
- char **retval;
- i = 0;
- while (str_array[i])
- ++i;
-
- retval = (char**)malloc(i+1);
- i = 0;
- while (str_array[i])
- {
- retval[i] = _strdup (str_array[i]);
- ++i;
- }
- retval[i] = NULL;
- return retval;
- }
- else
- return NULL;
- }
- TOOLKIT_API char* strjoinv (const char *separator,char **str_array)
- {
- char *string;
- char *ptr;
- if (separator == NULL)
- separator = "";
- if (*str_array)
- {
- int i;
- size_t len;
- size_t separator_len;
- separator_len = strlen (separator);
- /* First part, getting length */
- len = 1 + strlen (str_array[0]);
- for (i = 1; str_array[i] != NULL; i++)
- len += strlen (str_array[i]);
- len += separator_len * (i - 1);
- /* Second part, building string */
- string = (char*)malloc(len);
- ptr = stpcpy (string, *str_array);
- for (i = 1; str_array[i] != NULL; i++)
- {
- ptr = stpcpy (ptr, separator);
- ptr = stpcpy (ptr, str_array[i]);
- }
- }
- else
- string = _strdup ("");
- return string;
- }
- TOOLKIT_API char* TOOLKIT_CC strjoin (const char *separator, ...)
- {
- char *string, *s;
- va_list args;
- size_t len;
- size_t separator_len;
- char *ptr;
- if (separator == NULL)
- separator = "";
- separator_len = strlen (separator);
- va_start (args, separator);
- s = va_arg (args, char*);
- if (s)
- {
- /* First part, getting length */
- len = 1 + strlen (s);
- s = va_arg (args, char*);
- while (s)
- {
- len += separator_len + strlen (s);
- s = va_arg (args, char*);
- }
- va_end (args);
- /* Second part, building string */
- string = (char*)malloc(len);
- va_start (args, separator);
- s = va_arg (args, char*);
- ptr = stpcpy (string, s);
- s = va_arg (args, char*);
- while (s)
- {
- ptr = stpcpy (ptr, separator);
- ptr = stpcpy (ptr, s);
- s = va_arg (args, char*);
- }
- }
- else
- string = _strdup ("");
- va_end (args);
- return string;
- }
- TOOLKIT_API int str_has_suffix (const char *str, const char *suffix)
- {
- size_t str_len;
- size_t suffix_len;
-
- str_len = strlen (str);
- suffix_len = strlen (suffix);
- if (str_len < suffix_len)
- return FALSE;
- return strcmp (str + str_len - suffix_len, suffix) == 0;
- }
- TOOLKIT_API int str_has_prefix (const char *str,const char *prefix)
- {
- size_t str_len;
- size_t prefix_len;
-
- str_len = strlen (str);
- prefix_len = strlen (prefix);
- if (str_len < prefix_len)
- return FALSE;
-
- return strncmp (str, prefix, prefix_len) == 0;
- }
- TOOLKIT_API unsigned int strv_length (char **str_array)
- {
- unsigned int i = 0;
- while (str_array[i])
- ++i;
- return i;
- }
- /* asnprintf, extract from glib */
- #define FLAG_GROUP 1 /* ' flag */
- #define FLAG_LEFT 2 /* - flag */
- #define FLAG_SHOWSIGN 4 /* + flag */
- #define FLAG_SPACE 8 /* space flag */
- #define FLAG_ALT 16 /* # flag */
- #define FLAG_ZERO 32
- /* A parsed directive. */
- typedef struct
- {
- const char* dir_start;
- const char* dir_end;
- int flags;
- const char* width_start;
- const char* width_end;
- int width_arg_index;
- const char* precision_start;
- const char* precision_end;
- int precision_arg_index;
- char conversion; /* d i o u x X f e E g G c s p n U % but not C S */
- int arg_index;
- }
- char_directive;
- /* A parsed format string. */
- typedef struct
- {
- unsigned int count;
- char_directive *dir;
- unsigned int max_width_length;
- unsigned int max_precision_length;
- }
- char_directives;
- /* Argument types */
- typedef enum
- {
- TYPE_NONE,
- TYPE_SCHAR,
- TYPE_UCHAR,
- TYPE_SHORT,
- TYPE_USHORT,
- TYPE_INT,
- TYPE_UINT,
- TYPE_LONGINT,
- TYPE_ULONGINT,
- TYPE_LONGLONGINT,
- TYPE_ULONGLONGINT,
- TYPE_INT64,
- TYPE_UINT64,
- TYPE_DOUBLE,
- TYPE_LONGDOUBLE,
- TYPE_CHAR,
- TYPE_STRING,
- TYPE_POINTER,
- TYPE_COUNT_SCHAR_POINTER,
- TYPE_COUNT_SHORT_POINTER,
- TYPE_COUNT_INT_POINTER,
- TYPE_COUNT_LONGINT_POINTER
- , TYPE_COUNT_LONGLONGINT_POINTER
- } arg_type;
- /* Polymorphic argument */
- typedef struct
- {
- arg_type type;
- union
- {
- signed char a_schar;
- unsigned char a_uchar;
- short a_short;
- unsigned short a_ushort;
- int a_int;
- unsigned int a_uint;
- long int a_longint;
- unsigned long int a_ulongint;
- long long int a_longlongint;
- unsigned long long int a_ulonglongint;
- __int64 a_int64;
- u__int64_t a_uint64;
- float a_float;
- double a_double;
- long double a_longdouble;
- int a_char;
- const char* a_string;
- void* a_pointer;
- signed char * a_count_schar_pointer;
- short * a_count_short_pointer;
- int * a_count_int_pointer;
- long int * a_count_longint_pointer;
- long long int * a_count_longlongint_pointer;
- }
- a;
- }
- argument;
- typedef struct
- {
- unsigned int count;
- argument *arg;
- }
- arguments;
- static int printf_parse (const char *format, char_directives *d, arguments *a)
- {
- const char *cp = format; /* pointer into format */
- int arg_posn = 0; /* number of regular arguments consumed */
- unsigned int d_allocated; /* allocated elements of d->dir */
- unsigned int a_allocated; /* allocated elements of a->arg */
- unsigned int max_width_length = 0;
- unsigned int max_precision_length = 0;
- d->count = 0;
- d_allocated = 1;
- d->dir = malloc (d_allocated * sizeof (char_directive));
- if (d->dir == NULL)
- /* Out of memory. */
- return -1;
- a->count = 0;
- a_allocated = 0;
- a->arg = NULL;
- #define REGISTER_ARG(_index_,_type_) \
- { \
- unsigned int n = (_index_); \
- if (n >= a_allocated) \
- { \
- argument *memory; \
- a_allocated = 2 * a_allocated; \
- if (a_allocated <= n) \
- a_allocated = n + 1; \
- memory = (a->arg \
- ? realloc (a->arg, a_allocated * sizeof (argument)) \
- : malloc (a_allocated * sizeof (argument))); \
- if (memory == NULL) \
- /* Out of memory. */ \
- goto error; \
- a->arg = memory; \
- } \
- while (a->count <= n) \
- a->arg[a->count++].type = TYPE_NONE; \
- if (a->arg[n].type == TYPE_NONE) \
- a->arg[n].type = (_type_); \
- else if (a->arg[n].type != (_type_)) \
- /* Ambiguous type for positional argument. */ \
- goto error; \
- }
- while (*cp != '\0')
- {
- char c = *cp++;
- if (c == '%')
- {
- int arg_index = -1;
- char_directive *dp = &d->dir[d->count];/* pointer to next directive */
- /* Initialize the next directive. */
- dp->dir_start = cp - 1;
- dp->flags = 0;
- dp->width_start = NULL;
- dp->width_end = NULL;
- dp->width_arg_index = -1;
- dp->precision_start = NULL;
- dp->precision_end = NULL;
- dp->precision_arg_index = -1;
- dp->arg_index = -1;
- /* Test for positional argument. */
- if (*cp >= '0' && *cp <= '9')
- {
- const char *np;
- for (np = cp; *np >= '0' && *np <= '9'; np++)
- ;
- if (*np == '$')
- {
- unsigned int n = 0;
- for (np = cp; *np >= '0' && *np <= '9'; np++)
- n = 10 * n + (*np - '0');
- if (n == 0)
- /* Positional argument 0. */
- goto error;
- arg_index = n - 1;
- cp = np + 1;
- }
- }
- /* Read the flags. */
- for (;;)
- {
- if (*cp == '\'')
- {
- dp->flags |= FLAG_GROUP;
- cp++;
- }
- else if (*cp == '-')
- {
- dp->flags |= FLAG_LEFT;
- cp++;
- }
- else if (*cp == '+')
- {
- dp->flags |= FLAG_SHOWSIGN;
- cp++;
- }
- else if (*cp == ' ')
- {
- dp->flags |= FLAG_SPACE;
- cp++;
- }
- else if (*cp == '#')
- {
- dp->flags |= FLAG_ALT;
- cp++;
- }
- else if (*cp == '0')
- {
- dp->flags |= FLAG_ZERO;
- cp++;
- }
- else
- break;
- }
- /* Parse the field width. */
- if (*cp == '*')
- {
- dp->width_start = cp;
- cp++;
- dp->width_end = cp;
- if (max_width_length < 1)
- max_width_length = 1;
- /* Test for positional argument. */
- if (*cp >= '0' && *cp <= '9')
- {
- const char *np;
- for (np = cp; *np >= '0' && *np <= '9'; np++)
- ;
- if (*np == '$')
- {
- unsigned int n = 0;
- for (np = cp; *np >= '0' && *np <= '9'; np++)
- n = 10 * n + (*np - '0');
- if (n == 0)
- /* Positional argument 0. */
- goto error;
- dp->width_arg_index = n - 1;
- cp = np + 1;
- }
- }
- if (dp->width_arg_index < 0)
- dp->width_arg_index = arg_posn++;
- REGISTER_ARG (dp->width_arg_index, TYPE_INT);
- }
- else if (*cp >= '0' && *cp <= '9')
- {
- unsigned int width_length;
- dp->width_start = cp;
- for (; *cp >= '0' && *cp <= '9'; cp++)
- ;
- dp->width_end = cp;
- width_length = (unsigned int)(dp->width_end - dp->width_start);
- if (max_width_length < width_length)
- max_width_length = width_length;
- }
- /* Parse the precision. */
- if (*cp == '.')
- {
- cp++;
- if (*cp == '*')
- {
- dp->precision_start = cp - 1;
- cp++;
- dp->precision_end = cp;
- if (max_precision_length < 2)
- max_precision_length = 2;
- /* Test for positional argument. */
- if (*cp >= '0' && *cp <= '9')
- {
- const char *np;
- for (np = cp; *np >= '0' && *np <= '9'; np++)
- ;
- if (*np == '$')
- {
- unsigned int n = 0;
- for (np = cp; *np >= '0' && *np <= '9'; np++)
- n = 10 * n + (*np - '0');
- if (n == 0)
- /* Positional argument 0. */
- goto error;
- dp->precision_arg_index = n - 1;
- cp = np + 1;
- }
- }
- if (dp->precision_arg_index < 0)
- dp->precision_arg_index = arg_posn++;
- REGISTER_ARG (dp->precision_arg_index, TYPE_INT);
- }
- else
- {
- unsigned int precision_length;
- dp->precision_start = cp - 1;
- for (; *cp >= '0' && *cp <= '9'; cp++)
- ;
- dp->precision_end = cp;
- precision_length = (unsigned int)(dp->precision_end - dp->precision_start);
- if (max_precision_length < precision_length)
- max_precision_length = precision_length;
- }
- }
- {
- arg_type type;
- /* Parse argument type/size specifiers. */
- {
- int flags = 0;
- for (;;)
- {
- if (*cp == 'h')
- {
- flags |= (1 << (flags & 1));
- cp++;
- }
- else if (*cp == 'L')
- {
- flags |= 4;
- cp++;
- }
- else if (*cp == 'l')
- {
- flags += 8;
- cp++;
- }
- else if (cp[0] == 'I' &&
- cp[1] == '6' &&
- cp[2] == '4')
- {
- flags = 64;
- cp += 3;
- }
- else if (*cp == 'z' || *cp == 'Z')
- {
- /* 'z' is standardized in ISO C 99, but glibc uses 'Z'
- because the warning facility in gcc-2.95.2 understands
- only 'Z' (see gcc-2.95.2/gcc/c-common.c:1784). */
- if (sizeof (size_t) > sizeof (long))
- {
- /* size_t = long long */
- flags += 16;
- }
- else if (sizeof (size_t) > sizeof (int))
- {
- /* size_t = long */
- flags += 8;
- }
- cp++;
- }
- else if (*cp == 't')
- {
- if (sizeof (ptrdiff_t) > sizeof (long))
- {
- /* ptrdiff_t = long long */
- flags += 16;
- }
- else if (sizeof (ptrdiff_t) > sizeof (int))
- {
- /* ptrdiff_t = long */
- flags += 8;
- }
- cp++;
- }
- else
- break;
- }
- /* Read the conversion character. */
- c = *cp++;
- switch (c)
- {
- case 'd': case 'i':
- if (flags == 64)
- type = TYPE_INT64;
- else
- if (flags >= 16 || (flags & 4))
- type = TYPE_LONGLONGINT;
- else
- if (flags >= 8)
- type = TYPE_LONGINT;
- else if (flags & 2)
- type = TYPE_SCHAR;
- else if (flags & 1)
- type = TYPE_SHORT;
- else
- type = TYPE_INT;
- break;
- case 'o': case 'u': case 'x': case 'X':
- if (flags == 64)
- type = TYPE_UINT64;
- else
- if (flags >= 16 || (flags & 4))
- type = TYPE_ULONGLONGINT;
- else
- if (flags >= 8)
- type = TYPE_ULONGINT;
- else if (flags & 2)
- type = TYPE_UCHAR;
- else if (flags & 1)
- type = TYPE_USHORT;
- else
- type = TYPE_UINT;
- break;
- case 'f': case 'F': case 'e': case 'E': case 'g': case 'G':
- case 'a': case 'A':
- if (flags >= 16 || (flags & 4))
- type = TYPE_LONGDOUBLE;
- else
- type = TYPE_DOUBLE;
- break;
- case 'c':
- if (flags >= 8)
- goto error;
- else
- type = TYPE_CHAR;
- break;
- case 's':
- if (flags >= 8)
- goto error;
- else
- type = TYPE_STRING;
- break;
- case 'p':
- type = TYPE_POINTER;
- break;
- case 'n':
- if (flags >= 16 || (flags & 4))
- type = TYPE_COUNT_LONGLONGINT_POINTER;
- else
- if (flags >= 8)
- type = TYPE_COUNT_LONGINT_POINTER;
- else if (flags & 2)
- type = TYPE_COUNT_SCHAR_POINTER;
- else if (flags & 1)
- type = TYPE_COUNT_SHORT_POINTER;
- else
- type = TYPE_COUNT_INT_POINTER;
- break;
- case '%':
- type = TYPE_NONE;
- break;
- default:
- /* Unknown conversion character. */
- goto error;
- }
- }
- if (type != TYPE_NONE)
- {
- dp->arg_index = arg_index;
- if (dp->arg_index < 0)
- dp->arg_index = arg_posn++;
- REGISTER_ARG (dp->arg_index, type);
- }
- dp->conversion = c;
- dp->dir_end = cp;
- }
- d->count++;
- if (d->count >= d_allocated)
- {
- char_directive *memory;
- d_allocated = 2 * d_allocated;
- memory = realloc (d->dir, d_allocated * sizeof (char_directive));
- if (memory == NULL)
- /* Out of memory. */
- goto error;
- d->dir = memory;
- }
- }
- }
- d->dir[d->count].dir_start = cp;
- d->max_width_length = max_width_length;
- d->max_precision_length = max_precision_length;
- return 0;
- error:
- if (a->arg)
- free (a->arg);
- if (d->dir)
- free (d->dir);
- return -1;
- }
- static int printf_fetchargs (va_list args, arguments *a)
- {
- unsigned int i;
- argument *ap;
- for (i = 0, ap = &a->arg[0]; i < a->count; i++, ap++)
- switch (ap->type)
- {
- case TYPE_SCHAR:
- ap->a.a_schar = va_arg (args, /*signed char*/ int);
- break;
- case TYPE_UCHAR:
- ap->a.a_uchar = va_arg (args, /*unsigned char*/ int);
- break;
- case TYPE_SHORT:
- ap->a.a_short = va_arg (args, /*short*/ int);
- break;
- case TYPE_USHORT:
- ap->a.a_ushort = va_arg (args, /*unsigned short*/ int);
- break;
- case TYPE_INT:
- ap->a.a_int = va_arg (args, int);
- break;
- case TYPE_UINT:
- ap->a.a_uint = va_arg (args, unsigned int);
- break;
- case TYPE_LONGINT:
- ap->a.a_longint = va_arg (args, long int);
- break;
- case TYPE_ULONGINT:
- ap->a.a_ulongint = va_arg (args, unsigned long int);
- break;
- case TYPE_LONGLONGINT:
- ap->a.a_longlongint = va_arg (args, long long int);
- break;
- case TYPE_ULONGLONGINT:
- ap->a.a_ulonglongint = va_arg (args, unsigned long long int);
- break;
- case TYPE_INT64:
- ap->a.a_int64 = va_arg (args, __int64);
- break;
- case TYPE_UINT64:
- ap->a.a_uint64 = va_arg (args, u__int64_t);
- break;
- case TYPE_DOUBLE:
- ap->a.a_double = va_arg (args, double);
- break;
- case TYPE_LONGDOUBLE:
- ap->a.a_longdouble = va_arg (args, long double);
- break;
- case TYPE_CHAR:
- ap->a.a_char = va_arg (args, int);
- break;
- case TYPE_STRING:
- ap->a.a_string = va_arg (args, const char *);
- break;
- case TYPE_POINTER:
- ap->a.a_pointer = va_arg (args, void *);
- break;
- case TYPE_COUNT_SCHAR_POINTER:
- ap->a.a_count_schar_pointer = va_arg (args, signed char *);
- break;
- case TYPE_COUNT_SHORT_POINTER:
- ap->a.a_count_short_pointer = va_arg (args, short *);
- break;
- case TYPE_COUNT_INT_POINTER:
- ap->a.a_count_int_pointer = va_arg (args, int *);
- break;
- case TYPE_COUNT_LONGINT_POINTER:
- ap->a.a_count_longint_pointer = va_arg (args, long int *);
- break;
- case TYPE_COUNT_LONGLONGINT_POINTER:
- ap->a.a_count_longlongint_pointer = va_arg (args, long long int *);
- break;
- default:
- /* Unknown type. */
- return -1;
- }
- return 0;
- }
- static int print_long_long (char *buf, int len, int width,int precision,unsigned long flags,char conversion,unsigned long long number)
- {
- int negative = FALSE;
- char buffer[128];
- char *bufferend;
- char *pointer;
- int base;
- static const char *upper = "0123456789ABCDEFX";
- static const char *lower = "0123456789abcdefx";
- const char *digits;
- int i;
- char *p;
- int count;
- #define EMIT(c) \
- if (p - buf == len - 1) \
- { \
- *p++ = '\0'; \
- return len; \
- } \
- else \
- *p++ = c;
-
- p = buf;
-
- switch (conversion)
- {
- case 'o':
- base = 8;
- digits = lower;
- negative = FALSE;
- break;
- case 'x':
- base = 16;
- digits = lower;
- negative = FALSE;
- break;
- case 'X':
- base = 16;
- digits = upper;
- negative = FALSE;
- break;
- default:
- base = 10;
- digits = lower;
- negative = (long long)number < 0;
- if (negative)
- number = -((long long)number);
- break;
- }
- /* Build number */
- pointer = bufferend = &buffer[sizeof(buffer) - 1];
- *pointer-- = '\0';
- for (i = 1; i < (int)sizeof(buffer); i++)
- {
- *pointer-- = digits[number % base];
- number /= base;
- if (number == 0)
- break;
- }
- /* Adjust width */
- width -= (int)((bufferend - pointer) - 1);
- /* Adjust precision */
- if (precision != -1)
- {
- precision -= (int)((bufferend - pointer) - 1);
- if (precision < 0)
- precision = 0;
- flags |= FLAG_ZERO;
- }
- /* Adjust width further */
- if (negative || (flags & FLAG_SHOWSIGN) || (flags & FLAG_SPACE))
- width--;
- if (flags & FLAG_ALT)
- {
- switch (base)
- {
- case 16:
- width -= 2;
- break;
- case 8:
- width--;
- break;
- default:
- break;
- }
- }
- /* Output prefixes spaces if needed */
- if (! ((flags & FLAG_LEFT) ||
- ((flags & FLAG_ZERO) && (precision == -1))))
- {
- count = (precision == -1) ? 0 : precision;
- while (width-- > count)
- *p++ = ' ';
- }
- /* width has been adjusted for signs and alternatives */
- if (negative)
- {
- EMIT ('-');
- }
- else if (flags & FLAG_SHOWSIGN)
- {
- EMIT('+');
- }
- else if (flags & FLAG_SPACE)
- {
- EMIT(' ');
- }
-
- if (flags & FLAG_ALT)
- {
- switch (base)
- {
- case 8:
- EMIT('0');
- break;
- case 16:
- EMIT('0');
- EMIT(digits[16]);
- break;
- default:
- break;
- } /* switch base */
- }
-
- /* Output prefixed zero padding if needed */
- if (flags & FLAG_ZERO)
- {
- if (precision == -1)
- precision = width;
- while (precision-- > 0)
- {
- EMIT('0');
- width--;
- }
- }
-
- /* Output the number itself */
- while (*(++pointer))
- {
- EMIT(*pointer);
- }
-
- /* Output trailing spaces if needed */
- if (flags & FLAG_LEFT)
- {
- while (width-- > 0)
- EMIT(' ');
- }
-
- EMIT('\0');
-
- return (int)(p - buf - 1);
- }
- static char *vasnprintf (char *resultbuf, size_t *lengthp, const char *format, va_list args)
- {
- char_directives d;
- arguments a;
- if (printf_parse (format, &d, &a) < 0)
- {
- errno = EINVAL;
- return NULL;
- }
- #define CLEANUP() \
- free (d.dir); \
- if (a.arg) \
- free (a.arg);
- if (printf_fetchargs (args, &a) < 0)
- {
- CLEANUP ();
- errno = EINVAL;
- return NULL;
- }
- {
- char *buf =
- (char *) alloca (7 + d.max_width_length + d.max_precision_length + 6);
- const char *cp;
- unsigned int i;
- char_directive *dp;
- /* Output string accumulator. */
- char *result;
- size_t allocated;
- size_t length;
- if (resultbuf != NULL)
- {
- result = resultbuf;
- allocated = *lengthp;
- }
- else
- {
- result = NULL;
- allocated = 0;
- }
- length = 0;
- /* Invariants:
- result is either == resultbuf or == NULL or malloc-allocated.
- If length > 0, then result != NULL. */
- #define ENSURE_ALLOCATION(needed) \
- if ((needed) > allocated) \
- { \
- char *memory; \
- \
- allocated = (allocated > 0 ? 2 * allocated : 12); \
- if ((needed) > allocated) \
- allocated = (needed); \
- if (result == resultbuf || result == NULL) \
- memory = (char *) malloc (allocated); \
- else \
- memory = (char *) realloc (result, allocated); \
- \
- if (memory == NULL) \
- { \
- if (!(result == resultbuf || result == NULL)) \
- free (result); \
- CLEANUP (); \
- errno = ENOMEM; \
- return NULL; \
- } \
- if (result == resultbuf && length > 0) \
- memcpy (memory, result, length); \
- result = memory; \
- }
- for (cp = format, i = 0, dp = &d.dir[0]; ; cp = dp->dir_end, i++, dp++)
- {
- if (cp != dp->dir_start)
- {
- size_t n = dp->dir_start - cp;
- ENSURE_ALLOCATION (length + n);
- memcpy (result + length, cp, n);
- length += n;
- }
- if (i == d.count)
- break;
- /* Execute a single directive. */
- if (dp->conversion == '%')
- {
- if (!(dp->arg_index < 0))
- abort ();
- ENSURE_ALLOCATION (length + 1);
- result[length] = '%';
- length += 1;
- }
- else
- {
- if (!(dp->arg_index >= 0))
- abort ();
- if (dp->conversion == 'n')
- {
- switch (a.arg[dp->arg_index].type)
- {
- case TYPE_COUNT_SCHAR_POINTER:
- *a.arg[dp->arg_index].a.a_count_schar_pointer = (char)length;
- break;
- case TYPE_COUNT_SHORT_POINTER:
- *a.arg[dp->arg_index].a.a_count_short_pointer = (short)length;
- break;
- case TYPE_COUNT_INT_POINTER:
- *a.arg[dp->arg_index].a.a_count_int_pointer = (int)length;
- break;
- case TYPE_COUNT_LONGINT_POINTER:
- *a.arg[dp->arg_index].a.a_count_longint_pointer = (long)length;
- break;
- case TYPE_COUNT_LONGLONGINT_POINTER:
- *a.arg[dp->arg_index].a.a_count_longlongint_pointer = length;
- break;
- default:
- abort ();
- }
- }
- else
- {
- arg_type type = a.arg[dp->arg_index].type;
- char *p;
- unsigned int prefix_count;
- int prefixes[2];
- unsigned int tmp_length;
- char tmpbuf[700];
- char *tmp;
- /* Allocate a temporary buffer of sufficient size for calling
- sprintf. */
- {
- unsigned int width;
- unsigned int precision;
- width = 0;
- if (dp->width_start != dp->width_end)
- {
- if (dp->width_arg_index >= 0)
- {
- int arg;
- if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
- abort ();
- arg = a.arg[dp->width_arg_index].a.a_int;
- width = (arg < 0 ? -arg : arg);
- }
- else
- {
- const char *digitp = dp->width_start;
- do
- width = width * 10 + (*digitp++ - '0');
- while (digitp != dp->width_end);
- }
- }
- precision = 6;
- if (dp->precision_start != dp->precision_end)
- {
- if (dp->precision_arg_index >= 0)
- {
- int arg;
- if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
- abort ();
- arg = a.arg[dp->precision_arg_index].a.a_int;
- precision = (arg < 0 ? 0 : arg);
- }
- else
- {
- const char *digitp = dp->precision_start + 1;
- precision = 0;
- while (digitp != dp->precision_end)
- precision = precision * 10 + (*digitp++ - '0');
- }
- }
- switch (dp->conversion)
- {
- case 'd': case 'i': case 'u':
- if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
- tmp_length =
- (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
- * 0.30103 /* binary -> decimal */
- * 2 /* estimate for FLAG_GROUP */
- )
- + 1 /* turn floor into ceil */
- + 1; /* account for leading sign */
- else
- if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
- tmp_length =
- (unsigned int) (sizeof (unsigned long) * CHAR_BIT
- * 0.30103 /* binary -> decimal */
- * 2 /* estimate for FLAG_GROUP */
- )
- + 1 /* turn floor into ceil */
- + 1; /* account for leading sign */
- else
- tmp_length =
- (unsigned int) (sizeof (unsigned int) * CHAR_BIT
- * 0.30103 /* binary -> decimal */
- * 2 /* estimate for FLAG_GROUP */
- )
- + 1 /* turn floor into ceil */
- + 1; /* account for leading sign */
- break;
- case 'o':
- if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
- tmp_length =
- (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
- * 0.333334 /* binary -> octal */
- )
- + 1 /* turn floor into ceil */
- + 1; /* account for leading sign */
- else
- if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
- tmp_length =
- (unsigned int) (sizeof (unsigned long) * CHAR_BIT
- * 0.333334 /* binary -> octal */
- )
- + 1 /* turn floor into ceil */
- + 1; /* account for leading sign */
- else
- tmp_length =
- (unsigned int) (sizeof (unsigned int) * CHAR_BIT
- * 0.333334 /* binary -> octal */
- )
- + 1 /* turn floor into ceil */
- + 1; /* account for leading sign */
- break;
- case 'x': case 'X':
- if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
- tmp_length =
- (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
- * 0.25 /* binary -> hexadecimal */
- )
- + 1 /* turn floor into ceil */
- + 2; /* account for leading sign or alternate form */
- else
- if (type == TYPE_INT64 || type == TYPE_UINT64)
- tmp_length =
- (unsigned int) (sizeof (u__int64_t) * CHAR_BIT
- * 0.25 /* binary -> hexadecimal */
- )
- + 1 /* turn floor into ceil */
- + 2; /* account for leading sign or alternate form */
- else
- if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
- tmp_length =
- (unsigned int) (sizeof (unsigned long) * CHAR_BIT
- * 0.25 /* binary -> hexadecimal */
- )
- + 1 /* turn floor into ceil */
- + 2; /* account for leading sign or alternate form */
- else
- tmp_length =
- (unsigned int) (sizeof (unsigned int) * CHAR_BIT
- * 0.25 /* binary -> hexadecimal */
- )
- + 1 /* turn floor into ceil */
- + 2; /* account for leading sign or alternate form */
- break;
- case 'f': case 'F':
- if (type == TYPE_LONGDOUBLE)
- tmp_length =
- (unsigned int) (LDBL_MAX_EXP
- * 0.30103 /* binary -> decimal */
- * 2 /* estimate for FLAG_GROUP */
- )
- + 1 /* turn floor into ceil */
- + precision
- + 10; /* sign, decimal point etc. */
- else
- tmp_length =
- (unsigned int) (DBL_MAX_EXP
- * 0.30103 /* binary -> decimal */
- * 2 /* estimate for FLAG_GROUP */
- )
- + 1 /* turn floor into ceil */
- + precision
- + 10; /* sign, decimal point etc. */
- break;
- case 'e': case 'E': case 'g': case 'G':
- case 'a': case 'A':
- tmp_length =
- precision
- + 12; /* sign, decimal point, exponent etc. */
- break;
- case 'c':
- tmp_length = 1;
- break;
- case 's':
- tmp_length = (unsigned int)strlen (a.arg[dp->arg_index].a.a_string);
- break;
- case 'p':
- tmp_length =
- (unsigned int) (sizeof (void *) * CHAR_BIT
- * 0.25 /* binary -> hexadecimal */
- )
- + 1 /* turn floor into ceil */
- + 2; /* account for leading 0x */
- break;
- default:
- abort ();
- }
- if (tmp_length < width)
- tmp_length = width;
- tmp_length++; /* account for trailing NUL */
- }
- if (tmp_length <= sizeof (tmpbuf))
- tmp = tmpbuf;
- else
- {
- tmp = (char *) malloc (tmp_length);
- if (tmp == NULL)
- {
- /* Out of memory. */
- if (!(result == resultbuf || result == NULL))
- free (result);
- CLEANUP ();
- errno = ENOMEM;
- return NULL;
- }
- }
- /* Construct the format string for calling snprintf or
- sprintf. */
- p = buf;
- *p++ = '%';
- if (dp->flags & FLAG_GROUP)
- *p++ = '\'';
- if (dp->flags & FLAG_LEFT)
- *p++ = '-';
- if (dp->flags & FLAG_SHOWSIGN)
- *p++ = '+';
- if (dp->flags & FLAG_SPACE)
- *p++ = ' ';
- if (dp->flags & FLAG_ALT)
- *p++ = '#';
- if (dp->flags & FLAG_ZERO)
- *p++ = '0';
- if (dp->width_start != dp->width_end)
- {
- size_t n = dp->width_end - dp->width_start;
- memcpy (p, dp->width_start, n);
- p += n;
- }
- if (dp->precision_start != dp->precision_end)
- {
- size_t n = dp->precision_end - dp->precision_start;
- memcpy (p, dp->precision_start, n);
- p += n;
- }
- switch (type)
- {
- case TYPE_INT64:
- case TYPE_UINT64:
- *p++ = 'I';
- *p++ = '6';
- *p++ = '4';
- break;
- case TYPE_LONGLONGINT:
- case TYPE_ULONGLONGINT:
- *p++ = 'I';
- *p++ = '6';
- *p++ = '4';
- break;
- *p++ = 'l';
- /*FALLTHROUGH*/
- case TYPE_LONGINT:
- case TYPE_ULONGINT:
- *p++ = 'l';
- break;
- case TYPE_LONGDOUBLE:
- *p++ = 'L';
- break;
- default:
- break;
- }
- *p = dp->conversion;
- p[1] = '\0';
- /* Construct the arguments for calling snprintf or sprintf. */
- prefix_count = 0;
- if (dp->width_arg_index >= 0)
- {
- if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
- abort ();
- prefixes[prefix_count++] = a.arg[dp->width_arg_index].a.a_int;
- }
- if (dp->precision_arg_index >= 0)
- {
- if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
- abort ();
- prefixes[prefix_count++] = a.arg[dp->precision_arg_index].a.a_int;
- }
- for (;;)
- {
- size_t maxlen;
- int count;
- int retcount;
- maxlen = allocated - length;
- count = -1;
- retcount = 0;
- #define SNPRINTF_BUF(arg) \
- switch (prefix_count) \
- { \
- case 0: \
- count = sprintf (tmp, buf, arg); \
- break; \
- case 1: \
- count = sprintf (tmp, buf, prefixes[0], arg); \
- break; \
- case 2: \
- count = sprintf (tmp, buf, prefixes[0], prefixes[1],\
- arg); \
- break; \
- default: \
- abort (); \
- }
- switch (type)
- {
- case TYPE_SCHAR:
- {
- int arg = a.arg[dp->arg_index].a.a_schar;
- SNPRINTF_BUF (arg);
- }
- break;
- case TYPE_UCHAR:
- {
- unsigned int arg = a.arg[dp->arg_index].a.a_uchar;
- SNPRINTF_BUF (arg);
- }
- break;
- case TYPE_SHORT:
- {
- int arg = a.arg[dp->arg_index].a.a_short;
- SNPRINTF_BUF (arg);
- }
- break;
- case TYPE_USHORT:
- {
- unsigned int arg = a.arg[dp->arg_index].a.a_ushort;
- SNPRINTF_BUF (arg);
- }
- break;
- case TYPE_INT:
- {
- int arg = a.arg[dp->arg_index].a.a_int;
- SNPRINTF_BUF (arg);
- }
- break;
- case TYPE_UINT:
- {
- unsigned int arg = a.arg[dp->arg_index].a.a_uint;
- SNPRINTF_BUF (arg);
- }
- break;
- case TYPE_LONGINT:
- {
- long int arg = a.arg[dp->arg_index].a.a_longint;
- SNPRINTF_BUF (arg);
- }
- break;
- case TYPE_ULONGINT:
- {
- unsigned long int arg = a.arg[dp->arg_index].a.a_ulongint;
- SNPRINTF_BUF (arg);
- }
- break;
- case TYPE_INT64:
- {
- __int64 arg = a.arg[dp->arg_index].a.a_int64;
- SNPRINTF_BUF (arg);
- }
- break;
- case TYPE_UINT64:
- {
- u__int64_t arg = a.arg[dp->arg_index].a.a_uint64;
- SNPRINTF_BUF (arg);
- }
- break;
- case TYPE_LONGLONGINT:
- case TYPE_ULONGLONGINT:
- {
- unsigned long long int arg = a.arg[dp->arg_index].a.a_ulonglongint;
- int width;
- int precision;
- width = 0;
- if (dp->width_start != dp->width_end)
- {
- if (dp->width_arg_index >= 0)
- {
- int arg;
-
- if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
- abort ();
- arg = a.arg[dp->width_arg_index].a.a_int;
- width = (arg < 0 ? -arg : arg);
- }
- else
- {
- const char *digitp = dp->width_start;
-
- do
- width = width * 10 + (*digitp++ - '0');
- while (digitp != dp->width_end);
- }
- }
- precision = -1;
- if (dp->precision_start != dp->precision_end)
- {
- if (dp->precision_arg_index >= 0)
- {
- int arg;
-
- if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
- abort ();
- arg = a.arg[dp->precision_arg_index].a.a_int;
- precision = (arg < 0 ? 0 : arg);
- }
- else
- {
- const char *digitp = dp->precision_start + 1;
-
- precision = 0;
- do
- precision = precision * 10 + (*digitp++ - '0');
- while (digitp != dp->precision_end);
- }
- }
-
- count = print_long_long (tmp, tmp_length,
- width, precision,
- dp->flags,
- dp->conversion,
- arg);
- }
- break;
- case TYPE_DOUBLE:
- {
- double arg = a.arg[dp->arg_index].a.a_double;
- SNPRINTF_BUF (arg);
- }
- break;
- case TYPE_LONGDOUBLE:
- {
- long double arg = a.arg[dp->arg_index].a.a_longdouble;
- SNPRINTF_BUF (arg);
- }
- break;
- case TYPE_CHAR:
- {
- int arg = a.arg[dp->arg_index].a.a_char;
- SNPRINTF_BUF (arg);
- }
- break;
- case TYPE_STRING:
- {
- const char *arg = a.arg[dp->arg_index].a.a_string;
- SNPRINTF_BUF (arg);
- }
- break;
- case TYPE_POINTER:
- {
- void *arg = a.arg[dp->arg_index].a.a_pointer;
- SNPRINTF_BUF (arg);
- }
- break;
- default:
- abort ();
- }
- /* Attempt to handle failure. */
- if (count < 0)
- {
- if (!(result == resultbuf || result == NULL))
- free (result);
- CLEANUP ();
- errno = EINVAL;
- return NULL;
- }
- if (count >= (int)tmp_length)
- /* tmp_length was incorrectly calculated - fix the
- code above! */
- abort ();
- /* Make room for the result. */
- if (count >= (int)maxlen)
- {
- /* Need at least count bytes. But allocate
- proportionally, to avoid looping eternally if
- snprintf() reports a too small count. */
- size_t n = length + count;
- if (n < 2 * allocated)
- n = 2 * allocated;
- ENSURE_ALLOCATION (n);
- }
- /* Append the sprintf() result. */
- memcpy (result + length, tmp, count);
- if (tmp != tmpbuf)
- free (tmp);
- length += count;
- break;
- }
- }
- }
- }
- /* Add the final NUL. */
- ENSURE_ALLOCATION (length + 1);
- result[length] = '\0';
- if (result != resultbuf && length + 1 < allocated)
- {
- /* Shrink the allocated memory if possible. */
- char *memory;
- memory = (char *) realloc (result, length + 1);
- if (memory != NULL)
- result = memory;
- }
- CLEANUP ();
- *lengthp = length;
- return result;
- }
- }
- #define _XT(str) str
- #define CHR_LT_ _XT('<')
- #define CHR_GT_ _XT('>')
- #define CHR_AMP_ _XT('&')
- #define CHR_APOS_ _XT('\'')
- #define CHR_QUOT_ _XT('"')
- #define XML_LT_ _XT("<")
- #define XML_GT_ _XT(">")
- #define XML_AMP_ _XT("&")
- #define XML_APOS_ _XT("'")
- #define XML_QUOT_ _XT(""")
- enum
- {
- LT_SIZE_ = 4, /**< Size of < */
- GT_SIZE_ = 4, /**< Size of > */
- AMP_SIZE_ = 5, /**< Size of & */
- APOS_SIZE_ = 6, /**< Size of ' */
- QUOT_SIZE_ = 6 /**< Size of " */
- };
- TOOLKIT_API char *str_xml_escape(const char *src)
- {
- char *p = (char *) src;
- char *escaped = NULL;
- unsigned int len = 0;
- assert (src != NULL);
- /* We first need to calculate the size of the new escaped string. */
- while (*p != _XT('\0'))
- {
- switch (*p)
- {
- case CHR_LT_:
- len += LT_SIZE_;
- break;
- case CHR_GT_:
- len += GT_SIZE_;
- break;
- case CHR_AMP_:
- len += AMP_SIZE_;
- break;
- case CHR_APOS_:
- len += APOS_SIZE_;
- break;
- case CHR_QUOT_:
- len += QUOT_SIZE_;
- break;
- default:
- len += 1;
- break;
- }
- p += 1;
- }
- /* Allocate new string (if necessary). */
- escaped = calloc (len + 1, sizeof (char));
- /* Append characters to new string, escaping the needed ones. */
- p = (char *) src;
- len = 0;
- while (*p != _XT('\0'))
- {
- switch (*p)
- {
- case CHR_LT_:
- memcpy (&escaped[len], XML_LT_, LT_SIZE_);
- len += LT_SIZE_;
- break;
- case CHR_GT_:
- memcpy (&escaped[len], XML_GT_, GT_SIZE_);
- len += GT_SIZE_;
- break;
- case CHR_AMP_:
- memcpy (&escaped[len], XML_AMP_, AMP_SIZE_);
- len += AMP_SIZE_;
- break;
- case CHR_APOS_:
- memcpy (&escaped[len], XML_APOS_, APOS_SIZE_);
- len += APOS_SIZE_;
- break;
- case CHR_QUOT_:
- memcpy (&escaped[len], XML_QUOT_, QUOT_SIZE_);
- len += QUOT_SIZE_;
- break;
- default:
- escaped[len] = *p;
- len += 1;
- break;
- }
- p += 1;
- }
- return escaped;
- }
- /*!
- * @brief
- * @param[in] cmdstart
- * @param[out] argv, args, numargs, numchars
- * @return :
- */
- TOOLKIT_API void str_parse_cmdline (char *cmdstart, char **argv, char *args, int *numargs, int *numchars)
- {
- char *p;
- char c;
- int inquote; /* 1 = inside quotes */
- int copychar; /* 1 = copy char to *args */
- unsigned numslash; /* num of backslashes seen */
- assert(numargs);
- assert(numchars);
- *numchars = 0;
- *numargs = 1; /* the program name at least */
- /* first scan the program name, copy it, and count the bytes */
- p = cmdstart;
- if (argv)
- *argv++ = args;
- /* A quoted program name is handled here. The handling is much
- simpler than for other arguments. Basically, whatever lies
- between the leading double-quote and next one, or a terminal null
- character is simply accepted. Fancier handling is not required
- because the program name must be a legal NTFS/HPFS file name.
- Note that the double-quote characters are not copied, nor do they
- contribute to numchars. */
- inquote = FALSE;
- do {
- if (*p == '\"' )
- {
- inquote = !inquote;
- c = (char) *p++;
- continue;
- }
- ++*numchars;
- if (args)
- *args++ = *p; /*char in cmdstart, the capacity of {args} equals with numchars*/
- c = (char) *p++;
- } while ( (c != 0 && (inquote || (c !=' ' && c != '\t'))) );
- if ( c == 0 ) {
- p--;
- } else {
- if (args)
- *(args-1) = 0;
- }
- inquote = 0;
- /* loop on each argument */
- for(;;) {
- if ( *p ) {
- while (*p == ' ' || *p == '\t')
- ++p;
- }
- if (*p == 0)
- break; /* end of args */
- /* scan an argument */
- if (argv)
- *argv++ = args; /* store ptr to arg */
- ++*numargs;
- /* loop through scanning one argument */
- for (;;) {
- copychar = 1;
- /* Rules: 2N backslashes + " ==> N backslashes and begin/end quote
- 2N+1 backslashes + " ==> N backslashes + literal "
- N backslashes ==> N backslashes */
- numslash = 0;
- while (*p == '\\') {
- /* count number of backslashes for use below */
- ++p;
- ++numslash;
- }
- if (*p == '\"') {
- /* if 2N backslashes before, start/end quote, otherwise
- copy literally */
- if (numslash % 2 == 0) {
- if (inquote && p[1] == '\"') {
- p++; /* Double quote inside quoted string */
- } else { /* skip first quote char and copy second */
- copychar = 0; /* don't copy quote */
- inquote = !inquote;
- }
- }
- numslash /= 2; /* divide numslash by two */
- }
- /* copy slashes */
- while (numslash--) {
- if (args)
- *args++ = '\\';
- ++*numchars;
- }
- /* if at end of arg, break loop */
- if (*p == 0 || (!inquote && (*p == ' ' || *p == '\t')))
- break;
- if (copychar) {
- if (args)
- *args++ = *p;
- ++*numchars;
- }
- ++p;
- }
- /* null-terminate the argument */
- if (args)
- *args++ = 0; /* terminate string */
- ++*numchars;
- }
- /* We put one last argument in -- a null ptr */
- if (argv)
- *argv++ = NULL;
- ++*numargs;
- }
|