byte_buf.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710
  1. /**
  2. * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
  3. * SPDX-License-Identifier: Apache-2.0.
  4. */
  5. #include <aws/common/byte_buf.h>
  6. #include <aws/common/private/byte_buf.h>
  7. #include <stdarg.h>
  8. #ifdef _MSC_VER
  9. /* disables warning non const declared initializers for Microsoft compilers */
  10. # pragma warning(disable : 4204)
  11. # pragma warning(disable : 4706)
  12. #endif
  13. int aws_byte_buf_init(struct aws_byte_buf *buf, struct aws_allocator *allocator, size_t capacity) {
  14. AWS_PRECONDITION(buf);
  15. AWS_PRECONDITION(allocator);
  16. buf->buffer = (capacity == 0) ? NULL : aws_mem_acquire(allocator, capacity);
  17. if (capacity != 0 && buf->buffer == NULL) {
  18. AWS_ZERO_STRUCT(*buf);
  19. return AWS_OP_ERR;
  20. }
  21. buf->len = 0;
  22. buf->capacity = capacity;
  23. buf->allocator = allocator;
  24. AWS_POSTCONDITION(aws_byte_buf_is_valid(buf));
  25. return AWS_OP_SUCCESS;
  26. }
  27. int aws_byte_buf_init_copy(struct aws_byte_buf *dest, struct aws_allocator *allocator, const struct aws_byte_buf *src) {
  28. AWS_PRECONDITION(allocator);
  29. AWS_PRECONDITION(dest);
  30. AWS_ERROR_PRECONDITION(aws_byte_buf_is_valid(src));
  31. if (!src->buffer) {
  32. AWS_ZERO_STRUCT(*dest);
  33. dest->allocator = allocator;
  34. AWS_POSTCONDITION(aws_byte_buf_is_valid(dest));
  35. return AWS_OP_SUCCESS;
  36. }
  37. *dest = *src;
  38. dest->allocator = allocator;
  39. dest->buffer = (uint8_t *)aws_mem_acquire(allocator, src->capacity);
  40. if (dest->buffer == NULL) {
  41. AWS_ZERO_STRUCT(*dest);
  42. return AWS_OP_ERR;
  43. }
  44. memcpy(dest->buffer, src->buffer, src->len);
  45. AWS_POSTCONDITION(aws_byte_buf_is_valid(dest));
  46. return AWS_OP_SUCCESS;
  47. }
  48. bool aws_byte_buf_is_valid(const struct aws_byte_buf *const buf) {
  49. return buf != NULL &&
  50. ((buf->capacity == 0 && buf->len == 0 && buf->buffer == NULL) ||
  51. (buf->capacity > 0 && buf->len <= buf->capacity && AWS_MEM_IS_WRITABLE(buf->buffer, buf->capacity)));
  52. }
  53. bool aws_byte_cursor_is_valid(const struct aws_byte_cursor *cursor) {
  54. return cursor != NULL &&
  55. ((cursor->len == 0) || (cursor->len > 0 && cursor->ptr && AWS_MEM_IS_READABLE(cursor->ptr, cursor->len)));
  56. }
  57. void aws_byte_buf_reset(struct aws_byte_buf *buf, bool zero_contents) {
  58. if (zero_contents) {
  59. aws_byte_buf_secure_zero(buf);
  60. }
  61. buf->len = 0;
  62. }
  63. void aws_byte_buf_clean_up(struct aws_byte_buf *buf) {
  64. AWS_PRECONDITION(aws_byte_buf_is_valid(buf));
  65. if (buf->allocator && buf->buffer) {
  66. aws_mem_release(buf->allocator, (void *)buf->buffer);
  67. }
  68. buf->allocator = NULL;
  69. buf->buffer = NULL;
  70. buf->len = 0;
  71. buf->capacity = 0;
  72. }
  73. void aws_byte_buf_secure_zero(struct aws_byte_buf *buf) {
  74. AWS_PRECONDITION(aws_byte_buf_is_valid(buf));
  75. if (buf->buffer) {
  76. aws_secure_zero(buf->buffer, buf->capacity);
  77. }
  78. buf->len = 0;
  79. AWS_POSTCONDITION(aws_byte_buf_is_valid(buf));
  80. }
  81. void aws_byte_buf_clean_up_secure(struct aws_byte_buf *buf) {
  82. AWS_PRECONDITION(aws_byte_buf_is_valid(buf));
  83. aws_byte_buf_secure_zero(buf);
  84. aws_byte_buf_clean_up(buf);
  85. AWS_POSTCONDITION(aws_byte_buf_is_valid(buf));
  86. }
  87. bool aws_byte_buf_eq(const struct aws_byte_buf *const a, const struct aws_byte_buf *const b) {
  88. AWS_PRECONDITION(aws_byte_buf_is_valid(a));
  89. AWS_PRECONDITION(aws_byte_buf_is_valid(b));
  90. bool rval = aws_array_eq(a->buffer, a->len, b->buffer, b->len);
  91. AWS_POSTCONDITION(aws_byte_buf_is_valid(a));
  92. AWS_POSTCONDITION(aws_byte_buf_is_valid(b));
  93. return rval;
  94. }
  95. bool aws_byte_buf_eq_ignore_case(const struct aws_byte_buf *const a, const struct aws_byte_buf *const b) {
  96. AWS_PRECONDITION(aws_byte_buf_is_valid(a));
  97. AWS_PRECONDITION(aws_byte_buf_is_valid(b));
  98. bool rval = aws_array_eq_ignore_case(a->buffer, a->len, b->buffer, b->len);
  99. AWS_POSTCONDITION(aws_byte_buf_is_valid(a));
  100. AWS_POSTCONDITION(aws_byte_buf_is_valid(b));
  101. return rval;
  102. }
  103. bool aws_byte_buf_eq_c_str(const struct aws_byte_buf *const buf, const char *const c_str) {
  104. AWS_PRECONDITION(aws_byte_buf_is_valid(buf));
  105. AWS_PRECONDITION(c_str != NULL);
  106. bool rval = aws_array_eq_c_str(buf->buffer, buf->len, c_str);
  107. AWS_POSTCONDITION(aws_byte_buf_is_valid(buf));
  108. return rval;
  109. }
  110. bool aws_byte_buf_eq_c_str_ignore_case(const struct aws_byte_buf *const buf, const char *const c_str) {
  111. AWS_PRECONDITION(aws_byte_buf_is_valid(buf));
  112. AWS_PRECONDITION(c_str != NULL);
  113. bool rval = aws_array_eq_c_str_ignore_case(buf->buffer, buf->len, c_str);
  114. AWS_POSTCONDITION(aws_byte_buf_is_valid(buf));
  115. return rval;
  116. }
  117. int aws_byte_buf_init_copy_from_cursor(
  118. struct aws_byte_buf *dest,
  119. struct aws_allocator *allocator,
  120. struct aws_byte_cursor src) {
  121. AWS_PRECONDITION(allocator);
  122. AWS_PRECONDITION(dest);
  123. AWS_ERROR_PRECONDITION(aws_byte_cursor_is_valid(&src));
  124. AWS_ZERO_STRUCT(*dest);
  125. dest->buffer = (src.len > 0) ? (uint8_t *)aws_mem_acquire(allocator, src.len) : NULL;
  126. if (src.len != 0 && dest->buffer == NULL) {
  127. return AWS_OP_ERR;
  128. }
  129. dest->len = src.len;
  130. dest->capacity = src.len;
  131. dest->allocator = allocator;
  132. if (src.len > 0) {
  133. memcpy(dest->buffer, src.ptr, src.len);
  134. }
  135. AWS_POSTCONDITION(aws_byte_buf_is_valid(dest));
  136. return AWS_OP_SUCCESS;
  137. }
  138. int aws_byte_buf_init_cache_and_update_cursors(struct aws_byte_buf *dest, struct aws_allocator *allocator, ...) {
  139. AWS_PRECONDITION(allocator);
  140. AWS_PRECONDITION(dest);
  141. AWS_ZERO_STRUCT(*dest);
  142. size_t total_len = 0;
  143. va_list args;
  144. va_start(args, allocator);
  145. /* Loop until final NULL arg is encountered */
  146. struct aws_byte_cursor *cursor_i;
  147. while ((cursor_i = va_arg(args, struct aws_byte_cursor *)) != NULL) {
  148. AWS_ASSERT(aws_byte_cursor_is_valid(cursor_i));
  149. if (aws_add_size_checked(total_len, cursor_i->len, &total_len)) {
  150. return AWS_OP_ERR;
  151. }
  152. }
  153. va_end(args);
  154. if (aws_byte_buf_init(dest, allocator, total_len)) {
  155. return AWS_OP_ERR;
  156. }
  157. va_start(args, allocator);
  158. while ((cursor_i = va_arg(args, struct aws_byte_cursor *)) != NULL) {
  159. /* Impossible for this call to fail, we pre-allocated sufficient space */
  160. aws_byte_buf_append_and_update(dest, cursor_i);
  161. }
  162. va_end(args);
  163. return AWS_OP_SUCCESS;
  164. }
  165. bool aws_byte_cursor_next_split(
  166. const struct aws_byte_cursor *AWS_RESTRICT input_str,
  167. char split_on,
  168. struct aws_byte_cursor *AWS_RESTRICT substr) {
  169. AWS_PRECONDITION(aws_byte_cursor_is_valid(input_str));
  170. /* If substr is zeroed-out, then this is the first run. */
  171. const bool first_run = substr->ptr == NULL;
  172. /* It's legal for input_str to be zeroed out: {.ptr=NULL, .len=0}
  173. * Deal with this case separately */
  174. if (AWS_UNLIKELY(input_str->ptr == NULL)) {
  175. if (first_run) {
  176. /* Set substr->ptr to something non-NULL so that next split() call doesn't look like the first run */
  177. substr->ptr = (void *)"";
  178. substr->len = 0;
  179. return true;
  180. }
  181. /* done */
  182. AWS_ZERO_STRUCT(*substr);
  183. return false;
  184. }
  185. /* Rest of function deals with non-NULL input_str->ptr */
  186. if (first_run) {
  187. *substr = *input_str;
  188. } else {
  189. /* This is not the first run.
  190. * Advance substr past the previous split. */
  191. const uint8_t *input_end = input_str->ptr + input_str->len;
  192. substr->ptr += substr->len + 1;
  193. /* Note that it's ok if substr->ptr == input_end, this happens in the
  194. * final valid split of an input_str that ends with the split_on character:
  195. * Ex: "AB&" split on '&' produces "AB" and "" */
  196. if (substr->ptr > input_end || substr->ptr < input_str->ptr) { /* 2nd check is overflow check */
  197. /* done */
  198. AWS_ZERO_STRUCT(*substr);
  199. return false;
  200. }
  201. /* update len to be remainder of the string */
  202. substr->len = input_str->len - (substr->ptr - input_str->ptr);
  203. }
  204. /* substr is now remainder of string, search for next split */
  205. uint8_t *new_location = memchr(substr->ptr, split_on, substr->len);
  206. if (new_location) {
  207. /* Character found, update string length. */
  208. substr->len = new_location - substr->ptr;
  209. }
  210. AWS_POSTCONDITION(aws_byte_cursor_is_valid(substr));
  211. return true;
  212. }
  213. int aws_byte_cursor_split_on_char_n(
  214. const struct aws_byte_cursor *AWS_RESTRICT input_str,
  215. char split_on,
  216. size_t n,
  217. struct aws_array_list *AWS_RESTRICT output) {
  218. AWS_ASSERT(aws_byte_cursor_is_valid(input_str));
  219. AWS_ASSERT(output);
  220. AWS_ASSERT(output->item_size >= sizeof(struct aws_byte_cursor));
  221. size_t max_splits = n > 0 ? n : SIZE_MAX;
  222. size_t split_count = 0;
  223. struct aws_byte_cursor substr;
  224. AWS_ZERO_STRUCT(substr);
  225. /* Until we run out of substrs or hit the max split count, keep iterating and pushing into the array list. */
  226. while (split_count <= max_splits && aws_byte_cursor_next_split(input_str, split_on, &substr)) {
  227. if (split_count == max_splits) {
  228. /* If this is the last split, take the rest of the string. */
  229. substr.len = input_str->len - (substr.ptr - input_str->ptr);
  230. }
  231. if (AWS_UNLIKELY(aws_array_list_push_back(output, (const void *)&substr))) {
  232. return AWS_OP_ERR;
  233. }
  234. ++split_count;
  235. }
  236. return AWS_OP_SUCCESS;
  237. }
  238. int aws_byte_cursor_split_on_char(
  239. const struct aws_byte_cursor *AWS_RESTRICT input_str,
  240. char split_on,
  241. struct aws_array_list *AWS_RESTRICT output) {
  242. return aws_byte_cursor_split_on_char_n(input_str, split_on, 0, output);
  243. }
  244. int aws_byte_cursor_find_exact(
  245. const struct aws_byte_cursor *AWS_RESTRICT input_str,
  246. const struct aws_byte_cursor *AWS_RESTRICT to_find,
  247. struct aws_byte_cursor *first_find) {
  248. if (to_find->len > input_str->len) {
  249. return aws_raise_error(AWS_ERROR_STRING_MATCH_NOT_FOUND);
  250. }
  251. if (to_find->len < 1) {
  252. return aws_raise_error(AWS_ERROR_SHORT_BUFFER);
  253. }
  254. struct aws_byte_cursor working_cur = *input_str;
  255. while (working_cur.len) {
  256. uint8_t *first_char_location = memchr(working_cur.ptr, (char)*to_find->ptr, working_cur.len);
  257. if (!first_char_location) {
  258. return aws_raise_error(AWS_ERROR_STRING_MATCH_NOT_FOUND);
  259. }
  260. aws_byte_cursor_advance(&working_cur, first_char_location - working_cur.ptr);
  261. if (working_cur.len < to_find->len) {
  262. return aws_raise_error(AWS_ERROR_STRING_MATCH_NOT_FOUND);
  263. }
  264. if (!memcmp(working_cur.ptr, to_find->ptr, to_find->len)) {
  265. *first_find = working_cur;
  266. return AWS_OP_SUCCESS;
  267. }
  268. aws_byte_cursor_advance(&working_cur, 1);
  269. }
  270. return aws_raise_error(AWS_ERROR_STRING_MATCH_NOT_FOUND);
  271. }
  272. int aws_byte_buf_cat(struct aws_byte_buf *dest, size_t number_of_args, ...) {
  273. AWS_PRECONDITION(aws_byte_buf_is_valid(dest));
  274. va_list ap;
  275. va_start(ap, number_of_args);
  276. for (size_t i = 0; i < number_of_args; ++i) {
  277. struct aws_byte_buf *buffer = va_arg(ap, struct aws_byte_buf *);
  278. struct aws_byte_cursor cursor = aws_byte_cursor_from_buf(buffer);
  279. if (aws_byte_buf_append(dest, &cursor)) {
  280. va_end(ap);
  281. AWS_POSTCONDITION(aws_byte_buf_is_valid(dest));
  282. return AWS_OP_ERR;
  283. }
  284. }
  285. va_end(ap);
  286. AWS_POSTCONDITION(aws_byte_buf_is_valid(dest));
  287. return AWS_OP_SUCCESS;
  288. }
  289. bool aws_byte_cursor_eq(const struct aws_byte_cursor *a, const struct aws_byte_cursor *b) {
  290. AWS_PRECONDITION(aws_byte_cursor_is_valid(a));
  291. AWS_PRECONDITION(aws_byte_cursor_is_valid(b));
  292. bool rv = aws_array_eq(a->ptr, a->len, b->ptr, b->len);
  293. AWS_POSTCONDITION(aws_byte_cursor_is_valid(a));
  294. AWS_POSTCONDITION(aws_byte_cursor_is_valid(b));
  295. return rv;
  296. }
  297. bool aws_byte_cursor_eq_ignore_case(const struct aws_byte_cursor *a, const struct aws_byte_cursor *b) {
  298. AWS_PRECONDITION(aws_byte_cursor_is_valid(a));
  299. AWS_PRECONDITION(aws_byte_cursor_is_valid(b));
  300. bool rv = aws_array_eq_ignore_case(a->ptr, a->len, b->ptr, b->len);
  301. AWS_POSTCONDITION(aws_byte_cursor_is_valid(a));
  302. AWS_POSTCONDITION(aws_byte_cursor_is_valid(b));
  303. return rv;
  304. }
  305. /* Every possible uint8_t value, lowercased */
  306. static const uint8_t s_tolower_table[] = {
  307. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
  308. 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43,
  309. 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 'a',
  310. 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
  311. 'x', 'y', 'z', 91, 92, 93, 94, 95, 96, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
  312. 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 123, 124, 125, 126, 127, 128, 129, 130, 131,
  313. 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153,
  314. 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175,
  315. 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197,
  316. 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219,
  317. 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241,
  318. 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255};
  319. AWS_STATIC_ASSERT(AWS_ARRAY_SIZE(s_tolower_table) == 256);
  320. const uint8_t *aws_lookup_table_to_lower_get(void) {
  321. return s_tolower_table;
  322. }
  323. bool aws_array_eq_ignore_case(
  324. const void *const array_a,
  325. const size_t len_a,
  326. const void *const array_b,
  327. const size_t len_b) {
  328. AWS_PRECONDITION(
  329. (len_a == 0) || AWS_MEM_IS_READABLE(array_a, len_a), "Input array [array_a] must be readable up to [len_a].");
  330. AWS_PRECONDITION(
  331. (len_b == 0) || AWS_MEM_IS_READABLE(array_b, len_b), "Input array [array_b] must be readable up to [len_b].");
  332. if (len_a != len_b) {
  333. return false;
  334. }
  335. const uint8_t *bytes_a = array_a;
  336. const uint8_t *bytes_b = array_b;
  337. for (size_t i = 0; i < len_a; ++i) {
  338. if (s_tolower_table[bytes_a[i]] != s_tolower_table[bytes_b[i]]) {
  339. return false;
  340. }
  341. }
  342. return true;
  343. }
  344. bool aws_array_eq(const void *const array_a, const size_t len_a, const void *const array_b, const size_t len_b) {
  345. AWS_PRECONDITION(
  346. (len_a == 0) || AWS_MEM_IS_READABLE(array_a, len_a), "Input array [array_a] must be readable up to [len_a].");
  347. AWS_PRECONDITION(
  348. (len_b == 0) || AWS_MEM_IS_READABLE(array_b, len_b), "Input array [array_b] must be readable up to [len_b].");
  349. if (len_a != len_b) {
  350. return false;
  351. }
  352. if (len_a == 0) {
  353. return true;
  354. }
  355. return !memcmp(array_a, array_b, len_a);
  356. }
  357. bool aws_array_eq_c_str_ignore_case(const void *const array, const size_t array_len, const char *const c_str) {
  358. AWS_PRECONDITION(
  359. array || (array_len == 0),
  360. "Either input pointer [array_a] mustn't be NULL or input [array_len] mustn't be zero.");
  361. AWS_PRECONDITION(c_str != NULL);
  362. /* Simpler implementation could have been:
  363. * return aws_array_eq_ignore_case(array, array_len, c_str, strlen(c_str));
  364. * but that would have traversed c_str twice.
  365. * This implementation traverses c_str just once. */
  366. const uint8_t *array_bytes = array;
  367. const uint8_t *str_bytes = (const uint8_t *)c_str;
  368. for (size_t i = 0; i < array_len; ++i) {
  369. uint8_t s = str_bytes[i];
  370. if (s == '\0') {
  371. return false;
  372. }
  373. if (s_tolower_table[array_bytes[i]] != s_tolower_table[s]) {
  374. return false;
  375. }
  376. }
  377. return str_bytes[array_len] == '\0';
  378. }
  379. bool aws_array_eq_c_str(const void *const array, const size_t array_len, const char *const c_str) {
  380. AWS_PRECONDITION(
  381. array || (array_len == 0),
  382. "Either input pointer [array_a] mustn't be NULL or input [array_len] mustn't be zero.");
  383. AWS_PRECONDITION(c_str != NULL);
  384. /* Simpler implementation could have been:
  385. * return aws_array_eq(array, array_len, c_str, strlen(c_str));
  386. * but that would have traversed c_str twice.
  387. * This implementation traverses c_str just once. */
  388. const uint8_t *array_bytes = array;
  389. const uint8_t *str_bytes = (const uint8_t *)c_str;
  390. for (size_t i = 0; i < array_len; ++i) {
  391. uint8_t s = str_bytes[i];
  392. if (s == '\0') {
  393. return false;
  394. }
  395. if (array_bytes[i] != s) {
  396. return false;
  397. }
  398. }
  399. return str_bytes[array_len] == '\0';
  400. }
  401. uint64_t aws_hash_array_ignore_case(const void *array, const size_t len) {
  402. AWS_PRECONDITION(AWS_MEM_IS_READABLE(array, len));
  403. /* FNV-1a: https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function */
  404. const uint64_t fnv_offset_basis = 0xcbf29ce484222325ULL;
  405. const uint64_t fnv_prime = 0x100000001b3ULL;
  406. const uint8_t *i = array;
  407. const uint8_t *end = (i == NULL) ? NULL : (i + len);
  408. uint64_t hash = fnv_offset_basis;
  409. while (i != end) {
  410. const uint8_t lower = s_tolower_table[*i++];
  411. hash ^= lower;
  412. #ifdef CBMC
  413. # pragma CPROVER check push
  414. # pragma CPROVER check disable "unsigned-overflow"
  415. #endif
  416. hash *= fnv_prime;
  417. #ifdef CBMC
  418. # pragma CPROVER check pop
  419. #endif
  420. }
  421. return hash;
  422. }
  423. uint64_t aws_hash_byte_cursor_ptr_ignore_case(const void *item) {
  424. AWS_PRECONDITION(aws_byte_cursor_is_valid(item));
  425. const struct aws_byte_cursor *const cursor = item;
  426. uint64_t rval = aws_hash_array_ignore_case(cursor->ptr, cursor->len);
  427. AWS_POSTCONDITION(aws_byte_cursor_is_valid(item));
  428. return rval;
  429. }
  430. bool aws_byte_cursor_eq_byte_buf(const struct aws_byte_cursor *const a, const struct aws_byte_buf *const b) {
  431. AWS_PRECONDITION(aws_byte_cursor_is_valid(a));
  432. AWS_PRECONDITION(aws_byte_buf_is_valid(b));
  433. bool rv = aws_array_eq(a->ptr, a->len, b->buffer, b->len);
  434. AWS_POSTCONDITION(aws_byte_cursor_is_valid(a));
  435. AWS_POSTCONDITION(aws_byte_buf_is_valid(b));
  436. return rv;
  437. }
  438. bool aws_byte_cursor_eq_byte_buf_ignore_case(
  439. const struct aws_byte_cursor *const a,
  440. const struct aws_byte_buf *const b) {
  441. AWS_PRECONDITION(aws_byte_cursor_is_valid(a));
  442. AWS_PRECONDITION(aws_byte_buf_is_valid(b));
  443. bool rv = aws_array_eq_ignore_case(a->ptr, a->len, b->buffer, b->len);
  444. AWS_POSTCONDITION(aws_byte_cursor_is_valid(a));
  445. AWS_POSTCONDITION(aws_byte_buf_is_valid(b));
  446. return rv;
  447. }
  448. bool aws_byte_cursor_eq_c_str(const struct aws_byte_cursor *const cursor, const char *const c_str) {
  449. AWS_PRECONDITION(aws_byte_cursor_is_valid(cursor));
  450. AWS_PRECONDITION(c_str != NULL);
  451. bool rv = aws_array_eq_c_str(cursor->ptr, cursor->len, c_str);
  452. AWS_POSTCONDITION(aws_byte_cursor_is_valid(cursor));
  453. return rv;
  454. }
  455. bool aws_byte_cursor_eq_c_str_ignore_case(const struct aws_byte_cursor *const cursor, const char *const c_str) {
  456. AWS_PRECONDITION(aws_byte_cursor_is_valid(cursor));
  457. AWS_PRECONDITION(c_str != NULL);
  458. bool rv = aws_array_eq_c_str_ignore_case(cursor->ptr, cursor->len, c_str);
  459. AWS_POSTCONDITION(aws_byte_cursor_is_valid(cursor));
  460. return rv;
  461. }
  462. bool aws_byte_cursor_starts_with(const struct aws_byte_cursor *input, const struct aws_byte_cursor *prefix) {
  463. AWS_PRECONDITION(aws_byte_cursor_is_valid(input));
  464. AWS_PRECONDITION(aws_byte_cursor_is_valid(prefix));
  465. if (input->len < prefix->len) {
  466. return false;
  467. }
  468. struct aws_byte_cursor start = {.ptr = input->ptr, .len = prefix->len};
  469. bool rv = aws_byte_cursor_eq(&start, prefix);
  470. AWS_POSTCONDITION(aws_byte_cursor_is_valid(input));
  471. AWS_POSTCONDITION(aws_byte_cursor_is_valid(prefix));
  472. return rv;
  473. }
  474. bool aws_byte_cursor_starts_with_ignore_case(
  475. const struct aws_byte_cursor *input,
  476. const struct aws_byte_cursor *prefix) {
  477. AWS_PRECONDITION(aws_byte_cursor_is_valid(input));
  478. AWS_PRECONDITION(aws_byte_cursor_is_valid(prefix));
  479. if (input->len < prefix->len) {
  480. return false;
  481. }
  482. struct aws_byte_cursor start = {.ptr = input->ptr, .len = prefix->len};
  483. bool rv = aws_byte_cursor_eq_ignore_case(&start, prefix);
  484. AWS_POSTCONDITION(aws_byte_cursor_is_valid(input));
  485. AWS_POSTCONDITION(aws_byte_cursor_is_valid(prefix));
  486. return rv;
  487. }
  488. int aws_byte_buf_append(struct aws_byte_buf *to, const struct aws_byte_cursor *from) {
  489. AWS_PRECONDITION(aws_byte_buf_is_valid(to));
  490. AWS_PRECONDITION(aws_byte_cursor_is_valid(from));
  491. if (to->capacity - to->len < from->len) {
  492. AWS_POSTCONDITION(aws_byte_buf_is_valid(to));
  493. AWS_POSTCONDITION(aws_byte_cursor_is_valid(from));
  494. return aws_raise_error(AWS_ERROR_DEST_COPY_TOO_SMALL);
  495. }
  496. if (from->len > 0) {
  497. /* This assert teaches clang-tidy that from->ptr and to->buffer cannot be null in a non-empty buffers */
  498. AWS_ASSERT(from->ptr);
  499. AWS_ASSERT(to->buffer);
  500. memcpy(to->buffer + to->len, from->ptr, from->len);
  501. to->len += from->len;
  502. }
  503. AWS_POSTCONDITION(aws_byte_buf_is_valid(to));
  504. AWS_POSTCONDITION(aws_byte_cursor_is_valid(from));
  505. return AWS_OP_SUCCESS;
  506. }
  507. int aws_byte_buf_append_with_lookup(
  508. struct aws_byte_buf *AWS_RESTRICT to,
  509. const struct aws_byte_cursor *AWS_RESTRICT from,
  510. const uint8_t *lookup_table) {
  511. AWS_PRECONDITION(aws_byte_buf_is_valid(to));
  512. AWS_PRECONDITION(aws_byte_cursor_is_valid(from));
  513. AWS_PRECONDITION(
  514. AWS_MEM_IS_READABLE(lookup_table, 256), "Input array [lookup_table] must be at least 256 bytes long.");
  515. if (to->capacity - to->len < from->len) {
  516. AWS_POSTCONDITION(aws_byte_buf_is_valid(to));
  517. AWS_POSTCONDITION(aws_byte_cursor_is_valid(from));
  518. return aws_raise_error(AWS_ERROR_DEST_COPY_TOO_SMALL);
  519. }
  520. for (size_t i = 0; i < from->len; ++i) {
  521. to->buffer[to->len + i] = lookup_table[from->ptr[i]];
  522. }
  523. if (aws_add_size_checked(to->len, from->len, &to->len)) {
  524. return AWS_OP_ERR;
  525. }
  526. AWS_POSTCONDITION(aws_byte_buf_is_valid(to));
  527. AWS_POSTCONDITION(aws_byte_cursor_is_valid(from));
  528. return AWS_OP_SUCCESS;
  529. }
  530. static int s_aws_byte_buf_append_dynamic(
  531. struct aws_byte_buf *to,
  532. const struct aws_byte_cursor *from,
  533. bool clear_released_memory) {
  534. AWS_PRECONDITION(aws_byte_buf_is_valid(to));
  535. AWS_PRECONDITION(aws_byte_cursor_is_valid(from));
  536. AWS_ERROR_PRECONDITION(to->allocator);
  537. if (to->capacity - to->len < from->len) {
  538. /*
  539. * NewCapacity = Max(OldCapacity * 2, OldCapacity + MissingCapacity)
  540. */
  541. size_t missing_capacity = from->len - (to->capacity - to->len);
  542. size_t required_capacity = 0;
  543. if (aws_add_size_checked(to->capacity, missing_capacity, &required_capacity)) {
  544. AWS_POSTCONDITION(aws_byte_buf_is_valid(to));
  545. AWS_POSTCONDITION(aws_byte_cursor_is_valid(from));
  546. return AWS_OP_ERR;
  547. }
  548. /*
  549. * It's ok if this overflows, just clamp to max possible.
  550. * In theory this lets us still grow a buffer that's larger than 1/2 size_t space
  551. * at least enough to accommodate the append.
  552. */
  553. size_t growth_capacity = aws_add_size_saturating(to->capacity, to->capacity);
  554. size_t new_capacity = required_capacity;
  555. if (new_capacity < growth_capacity) {
  556. new_capacity = growth_capacity;
  557. }
  558. /*
  559. * Attempt to resize - we intentionally do not use reserve() in order to preserve
  560. * the (unlikely) use case of from and to being the same buffer range.
  561. */
  562. /*
  563. * Try the max, but if that fails and the required is smaller, try it in fallback
  564. */
  565. uint8_t *new_buffer = aws_mem_acquire(to->allocator, new_capacity);
  566. if (new_buffer == NULL) {
  567. if (new_capacity > required_capacity) {
  568. new_capacity = required_capacity;
  569. new_buffer = aws_mem_acquire(to->allocator, new_capacity);
  570. if (new_buffer == NULL) {
  571. AWS_POSTCONDITION(aws_byte_buf_is_valid(to));
  572. AWS_POSTCONDITION(aws_byte_cursor_is_valid(from));
  573. return AWS_OP_ERR;
  574. }
  575. } else {
  576. AWS_POSTCONDITION(aws_byte_buf_is_valid(to));
  577. AWS_POSTCONDITION(aws_byte_cursor_is_valid(from));
  578. return AWS_OP_ERR;
  579. }
  580. }
  581. /*
  582. * Copy old buffer -> new buffer
  583. */
  584. if (to->len > 0) {
  585. memcpy(new_buffer, to->buffer, to->len);
  586. }
  587. /*
  588. * Copy what we actually wanted to append in the first place
  589. */
  590. if (from->len > 0) {
  591. memcpy(new_buffer + to->len, from->ptr, from->len);
  592. }
  593. if (clear_released_memory) {
  594. aws_secure_zero(to->buffer, to->capacity);
  595. }
  596. /*
  597. * Get rid of the old buffer
  598. */
  599. aws_mem_release(to->allocator, to->buffer);
  600. /*
  601. * Switch to the new buffer
  602. */
  603. to->buffer = new_buffer;
  604. to->capacity = new_capacity;
  605. } else {
  606. if (from->len > 0) {
  607. /* This assert teaches clang-tidy that from->ptr and to->buffer cannot be null in a non-empty buffers */
  608. AWS_ASSERT(from->ptr);
  609. AWS_ASSERT(to->buffer);
  610. memcpy(to->buffer + to->len, from->ptr, from->len);
  611. }
  612. }
  613. to->len += from->len;
  614. AWS_POSTCONDITION(aws_byte_buf_is_valid(to));
  615. AWS_POSTCONDITION(aws_byte_cursor_is_valid(from));
  616. return AWS_OP_SUCCESS;
  617. }
  618. int aws_byte_buf_append_dynamic(struct aws_byte_buf *to, const struct aws_byte_cursor *from) {
  619. return s_aws_byte_buf_append_dynamic(to, from, false);
  620. }
  621. int aws_byte_buf_append_dynamic_secure(struct aws_byte_buf *to, const struct aws_byte_cursor *from) {
  622. return s_aws_byte_buf_append_dynamic(to, from, true);
  623. }
  624. static int s_aws_byte_buf_append_byte_dynamic(struct aws_byte_buf *buffer, uint8_t value, bool clear_released_memory) {
  625. #if defined(_MSC_VER)
  626. # pragma warning(push)
  627. # pragma warning(disable : 4221)
  628. #endif /* _MSC_VER */
  629. /* msvc isn't a fan of this pointer-to-local assignment */
  630. struct aws_byte_cursor eq_cursor = {.len = 1, .ptr = &value};
  631. #if defined(_MSC_VER)
  632. # pragma warning(pop)
  633. #endif /* _MSC_VER */
  634. return s_aws_byte_buf_append_dynamic(buffer, &eq_cursor, clear_released_memory);
  635. }
  636. int aws_byte_buf_append_byte_dynamic(struct aws_byte_buf *buffer, uint8_t value) {
  637. return s_aws_byte_buf_append_byte_dynamic(buffer, value, false);
  638. }
  639. int aws_byte_buf_append_byte_dynamic_secure(struct aws_byte_buf *buffer, uint8_t value) {
  640. return s_aws_byte_buf_append_byte_dynamic(buffer, value, true);
  641. }
  642. int aws_byte_buf_reserve(struct aws_byte_buf *buffer, size_t requested_capacity) {
  643. AWS_ERROR_PRECONDITION(buffer->allocator);
  644. AWS_ERROR_PRECONDITION(aws_byte_buf_is_valid(buffer));
  645. if (requested_capacity <= buffer->capacity) {
  646. AWS_POSTCONDITION(aws_byte_buf_is_valid(buffer));
  647. return AWS_OP_SUCCESS;
  648. }
  649. if (!buffer->buffer && !buffer->capacity && requested_capacity > buffer->capacity) {
  650. if (aws_byte_buf_init(buffer, buffer->allocator, requested_capacity)) {
  651. return AWS_OP_ERR;
  652. }
  653. AWS_POSTCONDITION(aws_byte_buf_is_valid(buffer));
  654. return AWS_OP_SUCCESS;
  655. }
  656. if (aws_mem_realloc(buffer->allocator, (void **)&buffer->buffer, buffer->capacity, requested_capacity)) {
  657. return AWS_OP_ERR;
  658. }
  659. buffer->capacity = requested_capacity;
  660. AWS_POSTCONDITION(aws_byte_buf_is_valid(buffer));
  661. return AWS_OP_SUCCESS;
  662. }
  663. int aws_byte_buf_reserve_relative(struct aws_byte_buf *buffer, size_t additional_length) {
  664. AWS_ERROR_PRECONDITION(buffer->allocator);
  665. AWS_ERROR_PRECONDITION(aws_byte_buf_is_valid(buffer));
  666. size_t requested_capacity = 0;
  667. if (AWS_UNLIKELY(aws_add_size_checked(buffer->len, additional_length, &requested_capacity))) {
  668. AWS_POSTCONDITION(aws_byte_buf_is_valid(buffer));
  669. return AWS_OP_ERR;
  670. }
  671. return aws_byte_buf_reserve(buffer, requested_capacity);
  672. }
  673. struct aws_byte_cursor aws_byte_cursor_right_trim_pred(
  674. const struct aws_byte_cursor *source,
  675. aws_byte_predicate_fn *predicate) {
  676. AWS_PRECONDITION(aws_byte_cursor_is_valid(source));
  677. AWS_PRECONDITION(predicate != NULL);
  678. struct aws_byte_cursor trimmed = *source;
  679. while (trimmed.len > 0 && predicate(*(trimmed.ptr + trimmed.len - 1))) {
  680. --trimmed.len;
  681. }
  682. AWS_POSTCONDITION(aws_byte_cursor_is_valid(source));
  683. AWS_POSTCONDITION(aws_byte_cursor_is_valid(&trimmed));
  684. return trimmed;
  685. }
  686. struct aws_byte_cursor aws_byte_cursor_left_trim_pred(
  687. const struct aws_byte_cursor *source,
  688. aws_byte_predicate_fn *predicate) {
  689. AWS_PRECONDITION(aws_byte_cursor_is_valid(source));
  690. AWS_PRECONDITION(predicate != NULL);
  691. struct aws_byte_cursor trimmed = *source;
  692. while (trimmed.len > 0 && predicate(*(trimmed.ptr))) {
  693. --trimmed.len;
  694. ++trimmed.ptr;
  695. }
  696. AWS_POSTCONDITION(aws_byte_cursor_is_valid(source));
  697. AWS_POSTCONDITION(aws_byte_cursor_is_valid(&trimmed));
  698. return trimmed;
  699. }
  700. struct aws_byte_cursor aws_byte_cursor_trim_pred(
  701. const struct aws_byte_cursor *source,
  702. aws_byte_predicate_fn *predicate) {
  703. AWS_PRECONDITION(aws_byte_cursor_is_valid(source));
  704. AWS_PRECONDITION(predicate != NULL);
  705. struct aws_byte_cursor left_trimmed = aws_byte_cursor_left_trim_pred(source, predicate);
  706. struct aws_byte_cursor dest = aws_byte_cursor_right_trim_pred(&left_trimmed, predicate);
  707. AWS_POSTCONDITION(aws_byte_cursor_is_valid(source));
  708. AWS_POSTCONDITION(aws_byte_cursor_is_valid(&dest));
  709. return dest;
  710. }
  711. bool aws_byte_cursor_satisfies_pred(const struct aws_byte_cursor *source, aws_byte_predicate_fn *predicate) {
  712. struct aws_byte_cursor trimmed = aws_byte_cursor_left_trim_pred(source, predicate);
  713. bool rval = (trimmed.len == 0);
  714. AWS_POSTCONDITION(aws_byte_cursor_is_valid(source));
  715. return rval;
  716. }
  717. int aws_byte_cursor_compare_lexical(const struct aws_byte_cursor *lhs, const struct aws_byte_cursor *rhs) {
  718. AWS_PRECONDITION(aws_byte_cursor_is_valid(lhs));
  719. AWS_PRECONDITION(aws_byte_cursor_is_valid(rhs));
  720. /* make sure we don't pass NULL pointers to memcmp */
  721. AWS_PRECONDITION(lhs->ptr != NULL);
  722. AWS_PRECONDITION(rhs->ptr != NULL);
  723. size_t comparison_length = lhs->len;
  724. if (comparison_length > rhs->len) {
  725. comparison_length = rhs->len;
  726. }
  727. int result = memcmp(lhs->ptr, rhs->ptr, comparison_length);
  728. AWS_POSTCONDITION(aws_byte_cursor_is_valid(lhs));
  729. AWS_POSTCONDITION(aws_byte_cursor_is_valid(rhs));
  730. if (result != 0) {
  731. return result;
  732. }
  733. if (lhs->len != rhs->len) {
  734. return comparison_length == lhs->len ? -1 : 1;
  735. }
  736. return 0;
  737. }
  738. int aws_byte_cursor_compare_lookup(
  739. const struct aws_byte_cursor *lhs,
  740. const struct aws_byte_cursor *rhs,
  741. const uint8_t *lookup_table) {
  742. AWS_PRECONDITION(aws_byte_cursor_is_valid(lhs));
  743. AWS_PRECONDITION(aws_byte_cursor_is_valid(rhs));
  744. AWS_PRECONDITION(AWS_MEM_IS_READABLE(lookup_table, 256));
  745. if (lhs->len == 0 && rhs->len == 0) {
  746. return 0;
  747. } else if (lhs->len == 0) {
  748. return -1;
  749. } else if (rhs->len == 0) {
  750. return 1;
  751. }
  752. const uint8_t *lhs_curr = lhs->ptr;
  753. const uint8_t *lhs_end = lhs_curr + lhs->len;
  754. const uint8_t *rhs_curr = rhs->ptr;
  755. const uint8_t *rhs_end = rhs_curr + rhs->len;
  756. while (lhs_curr < lhs_end && rhs_curr < rhs_end) {
  757. uint8_t lhc = lookup_table[*lhs_curr];
  758. uint8_t rhc = lookup_table[*rhs_curr];
  759. AWS_POSTCONDITION(aws_byte_cursor_is_valid(lhs));
  760. AWS_POSTCONDITION(aws_byte_cursor_is_valid(rhs));
  761. if (lhc < rhc) {
  762. return -1;
  763. }
  764. if (lhc > rhc) {
  765. return 1;
  766. }
  767. lhs_curr++;
  768. rhs_curr++;
  769. }
  770. AWS_POSTCONDITION(aws_byte_cursor_is_valid(lhs));
  771. AWS_POSTCONDITION(aws_byte_cursor_is_valid(rhs));
  772. if (lhs_curr < lhs_end) {
  773. return 1;
  774. }
  775. if (rhs_curr < rhs_end) {
  776. return -1;
  777. }
  778. return 0;
  779. }
  780. /**
  781. * For creating a byte buffer from a null-terminated string literal.
  782. */
  783. struct aws_byte_buf aws_byte_buf_from_c_str(const char *c_str) {
  784. struct aws_byte_buf buf;
  785. buf.len = (!c_str) ? 0 : strlen(c_str);
  786. buf.capacity = buf.len;
  787. buf.buffer = (buf.capacity == 0) ? NULL : (uint8_t *)c_str;
  788. buf.allocator = NULL;
  789. AWS_POSTCONDITION(aws_byte_buf_is_valid(&buf));
  790. return buf;
  791. }
  792. struct aws_byte_buf aws_byte_buf_from_array(const void *bytes, size_t len) {
  793. AWS_PRECONDITION(AWS_MEM_IS_WRITABLE(bytes, len), "Input array [bytes] must be writable up to [len] bytes.");
  794. struct aws_byte_buf buf;
  795. buf.buffer = (len > 0) ? (uint8_t *)bytes : NULL;
  796. buf.len = len;
  797. buf.capacity = len;
  798. buf.allocator = NULL;
  799. AWS_POSTCONDITION(aws_byte_buf_is_valid(&buf));
  800. return buf;
  801. }
  802. struct aws_byte_buf aws_byte_buf_from_empty_array(const void *bytes, size_t capacity) {
  803. AWS_PRECONDITION(
  804. AWS_MEM_IS_WRITABLE(bytes, capacity), "Input array [bytes] must be writable up to [capacity] bytes.");
  805. struct aws_byte_buf buf;
  806. buf.buffer = (capacity > 0) ? (uint8_t *)bytes : NULL;
  807. buf.len = 0;
  808. buf.capacity = capacity;
  809. buf.allocator = NULL;
  810. AWS_POSTCONDITION(aws_byte_buf_is_valid(&buf));
  811. return buf;
  812. }
  813. struct aws_byte_cursor aws_byte_cursor_from_buf(const struct aws_byte_buf *const buf) {
  814. AWS_PRECONDITION(aws_byte_buf_is_valid(buf));
  815. struct aws_byte_cursor cur;
  816. cur.ptr = buf->buffer;
  817. cur.len = buf->len;
  818. AWS_POSTCONDITION(aws_byte_cursor_is_valid(&cur));
  819. return cur;
  820. }
  821. struct aws_byte_cursor aws_byte_cursor_from_c_str(const char *c_str) {
  822. struct aws_byte_cursor cur;
  823. cur.ptr = (uint8_t *)c_str;
  824. cur.len = (cur.ptr) ? strlen(c_str) : 0;
  825. AWS_POSTCONDITION(aws_byte_cursor_is_valid(&cur));
  826. return cur;
  827. }
  828. struct aws_byte_cursor aws_byte_cursor_from_array(const void *const bytes, const size_t len) {
  829. AWS_PRECONDITION(len == 0 || AWS_MEM_IS_READABLE(bytes, len), "Input array [bytes] must be readable up to [len].");
  830. struct aws_byte_cursor cur;
  831. cur.ptr = (uint8_t *)bytes;
  832. cur.len = len;
  833. AWS_POSTCONDITION(aws_byte_cursor_is_valid(&cur));
  834. return cur;
  835. }
  836. #ifdef CBMC
  837. # pragma CPROVER check push
  838. # pragma CPROVER check disable "unsigned-overflow"
  839. #endif
  840. /**
  841. * If index >= bound, bound > (SIZE_MAX / 2), or index > (SIZE_MAX / 2), returns
  842. * 0. Otherwise, returns UINTPTR_MAX. This function is designed to return the correct
  843. * value even under CPU speculation conditions, and is intended to be used for
  844. * SPECTRE mitigation purposes.
  845. */
  846. size_t aws_nospec_mask(size_t index, size_t bound) {
  847. /*
  848. * SPECTRE mitigation - we compute a mask that will be zero if len < 0
  849. * or len >= buf->len, and all-ones otherwise, and AND it into the index.
  850. * It is critical that we avoid any branches in this logic.
  851. */
  852. /*
  853. * Hide the index value from the optimizer. This helps ensure that all this
  854. * logic doesn't get eliminated.
  855. */
  856. #if defined(__GNUC__) || defined(__clang__)
  857. __asm__ __volatile__("" : "+r"(index));
  858. #endif
  859. #if defined(_MSVC_LANG)
  860. /*
  861. * MSVC doesn't have a good way for us to blind the optimizer, and doesn't
  862. * even have inline asm on x64. Some experimentation indicates that this
  863. * hack seems to confuse it sufficiently for our needs.
  864. */
  865. *((volatile uint8_t *)&index) += 0;
  866. #endif
  867. /*
  868. * If len > (SIZE_MAX / 2), then we can end up with len - buf->len being
  869. * positive simply because the sign bit got inverted away. So we also check
  870. * that the sign bit isn't set from the start.
  871. *
  872. * We also check that bound <= (SIZE_MAX / 2) to catch cases where the
  873. * buffer is _already_ out of bounds.
  874. */
  875. size_t negative_mask = index | bound;
  876. size_t toobig_mask = bound - index - (uintptr_t)1;
  877. size_t combined_mask = negative_mask | toobig_mask;
  878. /*
  879. * combined_mask needs to have its sign bit OFF for us to be in range.
  880. * We'd like to expand this to a mask we can AND into our index, so flip
  881. * that bit (and everything else), shift it over so it's the only bit in the
  882. * ones position, and multiply across the entire register.
  883. *
  884. * First, extract the (inverse) top bit and move it to the lowest bit.
  885. * Because there's no standard SIZE_BIT in C99, we'll divide by a mask with
  886. * just the top bit set instead.
  887. */
  888. combined_mask = (~combined_mask) / (SIZE_MAX - (SIZE_MAX >> 1));
  889. /*
  890. * Now multiply it to replicate it across all bits.
  891. *
  892. * Note that GCC is smart enough to optimize the divide-and-multiply into
  893. * an arithmetic right shift operation on x86.
  894. */
  895. combined_mask = combined_mask * UINTPTR_MAX;
  896. return combined_mask;
  897. }
  898. #ifdef CBMC
  899. # pragma CPROVER check pop
  900. #endif
  901. /**
  902. * Tests if the given aws_byte_cursor has at least len bytes remaining. If so,
  903. * *buf is advanced by len bytes (incrementing ->ptr and decrementing ->len),
  904. * and an aws_byte_cursor referring to the first len bytes of the original *buf
  905. * is returned. Otherwise, an aws_byte_cursor with ->ptr = NULL, ->len = 0 is
  906. * returned.
  907. *
  908. * Note that if len is above (SIZE_MAX / 2), this function will also treat it as
  909. * a buffer overflow, and return NULL without changing *buf.
  910. */
  911. struct aws_byte_cursor aws_byte_cursor_advance(struct aws_byte_cursor *const cursor, const size_t len) {
  912. AWS_PRECONDITION(aws_byte_cursor_is_valid(cursor));
  913. struct aws_byte_cursor rv;
  914. if (cursor->len > (SIZE_MAX >> 1) || len > (SIZE_MAX >> 1) || len > cursor->len) {
  915. rv.ptr = NULL;
  916. rv.len = 0;
  917. } else {
  918. rv.ptr = cursor->ptr;
  919. rv.len = len;
  920. cursor->ptr = (cursor->ptr == NULL) ? NULL : cursor->ptr + len;
  921. cursor->len -= len;
  922. }
  923. AWS_POSTCONDITION(aws_byte_cursor_is_valid(cursor));
  924. AWS_POSTCONDITION(aws_byte_cursor_is_valid(&rv));
  925. return rv;
  926. }
  927. /**
  928. * Behaves identically to aws_byte_cursor_advance, but avoids speculative
  929. * execution potentially reading out-of-bounds pointers (by returning an
  930. * empty ptr in such speculated paths).
  931. *
  932. * This should generally be done when using an untrusted or
  933. * data-dependent value for 'len', to avoid speculating into a path where
  934. * cursor->ptr points outside the true ptr length.
  935. */
  936. struct aws_byte_cursor aws_byte_cursor_advance_nospec(struct aws_byte_cursor *const cursor, size_t len) {
  937. AWS_PRECONDITION(aws_byte_cursor_is_valid(cursor));
  938. struct aws_byte_cursor rv;
  939. if (len <= cursor->len && len <= (SIZE_MAX >> 1) && cursor->len <= (SIZE_MAX >> 1)) {
  940. /*
  941. * If we're speculating past a failed bounds check, null out the pointer. This ensures
  942. * that we don't try to read past the end of the buffer and leak information about other
  943. * memory through timing side-channels.
  944. */
  945. uintptr_t mask = aws_nospec_mask(len, cursor->len + 1);
  946. /* Make sure we don't speculate-underflow len either */
  947. len = len & mask;
  948. cursor->ptr = (uint8_t *)((uintptr_t)cursor->ptr & mask);
  949. /* Make sure subsequent nospec accesses don't advance ptr past NULL */
  950. cursor->len = cursor->len & mask;
  951. rv.ptr = cursor->ptr;
  952. /* Make sure anything acting upon the returned cursor _also_ doesn't advance past NULL */
  953. rv.len = len & mask;
  954. cursor->ptr = (cursor->ptr == NULL) ? NULL : cursor->ptr + len;
  955. cursor->len -= len;
  956. } else {
  957. rv.ptr = NULL;
  958. rv.len = 0;
  959. }
  960. AWS_POSTCONDITION(aws_byte_cursor_is_valid(cursor));
  961. AWS_POSTCONDITION(aws_byte_cursor_is_valid(&rv));
  962. return rv;
  963. }
  964. /**
  965. * Reads specified length of data from byte cursor and copies it to the
  966. * destination array.
  967. *
  968. * On success, returns true and updates the cursor pointer/length accordingly.
  969. * If there is insufficient space in the cursor, returns false, leaving the
  970. * cursor unchanged.
  971. */
  972. bool aws_byte_cursor_read(struct aws_byte_cursor *AWS_RESTRICT cur, void *AWS_RESTRICT dest, const size_t len) {
  973. AWS_PRECONDITION(aws_byte_cursor_is_valid(cur));
  974. AWS_PRECONDITION(AWS_MEM_IS_WRITABLE(dest, len));
  975. if (len == 0) {
  976. return true;
  977. }
  978. struct aws_byte_cursor slice = aws_byte_cursor_advance_nospec(cur, len);
  979. if (slice.ptr) {
  980. memcpy(dest, slice.ptr, len);
  981. AWS_POSTCONDITION(aws_byte_cursor_is_valid(cur));
  982. AWS_POSTCONDITION(AWS_MEM_IS_READABLE(dest, len));
  983. return true;
  984. }
  985. AWS_POSTCONDITION(aws_byte_cursor_is_valid(cur));
  986. return false;
  987. }
  988. /**
  989. * Reads as many bytes from cursor as size of buffer, and copies them to buffer.
  990. *
  991. * On success, returns true and updates the cursor pointer/length accordingly.
  992. * If there is insufficient space in the cursor, returns false, leaving the
  993. * cursor unchanged.
  994. */
  995. bool aws_byte_cursor_read_and_fill_buffer(
  996. struct aws_byte_cursor *AWS_RESTRICT cur,
  997. struct aws_byte_buf *AWS_RESTRICT dest) {
  998. AWS_PRECONDITION(aws_byte_cursor_is_valid(cur));
  999. AWS_PRECONDITION(aws_byte_buf_is_valid(dest));
  1000. if (aws_byte_cursor_read(cur, dest->buffer, dest->capacity)) {
  1001. dest->len = dest->capacity;
  1002. AWS_POSTCONDITION(aws_byte_cursor_is_valid(cur));
  1003. AWS_POSTCONDITION(aws_byte_buf_is_valid(dest));
  1004. return true;
  1005. }
  1006. AWS_POSTCONDITION(aws_byte_cursor_is_valid(cur));
  1007. AWS_POSTCONDITION(aws_byte_buf_is_valid(dest));
  1008. return false;
  1009. }
  1010. /**
  1011. * Reads a single byte from cursor, placing it in *var.
  1012. *
  1013. * On success, returns true and updates the cursor pointer/length accordingly.
  1014. * If there is insufficient space in the cursor, returns false, leaving the
  1015. * cursor unchanged.
  1016. */
  1017. bool aws_byte_cursor_read_u8(struct aws_byte_cursor *AWS_RESTRICT cur, uint8_t *AWS_RESTRICT var) {
  1018. AWS_PRECONDITION(aws_byte_cursor_is_valid(cur));
  1019. AWS_PRECONDITION(AWS_MEM_IS_WRITABLE(var, 1));
  1020. bool rv = aws_byte_cursor_read(cur, var, 1);
  1021. AWS_POSTCONDITION(aws_byte_cursor_is_valid(cur));
  1022. return rv;
  1023. }
  1024. /**
  1025. * Reads a 16-bit value in network byte order from cur, and places it in host
  1026. * byte order into var.
  1027. *
  1028. * On success, returns true and updates the cursor pointer/length accordingly.
  1029. * If there is insufficient space in the cursor, returns false, leaving the
  1030. * cursor unchanged.
  1031. */
  1032. bool aws_byte_cursor_read_be16(struct aws_byte_cursor *cur, uint16_t *var) {
  1033. AWS_PRECONDITION(aws_byte_cursor_is_valid(cur));
  1034. AWS_PRECONDITION(AWS_OBJECT_PTR_IS_WRITABLE(var));
  1035. bool rv = aws_byte_cursor_read(cur, var, 2);
  1036. if (AWS_LIKELY(rv)) {
  1037. *var = aws_ntoh16(*var);
  1038. }
  1039. AWS_POSTCONDITION(aws_byte_cursor_is_valid(cur));
  1040. return rv;
  1041. }
  1042. /**
  1043. * Reads an unsigned 24-bit value (3 bytes) in network byte order from cur,
  1044. * and places it in host byte order into 32-bit var.
  1045. * Ex: if cur's next 3 bytes are {0xAA, 0xBB, 0xCC}, then var becomes 0x00AABBCC.
  1046. *
  1047. * On success, returns true and updates the cursor pointer/length accordingly.
  1048. * If there is insufficient space in the cursor, returns false, leaving the
  1049. * cursor unchanged.
  1050. */
  1051. bool aws_byte_cursor_read_be24(struct aws_byte_cursor *cur, uint32_t *var) {
  1052. AWS_PRECONDITION(aws_byte_cursor_is_valid(cur));
  1053. AWS_PRECONDITION(AWS_OBJECT_PTR_IS_WRITABLE(var));
  1054. uint8_t *var_bytes = (void *)var;
  1055. /* read into "lower" 3 bytes */
  1056. bool rv = aws_byte_cursor_read(cur, &var_bytes[1], 3);
  1057. if (AWS_LIKELY(rv)) {
  1058. /* zero out "highest" 4th byte*/
  1059. var_bytes[0] = 0;
  1060. *var = aws_ntoh32(*var);
  1061. }
  1062. AWS_POSTCONDITION(aws_byte_cursor_is_valid(cur));
  1063. return rv;
  1064. }
  1065. /**
  1066. * Reads a 32-bit value in network byte order from cur, and places it in host
  1067. * byte order into var.
  1068. *
  1069. * On success, returns true and updates the cursor pointer/length accordingly.
  1070. * If there is insufficient space in the cursor, returns false, leaving the
  1071. * cursor unchanged.
  1072. */
  1073. bool aws_byte_cursor_read_be32(struct aws_byte_cursor *cur, uint32_t *var) {
  1074. AWS_PRECONDITION(aws_byte_cursor_is_valid(cur));
  1075. AWS_PRECONDITION(AWS_OBJECT_PTR_IS_WRITABLE(var));
  1076. bool rv = aws_byte_cursor_read(cur, var, 4);
  1077. if (AWS_LIKELY(rv)) {
  1078. *var = aws_ntoh32(*var);
  1079. }
  1080. AWS_POSTCONDITION(aws_byte_cursor_is_valid(cur));
  1081. return rv;
  1082. }
  1083. /**
  1084. * Reads a 32-bit value in network byte order from cur, and places it in host
  1085. * byte order into var.
  1086. *
  1087. * On success, returns true and updates the cursor pointer/length accordingly.
  1088. * If there is insufficient space in the cursor, returns false, leaving the
  1089. * cursor unchanged.
  1090. */
  1091. bool aws_byte_cursor_read_float_be32(struct aws_byte_cursor *cur, float *var) {
  1092. AWS_PRECONDITION(aws_byte_cursor_is_valid(cur));
  1093. AWS_PRECONDITION(AWS_OBJECT_PTR_IS_WRITABLE(var));
  1094. bool rv = aws_byte_cursor_read(cur, var, sizeof(float));
  1095. if (AWS_LIKELY(rv)) {
  1096. *var = aws_ntohf32(*var);
  1097. }
  1098. AWS_POSTCONDITION(aws_byte_cursor_is_valid(cur));
  1099. return rv;
  1100. }
  1101. /**
  1102. * Reads a 64-bit value in network byte order from cur, and places it in host
  1103. * byte order into var.
  1104. *
  1105. * On success, returns true and updates the cursor pointer/length accordingly.
  1106. * If there is insufficient space in the cursor, returns false, leaving the
  1107. * cursor unchanged.
  1108. */
  1109. bool aws_byte_cursor_read_float_be64(struct aws_byte_cursor *cur, double *var) {
  1110. AWS_PRECONDITION(aws_byte_cursor_is_valid(cur));
  1111. AWS_PRECONDITION(AWS_OBJECT_PTR_IS_WRITABLE(var));
  1112. bool rv = aws_byte_cursor_read(cur, var, sizeof(double));
  1113. if (AWS_LIKELY(rv)) {
  1114. *var = aws_ntohf64(*var);
  1115. }
  1116. AWS_POSTCONDITION(aws_byte_cursor_is_valid(cur));
  1117. return rv;
  1118. }
  1119. /**
  1120. * Reads a 64-bit value in network byte order from cur, and places it in host
  1121. * byte order into var.
  1122. *
  1123. * On success, returns true and updates the cursor pointer/length accordingly.
  1124. * If there is insufficient space in the cursor, returns false, leaving the
  1125. * cursor unchanged.
  1126. */
  1127. bool aws_byte_cursor_read_be64(struct aws_byte_cursor *cur, uint64_t *var) {
  1128. AWS_PRECONDITION(aws_byte_cursor_is_valid(cur));
  1129. AWS_PRECONDITION(AWS_OBJECT_PTR_IS_WRITABLE(var));
  1130. bool rv = aws_byte_cursor_read(cur, var, sizeof(*var));
  1131. if (AWS_LIKELY(rv)) {
  1132. *var = aws_ntoh64(*var);
  1133. }
  1134. AWS_POSTCONDITION(aws_byte_cursor_is_valid(cur));
  1135. return rv;
  1136. }
  1137. /* Lookup from '0' -> 0, 'f' -> 0xf, 'F' -> 0xF, etc
  1138. * invalid characters have value 255 */
  1139. /* clang-format off */
  1140. static const uint8_t s_hex_to_num_table[] = {
  1141. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  1142. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  1143. 255, 255,
  1144. /* 0 - 9 */
  1145. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
  1146. 255, 255, 255, 255, 255, 255, 255,
  1147. /* A - F */
  1148. 0xA, 0xB, 0xC, 0xD, 0xE, 0xF,
  1149. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  1150. 255, 255, 255,
  1151. /* a - f */
  1152. 0xa, 0xb, 0xc, 0xd, 0xe, 0xf,
  1153. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  1154. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  1155. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  1156. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  1157. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  1158. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  1159. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  1160. };
  1161. AWS_STATIC_ASSERT(AWS_ARRAY_SIZE(s_hex_to_num_table) == 256);
  1162. /* clang-format on */
  1163. const uint8_t *aws_lookup_table_hex_to_num_get(void) {
  1164. return s_hex_to_num_table;
  1165. }
  1166. bool aws_byte_cursor_read_hex_u8(struct aws_byte_cursor *cur, uint8_t *var) {
  1167. AWS_PRECONDITION(aws_byte_cursor_is_valid(cur));
  1168. AWS_PRECONDITION(AWS_OBJECT_PTR_IS_WRITABLE(var));
  1169. bool success = false;
  1170. if (AWS_LIKELY(cur->len >= 2)) {
  1171. const uint8_t hi = s_hex_to_num_table[cur->ptr[0]];
  1172. const uint8_t lo = s_hex_to_num_table[cur->ptr[1]];
  1173. /* table maps invalid characters to 255 */
  1174. if (AWS_LIKELY(hi != 255 && lo != 255)) {
  1175. *var = (hi << 4) | lo;
  1176. cur->ptr += 2;
  1177. cur->len -= 2;
  1178. success = true;
  1179. }
  1180. }
  1181. AWS_POSTCONDITION(aws_byte_cursor_is_valid(cur));
  1182. return success;
  1183. }
  1184. /**
  1185. * Appends a sub-buffer to the specified buffer.
  1186. *
  1187. * If the buffer has at least `len' bytes remaining (buffer->capacity - buffer->len >= len),
  1188. * then buffer->len is incremented by len, and an aws_byte_buf is assigned to *output corresponding
  1189. * to the last len bytes of the input buffer. The aws_byte_buf at *output will have a null
  1190. * allocator, a zero initial length, and a capacity of 'len'. The function then returns true.
  1191. *
  1192. * If there is insufficient space, then this function nulls all fields in *output and returns
  1193. * false.
  1194. */
  1195. bool aws_byte_buf_advance(
  1196. struct aws_byte_buf *const AWS_RESTRICT buffer,
  1197. struct aws_byte_buf *const AWS_RESTRICT output,
  1198. const size_t len) {
  1199. AWS_PRECONDITION(aws_byte_buf_is_valid(buffer));
  1200. AWS_PRECONDITION(aws_byte_buf_is_valid(output));
  1201. if (buffer->capacity - buffer->len >= len) {
  1202. *output = aws_byte_buf_from_array((buffer->buffer == NULL) ? NULL : buffer->buffer + buffer->len, len);
  1203. buffer->len += len;
  1204. output->len = 0;
  1205. AWS_POSTCONDITION(aws_byte_buf_is_valid(buffer));
  1206. AWS_POSTCONDITION(aws_byte_buf_is_valid(output));
  1207. return true;
  1208. } else {
  1209. AWS_ZERO_STRUCT(*output);
  1210. AWS_POSTCONDITION(aws_byte_buf_is_valid(buffer));
  1211. AWS_POSTCONDITION(aws_byte_buf_is_valid(output));
  1212. return false;
  1213. }
  1214. }
  1215. /**
  1216. * Write specified number of bytes from array to byte buffer.
  1217. *
  1218. * On success, returns true and updates the buffer length accordingly.
  1219. * If there is insufficient space in the buffer, returns false, leaving the
  1220. * buffer unchanged.
  1221. */
  1222. bool aws_byte_buf_write(struct aws_byte_buf *AWS_RESTRICT buf, const uint8_t *AWS_RESTRICT src, size_t len) {
  1223. AWS_PRECONDITION(aws_byte_buf_is_valid(buf));
  1224. AWS_PRECONDITION(AWS_MEM_IS_READABLE(src, len), "Input array [src] must be readable up to [len] bytes.");
  1225. if (len == 0) {
  1226. AWS_POSTCONDITION(aws_byte_buf_is_valid(buf));
  1227. return true;
  1228. }
  1229. if (buf->len > (SIZE_MAX >> 1) || len > (SIZE_MAX >> 1) || buf->len + len > buf->capacity) {
  1230. AWS_POSTCONDITION(aws_byte_buf_is_valid(buf));
  1231. return false;
  1232. }
  1233. memcpy(buf->buffer + buf->len, src, len);
  1234. buf->len += len;
  1235. AWS_POSTCONDITION(aws_byte_buf_is_valid(buf));
  1236. return true;
  1237. }
  1238. /**
  1239. * Copies all bytes from buffer to buffer.
  1240. *
  1241. * On success, returns true and updates the buffer /length accordingly.
  1242. * If there is insufficient space in the buffer, returns false, leaving the
  1243. * buffer unchanged.
  1244. */
  1245. bool aws_byte_buf_write_from_whole_buffer(struct aws_byte_buf *AWS_RESTRICT buf, struct aws_byte_buf src) {
  1246. AWS_PRECONDITION(aws_byte_buf_is_valid(buf));
  1247. AWS_PRECONDITION(aws_byte_buf_is_valid(&src));
  1248. return aws_byte_buf_write(buf, src.buffer, src.len);
  1249. }
  1250. /**
  1251. * Copies all bytes from buffer to buffer.
  1252. *
  1253. * On success, returns true and updates the buffer /length accordingly.
  1254. * If there is insufficient space in the buffer, returns false, leaving the
  1255. * buffer unchanged.
  1256. */
  1257. bool aws_byte_buf_write_from_whole_cursor(struct aws_byte_buf *AWS_RESTRICT buf, struct aws_byte_cursor src) {
  1258. AWS_PRECONDITION(aws_byte_buf_is_valid(buf));
  1259. AWS_PRECONDITION(aws_byte_cursor_is_valid(&src));
  1260. return aws_byte_buf_write(buf, src.ptr, src.len);
  1261. }
  1262. struct aws_byte_cursor aws_byte_buf_write_to_capacity(
  1263. struct aws_byte_buf *buf,
  1264. struct aws_byte_cursor *advancing_cursor) {
  1265. AWS_PRECONDITION(aws_byte_buf_is_valid(buf));
  1266. AWS_PRECONDITION(aws_byte_cursor_is_valid(advancing_cursor));
  1267. size_t available = buf->capacity - buf->len;
  1268. size_t write_size = aws_min_size(available, advancing_cursor->len);
  1269. struct aws_byte_cursor write_cursor = aws_byte_cursor_advance(advancing_cursor, write_size);
  1270. aws_byte_buf_write_from_whole_cursor(buf, write_cursor);
  1271. return write_cursor;
  1272. }
  1273. /**
  1274. * Copies one byte to buffer.
  1275. *
  1276. * On success, returns true and updates the cursor /length
  1277. accordingly.
  1278. * If there is insufficient space in the cursor, returns false, leaving the
  1279. cursor unchanged.
  1280. */
  1281. bool aws_byte_buf_write_u8(struct aws_byte_buf *AWS_RESTRICT buf, uint8_t c) {
  1282. AWS_PRECONDITION(aws_byte_buf_is_valid(buf));
  1283. return aws_byte_buf_write(buf, &c, 1);
  1284. }
  1285. /**
  1286. * Writes one byte repeatedly to buffer (like memset)
  1287. *
  1288. * If there is insufficient space in the buffer, returns false, leaving the
  1289. * buffer unchanged.
  1290. */
  1291. bool aws_byte_buf_write_u8_n(struct aws_byte_buf *buf, uint8_t c, size_t count) {
  1292. AWS_PRECONDITION(aws_byte_buf_is_valid(buf));
  1293. if (buf->len > (SIZE_MAX >> 1) || count > (SIZE_MAX >> 1) || buf->len + count > buf->capacity) {
  1294. AWS_POSTCONDITION(aws_byte_buf_is_valid(buf));
  1295. return false;
  1296. }
  1297. memset(buf->buffer + buf->len, c, count);
  1298. buf->len += count;
  1299. AWS_POSTCONDITION(aws_byte_buf_is_valid(buf));
  1300. return true;
  1301. }
  1302. /**
  1303. * Writes a 16-bit integer in network byte order (big endian) to buffer.
  1304. *
  1305. * On success, returns true and updates the cursor /length accordingly.
  1306. * If there is insufficient space in the cursor, returns false, leaving the
  1307. * cursor unchanged.
  1308. */
  1309. bool aws_byte_buf_write_be16(struct aws_byte_buf *buf, uint16_t x) {
  1310. AWS_PRECONDITION(aws_byte_buf_is_valid(buf));
  1311. x = aws_hton16(x);
  1312. return aws_byte_buf_write(buf, (uint8_t *)&x, 2);
  1313. }
  1314. /**
  1315. * Writes low 24-bits (3 bytes) of an unsigned integer in network byte order (big endian) to buffer.
  1316. * Ex: If x is 0x00AABBCC then {0xAA, 0xBB, 0xCC} is written to buffer.
  1317. *
  1318. * On success, returns true and updates the buffer /length accordingly.
  1319. * If there is insufficient space in the buffer, or x's value cannot fit in 3 bytes,
  1320. * returns false, leaving the buffer unchanged.
  1321. */
  1322. bool aws_byte_buf_write_be24(struct aws_byte_buf *buf, uint32_t x) {
  1323. AWS_PRECONDITION(aws_byte_buf_is_valid(buf));
  1324. if (x > 0x00FFFFFF) {
  1325. return false;
  1326. }
  1327. uint32_t be32 = aws_hton32(x);
  1328. uint8_t *be32_bytes = (uint8_t *)&be32;
  1329. /* write "lower" 3 bytes */
  1330. return aws_byte_buf_write(buf, &be32_bytes[1], 3);
  1331. }
  1332. /**
  1333. * Writes a 32-bit integer in network byte order (big endian) to buffer.
  1334. *
  1335. * On success, returns true and updates the cursor /length accordingly.
  1336. * If there is insufficient space in the cursor, returns false, leaving the
  1337. * cursor unchanged.
  1338. */
  1339. bool aws_byte_buf_write_be32(struct aws_byte_buf *buf, uint32_t x) {
  1340. AWS_PRECONDITION(aws_byte_buf_is_valid(buf));
  1341. x = aws_hton32(x);
  1342. return aws_byte_buf_write(buf, (uint8_t *)&x, 4);
  1343. }
  1344. /**
  1345. * Writes a 32-bit float in network byte order (big endian) to buffer.
  1346. *
  1347. * On success, returns true and updates the cursor /length accordingly.
  1348. * If there is insufficient space in the cursor, returns false, leaving the
  1349. * cursor unchanged.
  1350. */
  1351. bool aws_byte_buf_write_float_be32(struct aws_byte_buf *buf, float x) {
  1352. AWS_PRECONDITION(aws_byte_buf_is_valid(buf));
  1353. x = aws_htonf32(x);
  1354. return aws_byte_buf_write(buf, (uint8_t *)&x, 4);
  1355. }
  1356. /**
  1357. * Writes a 64-bit integer in network byte order (big endian) to buffer.
  1358. *
  1359. * On success, returns true and updates the cursor /length accordingly.
  1360. * If there is insufficient space in the cursor, returns false, leaving the
  1361. * cursor unchanged.
  1362. */
  1363. bool aws_byte_buf_write_be64(struct aws_byte_buf *buf, uint64_t x) {
  1364. AWS_PRECONDITION(aws_byte_buf_is_valid(buf));
  1365. x = aws_hton64(x);
  1366. return aws_byte_buf_write(buf, (uint8_t *)&x, 8);
  1367. }
  1368. /**
  1369. * Writes a 64-bit float in network byte order (big endian) to buffer.
  1370. *
  1371. * On success, returns true and updates the cursor /length accordingly.
  1372. * If there is insufficient space in the cursor, returns false, leaving the
  1373. * cursor unchanged.
  1374. */
  1375. bool aws_byte_buf_write_float_be64(struct aws_byte_buf *buf, double x) {
  1376. AWS_PRECONDITION(aws_byte_buf_is_valid(buf));
  1377. x = aws_htonf64(x);
  1378. return aws_byte_buf_write(buf, (uint8_t *)&x, 8);
  1379. }
  1380. int aws_byte_buf_append_and_update(struct aws_byte_buf *to, struct aws_byte_cursor *from_and_update) {
  1381. AWS_PRECONDITION(aws_byte_buf_is_valid(to));
  1382. AWS_PRECONDITION(aws_byte_cursor_is_valid(from_and_update));
  1383. if (aws_byte_buf_append(to, from_and_update)) {
  1384. return AWS_OP_ERR;
  1385. }
  1386. from_and_update->ptr = to->buffer == NULL ? NULL : to->buffer + (to->len - from_and_update->len);
  1387. return AWS_OP_SUCCESS;
  1388. }
  1389. static struct aws_byte_cursor s_null_terminator_cursor = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("\0");
  1390. int aws_byte_buf_append_null_terminator(struct aws_byte_buf *buf) {
  1391. return aws_byte_buf_append_dynamic(buf, &s_null_terminator_cursor);
  1392. }
  1393. bool aws_isalnum(uint8_t ch) {
  1394. return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9');
  1395. }
  1396. bool aws_isalpha(uint8_t ch) {
  1397. return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z');
  1398. }
  1399. bool aws_isdigit(uint8_t ch) {
  1400. return (ch >= '0' && ch <= '9');
  1401. }
  1402. bool aws_isxdigit(uint8_t ch) {
  1403. return (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F');
  1404. }
  1405. bool aws_isspace(uint8_t ch) {
  1406. switch (ch) {
  1407. case 0x20: /* ' ' - space */
  1408. case 0x09: /* '\t' - horizontal tab */
  1409. case 0x0A: /* '\n' - line feed */
  1410. case 0x0B: /* '\v' - vertical tab */
  1411. case 0x0C: /* '\f' - form feed */
  1412. case 0x0D: /* '\r' - carriage return */
  1413. return true;
  1414. default:
  1415. return false;
  1416. }
  1417. }
  1418. static int s_read_unsigned(struct aws_byte_cursor cursor, uint64_t *dst, uint8_t base) {
  1419. uint64_t val = 0;
  1420. *dst = 0;
  1421. if (cursor.len == 0) {
  1422. return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
  1423. }
  1424. const uint8_t *hex_to_num_table = aws_lookup_table_hex_to_num_get();
  1425. /* read from left to right */
  1426. for (size_t i = 0; i < cursor.len; ++i) {
  1427. const uint8_t c = cursor.ptr[i];
  1428. const uint8_t cval = hex_to_num_table[c];
  1429. if (cval >= base) {
  1430. return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
  1431. }
  1432. if (aws_mul_u64_checked(val, base, &val)) {
  1433. return aws_raise_error(AWS_ERROR_OVERFLOW_DETECTED);
  1434. }
  1435. if (aws_add_u64_checked(val, cval, &val)) {
  1436. return aws_raise_error(AWS_ERROR_OVERFLOW_DETECTED);
  1437. }
  1438. }
  1439. *dst = val;
  1440. return AWS_OP_SUCCESS;
  1441. }
  1442. int aws_byte_cursor_utf8_parse_u64(struct aws_byte_cursor cursor, uint64_t *dst) {
  1443. return s_read_unsigned(cursor, dst, 10 /*base*/);
  1444. }
  1445. int aws_byte_cursor_utf8_parse_u64_hex(struct aws_byte_cursor cursor, uint64_t *dst) {
  1446. return s_read_unsigned(cursor, dst, 16 /*base*/);
  1447. }