calldata.c 5.5 KB

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