strutil.c 51 KB

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