str_parms.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /*
  2. * Copyright (C) 2011 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <cutils/str_parms.h>
  17. #define LOG_TAG "str_params"
  18. //#define LOG_NDEBUG 0
  19. #define _GNU_SOURCE 1
  20. #include <errno.h>
  21. #include <stdint.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <cutils/hashmap.h>
  26. #include <cutils/memory.h>
  27. #include <log/log.h>
  28. /* When an object is allocated but not freed in a function,
  29. * because its ownership is released to other object like a hashmap,
  30. * call RELEASE_OWNERSHIP to tell the clang analyzer and avoid
  31. * false warnings about potential memory leak.
  32. * For now, a "temporary" assignment to global variables
  33. * is enough to confuse the clang static analyzer.
  34. */
  35. #ifdef __clang_analyzer__
  36. static void *released_pointer;
  37. #define RELEASE_OWNERSHIP(x) { released_pointer = x; released_pointer = 0; }
  38. #else
  39. #define RELEASE_OWNERSHIP(x)
  40. #endif
  41. struct str_parms {
  42. Hashmap *map;
  43. };
  44. static bool str_eq(void *key_a, void *key_b)
  45. {
  46. return !strcmp((const char *)key_a, (const char *)key_b);
  47. }
  48. /* use djb hash unless we find it inadequate */
  49. #ifdef __clang__
  50. __attribute__((no_sanitize("integer")))
  51. #endif
  52. static int str_hash_fn(void *str)
  53. {
  54. uint32_t hash = 5381;
  55. for (char* p = static_cast<char*>(str); p && *p; p++)
  56. hash = ((hash << 5) + hash) + *p;
  57. return (int)hash;
  58. }
  59. struct str_parms *str_parms_create(void)
  60. {
  61. str_parms* s = static_cast<str_parms*>(calloc(1, sizeof(str_parms)));
  62. if (!s) return NULL;
  63. s->map = hashmapCreate(5, str_hash_fn, str_eq);
  64. if (!s->map) {
  65. free(s);
  66. return NULL;
  67. }
  68. return s;
  69. }
  70. struct remove_ctxt {
  71. struct str_parms *str_parms;
  72. const char *key;
  73. };
  74. static bool remove_pair(void *key, void *value, void *context)
  75. {
  76. remove_ctxt* ctxt = static_cast<remove_ctxt*>(context);
  77. bool should_continue;
  78. /*
  79. * - if key is not supplied, then we are removing all entries,
  80. * so remove key and continue (i.e. return true)
  81. * - if key is supplied and matches, then remove it and don't
  82. * continue (return false). Otherwise, return true and keep searching
  83. * for key.
  84. *
  85. */
  86. if (!ctxt->key) {
  87. should_continue = true;
  88. goto do_remove;
  89. } else if (!strcmp(ctxt->key, static_cast<const char*>(key))) {
  90. should_continue = false;
  91. goto do_remove;
  92. }
  93. return true;
  94. do_remove:
  95. hashmapRemove(ctxt->str_parms->map, key);
  96. free(key);
  97. free(value);
  98. return should_continue;
  99. }
  100. void str_parms_del(struct str_parms *str_parms, const char *key)
  101. {
  102. struct remove_ctxt ctxt = {
  103. .str_parms = str_parms,
  104. .key = key,
  105. };
  106. hashmapForEach(str_parms->map, remove_pair, &ctxt);
  107. }
  108. void str_parms_destroy(struct str_parms *str_parms)
  109. {
  110. struct remove_ctxt ctxt = {
  111. .str_parms = str_parms,
  112. };
  113. hashmapForEach(str_parms->map, remove_pair, &ctxt);
  114. hashmapFree(str_parms->map);
  115. free(str_parms);
  116. }
  117. struct str_parms *str_parms_create_str(const char *_string)
  118. {
  119. struct str_parms *str_parms;
  120. char *str;
  121. char *kvpair;
  122. char *tmpstr;
  123. int items = 0;
  124. str_parms = str_parms_create();
  125. if (!str_parms)
  126. goto err_create_str_parms;
  127. str = strdup(_string);
  128. if (!str)
  129. goto err_strdup;
  130. ALOGV("%s: source string == '%s'\n", __func__, _string);
  131. kvpair = strtok_r(str, ";", &tmpstr);
  132. while (kvpair && *kvpair) {
  133. char *eq = strchr(kvpair, '='); /* would love strchrnul */
  134. char *value;
  135. char *key;
  136. void *old_val;
  137. if (eq == kvpair)
  138. goto next_pair;
  139. if (eq) {
  140. key = strndup(kvpair, eq - kvpair);
  141. if (*(++eq))
  142. value = strdup(eq);
  143. else
  144. value = strdup("");
  145. } else {
  146. key = strdup(kvpair);
  147. value = strdup("");
  148. }
  149. /* if we replaced a value, free it */
  150. old_val = hashmapPut(str_parms->map, key, value);
  151. RELEASE_OWNERSHIP(value);
  152. if (old_val) {
  153. free(old_val);
  154. free(key);
  155. } else {
  156. RELEASE_OWNERSHIP(key);
  157. }
  158. items++;
  159. next_pair:
  160. kvpair = strtok_r(NULL, ";", &tmpstr);
  161. }
  162. if (!items)
  163. ALOGV("%s: no items found in string\n", __func__);
  164. free(str);
  165. return str_parms;
  166. err_strdup:
  167. str_parms_destroy(str_parms);
  168. err_create_str_parms:
  169. return NULL;
  170. }
  171. int str_parms_add_str(struct str_parms *str_parms, const char *key,
  172. const char *value)
  173. {
  174. void *tmp_key = NULL;
  175. void *tmp_val = NULL;
  176. void *old_val = NULL;
  177. // strdup and hashmapPut both set errno on failure.
  178. // Set errno to 0 so we can recognize whether anything went wrong.
  179. int saved_errno = errno;
  180. errno = 0;
  181. tmp_key = strdup(key);
  182. if (tmp_key == NULL) {
  183. goto clean_up;
  184. }
  185. tmp_val = strdup(value);
  186. if (tmp_val == NULL) {
  187. goto clean_up;
  188. }
  189. old_val = hashmapPut(str_parms->map, tmp_key, tmp_val);
  190. if (old_val == NULL) {
  191. // Did hashmapPut fail?
  192. if (errno == ENOMEM) {
  193. goto clean_up;
  194. }
  195. // For new keys, hashmap takes ownership of tmp_key and tmp_val.
  196. RELEASE_OWNERSHIP(tmp_key);
  197. RELEASE_OWNERSHIP(tmp_val);
  198. tmp_key = tmp_val = NULL;
  199. } else {
  200. // For existing keys, hashmap takes ownership of tmp_val.
  201. // (It also gives up ownership of old_val.)
  202. RELEASE_OWNERSHIP(tmp_val);
  203. tmp_val = NULL;
  204. }
  205. clean_up:
  206. free(tmp_key);
  207. free(tmp_val);
  208. free(old_val);
  209. int result = -errno;
  210. errno = saved_errno;
  211. return result;
  212. }
  213. int str_parms_add_int(struct str_parms *str_parms, const char *key, int value)
  214. {
  215. char val_str[12];
  216. int ret;
  217. ret = snprintf(val_str, sizeof(val_str), "%d", value);
  218. if (ret < 0)
  219. return -EINVAL;
  220. ret = str_parms_add_str(str_parms, key, val_str);
  221. return ret;
  222. }
  223. int str_parms_add_float(struct str_parms *str_parms, const char *key,
  224. float value)
  225. {
  226. char val_str[23];
  227. int ret;
  228. ret = snprintf(val_str, sizeof(val_str), "%.10f", value);
  229. if (ret < 0)
  230. return -EINVAL;
  231. ret = str_parms_add_str(str_parms, key, val_str);
  232. return ret;
  233. }
  234. int str_parms_has_key(struct str_parms *str_parms, const char *key) {
  235. return hashmapGet(str_parms->map, (void *)key) != NULL;
  236. }
  237. int str_parms_get_str(struct str_parms *str_parms, const char *key, char *val,
  238. int len)
  239. {
  240. // TODO: hashmapGet should take a const* key.
  241. char* value = static_cast<char*>(hashmapGet(str_parms->map, (void*)key));
  242. if (value)
  243. return strlcpy(val, value, len);
  244. return -ENOENT;
  245. }
  246. int str_parms_get_int(struct str_parms *str_parms, const char *key, int *val)
  247. {
  248. char *end;
  249. // TODO: hashmapGet should take a const* key.
  250. char* value = static_cast<char*>(hashmapGet(str_parms->map, (void*)key));
  251. if (!value)
  252. return -ENOENT;
  253. *val = (int)strtol(value, &end, 0);
  254. if (*value != '\0' && *end == '\0')
  255. return 0;
  256. return -EINVAL;
  257. }
  258. int str_parms_get_float(struct str_parms *str_parms, const char *key,
  259. float *val)
  260. {
  261. float out;
  262. char *end;
  263. // TODO: hashmapGet should take a const* key.
  264. char* value = static_cast<char*>(hashmapGet(str_parms->map, (void*)(key)));
  265. if (!value)
  266. return -ENOENT;
  267. out = strtof(value, &end);
  268. if (*value == '\0' || *end != '\0')
  269. return -EINVAL;
  270. *val = out;
  271. return 0;
  272. }
  273. static bool combine_strings(void *key, void *value, void *context)
  274. {
  275. char** old_str = static_cast<char**>(context);
  276. char *new_str;
  277. int ret;
  278. ret = asprintf(&new_str, "%s%s%s=%s",
  279. *old_str ? *old_str : "",
  280. *old_str ? ";" : "",
  281. (char *)key,
  282. (char *)value);
  283. if (*old_str)
  284. free(*old_str);
  285. if (ret >= 0) {
  286. *old_str = new_str;
  287. return true;
  288. }
  289. *old_str = NULL;
  290. return false;
  291. }
  292. char *str_parms_to_str(struct str_parms *str_parms)
  293. {
  294. char *str = NULL;
  295. hashmapForEach(str_parms->map, combine_strings, &str);
  296. return (str != NULL) ? str : strdup("");
  297. }
  298. static bool dump_entry(void* key, void* value, void* /*context*/) {
  299. ALOGI("key: '%s' value: '%s'\n", (char *)key, (char *)value);
  300. return true;
  301. }
  302. void str_parms_dump(struct str_parms *str_parms)
  303. {
  304. hashmapForEach(str_parms->map, dump_entry, str_parms);
  305. }