avb_util.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /*
  2. * Copyright (C) 2016 The Android Open Source Project
  3. *
  4. * Permission is hereby granted, free of charge, to any person
  5. * obtaining a copy of this software and associated documentation
  6. * files (the "Software"), to deal in the Software without
  7. * restriction, including without limitation the rights to use, copy,
  8. * modify, merge, publish, distribute, sublicense, and/or sell copies
  9. * of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  19. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  20. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. * SOFTWARE.
  23. */
  24. #include "avb_util.h"
  25. #include <stdarg.h>
  26. uint32_t avb_be32toh(uint32_t in) {
  27. uint8_t* d = (uint8_t*)&in;
  28. uint32_t ret;
  29. ret = ((uint32_t)d[0]) << 24;
  30. ret |= ((uint32_t)d[1]) << 16;
  31. ret |= ((uint32_t)d[2]) << 8;
  32. ret |= ((uint32_t)d[3]);
  33. return ret;
  34. }
  35. uint64_t avb_be64toh(uint64_t in) {
  36. uint8_t* d = (uint8_t*)&in;
  37. uint64_t ret;
  38. ret = ((uint64_t)d[0]) << 56;
  39. ret |= ((uint64_t)d[1]) << 48;
  40. ret |= ((uint64_t)d[2]) << 40;
  41. ret |= ((uint64_t)d[3]) << 32;
  42. ret |= ((uint64_t)d[4]) << 24;
  43. ret |= ((uint64_t)d[5]) << 16;
  44. ret |= ((uint64_t)d[6]) << 8;
  45. ret |= ((uint64_t)d[7]);
  46. return ret;
  47. }
  48. /* Converts a 32-bit unsigned integer from host to big-endian byte order. */
  49. uint32_t avb_htobe32(uint32_t in) {
  50. union {
  51. uint32_t word;
  52. uint8_t bytes[4];
  53. } ret;
  54. ret.bytes[0] = (in >> 24) & 0xff;
  55. ret.bytes[1] = (in >> 16) & 0xff;
  56. ret.bytes[2] = (in >> 8) & 0xff;
  57. ret.bytes[3] = in & 0xff;
  58. return ret.word;
  59. }
  60. /* Converts a 64-bit unsigned integer from host to big-endian byte order. */
  61. uint64_t avb_htobe64(uint64_t in) {
  62. union {
  63. uint64_t word;
  64. uint8_t bytes[8];
  65. } ret;
  66. ret.bytes[0] = (in >> 56) & 0xff;
  67. ret.bytes[1] = (in >> 48) & 0xff;
  68. ret.bytes[2] = (in >> 40) & 0xff;
  69. ret.bytes[3] = (in >> 32) & 0xff;
  70. ret.bytes[4] = (in >> 24) & 0xff;
  71. ret.bytes[5] = (in >> 16) & 0xff;
  72. ret.bytes[6] = (in >> 8) & 0xff;
  73. ret.bytes[7] = in & 0xff;
  74. return ret.word;
  75. }
  76. int avb_safe_memcmp(const void* s1, const void* s2, size_t n) {
  77. const unsigned char* us1 = s1;
  78. const unsigned char* us2 = s2;
  79. int result = 0;
  80. if (0 == n) {
  81. return 0;
  82. }
  83. /*
  84. * Code snippet without data-dependent branch due to Nate Lawson
  85. * (nate@root.org) of Root Labs.
  86. */
  87. while (n--) {
  88. result |= *us1++ ^ *us2++;
  89. }
  90. return result != 0;
  91. }
  92. bool avb_safe_add_to(uint64_t* value, uint64_t value_to_add) {
  93. uint64_t original_value;
  94. avb_assert(value != NULL);
  95. original_value = *value;
  96. *value += value_to_add;
  97. if (*value < original_value) {
  98. avb_error("Overflow when adding values.\n");
  99. return false;
  100. }
  101. return true;
  102. }
  103. bool avb_safe_add(uint64_t* out_result, uint64_t a, uint64_t b) {
  104. uint64_t dummy;
  105. if (out_result == NULL) {
  106. out_result = &dummy;
  107. }
  108. *out_result = a;
  109. return avb_safe_add_to(out_result, b);
  110. }
  111. bool avb_validate_utf8(const uint8_t* data, size_t num_bytes) {
  112. size_t n;
  113. unsigned int num_cc;
  114. for (n = 0, num_cc = 0; n < num_bytes; n++) {
  115. uint8_t c = data[n];
  116. if (num_cc > 0) {
  117. if ((c & (0x80 | 0x40)) == 0x80) {
  118. /* 10xx xxxx */
  119. } else {
  120. goto fail;
  121. }
  122. num_cc--;
  123. } else {
  124. if (c < 0x80) {
  125. num_cc = 0;
  126. } else if ((c & (0x80 | 0x40 | 0x20)) == (0x80 | 0x40)) {
  127. /* 110x xxxx */
  128. num_cc = 1;
  129. } else if ((c & (0x80 | 0x40 | 0x20 | 0x10)) == (0x80 | 0x40 | 0x20)) {
  130. /* 1110 xxxx */
  131. num_cc = 2;
  132. } else if ((c & (0x80 | 0x40 | 0x20 | 0x10 | 0x08)) ==
  133. (0x80 | 0x40 | 0x20 | 0x10)) {
  134. /* 1111 0xxx */
  135. num_cc = 3;
  136. } else {
  137. goto fail;
  138. }
  139. }
  140. }
  141. if (num_cc != 0) {
  142. goto fail;
  143. }
  144. return true;
  145. fail:
  146. return false;
  147. }
  148. bool avb_str_concat(char* buf,
  149. size_t buf_size,
  150. const char* str1,
  151. size_t str1_len,
  152. const char* str2,
  153. size_t str2_len) {
  154. uint64_t combined_len;
  155. if (!avb_safe_add(&combined_len, str1_len, str2_len)) {
  156. avb_error("Overflow when adding string sizes.\n");
  157. return false;
  158. }
  159. if (combined_len > buf_size - 1) {
  160. avb_error("Insufficient buffer space.\n");
  161. return false;
  162. }
  163. avb_memcpy(buf, str1, str1_len);
  164. avb_memcpy(buf + str1_len, str2, str2_len);
  165. buf[combined_len] = '\0';
  166. return true;
  167. }
  168. void* avb_malloc(size_t size) {
  169. void* ret = avb_malloc_(size);
  170. if (ret == NULL) {
  171. avb_error("Failed to allocate memory.\n");
  172. return NULL;
  173. }
  174. return ret;
  175. }
  176. void* avb_calloc(size_t size) {
  177. void* ret = avb_malloc(size);
  178. if (ret == NULL) {
  179. return NULL;
  180. }
  181. avb_memset(ret, '\0', size);
  182. return ret;
  183. }
  184. char* avb_strdup(const char* str) {
  185. size_t len = avb_strlen(str);
  186. char* ret = avb_malloc(len + 1);
  187. if (ret == NULL) {
  188. return NULL;
  189. }
  190. avb_memcpy(ret, str, len);
  191. ret[len] = '\0';
  192. return ret;
  193. }
  194. const char* avb_strstr(const char* haystack, const char* needle) {
  195. size_t n, m;
  196. /* Look through |haystack| and check if the first character of
  197. * |needle| matches. If so, check the rest of |needle|.
  198. */
  199. for (n = 0; haystack[n] != '\0'; n++) {
  200. if (haystack[n] != needle[0]) {
  201. continue;
  202. }
  203. for (m = 1;; m++) {
  204. if (needle[m] == '\0') {
  205. return haystack + n;
  206. }
  207. if (haystack[n + m] != needle[m]) {
  208. break;
  209. }
  210. }
  211. }
  212. return NULL;
  213. }
  214. const char* avb_strv_find_str(const char* const* strings,
  215. const char* str,
  216. size_t str_size) {
  217. size_t n;
  218. for (n = 0; strings[n] != NULL; n++) {
  219. if (avb_strlen(strings[n]) == str_size &&
  220. avb_memcmp(strings[n], str, str_size) == 0) {
  221. return strings[n];
  222. }
  223. }
  224. return NULL;
  225. }
  226. char* avb_replace(const char* str, const char* search, const char* replace) {
  227. char* ret = NULL;
  228. size_t ret_len = 0;
  229. size_t search_len, replace_len;
  230. const char* str_after_last_replace;
  231. search_len = avb_strlen(search);
  232. replace_len = avb_strlen(replace);
  233. str_after_last_replace = str;
  234. while (*str != '\0') {
  235. const char* s;
  236. size_t num_before;
  237. size_t num_new;
  238. s = avb_strstr(str, search);
  239. if (s == NULL) {
  240. break;
  241. }
  242. num_before = s - str;
  243. if (ret == NULL) {
  244. num_new = num_before + replace_len + 1;
  245. ret = avb_malloc(num_new);
  246. if (ret == NULL) {
  247. goto out;
  248. }
  249. avb_memcpy(ret, str, num_before);
  250. avb_memcpy(ret + num_before, replace, replace_len);
  251. ret[num_new - 1] = '\0';
  252. ret_len = num_new - 1;
  253. } else {
  254. char* new_str;
  255. num_new = ret_len + num_before + replace_len + 1;
  256. new_str = avb_malloc(num_new);
  257. if (new_str == NULL) {
  258. goto out;
  259. }
  260. avb_memcpy(new_str, ret, ret_len);
  261. avb_memcpy(new_str + ret_len, str, num_before);
  262. avb_memcpy(new_str + ret_len + num_before, replace, replace_len);
  263. new_str[num_new - 1] = '\0';
  264. avb_free(ret);
  265. ret = new_str;
  266. ret_len = num_new - 1;
  267. }
  268. str = s + search_len;
  269. str_after_last_replace = str;
  270. }
  271. if (ret == NULL) {
  272. ret = avb_strdup(str_after_last_replace);
  273. if (ret == NULL) {
  274. goto out;
  275. }
  276. } else {
  277. size_t num_remaining = avb_strlen(str_after_last_replace);
  278. size_t num_new = ret_len + num_remaining + 1;
  279. char* new_str = avb_malloc(num_new);
  280. if (new_str == NULL) {
  281. goto out;
  282. }
  283. avb_memcpy(new_str, ret, ret_len);
  284. avb_memcpy(new_str + ret_len, str_after_last_replace, num_remaining);
  285. new_str[num_new - 1] = '\0';
  286. avb_free(ret);
  287. ret = new_str;
  288. ret_len = num_new - 1;
  289. }
  290. out:
  291. return ret;
  292. }
  293. /* We only support a limited amount of strings in avb_strdupv(). */
  294. #define AVB_STRDUPV_MAX_NUM_STRINGS 32
  295. char* avb_strdupv(const char* str, ...) {
  296. va_list ap;
  297. const char* strings[AVB_STRDUPV_MAX_NUM_STRINGS];
  298. size_t lengths[AVB_STRDUPV_MAX_NUM_STRINGS];
  299. size_t num_strings, n;
  300. uint64_t total_length;
  301. char *ret = NULL, *dest;
  302. num_strings = 0;
  303. total_length = 0;
  304. va_start(ap, str);
  305. do {
  306. size_t str_len = avb_strlen(str);
  307. strings[num_strings] = str;
  308. lengths[num_strings] = str_len;
  309. if (!avb_safe_add_to(&total_length, str_len)) {
  310. avb_fatal("Overflow while determining total length.\n");
  311. break;
  312. }
  313. num_strings++;
  314. if (num_strings == AVB_STRDUPV_MAX_NUM_STRINGS) {
  315. avb_fatal("Too many strings passed.\n");
  316. break;
  317. }
  318. str = va_arg(ap, const char*);
  319. } while (str != NULL);
  320. va_end(ap);
  321. ret = avb_malloc(total_length + 1);
  322. if (ret == NULL) {
  323. goto out;
  324. }
  325. dest = ret;
  326. for (n = 0; n < num_strings; n++) {
  327. avb_memcpy(dest, strings[n], lengths[n]);
  328. dest += lengths[n];
  329. }
  330. *dest = '\0';
  331. avb_assert(dest == ret + total_length);
  332. out:
  333. return ret;
  334. }
  335. const char* avb_basename(const char* str) {
  336. int64_t n;
  337. size_t len;
  338. len = avb_strlen(str);
  339. if (len >= 2) {
  340. for (n = len - 2; n >= 0; n--) {
  341. if (str[n] == '/') {
  342. return str + n + 1;
  343. }
  344. }
  345. }
  346. return str;
  347. }
  348. void avb_uppercase(char* str) {
  349. size_t i;
  350. for (i = 0; str[i] != '\0'; ++i) {
  351. if (str[i] <= 0x7A && str[i] >= 0x61) {
  352. str[i] -= 0x20;
  353. }
  354. }
  355. }
  356. char* avb_bin2hex(const uint8_t* data, size_t data_len) {
  357. const char hex_digits[17] = "0123456789abcdef";
  358. char* hex_data;
  359. size_t n;
  360. hex_data = avb_malloc(data_len * 2 + 1);
  361. if (hex_data == NULL) {
  362. return NULL;
  363. }
  364. for (n = 0; n < data_len; n++) {
  365. hex_data[n * 2] = hex_digits[data[n] >> 4];
  366. hex_data[n * 2 + 1] = hex_digits[data[n] & 0x0f];
  367. }
  368. hex_data[n * 2] = '\0';
  369. return hex_data;
  370. }