strutil.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537
  1. #include "precompile.h"
  2. #include <float.h>
  3. #include <ctype.h>
  4. #include <stdarg.h>
  5. #include <assert.h>
  6. #include <string.h>
  7. #include <limits.h> /* SSIZE_MAX */
  8. #include "strutil.h"
  9. #include "memutil.h"
  10. #include "toolkit.h"
  11. #include <winpr/wtypes.h>
  12. #include <winpr/string.h>
  13. #include <winpr/crt.h>
  14. #include "dbgutil.h"
  15. #pragma warning(disable : 4311)
  16. TOOLKIT_API const char *memstr(const char *buf, int n, const char *str)
  17. {
  18. const char *p, *e;
  19. size_t len;
  20. TOOLKIT_ASSERT(buf);
  21. TOOLKIT_ASSERT(str);
  22. len = strlen(str);
  23. for (p = buf, e = buf+n-len; p <= e; ++p) {
  24. if (memcmp(p, str, len) == 0)
  25. return p;
  26. }
  27. return NULL;
  28. }
  29. TOOLKIT_API char *memstr1(char *buf, int n, const char *str)
  30. {
  31. char *p, *e;
  32. size_t len;
  33. TOOLKIT_ASSERT(buf);
  34. TOOLKIT_ASSERT(str);
  35. len = strlen(str);
  36. for (p = buf, e = buf+n-len; p <= e; ++p) {
  37. if (memcmp(p, str, len) == 0)
  38. return p;
  39. }
  40. return NULL;
  41. }
  42. TOOLKIT_API const char *memrstr(const char *buf, int n, const char *str)
  43. {
  44. const char *p, *e;
  45. size_t len;
  46. TOOLKIT_ASSERT(buf);
  47. TOOLKIT_ASSERT(str);
  48. len = strlen(str);
  49. for (p = buf, e = buf+n-len; e >= p; e--) {
  50. if (memcmp(e, str, len) == 0)
  51. return e;
  52. }
  53. return NULL;
  54. }
  55. TOOLKIT_API const char *memistr(const char *buf, int n, const char *str)
  56. {
  57. const char *p, *e;
  58. size_t len;
  59. TOOLKIT_ASSERT(buf);
  60. TOOLKIT_ASSERT(str);
  61. len = strlen(str);
  62. for (p = buf, e = buf+n-len; p <= e; ++p) {
  63. if (_memicmp(p, str, len) == 0)
  64. return p;
  65. }
  66. return NULL;
  67. }
  68. TOOLKIT_API const char *memristr(const char *buf, int n, const char *str)
  69. {
  70. const char *p, *e;
  71. size_t len;
  72. TOOLKIT_ASSERT(buf);
  73. TOOLKIT_ASSERT(str);
  74. len = strlen(str);
  75. for (p = buf, e = buf+n-len; e >= p; e--) {
  76. if (_memicmp(e, str, len) == 0)
  77. return e;
  78. }
  79. return NULL;
  80. }
  81. #ifdef _WIN32
  82. /* Naive implementation of memmem() */
  83. TOOLKIT_API void *memmem(const void *haystack, size_t haystacklen,
  84. const void *needle, size_t needlelen)
  85. {
  86. size_t i;
  87. char const *hs = haystack;
  88. if (needlelen == 0)
  89. return (void *)haystack;
  90. if (needlelen > haystacklen || haystack == NULL || needle == NULL)
  91. return NULL;
  92. for (i = 0; i <= haystacklen - needlelen; i++) {
  93. if (memcmp(hs + i, needle, needlelen) == 0)
  94. return (void *)(hs + i);
  95. }
  96. return NULL;
  97. }
  98. /* Naive implementation of strcasestr() */
  99. TOOLKIT_API char *strcasestr(const char *haystack,
  100. const char *needle)
  101. {
  102. unsigned char lcn, ucn;
  103. unsigned i;
  104. if (haystack == NULL || needle == NULL)
  105. return NULL;
  106. lcn = ucn = needle[0];
  107. if (isupper(lcn))
  108. lcn = tolower(lcn);
  109. else if (islower(ucn))
  110. ucn = toupper(ucn);
  111. if (lcn == 0)
  112. return (char *)haystack;
  113. while (haystack[0] != 0) {
  114. if (lcn == haystack[0] || ucn == haystack[0]) {
  115. for (i = 1; ; i++) {
  116. char n = needle[i], h = haystack[i];
  117. if (n == 0)
  118. return (char *)haystack;
  119. if (h == 0)
  120. return NULL;
  121. if (isupper(n)) n = tolower(n);
  122. if (isupper(h)) h = tolower(h);
  123. if (n != h)
  124. break;
  125. }
  126. }
  127. haystack++;
  128. }
  129. return NULL; /* Not found */
  130. }
  131. #endif //_WIN32
  132. TOOLKIT_API char *strltrim(char *s, const char *trims)
  133. {
  134. char *k = s;
  135. char *p;
  136. while (strchr(trims, *k))
  137. ++k;
  138. if (k != s)
  139. for (p = s; *p++ = *k++;);
  140. return s;
  141. }
  142. TOOLKIT_API char *strrtrim(char *s, const char *trims)
  143. {
  144. size_t n = strlen(s);
  145. while (n && strchr(trims, s[n-1]))
  146. n--;
  147. s[n] = 0;
  148. return s;
  149. }
  150. TOOLKIT_API char *strtrim(char *s, const char *trims)
  151. {
  152. return strltrim(strrtrim(s, trims), trims);
  153. }
  154. TOOLKIT_API char *strnormws(char *s)
  155. {
  156. char *in = s, *out = s;
  157. int ch;
  158. while (isspace((ch = *in++)))
  159. ;
  160. if (ch != '\0')
  161. for (;;) {
  162. *out++ = ch;
  163. ch = *in++;
  164. if (ch == '\0')
  165. break;
  166. if (isspace(ch)) {
  167. while(isspace((ch = *in++)))
  168. ;
  169. if (ch == '\0')
  170. break;
  171. *out++ = ' ';
  172. }
  173. }
  174. *out = '\0';
  175. return s;
  176. }
  177. #define ONES_WORD 0x01010101
  178. #define EIGHTS_WORD 0x80808080
  179. #ifdef _WIN32
  180. #define word_has_nullbyte(w) (((w) - 0x01010101) & ~(w) & EIGHTS_WORD)
  181. TOOLKIT_API char *stpcpy(char *dst, const char *src)
  182. {
  183. const int *p = (const int *)src;
  184. int *q = (int*)dst;
  185. const char *sp;
  186. char *sq;
  187. if (!((int)p&(sizeof(int)-1)) && !((int)q&(sizeof(int)-1))) {
  188. int x = *p;
  189. while (!word_has_nullbyte(x)) {
  190. *q++ = x;
  191. x = *++p;
  192. }
  193. }
  194. sp = (const char*)p;
  195. sq = (char*)q;
  196. while (*sq = *sp++)
  197. sq++;
  198. return sq;
  199. }
  200. TOOLKIT_API char* strsep(char** stringp, const char* delim)
  201. {
  202. char* str, * end;
  203. str = *stringp + strspn(*stringp, delim);
  204. if (*str == '\0') {
  205. end = str;
  206. str = NULL;
  207. }
  208. else {
  209. end = str + strcspn(str, delim);
  210. if (*end != '\0')
  211. *end++ = '\0';
  212. }
  213. *stringp = end;
  214. return str;
  215. }
  216. TOOLKIT_API char* strtok_r(char* str, const char* delim, char** tracker)
  217. {
  218. if (str != NULL)
  219. *tracker = str;
  220. return strsep(tracker, delim);
  221. }
  222. #endif //_WIN32
  223. TOOLKIT_API char * strreplace(const char *src, const char *old, const char *news)
  224. {
  225. # define SUBS_TOP 32
  226. const char *subs[SUBS_TOP], *p, *sub_start, *src_without_old;
  227. char *dest, *ret;
  228. size_t old_len, new_len, dest_size, tmp;
  229. int i;
  230. old_len = strlen(old);
  231. new_len = strlen(news);
  232. /* Find substrings and compute size of result */
  233. dest_size = 1;
  234. src_without_old = src;
  235. for (i = 0, p = src ;; p = sub_start + old_len) {
  236. sub_start = strstr(p, old);
  237. if (i < SUBS_TOP)
  238. subs[i++] = sub_start;
  239. if (sub_start == NULL)
  240. break;
  241. dest_size += new_len;
  242. if (dest_size < new_len) {
  243. return NULL;
  244. }
  245. src_without_old += old_len;
  246. }
  247. tmp = (p - src_without_old) + strlen(p);
  248. dest_size += tmp;
  249. if (dest_size < tmp) {
  250. return NULL;
  251. }
  252. /* Make result string */
  253. ret = malloc(dest_size);
  254. if (ret) {
  255. dest = ret;
  256. for (i = 0;; ) {
  257. p = (i < SUBS_TOP ? subs[i++] : strstr(src, old));
  258. if (p == NULL)
  259. break;
  260. tmp = p - src;
  261. memcpy(dest, src, tmp);
  262. src += tmp + old_len;
  263. dest = stpcpy(dest + tmp, news);
  264. }
  265. strcpy(dest, src);
  266. }
  267. return ret;
  268. }
  269. TOOLKIT_API char **strsplit(const char *s, const char *delim)
  270. {
  271. const char *sp;
  272. char *p, **ret, **rp;
  273. size_t i;
  274. /* skip to first substring */
  275. s += strspn(s, delim);
  276. /* allocate array */
  277. for (i = 1, sp = s; *sp != '\0'; i++) {
  278. sp += strcspn(sp, delim);
  279. sp += strspn(sp, delim);
  280. }
  281. ret = rp = malloc(i * sizeof(char *));
  282. if (ret != NULL) {
  283. while (*s != '\0') {
  284. /* found new substring */
  285. i = strcspn(s, delim);
  286. *rp++ = p = malloc(i + 1);
  287. if (p == NULL) {
  288. strfreev(ret);
  289. return NULL;
  290. }
  291. memcpy(p, s, i);
  292. p[i] = '\0';
  293. s += i;
  294. s += strspn(s, delim);
  295. }
  296. *rp = NULL;
  297. }
  298. return ret;
  299. }
  300. TOOLKIT_API void strfreev(char **strings)
  301. {
  302. if (strings) {
  303. char **p;
  304. for (p = strings; *p != NULL; ++p)
  305. free(*p);
  306. free(strings);
  307. }
  308. }
  309. TOOLKIT_API char *strsub(char *dest,char *src,size_t offset,size_t len)
  310. {
  311. *dest = '\0';
  312. if (!memchr(src, '\0', offset))
  313. strncat(dest, src + offset, len);
  314. return dest;
  315. }
  316. TOOLKIT_API char *strleft(char *dest, char *src, size_t len)
  317. {
  318. *dest = '\0';
  319. strncat(dest, src, len);
  320. return dest;
  321. }
  322. TOOLKIT_API char *strright(char *dest, char *src, size_t len)
  323. {
  324. size_t src_len;
  325. src_len = strlen(src);
  326. if(src_len > len)
  327. src += src_len - len;
  328. return strcpy(dest, src);
  329. }
  330. static char * TOOLKIT_CC __strallocv(int stack, const char *arg1, va_list arg_list)
  331. {
  332. size_t len;
  333. const char *arg;
  334. char *ret, *end;
  335. int bad;
  336. va_list ap;
  337. /* compute length of result string */
  338. len = 1;
  339. bad = 0;
  340. //TODO: need to test its validity
  341. #ifdef _WIN32
  342. /*gcc compile error: assignment to expression with array type*/
  343. ap = arg_list;
  344. #else
  345. va_copy(ap, arg_list);
  346. #endif
  347. for (arg = arg1; arg != (char *)0; arg = va_arg(ap, char *)) {
  348. size_t arglen = strlen(arg);
  349. len += arglen;
  350. if(len < arglen) {
  351. /* string length too large for size_t */
  352. bad = 1;
  353. break;
  354. }
  355. }
  356. if(bad) {
  357. return NULL;
  358. }
  359. /* create result string */
  360. if (!stack)
  361. ret = malloc(len);
  362. else
  363. ret = _alloca(len);
  364. if(ret != NULL) {
  365. end = ret;
  366. *end = '\0';
  367. #ifdef _WIN32
  368. /*gcc compile error: assignment to expression with array type*/
  369. ap = arg_list;
  370. #else
  371. va_copy(ap, arg_list);
  372. #endif
  373. for (arg = arg1; arg != (char *)0; arg = va_arg(ap, char *))
  374. end = stpcpy(end, arg);
  375. }
  376. return ret;
  377. }
  378. TOOLKIT_API char * TOOLKIT_CC stralloc(const char *arg1, ...)
  379. {
  380. char *r;
  381. va_list ap;
  382. va_start(ap, arg1);
  383. r = __strallocv(0, arg1, ap);
  384. va_end(ap);
  385. return r;
  386. }
  387. TOOLKIT_API char * TOOLKIT_CC stralloca(const char *arg1, ...)
  388. {
  389. char *r;
  390. va_list ap;
  391. va_start(ap, arg1);
  392. r = __strallocv(1, arg1, ap);
  393. va_end(ap);
  394. return r;
  395. }
  396. TOOLKIT_API void *memdup(const void *buf, int len)
  397. {
  398. void *ret;
  399. if (buf) {
  400. ret = malloc(len);
  401. if (ret) {
  402. memcpy(ret, buf, len);
  403. }
  404. } else {
  405. ret = NULL;
  406. }
  407. return ret;
  408. }
  409. TOOLKIT_API void *memdupa(const void *buf, int len)
  410. {
  411. void *ret;
  412. if (buf) {
  413. ret = _alloca(len);
  414. if (ret) {
  415. memcpy(ret, buf, len);
  416. }
  417. } else {
  418. ret = NULL;
  419. }
  420. return ret;
  421. }
  422. #ifdef _WIN32
  423. TOOLKIT_API char *strdupa(const char * s)
  424. {
  425. size_t size;
  426. char *p;
  427. TOOLKIT_ASSERT(s);
  428. size = strlen(s) + 1;
  429. if (size == 0)
  430. p = NULL;
  431. else if ((p = _alloca(size)) != NULL)
  432. memcpy(p, s, size);
  433. return p;
  434. }
  435. TOOLKIT_API char *strndup(const char *str, int n)
  436. {
  437. char *new_str;
  438. if (str) {
  439. new_str = (char*)malloc(n+1);
  440. strncpy(new_str, str, n);
  441. new_str[n] = '\0';
  442. } else {
  443. new_str = NULL;
  444. }
  445. return new_str;
  446. }
  447. #endif //_WIN32
  448. TOOLKIT_API char* strnfill(int length, int fill_char)
  449. {
  450. char *str;
  451. str = (char*)malloc(length + 1);
  452. memset (str, fill_char, length);
  453. str[length] = '\0';
  454. return str;
  455. }
  456. TOOLKIT_API int strcmp0 (const char *str1,const char *str2)
  457. {
  458. if (!str1)
  459. return -(str1 != str2);
  460. if (!str2)
  461. return str1 != str2;
  462. return strcmp (str1, str2);
  463. }
  464. TOOLKIT_API char* TOOLKIT_CC strdup_printf(const char *format, ...)
  465. {
  466. char *buffer;
  467. va_list args;
  468. va_start(args, format);
  469. buffer = strdup_vprintf(format, args);
  470. va_end(args);
  471. return buffer;
  472. }
  473. static char *vasnprintf (char *resultbuf, size_t *lengthp, const char *format, va_list args);
  474. TOOLKIT_API char* TOOLKIT_CC strdup_vprintf(const char *format, va_list args)
  475. {
  476. size_t length;
  477. return vasnprintf (NULL, &length, format, args);
  478. }
  479. TOOLKIT_API size_t strlcpy(char *dest, const char *src, size_t size)
  480. {
  481. const char *start;
  482. start = src;
  483. if (size) {
  484. while (--size && *src)
  485. *dest++ = *src++;
  486. *dest = '\0';
  487. }
  488. while (*src)
  489. ++src;
  490. return src - start;
  491. }
  492. TOOLKIT_API size_t strlcat(char * dest, const char *src, size_t size)
  493. {
  494. size_t len;
  495. char *end;
  496. for (end = dest; *end; ++end) ;
  497. len = end - dest;
  498. return len + strlcpy(end, src, size > len ? size - len : 0);
  499. }
  500. TOOLKIT_API ssize_t strscpy(char* dest, const char* src, size_t size)
  501. {
  502. size_t i;
  503. for (i = 0; i < size; i++)
  504. if ('\0' == (dest[i] = src[i]))
  505. return i > SSIZE_MAX ? TOOLKIT_E2BIG : (ssize_t)i;
  506. if (i == 0)
  507. return 0;
  508. dest[--i] = '\0';
  509. return TOOLKIT_E2BIG;
  510. }
  511. TOOLKIT_API char** strdupv (char **str_array)
  512. {
  513. if (str_array)
  514. {
  515. int i;
  516. char **retval;
  517. i = 0;
  518. while (str_array[i])
  519. ++i;
  520. retval = (char**)malloc(i+1);
  521. i = 0;
  522. while (str_array[i])
  523. {
  524. retval[i] = _strdup (str_array[i]);
  525. ++i;
  526. }
  527. retval[i] = NULL;
  528. return retval;
  529. }
  530. else
  531. return NULL;
  532. }
  533. TOOLKIT_API char* strjoinv (const char *separator,char **str_array)
  534. {
  535. char *string;
  536. char *ptr;
  537. if (separator == NULL)
  538. separator = "";
  539. if (*str_array)
  540. {
  541. int i;
  542. size_t len;
  543. size_t separator_len;
  544. separator_len = strlen (separator);
  545. /* First part, getting length */
  546. len = 1 + strlen (str_array[0]);
  547. for (i = 1; str_array[i] != NULL; i++)
  548. len += strlen (str_array[i]);
  549. len += separator_len * (i - 1);
  550. /* Second part, building string */
  551. string = (char*)malloc(len);
  552. ptr = stpcpy (string, *str_array);
  553. for (i = 1; str_array[i] != NULL; i++)
  554. {
  555. ptr = stpcpy (ptr, separator);
  556. ptr = stpcpy (ptr, str_array[i]);
  557. }
  558. }
  559. else
  560. string = _strdup ("");
  561. return string;
  562. }
  563. TOOLKIT_API char* TOOLKIT_CC strjoin (const char *separator, ...)
  564. {
  565. char *string, *s;
  566. va_list args;
  567. size_t len;
  568. size_t separator_len;
  569. char *ptr;
  570. if (separator == NULL)
  571. separator = "";
  572. separator_len = strlen (separator);
  573. va_start (args, separator);
  574. s = va_arg (args, char*);
  575. if (s)
  576. {
  577. /* First part, getting length */
  578. len = 1 + strlen (s);
  579. s = va_arg (args, char*);
  580. while (s)
  581. {
  582. len += separator_len + strlen (s);
  583. s = va_arg (args, char*);
  584. }
  585. va_end (args);
  586. /* Second part, building string */
  587. string = (char*)malloc(len);
  588. va_start (args, separator);
  589. s = va_arg (args, char*);
  590. ptr = stpcpy (string, s);
  591. s = va_arg (args, char*);
  592. while (s)
  593. {
  594. ptr = stpcpy (ptr, separator);
  595. ptr = stpcpy (ptr, s);
  596. s = va_arg (args, char*);
  597. }
  598. }
  599. else
  600. string = _strdup ("");
  601. va_end (args);
  602. return string;
  603. }
  604. TOOLKIT_API int str_has_suffix (const char *str, const char *suffix)
  605. {
  606. size_t str_len;
  607. size_t suffix_len;
  608. str_len = strlen (str);
  609. suffix_len = strlen (suffix);
  610. if (str_len < suffix_len)
  611. return FALSE;
  612. return strcmp (str + str_len - suffix_len, suffix) == 0;
  613. }
  614. TOOLKIT_API int str_has_prefix (const char *str,const char *prefix)
  615. {
  616. size_t str_len;
  617. size_t prefix_len;
  618. str_len = strlen (str);
  619. prefix_len = strlen (prefix);
  620. if (str_len < prefix_len)
  621. return FALSE;
  622. return strncmp (str, prefix, prefix_len) == 0;
  623. }
  624. TOOLKIT_API unsigned int strv_length (char **str_array)
  625. {
  626. unsigned int i = 0;
  627. while (str_array[i])
  628. ++i;
  629. return i;
  630. }
  631. /* asnprintf, extract from glib */
  632. #define FLAG_GROUP 1 /* ' flag */
  633. #define FLAG_LEFT 2 /* - flag */
  634. #define FLAG_SHOWSIGN 4 /* + flag */
  635. #define FLAG_SPACE 8 /* space flag */
  636. #define FLAG_ALT 16 /* # flag */
  637. #define FLAG_ZERO 32
  638. /* A parsed directive. */
  639. typedef struct
  640. {
  641. const char* dir_start;
  642. const char* dir_end;
  643. int flags;
  644. const char* width_start;
  645. const char* width_end;
  646. int width_arg_index;
  647. const char* precision_start;
  648. const char* precision_end;
  649. int precision_arg_index;
  650. char conversion; /* d i o u x X f e E g G c s p n U % but not C S */
  651. int arg_index;
  652. }
  653. char_directive;
  654. /* A parsed format string. */
  655. typedef struct
  656. {
  657. unsigned int count;
  658. char_directive *dir;
  659. unsigned int max_width_length;
  660. unsigned int max_precision_length;
  661. }
  662. char_directives;
  663. /* Argument types */
  664. typedef enum
  665. {
  666. TYPE_NONE,
  667. TYPE_SCHAR,
  668. TYPE_UCHAR,
  669. TYPE_SHORT,
  670. TYPE_USHORT,
  671. TYPE_INT,
  672. TYPE_UINT,
  673. TYPE_LONGINT,
  674. TYPE_ULONGINT,
  675. TYPE_LONGLONGINT,
  676. TYPE_ULONGLONGINT,
  677. TYPE_INT64,
  678. TYPE_UINT64,
  679. TYPE_DOUBLE,
  680. TYPE_LONGDOUBLE,
  681. TYPE_CHAR,
  682. TYPE_STRING,
  683. TYPE_POINTER,
  684. TYPE_COUNT_SCHAR_POINTER,
  685. TYPE_COUNT_SHORT_POINTER,
  686. TYPE_COUNT_INT_POINTER,
  687. TYPE_COUNT_LONGINT_POINTER
  688. , TYPE_COUNT_LONGLONGINT_POINTER
  689. } arg_type;
  690. /* Polymorphic argument */
  691. typedef struct
  692. {
  693. arg_type type;
  694. union
  695. {
  696. signed char a_schar;
  697. unsigned char a_uchar;
  698. short a_short;
  699. unsigned short a_ushort;
  700. int a_int;
  701. unsigned int a_uint;
  702. long int a_longint;
  703. unsigned long int a_ulongint;
  704. long long int a_longlongint;
  705. unsigned long long int a_ulonglongint;
  706. __int64 a_int64;
  707. u__int64_t a_uint64;
  708. float a_float;
  709. double a_double;
  710. long double a_longdouble;
  711. int a_char;
  712. const char* a_string;
  713. void* a_pointer;
  714. signed char * a_count_schar_pointer;
  715. short * a_count_short_pointer;
  716. int * a_count_int_pointer;
  717. long int * a_count_longint_pointer;
  718. long long int * a_count_longlongint_pointer;
  719. }
  720. a;
  721. }
  722. argument;
  723. typedef struct
  724. {
  725. unsigned int count;
  726. argument *arg;
  727. }
  728. arguments;
  729. static int printf_parse (const char *format, char_directives *d, arguments *a)
  730. {
  731. const char *cp = format; /* pointer into format */
  732. int arg_posn = 0; /* number of regular arguments consumed */
  733. unsigned int d_allocated; /* allocated elements of d->dir */
  734. unsigned int a_allocated; /* allocated elements of a->arg */
  735. unsigned int max_width_length = 0;
  736. unsigned int max_precision_length = 0;
  737. d->count = 0;
  738. d_allocated = 1;
  739. d->dir = malloc (d_allocated * sizeof (char_directive));
  740. if (d->dir == NULL)
  741. /* Out of memory. */
  742. return -1;
  743. a->count = 0;
  744. a_allocated = 0;
  745. a->arg = NULL;
  746. #define REGISTER_ARG(_index_,_type_) \
  747. { \
  748. unsigned int n = (_index_); \
  749. if (n >= a_allocated) \
  750. { \
  751. argument *memory; \
  752. a_allocated = 2 * a_allocated; \
  753. if (a_allocated <= n) \
  754. a_allocated = n + 1; \
  755. memory = (a->arg \
  756. ? realloc (a->arg, a_allocated * sizeof (argument)) \
  757. : malloc (a_allocated * sizeof (argument))); \
  758. if (memory == NULL) \
  759. /* Out of memory. */ \
  760. goto error; \
  761. a->arg = memory; \
  762. } \
  763. while (a->count <= n) \
  764. a->arg[a->count++].type = TYPE_NONE; \
  765. if (a->arg[n].type == TYPE_NONE) \
  766. a->arg[n].type = (_type_); \
  767. else if (a->arg[n].type != (_type_)) \
  768. /* Ambiguous type for positional argument. */ \
  769. goto error; \
  770. }
  771. while (*cp != '\0')
  772. {
  773. char c = *cp++;
  774. if (c == '%')
  775. {
  776. int arg_index = -1;
  777. char_directive *dp = &d->dir[d->count];/* pointer to next directive */
  778. /* Initialize the next directive. */
  779. dp->dir_start = cp - 1;
  780. dp->flags = 0;
  781. dp->width_start = NULL;
  782. dp->width_end = NULL;
  783. dp->width_arg_index = -1;
  784. dp->precision_start = NULL;
  785. dp->precision_end = NULL;
  786. dp->precision_arg_index = -1;
  787. dp->arg_index = -1;
  788. /* Test for positional argument. */
  789. if (*cp >= '0' && *cp <= '9')
  790. {
  791. const char *np;
  792. for (np = cp; *np >= '0' && *np <= '9'; np++)
  793. ;
  794. if (*np == '$')
  795. {
  796. unsigned int n = 0;
  797. for (np = cp; *np >= '0' && *np <= '9'; np++)
  798. n = 10 * n + (*np - '0');
  799. if (n == 0)
  800. /* Positional argument 0. */
  801. goto error;
  802. arg_index = n - 1;
  803. cp = np + 1;
  804. }
  805. }
  806. /* Read the flags. */
  807. for (;;)
  808. {
  809. if (*cp == '\'')
  810. {
  811. dp->flags |= FLAG_GROUP;
  812. cp++;
  813. }
  814. else if (*cp == '-')
  815. {
  816. dp->flags |= FLAG_LEFT;
  817. cp++;
  818. }
  819. else if (*cp == '+')
  820. {
  821. dp->flags |= FLAG_SHOWSIGN;
  822. cp++;
  823. }
  824. else if (*cp == ' ')
  825. {
  826. dp->flags |= FLAG_SPACE;
  827. cp++;
  828. }
  829. else if (*cp == '#')
  830. {
  831. dp->flags |= FLAG_ALT;
  832. cp++;
  833. }
  834. else if (*cp == '0')
  835. {
  836. dp->flags |= FLAG_ZERO;
  837. cp++;
  838. }
  839. else
  840. break;
  841. }
  842. /* Parse the field width. */
  843. if (*cp == '*')
  844. {
  845. dp->width_start = cp;
  846. cp++;
  847. dp->width_end = cp;
  848. if (max_width_length < 1)
  849. max_width_length = 1;
  850. /* Test for positional argument. */
  851. if (*cp >= '0' && *cp <= '9')
  852. {
  853. const char *np;
  854. for (np = cp; *np >= '0' && *np <= '9'; np++)
  855. ;
  856. if (*np == '$')
  857. {
  858. unsigned int n = 0;
  859. for (np = cp; *np >= '0' && *np <= '9'; np++)
  860. n = 10 * n + (*np - '0');
  861. if (n == 0)
  862. /* Positional argument 0. */
  863. goto error;
  864. dp->width_arg_index = n - 1;
  865. cp = np + 1;
  866. }
  867. }
  868. if (dp->width_arg_index < 0)
  869. dp->width_arg_index = arg_posn++;
  870. REGISTER_ARG (dp->width_arg_index, TYPE_INT);
  871. }
  872. else if (*cp >= '0' && *cp <= '9')
  873. {
  874. unsigned int width_length;
  875. dp->width_start = cp;
  876. for (; *cp >= '0' && *cp <= '9'; cp++)
  877. ;
  878. dp->width_end = cp;
  879. width_length = (unsigned int)(dp->width_end - dp->width_start);
  880. if (max_width_length < width_length)
  881. max_width_length = width_length;
  882. }
  883. /* Parse the precision. */
  884. if (*cp == '.')
  885. {
  886. cp++;
  887. if (*cp == '*')
  888. {
  889. dp->precision_start = cp - 1;
  890. cp++;
  891. dp->precision_end = cp;
  892. if (max_precision_length < 2)
  893. max_precision_length = 2;
  894. /* Test for positional argument. */
  895. if (*cp >= '0' && *cp <= '9')
  896. {
  897. const char *np;
  898. for (np = cp; *np >= '0' && *np <= '9'; np++)
  899. ;
  900. if (*np == '$')
  901. {
  902. unsigned int n = 0;
  903. for (np = cp; *np >= '0' && *np <= '9'; np++)
  904. n = 10 * n + (*np - '0');
  905. if (n == 0)
  906. /* Positional argument 0. */
  907. goto error;
  908. dp->precision_arg_index = n - 1;
  909. cp = np + 1;
  910. }
  911. }
  912. if (dp->precision_arg_index < 0)
  913. dp->precision_arg_index = arg_posn++;
  914. REGISTER_ARG (dp->precision_arg_index, TYPE_INT);
  915. }
  916. else
  917. {
  918. unsigned int precision_length;
  919. dp->precision_start = cp - 1;
  920. for (; *cp >= '0' && *cp <= '9'; cp++)
  921. ;
  922. dp->precision_end = cp;
  923. precision_length = (unsigned int)(dp->precision_end - dp->precision_start);
  924. if (max_precision_length < precision_length)
  925. max_precision_length = precision_length;
  926. }
  927. }
  928. {
  929. arg_type type;
  930. /* Parse argument type/size specifiers. */
  931. {
  932. int flags = 0;
  933. for (;;)
  934. {
  935. if (*cp == 'h')
  936. {
  937. flags |= (1 << (flags & 1));
  938. cp++;
  939. }
  940. else if (*cp == 'L')
  941. {
  942. flags |= 4;
  943. cp++;
  944. }
  945. else if (*cp == 'l')
  946. {
  947. flags += 8;
  948. cp++;
  949. }
  950. else if (cp[0] == 'I' &&
  951. cp[1] == '6' &&
  952. cp[2] == '4')
  953. {
  954. flags = 64;
  955. cp += 3;
  956. }
  957. else if (*cp == 'z' || *cp == 'Z')
  958. {
  959. /* 'z' is standardized in ISO C 99, but glibc uses 'Z'
  960. because the warning facility in gcc-2.95.2 understands
  961. only 'Z' (see gcc-2.95.2/gcc/c-common.c:1784). */
  962. if (sizeof (size_t) > sizeof (long))
  963. {
  964. /* size_t = long long */
  965. flags += 16;
  966. }
  967. else if (sizeof (size_t) > sizeof (int))
  968. {
  969. /* size_t = long */
  970. flags += 8;
  971. }
  972. cp++;
  973. }
  974. else if (*cp == 't')
  975. {
  976. if (sizeof (ptrdiff_t) > sizeof (long))
  977. {
  978. /* ptrdiff_t = long long */
  979. flags += 16;
  980. }
  981. else if (sizeof (ptrdiff_t) > sizeof (int))
  982. {
  983. /* ptrdiff_t = long */
  984. flags += 8;
  985. }
  986. cp++;
  987. }
  988. else
  989. break;
  990. }
  991. /* Read the conversion character. */
  992. c = *cp++;
  993. switch (c)
  994. {
  995. case 'd': case 'i':
  996. if (flags == 64)
  997. type = TYPE_INT64;
  998. else
  999. if (flags >= 16 || (flags & 4))
  1000. type = TYPE_LONGLONGINT;
  1001. else
  1002. if (flags >= 8)
  1003. type = TYPE_LONGINT;
  1004. else if (flags & 2)
  1005. type = TYPE_SCHAR;
  1006. else if (flags & 1)
  1007. type = TYPE_SHORT;
  1008. else
  1009. type = TYPE_INT;
  1010. break;
  1011. case 'o': case 'u': case 'x': case 'X':
  1012. if (flags == 64)
  1013. type = TYPE_UINT64;
  1014. else
  1015. if (flags >= 16 || (flags & 4))
  1016. type = TYPE_ULONGLONGINT;
  1017. else
  1018. if (flags >= 8)
  1019. type = TYPE_ULONGINT;
  1020. else if (flags & 2)
  1021. type = TYPE_UCHAR;
  1022. else if (flags & 1)
  1023. type = TYPE_USHORT;
  1024. else
  1025. type = TYPE_UINT;
  1026. break;
  1027. case 'f': case 'F': case 'e': case 'E': case 'g': case 'G':
  1028. case 'a': case 'A':
  1029. if (flags >= 16 || (flags & 4))
  1030. type = TYPE_LONGDOUBLE;
  1031. else
  1032. type = TYPE_DOUBLE;
  1033. break;
  1034. case 'c':
  1035. if (flags >= 8)
  1036. goto error;
  1037. else
  1038. type = TYPE_CHAR;
  1039. break;
  1040. case 's':
  1041. if (flags >= 8)
  1042. goto error;
  1043. else
  1044. type = TYPE_STRING;
  1045. break;
  1046. case 'p':
  1047. type = TYPE_POINTER;
  1048. break;
  1049. case 'n':
  1050. if (flags >= 16 || (flags & 4))
  1051. type = TYPE_COUNT_LONGLONGINT_POINTER;
  1052. else
  1053. if (flags >= 8)
  1054. type = TYPE_COUNT_LONGINT_POINTER;
  1055. else if (flags & 2)
  1056. type = TYPE_COUNT_SCHAR_POINTER;
  1057. else if (flags & 1)
  1058. type = TYPE_COUNT_SHORT_POINTER;
  1059. else
  1060. type = TYPE_COUNT_INT_POINTER;
  1061. break;
  1062. case '%':
  1063. type = TYPE_NONE;
  1064. break;
  1065. default:
  1066. /* Unknown conversion character. */
  1067. goto error;
  1068. }
  1069. }
  1070. if (type != TYPE_NONE)
  1071. {
  1072. dp->arg_index = arg_index;
  1073. if (dp->arg_index < 0)
  1074. dp->arg_index = arg_posn++;
  1075. REGISTER_ARG (dp->arg_index, type);
  1076. }
  1077. dp->conversion = c;
  1078. dp->dir_end = cp;
  1079. }
  1080. d->count++;
  1081. if (d->count >= d_allocated)
  1082. {
  1083. char_directive *memory;
  1084. d_allocated = 2 * d_allocated;
  1085. memory = realloc (d->dir, d_allocated * sizeof (char_directive));
  1086. if (memory == NULL)
  1087. /* Out of memory. */
  1088. goto error;
  1089. d->dir = memory;
  1090. }
  1091. }
  1092. }
  1093. d->dir[d->count].dir_start = cp;
  1094. d->max_width_length = max_width_length;
  1095. d->max_precision_length = max_precision_length;
  1096. return 0;
  1097. error:
  1098. if (a->arg)
  1099. free (a->arg);
  1100. if (d->dir)
  1101. free (d->dir);
  1102. return -1;
  1103. }
  1104. static int printf_fetchargs (va_list args, arguments *a)
  1105. {
  1106. unsigned int i;
  1107. argument *ap;
  1108. for (i = 0, ap = &a->arg[0]; i < a->count; i++, ap++)
  1109. switch (ap->type)
  1110. {
  1111. case TYPE_SCHAR:
  1112. ap->a.a_schar = va_arg (args, /*signed char*/ int);
  1113. break;
  1114. case TYPE_UCHAR:
  1115. ap->a.a_uchar = va_arg (args, /*unsigned char*/ int);
  1116. break;
  1117. case TYPE_SHORT:
  1118. ap->a.a_short = va_arg (args, /*short*/ int);
  1119. break;
  1120. case TYPE_USHORT:
  1121. ap->a.a_ushort = va_arg (args, /*unsigned short*/ int);
  1122. break;
  1123. case TYPE_INT:
  1124. ap->a.a_int = va_arg (args, int);
  1125. break;
  1126. case TYPE_UINT:
  1127. ap->a.a_uint = va_arg (args, unsigned int);
  1128. break;
  1129. case TYPE_LONGINT:
  1130. ap->a.a_longint = va_arg (args, long int);
  1131. break;
  1132. case TYPE_ULONGINT:
  1133. ap->a.a_ulongint = va_arg (args, unsigned long int);
  1134. break;
  1135. case TYPE_LONGLONGINT:
  1136. ap->a.a_longlongint = va_arg (args, long long int);
  1137. break;
  1138. case TYPE_ULONGLONGINT:
  1139. ap->a.a_ulonglongint = va_arg (args, unsigned long long int);
  1140. break;
  1141. case TYPE_INT64:
  1142. ap->a.a_int64 = va_arg (args, __int64);
  1143. break;
  1144. case TYPE_UINT64:
  1145. ap->a.a_uint64 = va_arg (args, u__int64_t);
  1146. break;
  1147. case TYPE_DOUBLE:
  1148. ap->a.a_double = va_arg (args, double);
  1149. break;
  1150. case TYPE_LONGDOUBLE:
  1151. ap->a.a_longdouble = va_arg (args, long double);
  1152. break;
  1153. case TYPE_CHAR:
  1154. ap->a.a_char = va_arg (args, int);
  1155. break;
  1156. case TYPE_STRING:
  1157. ap->a.a_string = va_arg (args, const char *);
  1158. break;
  1159. case TYPE_POINTER:
  1160. ap->a.a_pointer = va_arg (args, void *);
  1161. break;
  1162. case TYPE_COUNT_SCHAR_POINTER:
  1163. ap->a.a_count_schar_pointer = va_arg (args, signed char *);
  1164. break;
  1165. case TYPE_COUNT_SHORT_POINTER:
  1166. ap->a.a_count_short_pointer = va_arg (args, short *);
  1167. break;
  1168. case TYPE_COUNT_INT_POINTER:
  1169. ap->a.a_count_int_pointer = va_arg (args, int *);
  1170. break;
  1171. case TYPE_COUNT_LONGINT_POINTER:
  1172. ap->a.a_count_longint_pointer = va_arg (args, long int *);
  1173. break;
  1174. case TYPE_COUNT_LONGLONGINT_POINTER:
  1175. ap->a.a_count_longlongint_pointer = va_arg (args, long long int *);
  1176. break;
  1177. default:
  1178. /* Unknown type. */
  1179. return -1;
  1180. }
  1181. return 0;
  1182. }
  1183. static int print_long_long (char *buf, int len, int width,int precision,unsigned long flags,char conversion,unsigned long long number)
  1184. {
  1185. int negative = FALSE;
  1186. char buffer[128];
  1187. char *bufferend;
  1188. char *pointer;
  1189. int base;
  1190. static const char *upper = "0123456789ABCDEFX";
  1191. static const char *lower = "0123456789abcdefx";
  1192. const char *digits;
  1193. int i;
  1194. char *p;
  1195. int count;
  1196. #define EMIT(c) \
  1197. if (p - buf == len - 1) \
  1198. { \
  1199. *p++ = '\0'; \
  1200. return len; \
  1201. } \
  1202. else \
  1203. *p++ = c;
  1204. p = buf;
  1205. switch (conversion)
  1206. {
  1207. case 'o':
  1208. base = 8;
  1209. digits = lower;
  1210. negative = FALSE;
  1211. break;
  1212. case 'x':
  1213. base = 16;
  1214. digits = lower;
  1215. negative = FALSE;
  1216. break;
  1217. case 'X':
  1218. base = 16;
  1219. digits = upper;
  1220. negative = FALSE;
  1221. break;
  1222. default:
  1223. base = 10;
  1224. digits = lower;
  1225. negative = (long long)number < 0;
  1226. if (negative)
  1227. number = -((long long)number);
  1228. break;
  1229. }
  1230. /* Build number */
  1231. pointer = bufferend = &buffer[sizeof(buffer) - 1];
  1232. *pointer-- = '\0';
  1233. for (i = 1; i < (int)sizeof(buffer); i++)
  1234. {
  1235. *pointer-- = digits[number % base];
  1236. number /= base;
  1237. if (number == 0)
  1238. break;
  1239. }
  1240. /* Adjust width */
  1241. width -= (int)((bufferend - pointer) - 1);
  1242. /* Adjust precision */
  1243. if (precision != -1)
  1244. {
  1245. precision -= (int)((bufferend - pointer) - 1);
  1246. if (precision < 0)
  1247. precision = 0;
  1248. flags |= FLAG_ZERO;
  1249. }
  1250. /* Adjust width further */
  1251. if (negative || (flags & FLAG_SHOWSIGN) || (flags & FLAG_SPACE))
  1252. width--;
  1253. if (flags & FLAG_ALT)
  1254. {
  1255. switch (base)
  1256. {
  1257. case 16:
  1258. width -= 2;
  1259. break;
  1260. case 8:
  1261. width--;
  1262. break;
  1263. default:
  1264. break;
  1265. }
  1266. }
  1267. /* Output prefixes spaces if needed */
  1268. if (! ((flags & FLAG_LEFT) ||
  1269. ((flags & FLAG_ZERO) && (precision == -1))))
  1270. {
  1271. count = (precision == -1) ? 0 : precision;
  1272. while (width-- > count)
  1273. *p++ = ' ';
  1274. }
  1275. /* width has been adjusted for signs and alternatives */
  1276. if (negative)
  1277. {
  1278. EMIT ('-');
  1279. }
  1280. else if (flags & FLAG_SHOWSIGN)
  1281. {
  1282. EMIT('+');
  1283. }
  1284. else if (flags & FLAG_SPACE)
  1285. {
  1286. EMIT(' ');
  1287. }
  1288. if (flags & FLAG_ALT)
  1289. {
  1290. switch (base)
  1291. {
  1292. case 8:
  1293. EMIT('0');
  1294. break;
  1295. case 16:
  1296. EMIT('0');
  1297. EMIT(digits[16]);
  1298. break;
  1299. default:
  1300. break;
  1301. } /* switch base */
  1302. }
  1303. /* Output prefixed zero padding if needed */
  1304. if (flags & FLAG_ZERO)
  1305. {
  1306. if (precision == -1)
  1307. precision = width;
  1308. while (precision-- > 0)
  1309. {
  1310. EMIT('0');
  1311. width--;
  1312. }
  1313. }
  1314. /* Output the number itself */
  1315. while (*(++pointer))
  1316. {
  1317. EMIT(*pointer);
  1318. }
  1319. /* Output trailing spaces if needed */
  1320. if (flags & FLAG_LEFT)
  1321. {
  1322. while (width-- > 0)
  1323. EMIT(' ');
  1324. }
  1325. EMIT('\0');
  1326. return (int)(p - buf - 1);
  1327. }
  1328. static char *vasnprintf (char *resultbuf, size_t *lengthp, const char *format, va_list args)
  1329. {
  1330. char_directives d;
  1331. arguments a;
  1332. if (printf_parse (format, &d, &a) < 0)
  1333. {
  1334. errno = EINVAL;
  1335. return NULL;
  1336. }
  1337. #define CLEANUP() \
  1338. free (d.dir); \
  1339. if (a.arg) \
  1340. free (a.arg);
  1341. if (printf_fetchargs (args, &a) < 0)
  1342. {
  1343. CLEANUP ();
  1344. errno = EINVAL;
  1345. return NULL;
  1346. }
  1347. {
  1348. char *buf =
  1349. (char *) alloca (7 + d.max_width_length + d.max_precision_length + 6);
  1350. const char *cp;
  1351. unsigned int i;
  1352. char_directive *dp;
  1353. /* Output string accumulator. */
  1354. char *result;
  1355. size_t allocated;
  1356. size_t length;
  1357. if (resultbuf != NULL)
  1358. {
  1359. result = resultbuf;
  1360. allocated = *lengthp;
  1361. }
  1362. else
  1363. {
  1364. result = NULL;
  1365. allocated = 0;
  1366. }
  1367. length = 0;
  1368. /* Invariants:
  1369. result is either == resultbuf or == NULL or malloc-allocated.
  1370. If length > 0, then result != NULL. */
  1371. #define ENSURE_ALLOCATION(needed) \
  1372. if ((needed) > allocated) \
  1373. { \
  1374. char *memory; \
  1375. \
  1376. allocated = (allocated > 0 ? 2 * allocated : 12); \
  1377. if ((needed) > allocated) \
  1378. allocated = (needed); \
  1379. if (result == resultbuf || result == NULL) \
  1380. memory = (char *) malloc (allocated); \
  1381. else \
  1382. memory = (char *) realloc (result, allocated); \
  1383. \
  1384. if (memory == NULL) \
  1385. { \
  1386. if (!(result == resultbuf || result == NULL)) \
  1387. free (result); \
  1388. CLEANUP (); \
  1389. errno = ENOMEM; \
  1390. return NULL; \
  1391. } \
  1392. if (result == resultbuf && length > 0) \
  1393. memcpy (memory, result, length); \
  1394. result = memory; \
  1395. }
  1396. for (cp = format, i = 0, dp = &d.dir[0]; ; cp = dp->dir_end, i++, dp++)
  1397. {
  1398. if (cp != dp->dir_start)
  1399. {
  1400. size_t n = dp->dir_start - cp;
  1401. ENSURE_ALLOCATION (length + n);
  1402. memcpy (result + length, cp, n);
  1403. length += n;
  1404. }
  1405. if (i == d.count)
  1406. break;
  1407. /* Execute a single directive. */
  1408. if (dp->conversion == '%')
  1409. {
  1410. if (!(dp->arg_index < 0))
  1411. abort ();
  1412. ENSURE_ALLOCATION (length + 1);
  1413. result[length] = '%';
  1414. length += 1;
  1415. }
  1416. else
  1417. {
  1418. if (!(dp->arg_index >= 0))
  1419. abort ();
  1420. if (dp->conversion == 'n')
  1421. {
  1422. switch (a.arg[dp->arg_index].type)
  1423. {
  1424. case TYPE_COUNT_SCHAR_POINTER:
  1425. *a.arg[dp->arg_index].a.a_count_schar_pointer = (char)length;
  1426. break;
  1427. case TYPE_COUNT_SHORT_POINTER:
  1428. *a.arg[dp->arg_index].a.a_count_short_pointer = (short)length;
  1429. break;
  1430. case TYPE_COUNT_INT_POINTER:
  1431. *a.arg[dp->arg_index].a.a_count_int_pointer = (int)length;
  1432. break;
  1433. case TYPE_COUNT_LONGINT_POINTER:
  1434. *a.arg[dp->arg_index].a.a_count_longint_pointer = (long)length;
  1435. break;
  1436. case TYPE_COUNT_LONGLONGINT_POINTER:
  1437. *a.arg[dp->arg_index].a.a_count_longlongint_pointer = length;
  1438. break;
  1439. default:
  1440. abort ();
  1441. }
  1442. }
  1443. else
  1444. {
  1445. arg_type type = a.arg[dp->arg_index].type;
  1446. char *p;
  1447. unsigned int prefix_count;
  1448. int prefixes[2];
  1449. unsigned int tmp_length;
  1450. char tmpbuf[700];
  1451. char *tmp;
  1452. /* Allocate a temporary buffer of sufficient size for calling
  1453. sprintf. */
  1454. {
  1455. unsigned int width;
  1456. unsigned int precision;
  1457. width = 0;
  1458. if (dp->width_start != dp->width_end)
  1459. {
  1460. if (dp->width_arg_index >= 0)
  1461. {
  1462. int arg;
  1463. if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
  1464. abort ();
  1465. arg = a.arg[dp->width_arg_index].a.a_int;
  1466. width = (arg < 0 ? -arg : arg);
  1467. }
  1468. else
  1469. {
  1470. const char *digitp = dp->width_start;
  1471. do
  1472. width = width * 10 + (*digitp++ - '0');
  1473. while (digitp != dp->width_end);
  1474. }
  1475. }
  1476. precision = 6;
  1477. if (dp->precision_start != dp->precision_end)
  1478. {
  1479. if (dp->precision_arg_index >= 0)
  1480. {
  1481. int arg;
  1482. if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
  1483. abort ();
  1484. arg = a.arg[dp->precision_arg_index].a.a_int;
  1485. precision = (arg < 0 ? 0 : arg);
  1486. }
  1487. else
  1488. {
  1489. const char *digitp = dp->precision_start + 1;
  1490. precision = 0;
  1491. while (digitp != dp->precision_end)
  1492. precision = precision * 10 + (*digitp++ - '0');
  1493. }
  1494. }
  1495. switch (dp->conversion)
  1496. {
  1497. case 'd': case 'i': case 'u':
  1498. if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
  1499. tmp_length =
  1500. (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
  1501. * 0.30103 /* binary -> decimal */
  1502. * 2 /* estimate for FLAG_GROUP */
  1503. )
  1504. + 1 /* turn floor into ceil */
  1505. + 1; /* account for leading sign */
  1506. else
  1507. if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
  1508. tmp_length =
  1509. (unsigned int) (sizeof (unsigned long) * CHAR_BIT
  1510. * 0.30103 /* binary -> decimal */
  1511. * 2 /* estimate for FLAG_GROUP */
  1512. )
  1513. + 1 /* turn floor into ceil */
  1514. + 1; /* account for leading sign */
  1515. else
  1516. tmp_length =
  1517. (unsigned int) (sizeof (unsigned int) * CHAR_BIT
  1518. * 0.30103 /* binary -> decimal */
  1519. * 2 /* estimate for FLAG_GROUP */
  1520. )
  1521. + 1 /* turn floor into ceil */
  1522. + 1; /* account for leading sign */
  1523. break;
  1524. case 'o':
  1525. if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
  1526. tmp_length =
  1527. (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
  1528. * 0.333334 /* binary -> octal */
  1529. )
  1530. + 1 /* turn floor into ceil */
  1531. + 1; /* account for leading sign */
  1532. else
  1533. if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
  1534. tmp_length =
  1535. (unsigned int) (sizeof (unsigned long) * CHAR_BIT
  1536. * 0.333334 /* binary -> octal */
  1537. )
  1538. + 1 /* turn floor into ceil */
  1539. + 1; /* account for leading sign */
  1540. else
  1541. tmp_length =
  1542. (unsigned int) (sizeof (unsigned int) * CHAR_BIT
  1543. * 0.333334 /* binary -> octal */
  1544. )
  1545. + 1 /* turn floor into ceil */
  1546. + 1; /* account for leading sign */
  1547. break;
  1548. case 'x': case 'X':
  1549. if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
  1550. tmp_length =
  1551. (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
  1552. * 0.25 /* binary -> hexadecimal */
  1553. )
  1554. + 1 /* turn floor into ceil */
  1555. + 2; /* account for leading sign or alternate form */
  1556. else
  1557. if (type == TYPE_INT64 || type == TYPE_UINT64)
  1558. tmp_length =
  1559. (unsigned int) (sizeof (u__int64_t) * CHAR_BIT
  1560. * 0.25 /* binary -> hexadecimal */
  1561. )
  1562. + 1 /* turn floor into ceil */
  1563. + 2; /* account for leading sign or alternate form */
  1564. else
  1565. if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
  1566. tmp_length =
  1567. (unsigned int) (sizeof (unsigned long) * CHAR_BIT
  1568. * 0.25 /* binary -> hexadecimal */
  1569. )
  1570. + 1 /* turn floor into ceil */
  1571. + 2; /* account for leading sign or alternate form */
  1572. else
  1573. tmp_length =
  1574. (unsigned int) (sizeof (unsigned int) * CHAR_BIT
  1575. * 0.25 /* binary -> hexadecimal */
  1576. )
  1577. + 1 /* turn floor into ceil */
  1578. + 2; /* account for leading sign or alternate form */
  1579. break;
  1580. case 'f': case 'F':
  1581. if (type == TYPE_LONGDOUBLE)
  1582. tmp_length =
  1583. (unsigned int) (LDBL_MAX_EXP
  1584. * 0.30103 /* binary -> decimal */
  1585. * 2 /* estimate for FLAG_GROUP */
  1586. )
  1587. + 1 /* turn floor into ceil */
  1588. + precision
  1589. + 10; /* sign, decimal point etc. */
  1590. else
  1591. tmp_length =
  1592. (unsigned int) (DBL_MAX_EXP
  1593. * 0.30103 /* binary -> decimal */
  1594. * 2 /* estimate for FLAG_GROUP */
  1595. )
  1596. + 1 /* turn floor into ceil */
  1597. + precision
  1598. + 10; /* sign, decimal point etc. */
  1599. break;
  1600. case 'e': case 'E': case 'g': case 'G':
  1601. case 'a': case 'A':
  1602. tmp_length =
  1603. precision
  1604. + 12; /* sign, decimal point, exponent etc. */
  1605. break;
  1606. case 'c':
  1607. tmp_length = 1;
  1608. break;
  1609. case 's':
  1610. tmp_length = (unsigned int)strlen (a.arg[dp->arg_index].a.a_string);
  1611. break;
  1612. case 'p':
  1613. tmp_length =
  1614. (unsigned int) (sizeof (void *) * CHAR_BIT
  1615. * 0.25 /* binary -> hexadecimal */
  1616. )
  1617. + 1 /* turn floor into ceil */
  1618. + 2; /* account for leading 0x */
  1619. break;
  1620. default:
  1621. abort ();
  1622. }
  1623. if (tmp_length < width)
  1624. tmp_length = width;
  1625. tmp_length++; /* account for trailing NUL */
  1626. }
  1627. if (tmp_length <= sizeof (tmpbuf))
  1628. tmp = tmpbuf;
  1629. else
  1630. {
  1631. tmp = (char *) malloc (tmp_length);
  1632. if (tmp == NULL)
  1633. {
  1634. /* Out of memory. */
  1635. if (!(result == resultbuf || result == NULL))
  1636. free (result);
  1637. CLEANUP ();
  1638. errno = ENOMEM;
  1639. return NULL;
  1640. }
  1641. }
  1642. /* Construct the format string for calling snprintf or
  1643. sprintf. */
  1644. p = buf;
  1645. *p++ = '%';
  1646. if (dp->flags & FLAG_GROUP)
  1647. *p++ = '\'';
  1648. if (dp->flags & FLAG_LEFT)
  1649. *p++ = '-';
  1650. if (dp->flags & FLAG_SHOWSIGN)
  1651. *p++ = '+';
  1652. if (dp->flags & FLAG_SPACE)
  1653. *p++ = ' ';
  1654. if (dp->flags & FLAG_ALT)
  1655. *p++ = '#';
  1656. if (dp->flags & FLAG_ZERO)
  1657. *p++ = '0';
  1658. if (dp->width_start != dp->width_end)
  1659. {
  1660. size_t n = dp->width_end - dp->width_start;
  1661. memcpy (p, dp->width_start, n);
  1662. p += n;
  1663. }
  1664. if (dp->precision_start != dp->precision_end)
  1665. {
  1666. size_t n = dp->precision_end - dp->precision_start;
  1667. memcpy (p, dp->precision_start, n);
  1668. p += n;
  1669. }
  1670. switch (type)
  1671. {
  1672. case TYPE_INT64:
  1673. case TYPE_UINT64:
  1674. *p++ = 'I';
  1675. *p++ = '6';
  1676. *p++ = '4';
  1677. break;
  1678. case TYPE_LONGLONGINT:
  1679. case TYPE_ULONGLONGINT:
  1680. *p++ = 'I';
  1681. *p++ = '6';
  1682. *p++ = '4';
  1683. break;
  1684. /*FALLTHROUGH*/
  1685. case TYPE_LONGINT:
  1686. case TYPE_ULONGINT:
  1687. *p++ = 'l';
  1688. break;
  1689. case TYPE_LONGDOUBLE:
  1690. *p++ = 'L';
  1691. break;
  1692. default:
  1693. break;
  1694. }
  1695. *p = dp->conversion;
  1696. p[1] = '\0';
  1697. /* Construct the arguments for calling snprintf or sprintf. */
  1698. prefix_count = 0;
  1699. if (dp->width_arg_index >= 0)
  1700. {
  1701. if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
  1702. abort ();
  1703. prefixes[prefix_count++] = a.arg[dp->width_arg_index].a.a_int;
  1704. }
  1705. if (dp->precision_arg_index >= 0)
  1706. {
  1707. if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
  1708. abort ();
  1709. prefixes[prefix_count++] = a.arg[dp->precision_arg_index].a.a_int;
  1710. }
  1711. for (;;)
  1712. {
  1713. size_t maxlen;
  1714. int count;
  1715. int retcount;
  1716. maxlen = allocated - length;
  1717. count = -1;
  1718. retcount = 0;
  1719. #define SNPRINTF_BUF(arg) \
  1720. switch (prefix_count) \
  1721. { \
  1722. case 0: \
  1723. count = sprintf (tmp, buf, arg); \
  1724. break; \
  1725. case 1: \
  1726. count = sprintf (tmp, buf, prefixes[0], arg); \
  1727. break; \
  1728. case 2: \
  1729. count = sprintf (tmp, buf, prefixes[0], prefixes[1],\
  1730. arg); \
  1731. break; \
  1732. default: \
  1733. abort (); \
  1734. }
  1735. switch (type)
  1736. {
  1737. case TYPE_SCHAR:
  1738. {
  1739. int arg = a.arg[dp->arg_index].a.a_schar;
  1740. SNPRINTF_BUF (arg);
  1741. }
  1742. break;
  1743. case TYPE_UCHAR:
  1744. {
  1745. unsigned int arg = a.arg[dp->arg_index].a.a_uchar;
  1746. SNPRINTF_BUF (arg);
  1747. }
  1748. break;
  1749. case TYPE_SHORT:
  1750. {
  1751. int arg = a.arg[dp->arg_index].a.a_short;
  1752. SNPRINTF_BUF (arg);
  1753. }
  1754. break;
  1755. case TYPE_USHORT:
  1756. {
  1757. unsigned int arg = a.arg[dp->arg_index].a.a_ushort;
  1758. SNPRINTF_BUF (arg);
  1759. }
  1760. break;
  1761. case TYPE_INT:
  1762. {
  1763. int arg = a.arg[dp->arg_index].a.a_int;
  1764. SNPRINTF_BUF (arg);
  1765. }
  1766. break;
  1767. case TYPE_UINT:
  1768. {
  1769. unsigned int arg = a.arg[dp->arg_index].a.a_uint;
  1770. SNPRINTF_BUF (arg);
  1771. }
  1772. break;
  1773. case TYPE_LONGINT:
  1774. {
  1775. long int arg = a.arg[dp->arg_index].a.a_longint;
  1776. SNPRINTF_BUF (arg);
  1777. }
  1778. break;
  1779. case TYPE_ULONGINT:
  1780. {
  1781. unsigned long int arg = a.arg[dp->arg_index].a.a_ulongint;
  1782. SNPRINTF_BUF (arg);
  1783. }
  1784. break;
  1785. case TYPE_INT64:
  1786. {
  1787. __int64 arg = a.arg[dp->arg_index].a.a_int64;
  1788. SNPRINTF_BUF (arg);
  1789. }
  1790. break;
  1791. case TYPE_UINT64:
  1792. {
  1793. u__int64_t arg = a.arg[dp->arg_index].a.a_uint64;
  1794. SNPRINTF_BUF (arg);
  1795. }
  1796. break;
  1797. case TYPE_LONGLONGINT:
  1798. case TYPE_ULONGLONGINT:
  1799. {
  1800. unsigned long long int arg = a.arg[dp->arg_index].a.a_ulonglongint;
  1801. int width;
  1802. int precision;
  1803. width = 0;
  1804. if (dp->width_start != dp->width_end)
  1805. {
  1806. if (dp->width_arg_index >= 0)
  1807. {
  1808. int arg;
  1809. if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
  1810. abort ();
  1811. arg = a.arg[dp->width_arg_index].a.a_int;
  1812. width = (arg < 0 ? -arg : arg);
  1813. }
  1814. else
  1815. {
  1816. const char *digitp = dp->width_start;
  1817. do
  1818. width = width * 10 + (*digitp++ - '0');
  1819. while (digitp != dp->width_end);
  1820. }
  1821. }
  1822. precision = -1;
  1823. if (dp->precision_start != dp->precision_end)
  1824. {
  1825. if (dp->precision_arg_index >= 0)
  1826. {
  1827. if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
  1828. abort ();
  1829. arg = a.arg[dp->precision_arg_index].a.a_int;
  1830. precision = (arg < 0 ? 0 : arg);
  1831. }
  1832. else
  1833. {
  1834. const char *digitp = dp->precision_start + 1;
  1835. precision = 0;
  1836. do
  1837. precision = precision * 10 + (*digitp++ - '0');
  1838. while (digitp != dp->precision_end);
  1839. }
  1840. }
  1841. count = print_long_long (tmp, tmp_length,
  1842. width, precision,
  1843. dp->flags,
  1844. dp->conversion,
  1845. arg);
  1846. }
  1847. break;
  1848. case TYPE_DOUBLE:
  1849. {
  1850. double arg = a.arg[dp->arg_index].a.a_double;
  1851. SNPRINTF_BUF (arg);
  1852. }
  1853. break;
  1854. case TYPE_LONGDOUBLE:
  1855. {
  1856. long double arg = a.arg[dp->arg_index].a.a_longdouble;
  1857. SNPRINTF_BUF (arg);
  1858. }
  1859. break;
  1860. case TYPE_CHAR:
  1861. {
  1862. int arg = a.arg[dp->arg_index].a.a_char;
  1863. SNPRINTF_BUF (arg);
  1864. }
  1865. break;
  1866. case TYPE_STRING:
  1867. {
  1868. const char *arg = a.arg[dp->arg_index].a.a_string;
  1869. SNPRINTF_BUF (arg);
  1870. }
  1871. break;
  1872. case TYPE_POINTER:
  1873. {
  1874. void *arg = a.arg[dp->arg_index].a.a_pointer;
  1875. SNPRINTF_BUF (arg);
  1876. }
  1877. break;
  1878. default:
  1879. abort ();
  1880. }
  1881. /* Attempt to handle failure. */
  1882. if (count < 0)
  1883. {
  1884. if (!(result == resultbuf || result == NULL))
  1885. free (result);
  1886. CLEANUP ();
  1887. errno = EINVAL;
  1888. return NULL;
  1889. }
  1890. if (count >= (int)tmp_length)
  1891. /* tmp_length was incorrectly calculated - fix the
  1892. code above! */
  1893. abort ();
  1894. /* Make room for the result. */
  1895. if (count >= (int)maxlen)
  1896. {
  1897. /* Need at least count bytes. But allocate
  1898. proportionally, to avoid looping eternally if
  1899. snprintf() reports a too small count. */
  1900. size_t n = length + count;
  1901. if (n < 2 * allocated)
  1902. n = 2 * allocated;
  1903. ENSURE_ALLOCATION (n);
  1904. }
  1905. /* Append the sprintf() result. */
  1906. memcpy (result + length, tmp, count);
  1907. if (tmp != tmpbuf)
  1908. free (tmp);
  1909. length += count;
  1910. break;
  1911. }
  1912. }
  1913. }
  1914. }
  1915. /* Add the final NUL. */
  1916. ENSURE_ALLOCATION (length + 1);
  1917. result[length] = '\0';
  1918. if (result != resultbuf && length + 1 < allocated)
  1919. {
  1920. /* Shrink the allocated memory if possible. */
  1921. char *memory;
  1922. memory = (char *) realloc (result, length + 1);
  1923. if (memory != NULL)
  1924. result = memory;
  1925. }
  1926. CLEANUP ();
  1927. *lengthp = length;
  1928. return result;
  1929. }
  1930. }
  1931. #define _XT(str) str
  1932. #define CHR_LT_ _XT('<')
  1933. #define CHR_GT_ _XT('>')
  1934. #define CHR_AMP_ _XT('&')
  1935. #define CHR_APOS_ _XT('\'')
  1936. #define CHR_QUOT_ _XT('"')
  1937. #define XML_LT_ _XT("&lt;")
  1938. #define XML_GT_ _XT("&gt;")
  1939. #define XML_AMP_ _XT("&amp;")
  1940. #define XML_APOS_ _XT("&apos;")
  1941. #define XML_QUOT_ _XT("&quot;")
  1942. enum
  1943. {
  1944. LT_SIZE_ = 4, /**< Size of &lt; */
  1945. GT_SIZE_ = 4, /**< Size of &gt; */
  1946. AMP_SIZE_ = 5, /**< Size of &amp; */
  1947. APOS_SIZE_ = 6, /**< Size of &apos; */
  1948. QUOT_SIZE_ = 6 /**< Size of &quot; */
  1949. };
  1950. TOOLKIT_API char *str_xml_escape(const char *src)
  1951. {
  1952. char *p = (char *) src;
  1953. char *escaped = NULL;
  1954. unsigned int len = 0;
  1955. assert (src != NULL);
  1956. /* We first need to calculate the size of the new escaped string. */
  1957. while (*p != _XT('\0'))
  1958. {
  1959. switch (*p)
  1960. {
  1961. case CHR_LT_:
  1962. len += LT_SIZE_;
  1963. break;
  1964. case CHR_GT_:
  1965. len += GT_SIZE_;
  1966. break;
  1967. case CHR_AMP_:
  1968. len += AMP_SIZE_;
  1969. break;
  1970. case CHR_APOS_:
  1971. len += APOS_SIZE_;
  1972. break;
  1973. case CHR_QUOT_:
  1974. len += QUOT_SIZE_;
  1975. break;
  1976. default:
  1977. len += 1;
  1978. break;
  1979. }
  1980. p += 1;
  1981. }
  1982. /* Allocate new string (if necessary). */
  1983. escaped = calloc (len + 1, sizeof (char));
  1984. /* Append characters to new string, escaping the needed ones. */
  1985. p = (char *) src;
  1986. len = 0;
  1987. while (*p != _XT('\0'))
  1988. {
  1989. switch (*p)
  1990. {
  1991. case CHR_LT_:
  1992. memcpy (&escaped[len], XML_LT_, LT_SIZE_);
  1993. len += LT_SIZE_;
  1994. break;
  1995. case CHR_GT_:
  1996. memcpy (&escaped[len], XML_GT_, GT_SIZE_);
  1997. len += GT_SIZE_;
  1998. break;
  1999. case CHR_AMP_:
  2000. memcpy (&escaped[len], XML_AMP_, AMP_SIZE_);
  2001. len += AMP_SIZE_;
  2002. break;
  2003. case CHR_APOS_:
  2004. memcpy (&escaped[len], XML_APOS_, APOS_SIZE_);
  2005. len += APOS_SIZE_;
  2006. break;
  2007. case CHR_QUOT_:
  2008. memcpy (&escaped[len], XML_QUOT_, QUOT_SIZE_);
  2009. len += QUOT_SIZE_;
  2010. break;
  2011. default:
  2012. escaped[len] = *p;
  2013. len += 1;
  2014. break;
  2015. }
  2016. p += 1;
  2017. }
  2018. return escaped;
  2019. }
  2020. /*!
  2021. * @brief
  2022. * @param[in] cmdstart
  2023. * @param[out] argv, args, numargs, numchars
  2024. * @return :
  2025. */
  2026. TOOLKIT_API void str_parse_cmdline (char *cmdstart, char **argv, char *args, int *numargs, int *numchars)
  2027. {
  2028. char *p;
  2029. char c;
  2030. int inquote; /* 1 = inside quotes */
  2031. int copychar; /* 1 = copy char to *args */
  2032. unsigned numslash; /* num of backslashes seen */
  2033. TOOLKIT_ASSERT(numargs);
  2034. TOOLKIT_ASSERT(numchars);
  2035. *numchars = 0;
  2036. *numargs = 1; /* the program name at least */
  2037. /* first scan the program name, copy it, and count the bytes */
  2038. p = cmdstart;
  2039. if (argv)
  2040. *argv++ = args;
  2041. /* A quoted program name is handled here. The handling is much
  2042. simpler than for other arguments. Basically, whatever lies
  2043. between the leading double-quote and next one, or a terminal null
  2044. character is simply accepted. Fancier handling is not required
  2045. because the program name must be a legal NTFS/HPFS file name.
  2046. Note that the double-quote characters are not copied, nor do they
  2047. contribute to numchars. */
  2048. inquote = FALSE;
  2049. do {
  2050. if (*p == '\"' )
  2051. {
  2052. inquote = !inquote;
  2053. c = (char) *p++;
  2054. continue;
  2055. }
  2056. ++*numchars;
  2057. if (args)
  2058. *args++ = *p; /*char in cmdstart, the capacity of {args} equals with numchars*/
  2059. c = (char) *p++;
  2060. } while ( (c != 0 && (inquote || (c !=' ' && c != '\t'))) );
  2061. if ( c == 0 ) {
  2062. p--;
  2063. } else {
  2064. if (args)
  2065. *(args-1) = 0;
  2066. }
  2067. inquote = 0;
  2068. /* loop on each argument */
  2069. for(;;) {
  2070. if ( *p ) {
  2071. while (*p == ' ' || *p == '\t')
  2072. ++p;
  2073. }
  2074. if (*p == 0)
  2075. break; /* end of args */
  2076. /* scan an argument */
  2077. if (argv)
  2078. *argv++ = args; /* store ptr to arg */
  2079. ++*numargs;
  2080. /* loop through scanning one argument */
  2081. for (;;) {
  2082. copychar = 1;
  2083. /* Rules: 2N backslashes + " ==> N backslashes and begin/end quote
  2084. 2N+1 backslashes + " ==> N backslashes + literal "
  2085. N backslashes ==> N backslashes */
  2086. numslash = 0;
  2087. while (*p == '\\') {
  2088. /* count number of backslashes for use below */
  2089. ++p;
  2090. ++numslash;
  2091. }
  2092. if (*p == '\"') {
  2093. /* if 2N backslashes before, start/end quote, otherwise
  2094. copy literally */
  2095. if (numslash % 2 == 0) {
  2096. if (inquote && p[1] == '\"') {
  2097. p++; /* Double quote inside quoted string */
  2098. } else { /* skip first quote char and copy second */
  2099. copychar = 0; /* don't copy quote */
  2100. inquote = !inquote;
  2101. }
  2102. }
  2103. numslash /= 2; /* divide numslash by two */
  2104. }
  2105. /* copy slashes */
  2106. while (numslash--) {
  2107. if (args)
  2108. *args++ = '\\';
  2109. ++*numchars;
  2110. }
  2111. /* if at end of arg, break loop */
  2112. if (*p == 0 || (!inquote && (*p == ' ' || *p == '\t')))
  2113. break;
  2114. if (copychar) {
  2115. if (args)
  2116. *args++ = *p;
  2117. ++*numchars;
  2118. }
  2119. ++p;
  2120. }
  2121. /* null-terminate the argument */
  2122. if (args)
  2123. *args++ = 0; /* terminate string */
  2124. ++*numchars;
  2125. }
  2126. /* We put one last argument in -- a null ptr */
  2127. if (argv)
  2128. *argv++ = NULL;
  2129. ++*numargs;
  2130. }
  2131. TOOLKIT_API void str_get_format_uuid(char* strbuffer, size_t ulen)
  2132. {
  2133. #if defined(_MSC_VER)
  2134. UUID uuid;
  2135. size_t uuidlen = 0;
  2136. RPC_CSTR buf;
  2137. UuidCreate((UUID*)&uuid);
  2138. UuidToString((UUID*)&uuid, &buf);
  2139. uuidlen = strlen((const char*)buf);
  2140. if (uuidlen < ulen) {
  2141. memcpy(strbuffer, (const char*)buf, uuidlen);
  2142. }
  2143. RpcStringFree(&buf);
  2144. #else
  2145. uuid_t uuid;
  2146. uuid_generate(uuid);
  2147. uuid_unparse(uuid, strbuffer);
  2148. #endif
  2149. }
  2150. TOOLKIT_API int str_reset_buffer(void* pbuf, int idata, size_t ulen)
  2151. {
  2152. int ret = -1;
  2153. if (pbuf) {
  2154. memset(pbuf, idata, ulen);
  2155. ret = 0;
  2156. }
  2157. return ret;
  2158. }
  2159. TOOLKIT_API int str_rend_string(char* pSrc, int iflag)
  2160. {
  2161. int ret = -1;
  2162. if (pSrc) {
  2163. char* pindex = pSrc;
  2164. while (*pindex != iflag) {
  2165. pindex++;
  2166. };
  2167. *pindex = '\0';
  2168. ret = 0;
  2169. }
  2170. return ret;
  2171. }
  2172. TOOLKIT_API int str_fifter_string(char* pbuf, size_t usize, const char* psrc, const char cflag)
  2173. {
  2174. int ret = -1;
  2175. int i = 0;
  2176. int j = 0;
  2177. size_t ulen = 0;
  2178. const char* pindex = psrc;
  2179. if (NULL == psrc) {
  2180. return ret;
  2181. }
  2182. ulen = strlen(psrc);
  2183. for (i = 0; i < ulen && i < usize; i++) {
  2184. if (pindex[i] != cflag) {
  2185. pbuf[j++] = pindex[i];
  2186. }
  2187. }
  2188. if (j == ulen - 2) {
  2189. ret = 0;
  2190. }
  2191. return ret;
  2192. }
  2193. TOOLKIT_API int str_get_interger_netaddr(char* strbuf, size_t ubufszie, const char* szip)
  2194. {
  2195. int ret = -1;
  2196. unsigned long ulip = 0;
  2197. if (NULL == strbuf || NULL == szip) {
  2198. return ret;
  2199. }
  2200. ulip = inet_addr(szip);
  2201. _ultoa(ulip, strbuf, 10);
  2202. ret = 0;
  2203. return ret;
  2204. }
  2205. TOOLKIT_API int str_convert_interaddr_strip(char* strbuf, size_t ubufszie, const char* szinter_ip)
  2206. {
  2207. int ret = -1;
  2208. char* strip = NULL;
  2209. struct sockaddr_in addr = { 0 };
  2210. if (!szinter_ip || !strbuf) {
  2211. return ret;
  2212. }
  2213. addr.sin_addr.s_addr = strtoul(szinter_ip, NULL, 10);//inet_addr(config->switch_server);
  2214. strip = inet_ntoa(addr.sin_addr);
  2215. if (strip) {
  2216. size_t ulen = strlen(strip);
  2217. if (ulen < ubufszie) {
  2218. memcpy(strbuf, strip, ulen);
  2219. ret = 0;
  2220. }
  2221. }
  2222. return ret;
  2223. }
  2224. TOOLKIT_API int str_connect_strings(char* strbuf, size_t ulen, const char* psrca, const char* pstr, const char* psrcb)
  2225. {
  2226. int ret = -1;
  2227. size_t ulena = 0;
  2228. size_t ulenb = 0;
  2229. size_t ulenstr = 0;
  2230. if (!psrca || !psrcb || !pstr) {
  2231. return ret;
  2232. }
  2233. ulena = strlen(psrca);
  2234. ulenb = strlen(psrcb);
  2235. ulenstr = strlen(pstr);
  2236. if (ulen > ulena + ulenb + ulenstr) {
  2237. snprintf(strbuf, ulen, "%s%s%s", psrca, pstr, psrcb);
  2238. ret = 0;
  2239. }
  2240. return ret;
  2241. }