avb_util.c 9.0 KB

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