string.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. /*
  2. *
  3. * Embedded Linux library
  4. *
  5. * Copyright (C) 2011-2014 Intel Corporation. All rights reserved.
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. */
  22. #ifdef HAVE_CONFIG_H
  23. #include <config.h>
  24. #endif
  25. #include <stdio.h>
  26. #include "strv.h"
  27. #include "string.h"
  28. #include "private.h"
  29. #include "useful.h"
  30. /**
  31. * SECTION:string
  32. * @short_description: Growable string buffer
  33. *
  34. * Growable string buffer support
  35. */
  36. /**
  37. * l_string:
  38. *
  39. * Opague object representing the string buffer.
  40. */
  41. struct l_string {
  42. size_t max;
  43. size_t len;
  44. char *str;
  45. };
  46. static inline size_t next_power(size_t len)
  47. {
  48. size_t n = 1;
  49. if (len > SIZE_MAX / 2)
  50. return SIZE_MAX;
  51. while (n < len)
  52. n = n << 1;
  53. return n;
  54. }
  55. static void grow_string(struct l_string *str, size_t extra)
  56. {
  57. if (str->len + extra < str->max)
  58. return;
  59. str->max = next_power(str->len + extra + 1);
  60. str->str = l_realloc(str->str, str->max);
  61. }
  62. /**
  63. * l_string_new:
  64. * @initial_length: Initial length of the groable string
  65. *
  66. * Create new growable string. If the @initial_length is 0, then a safe
  67. * default is chosen.
  68. *
  69. * Returns: a newly allocated #l_string object.
  70. **/
  71. LIB_EXPORT struct l_string *l_string_new(size_t initial_length)
  72. {
  73. static const size_t DEFAULT_INITIAL_LENGTH = 127;
  74. struct l_string *ret;
  75. ret = l_new(struct l_string, 1);
  76. if (initial_length == 0)
  77. initial_length = DEFAULT_INITIAL_LENGTH;
  78. grow_string(ret, initial_length);
  79. ret->str[0] = '\0';
  80. return ret;
  81. }
  82. /**
  83. * l_string_free:
  84. * @string: growable string object
  85. *
  86. * Free the growable string object and all associated data
  87. **/
  88. LIB_EXPORT void l_string_free(struct l_string *string)
  89. {
  90. if (unlikely(!string))
  91. return;
  92. l_free(string->str);
  93. l_free(string);
  94. }
  95. /**
  96. * l_string_unwrap:
  97. * @string: growable string object
  98. *
  99. * Free the growable string object and return the internal string data.
  100. * The caller is responsible for freeing the string data using l_free(),
  101. * and the string object is no longer usable.
  102. *
  103. * Returns: @string's internal buffer
  104. **/
  105. LIB_EXPORT char *l_string_unwrap(struct l_string *string)
  106. {
  107. char *result;
  108. if (unlikely(!string))
  109. return NULL;
  110. result = string->str;
  111. l_free(string);
  112. return result;
  113. }
  114. /**
  115. * l_string_append:
  116. * @dest: growable string object
  117. * @src: C-style string to copy
  118. *
  119. * Appends the contents of @src to @dest. The internal buffer of @dest is
  120. * grown if necessary.
  121. *
  122. * Returns: @dest
  123. **/
  124. LIB_EXPORT struct l_string *l_string_append(struct l_string *dest,
  125. const char *src)
  126. {
  127. size_t size;
  128. if (unlikely(!dest || !src))
  129. return NULL;
  130. size = strlen(src);
  131. grow_string(dest, size);
  132. memcpy(dest->str + dest->len, src, size);
  133. dest->len += size;
  134. dest->str[dest->len] = '\0';
  135. return dest;
  136. }
  137. /**
  138. * l_string_append_c:
  139. * @dest: growable string object
  140. * @c: Character
  141. *
  142. * Appends character given by @c to @dest. The internal buffer of @dest is
  143. * grown if necessary.
  144. *
  145. * Returns: @dest
  146. **/
  147. LIB_EXPORT struct l_string *l_string_append_c(struct l_string *dest,
  148. const char c)
  149. {
  150. if (unlikely(!dest))
  151. return NULL;
  152. grow_string(dest, 1);
  153. dest->str[dest->len++] = c;
  154. dest->str[dest->len] = '\0';
  155. return dest;
  156. }
  157. /**
  158. * l_string_append_fixed:
  159. * @dest: growable string object
  160. * @src: Character array to copy from
  161. * @max: Maximum number of characters to copy
  162. *
  163. * Appends the contents of a fixed size string array @src to @dest.
  164. * The internal buffer of @dest is grown if necessary. Up to a maximum of
  165. * @max characters are copied. If a null is encountered in the first @max
  166. * characters, the string is copied only up to the NULL character.
  167. *
  168. * Returns: @dest
  169. **/
  170. LIB_EXPORT struct l_string *l_string_append_fixed(struct l_string *dest,
  171. const char *src,
  172. size_t max)
  173. {
  174. const char *nul;
  175. if (unlikely(!dest || !src || !max))
  176. return NULL;
  177. nul = memchr(src, 0, max);
  178. if (nul)
  179. max = nul - src;
  180. grow_string(dest, max);
  181. memcpy(dest->str + dest->len, src, max);
  182. dest->len += max;
  183. dest->str[dest->len] = '\0';
  184. return dest;
  185. }
  186. /**
  187. * l_string_append_vprintf:
  188. * @dest: growable string object
  189. * @format: the string format. See the sprintf() documentation
  190. * @args: the parameters to insert
  191. *
  192. * Appends a formatted string to the growable string buffer. This function
  193. * is equivalent to l_string_append_printf except that the arguments are
  194. * passed as a va_list.
  195. **/
  196. LIB_EXPORT void l_string_append_vprintf(struct l_string *dest,
  197. const char *format, va_list args)
  198. {
  199. size_t len;
  200. size_t have_space;
  201. va_list args_copy;
  202. if (unlikely(!dest))
  203. return;
  204. #if __STDC_VERSION__ > 199409L
  205. va_copy(args_copy, args);
  206. #else
  207. __va_copy(args_copy, args);
  208. #endif
  209. have_space = dest->max - dest->len;
  210. len = vsnprintf(dest->str + dest->len, have_space, format, args);
  211. if (len >= have_space) {
  212. grow_string(dest, len);
  213. len = vsprintf(dest->str + dest->len, format, args_copy);
  214. }
  215. dest->len += len;
  216. va_end(args_copy);
  217. }
  218. /**
  219. * l_string_append_printf:
  220. * @dest: growable string object
  221. * @format: the string format. See the sprintf() documentation
  222. * @...: the parameters to insert
  223. *
  224. * Appends a formatted string to the growable string buffer, growing it as
  225. * necessary.
  226. **/
  227. LIB_EXPORT void l_string_append_printf(struct l_string *dest,
  228. const char *format, ...)
  229. {
  230. va_list args;
  231. if (unlikely(!dest))
  232. return;
  233. va_start(args, format);
  234. l_string_append_vprintf(dest, format, args);
  235. va_end(args);
  236. }
  237. /**
  238. * l_string_length:
  239. * @string: growable string object
  240. *
  241. * Returns: bytes used in the string.
  242. **/
  243. LIB_EXPORT unsigned int l_string_length(struct l_string *string)
  244. {
  245. if (unlikely(!string))
  246. return 0;
  247. return string->len;
  248. }
  249. LIB_EXPORT struct l_string *l_string_truncate(struct l_string *string,
  250. size_t new_size)
  251. {
  252. if (unlikely(!string))
  253. return NULL;
  254. if (new_size >= string->len)
  255. return string;
  256. string->len = new_size;
  257. string->str[new_size] = '\0';
  258. return string;
  259. }
  260. struct arg {
  261. size_t max_len;
  262. size_t cur_len;
  263. char *chars;
  264. };
  265. static inline void arg_init(struct arg *arg)
  266. {
  267. arg->max_len = 0;
  268. arg->cur_len = 0;
  269. arg->chars = NULL;
  270. }
  271. static void arg_putchar(struct arg *arg, char ch)
  272. {
  273. if (arg->cur_len == arg->max_len) {
  274. arg->max_len += 32; /* Grow by at least 32 bytes */
  275. arg->chars = l_realloc(arg->chars, 1 + arg->max_len);
  276. }
  277. arg->chars[arg->cur_len++] = ch;
  278. arg->chars[arg->cur_len] = '\0';
  279. }
  280. static void arg_putmem(struct arg *arg, const void *mem, size_t len)
  281. {
  282. if (len == 0)
  283. return;
  284. if (arg->cur_len + len > arg->max_len) {
  285. size_t growby = len * 2;
  286. if (growby < 32)
  287. growby = 32;
  288. arg->max_len += growby;
  289. arg->chars = l_realloc(arg->chars, 1 + arg->max_len);
  290. }
  291. memcpy(arg->chars + arg->cur_len, mem, len);
  292. arg->cur_len += len;
  293. arg->chars[arg->cur_len] = '\0';
  294. }
  295. static bool parse_backslash(struct arg *arg, const char *args, size_t *pos)
  296. {
  297. /* We're at the backslash, not within double quotes */
  298. char c = args[*pos + 1];
  299. switch (c) {
  300. case 0:
  301. return false;
  302. case '\n':
  303. break;
  304. default:
  305. arg_putchar(arg, c);
  306. break;
  307. }
  308. *pos += 1;
  309. return true;
  310. }
  311. static bool parse_quoted_backslash(struct arg *arg,
  312. const char *args, size_t *pos)
  313. {
  314. /* We're at the backslash, within double quotes */
  315. char c = args[*pos + 1];
  316. switch (c) {
  317. case 0:
  318. return false;
  319. case '\n':
  320. break;
  321. case '"':
  322. case '\\':
  323. arg_putchar(arg, c);
  324. break;
  325. default:
  326. arg_putchar(arg, '\\');
  327. arg_putchar(arg, c);
  328. break;
  329. }
  330. *pos += 1;
  331. return true;
  332. }
  333. static bool parse_single_quote(struct arg *arg, const char *args, size_t *pos)
  334. {
  335. /* We're just past the single quote */
  336. size_t start = *pos;
  337. for (; args[*pos]; *pos += 1) {
  338. if (args[*pos] != '\'')
  339. continue;
  340. arg_putmem(arg, args + start, *pos - start);
  341. return true;
  342. }
  343. /* Unterminated ' */
  344. return false;
  345. }
  346. static bool parse_double_quote(struct arg *arg, const char *args, size_t *pos)
  347. {
  348. /* We're just past the double quote */
  349. for (; args[*pos]; *pos += 1) {
  350. char c = args[*pos];
  351. switch (c) {
  352. case '"':
  353. return true;
  354. case '\\':
  355. if (!parse_quoted_backslash(arg, args, pos))
  356. return false;
  357. break;
  358. default:
  359. arg_putchar(arg, c);
  360. break;
  361. }
  362. }
  363. /* Unterminated */
  364. return false;
  365. }
  366. static void add_arg(char ***args, char *arg, int *n_args)
  367. {
  368. *args = l_realloc(*args, sizeof(char *) * (2 + *n_args));
  369. (*args)[*n_args] = arg;
  370. (*args)[*n_args + 1] = NULL;
  371. *n_args += 1;
  372. }
  373. LIB_EXPORT char **l_parse_args(const char *args, int *out_n_args)
  374. {
  375. size_t i;
  376. struct arg arg;
  377. char **ret = l_realloc(NULL, sizeof(char *));
  378. int n_args = 0;
  379. ret[0] = NULL;
  380. arg_init(&arg);
  381. for (i = 0; args[i]; i++) {
  382. switch (args[i]) {
  383. case '\\':
  384. if (!parse_backslash(&arg, args, &i))
  385. goto error;
  386. break;
  387. case '"':
  388. i += 1;
  389. if (!parse_double_quote(&arg, args, &i))
  390. goto error;
  391. /* Add an empty string */
  392. if (!arg.cur_len)
  393. add_arg(&ret, l_strdup(""), &n_args);
  394. break;
  395. case '\'':
  396. i += 1;
  397. if (!parse_single_quote(&arg, args, &i))
  398. goto error;
  399. /* Add an empty string */
  400. if (!arg.cur_len)
  401. add_arg(&ret, l_strdup(""), &n_args);
  402. break;
  403. default:
  404. if (!strchr(" \t", args[i])) {
  405. if (args[i] == '\n')
  406. goto error;
  407. arg_putchar(&arg, args[i]);
  408. continue;
  409. }
  410. if (arg.cur_len)
  411. add_arg(&ret, arg.chars, &n_args);
  412. arg_init(&arg);
  413. break;
  414. }
  415. }
  416. if (arg.cur_len)
  417. add_arg(&ret, arg.chars, &n_args);
  418. if (out_n_args)
  419. *out_n_args = n_args;
  420. return ret;
  421. error:
  422. l_free(arg.chars);
  423. l_strfreev(ret);
  424. return NULL;
  425. }