1
0

calldata.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * Copyright (c) 2013 Hugh 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 void cd_serialize(uint8_t **pos, void *ptr, size_t size)
  40. {
  41. memcpy(ptr, *pos, size);
  42. *pos += size;
  43. }
  44. static inline size_t cd_serialize_size(uint8_t **pos)
  45. {
  46. size_t size = 0;
  47. memcpy(&size, *pos, sizeof(size_t));
  48. *pos += sizeof(size_t);
  49. return size;
  50. }
  51. static inline const char *cd_serialize_string(uint8_t **pos)
  52. {
  53. size_t size = cd_serialize_size(pos);
  54. const char *str = (const char *)*pos;
  55. *pos += size;
  56. return (size != 0) ? str : NULL;
  57. }
  58. static bool cd_getparam(const calldata_t *data, const char *name, uint8_t **pos)
  59. {
  60. size_t name_size;
  61. if (!data->size)
  62. return false;
  63. *pos = data->stack;
  64. name_size = cd_serialize_size(pos);
  65. while (name_size != 0) {
  66. const char *param_name = (const char *)*pos;
  67. size_t param_size;
  68. *pos += name_size;
  69. if (strcmp(param_name, name) == 0)
  70. return true;
  71. param_size = cd_serialize_size(pos);
  72. *pos += param_size;
  73. name_size = cd_serialize_size(pos);
  74. }
  75. *pos -= sizeof(size_t);
  76. return false;
  77. }
  78. static inline void cd_copy_string(uint8_t **pos, const char *str, size_t len)
  79. {
  80. if (!len)
  81. len = strlen(str) + 1;
  82. memcpy(*pos, &len, sizeof(size_t));
  83. *pos += sizeof(size_t);
  84. memcpy(*pos, str, len);
  85. *pos += len;
  86. }
  87. static inline void cd_copy_data(uint8_t **pos, const void *in, size_t size)
  88. {
  89. memcpy(*pos, &size, sizeof(size_t));
  90. *pos += sizeof(size_t);
  91. if (size) {
  92. memcpy(*pos, in, size);
  93. *pos += size;
  94. }
  95. }
  96. static inline void cd_set_first_param(calldata_t *data, const char *name,
  97. const void *in, size_t size)
  98. {
  99. uint8_t *pos;
  100. size_t capacity;
  101. size_t name_len = strlen(name) + 1;
  102. capacity = sizeof(size_t) * 3 + name_len + size;
  103. data->size = capacity;
  104. if (capacity < 128)
  105. capacity = 128;
  106. data->capacity = capacity;
  107. data->stack = bmalloc(capacity);
  108. pos = data->stack;
  109. cd_copy_string(&pos, name, name_len);
  110. cd_copy_data(&pos, in, size);
  111. memset(pos, 0, sizeof(size_t));
  112. }
  113. static inline bool cd_ensure_capacity(calldata_t *data, uint8_t **pos,
  114. size_t new_size)
  115. {
  116. size_t offset;
  117. size_t new_capacity;
  118. if (new_size < data->capacity)
  119. return true;
  120. if (data->fixed) {
  121. blog(LOG_ERROR, "Tried to go above fixed calldata stack size!");
  122. return false;
  123. }
  124. offset = *pos - data->stack;
  125. new_capacity = data->capacity * 2;
  126. if (new_capacity < new_size)
  127. new_capacity = new_size;
  128. data->stack = brealloc(data->stack, new_capacity);
  129. data->capacity = new_capacity;
  130. *pos = data->stack + offset;
  131. return true;
  132. }
  133. /* ------------------------------------------------------------------------- */
  134. bool calldata_get_data(const calldata_t *data, const char *name, void *out,
  135. size_t size)
  136. {
  137. uint8_t *pos;
  138. size_t data_size;
  139. if (!data || !name || !*name)
  140. return false;
  141. if (!cd_getparam(data, name, &pos))
  142. return false;
  143. data_size = cd_serialize_size(&pos);
  144. if (data_size != size)
  145. return false;
  146. memcpy(out, pos, size);
  147. return true;
  148. }
  149. void calldata_set_data(calldata_t *data, const char *name, const void *in,
  150. size_t size)
  151. {
  152. uint8_t *pos = NULL;
  153. if (!data || !name || !*name)
  154. return;
  155. if (!data->fixed && !data->stack) {
  156. cd_set_first_param(data, name, in, size);
  157. return;
  158. }
  159. if (cd_getparam(data, name, &pos)) {
  160. size_t cur_size;
  161. memcpy(&cur_size, pos, sizeof(size_t));
  162. if (cur_size < size) {
  163. size_t offset = size - cur_size;
  164. size_t bytes = data->size;
  165. if (!cd_ensure_capacity(data, &pos, bytes + offset))
  166. return;
  167. memmove(pos + offset, pos, bytes - (pos - data->stack));
  168. data->size += offset;
  169. } else if (cur_size > size) {
  170. size_t offset = cur_size - size;
  171. size_t bytes = data->size - offset;
  172. memmove(pos, pos + offset, bytes - (pos - data->stack));
  173. data->size -= offset;
  174. }
  175. cd_copy_data(&pos, in, size);
  176. } else {
  177. size_t name_len = strlen(name) + 1;
  178. size_t offset = name_len + size + sizeof(size_t) * 2;
  179. if (!cd_ensure_capacity(data, &pos, data->size + offset))
  180. return;
  181. data->size += offset;
  182. cd_copy_string(&pos, name, 0);
  183. cd_copy_data(&pos, in, size);
  184. memset(pos, 0, sizeof(size_t));
  185. }
  186. }
  187. bool calldata_get_string(const calldata_t *data, const char *name,
  188. const char **str)
  189. {
  190. uint8_t *pos;
  191. if (!data || !name || !*name)
  192. return false;
  193. if (!cd_getparam(data, name, &pos))
  194. return false;
  195. *str = cd_serialize_string(&pos);
  196. return true;
  197. }