avb_util.c 9.0 KB

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