calldata.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * Copyright (c) 2023 Lain Bailey <[email protected]>
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <string.h>
  17. #include "../util/bmem.h"
  18. #include "../util/base.h"
  19. #include "calldata.h"
  20. /*
  21. * Uses a data stack. Probably more complex than it should be, but reduces
  22. * fetching.
  23. *
  24. * Stack format is:
  25. * [size_t param1_name_size]
  26. * [char[] param1_name]
  27. * [size_t param1_data_size]
  28. * [uint8_t[] param1_data]
  29. * [size_t param2_name_size]
  30. * [char[] param2_name]
  31. * [size_t param2_data_size]
  32. * [uint8_t[] param2_data]
  33. * [...]
  34. * [size_t 0]
  35. *
  36. * Strings and string sizes always include the null terminator to allow for
  37. * direct referencing.
  38. */
  39. static inline size_t cd_serialize_size(uint8_t **pos)
  40. {
  41. size_t size = 0;
  42. memcpy(&size, *pos, sizeof(size_t));
  43. *pos += sizeof(size_t);
  44. return size;
  45. }
  46. static inline const char *cd_serialize_string(uint8_t **pos)
  47. {
  48. size_t size = cd_serialize_size(pos);
  49. const char *str = (const char *)*pos;
  50. *pos += size;
  51. return (size != 0) ? str : NULL;
  52. }
  53. static bool cd_getparam(const calldata_t *data, const char *name, uint8_t **pos)
  54. {
  55. size_t name_size;
  56. if (!data->size)
  57. return false;
  58. *pos = data->stack;
  59. name_size = cd_serialize_size(pos);
  60. while (name_size != 0) {
  61. const char *param_name = (const char *)*pos;
  62. size_t param_size;
  63. *pos += name_size;
  64. if (strcmp(param_name, name) == 0)
  65. return true;
  66. param_size = cd_serialize_size(pos);
  67. *pos += param_size;
  68. name_size = cd_serialize_size(pos);
  69. }
  70. *pos -= sizeof(size_t);
  71. return false;
  72. }
  73. static inline void cd_copy_string(uint8_t **pos, const char *str, size_t len)
  74. {
  75. if (!len)
  76. len = strlen(str) + 1;
  77. memcpy(*pos, &len, sizeof(size_t));
  78. *pos += sizeof(size_t);
  79. memcpy(*pos, str, len);
  80. *pos += len;
  81. }
  82. static inline void cd_copy_data(uint8_t **pos, const void *in, size_t size)
  83. {
  84. memcpy(*pos, &size, sizeof(size_t));
  85. *pos += sizeof(size_t);
  86. if (size) {
  87. memcpy(*pos, in, size);
  88. *pos += size;
  89. }
  90. }
  91. static inline void cd_set_first_param(calldata_t *data, const char *name, const void *in, size_t size)
  92. {
  93. uint8_t *pos;
  94. size_t capacity;
  95. size_t name_len = strlen(name) + 1;
  96. capacity = sizeof(size_t) * 3 + name_len + size;
  97. data->size = capacity;
  98. if (capacity < 128)
  99. capacity = 128;
  100. data->capacity = capacity;
  101. data->stack = bmalloc(capacity);
  102. pos = data->stack;
  103. cd_copy_string(&pos, name, name_len);
  104. cd_copy_data(&pos, in, size);
  105. memset(pos, 0, sizeof(size_t));
  106. }
  107. static inline bool cd_ensure_capacity(calldata_t *data, uint8_t **pos, size_t new_size)
  108. {
  109. size_t offset;
  110. size_t new_capacity;
  111. if (new_size < data->capacity)
  112. return true;
  113. if (data->fixed) {
  114. blog(LOG_ERROR, "Tried to go above fixed calldata stack size!");
  115. return false;
  116. }
  117. offset = *pos - data->stack;
  118. new_capacity = data->capacity * 2;
  119. if (new_capacity < new_size)
  120. new_capacity = new_size;
  121. data->stack = brealloc(data->stack, new_capacity);
  122. data->capacity = new_capacity;
  123. *pos = data->stack + offset;
  124. return true;
  125. }
  126. /* ------------------------------------------------------------------------- */
  127. bool calldata_get_data(const calldata_t *data, const char *name, void *out, size_t size)
  128. {
  129. uint8_t *pos;
  130. size_t data_size;
  131. if (!data || !name || !*name)
  132. return false;
  133. if (!cd_getparam(data, name, &pos))
  134. return false;
  135. data_size = cd_serialize_size(&pos);
  136. if (data_size != size)
  137. return false;
  138. memcpy(out, pos, size);
  139. return true;
  140. }
  141. void calldata_set_data(calldata_t *data, const char *name, const void *in, size_t size)
  142. {
  143. uint8_t *pos = NULL;
  144. if (!data || !name || !*name)
  145. return;
  146. if (!data->fixed && !data->stack) {
  147. cd_set_first_param(data, name, in, size);
  148. return;
  149. }
  150. if (cd_getparam(data, name, &pos)) {
  151. size_t cur_size;
  152. memcpy(&cur_size, pos, sizeof(size_t));
  153. if (cur_size < size) {
  154. size_t offset = size - cur_size;
  155. size_t bytes = data->size;
  156. if (!cd_ensure_capacity(data, &pos, bytes + offset))
  157. return;
  158. memmove(pos + offset, pos, bytes - (pos - data->stack));
  159. data->size += offset;
  160. } else if (cur_size > size) {
  161. size_t offset = cur_size - size;
  162. size_t bytes = data->size - offset;
  163. memmove(pos, pos + offset, bytes - (pos - data->stack));
  164. data->size -= offset;
  165. }
  166. cd_copy_data(&pos, in, size);
  167. } else {
  168. size_t name_len = strlen(name) + 1;
  169. size_t offset = name_len + size + sizeof(size_t) * 2;
  170. if (!cd_ensure_capacity(data, &pos, data->size + offset))
  171. return;
  172. data->size += offset;
  173. cd_copy_string(&pos, name, 0);
  174. cd_copy_data(&pos, in, size);
  175. memset(pos, 0, sizeof(size_t));
  176. }
  177. }
  178. bool calldata_get_string(const calldata_t *data, const char *name, const char **str)
  179. {
  180. uint8_t *pos;
  181. if (!data || !name || !*name)
  182. return false;
  183. if (!cd_getparam(data, name, &pos))
  184. return false;
  185. *str = cd_serialize_string(&pos);
  186. return true;
  187. }