calldata.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 "calldata.h"
  19. /*
  20. * Uses a data stack. Probably more complex than it should be, but reduces
  21. * fetching.
  22. *
  23. * Stack format is:
  24. * [size_t param1_name_size]
  25. * [char[] param1_name]
  26. * [size_t param1_data_size]
  27. * [uint8_t[] param1_data]
  28. * [size_t param2_name_size]
  29. * [char[] param2_name]
  30. * [size_t param2_data_size]
  31. * [uint8_t[] param2_data]
  32. * [...]
  33. * [size_t 0]
  34. *
  35. * Strings and string sizes always include the null terminator to allow for
  36. * direct referencing.
  37. */
  38. static inline void cd_serialize(uint8_t **pos, void *ptr, size_t size)
  39. {
  40. memcpy(ptr, *pos, size);
  41. *pos += size;
  42. }
  43. static inline size_t cd_serialize_size(uint8_t **pos)
  44. {
  45. size_t size = *(size_t*)*pos;
  46. *pos += sizeof(size_t);
  47. return size;
  48. }
  49. static inline const char *cd_serialize_string(uint8_t **pos)
  50. {
  51. size_t size = cd_serialize_size(pos);
  52. const char *str = (const char *)*pos;
  53. *pos += size;
  54. return (size != 0) ? str : NULL;
  55. }
  56. static bool cd_getparam(calldata_t *data, const char *name,
  57. uint8_t **pos)
  58. {
  59. size_t name_size;
  60. if (!data->size)
  61. return false;
  62. *pos = data->stack;
  63. name_size = cd_serialize_size(pos);
  64. while (name_size != 0) {
  65. const char *param_name = (const char *)*pos;
  66. size_t param_size;
  67. *pos += name_size;
  68. if (strcmp(param_name, name) == 0)
  69. return true;
  70. param_size = cd_serialize_size(pos);
  71. *pos += param_size;
  72. name_size = cd_serialize_size(pos);
  73. }
  74. *pos -= sizeof(size_t);
  75. return false;
  76. }
  77. static inline void cd_copy_string(uint8_t **pos, const char *str, size_t len)
  78. {
  79. if (!len)
  80. len = strlen(str)+1;
  81. *(size_t*)*pos = len;
  82. *pos += sizeof(size_t);
  83. memcpy(*pos, str, len);
  84. *pos += len;
  85. }
  86. static inline void cd_copy_data(uint8_t **pos, const void *in, size_t size)
  87. {
  88. *(size_t*)*pos = size;
  89. *pos += sizeof(size_t);
  90. if (size) {
  91. memcpy(*pos, in, size);
  92. *pos += size;
  93. }
  94. }
  95. static inline void cd_set_first_param(calldata_t *data, const char *name,
  96. const void *in, size_t size)
  97. {
  98. uint8_t *pos;
  99. size_t capacity;
  100. size_t name_len = strlen(name)+1;
  101. capacity = sizeof(size_t)*3 + name_len + size;
  102. data->size = capacity;
  103. if (capacity < 128)
  104. capacity = 128;
  105. data->capacity = capacity;
  106. data->stack = bmalloc(capacity);
  107. pos = data->stack;
  108. cd_copy_string(&pos, name, name_len);
  109. cd_copy_data(&pos, in, size);
  110. *(size_t*)pos = 0;
  111. }
  112. static inline void cd_ensure_capacity(calldata_t *data, uint8_t **pos,
  113. size_t new_size)
  114. {
  115. size_t offset;
  116. size_t new_capacity;
  117. if (new_size < data->capacity)
  118. return;
  119. offset = *pos - data->stack;
  120. new_capacity = data->capacity * 2;
  121. if (new_capacity < new_size)
  122. new_capacity = new_size;
  123. data->stack = brealloc(data->stack, new_capacity);
  124. data->capacity = new_capacity;
  125. *pos = data->stack + offset;
  126. }
  127. /* ------------------------------------------------------------------------- */
  128. bool calldata_get_data(calldata_t *data, const char *name, void *out,
  129. size_t size)
  130. {
  131. uint8_t *pos;
  132. size_t data_size;
  133. if (!data || !name || !*name)
  134. return false;
  135. if (!cd_getparam(data, name, &pos))
  136. return false;
  137. data_size = cd_serialize_size(&pos);
  138. if (data_size != size)
  139. return false;
  140. memcpy(out, pos, size);
  141. return true;
  142. }
  143. void calldata_set_data(calldata_t *data, const char *name, const void *in,
  144. size_t size)
  145. {
  146. uint8_t *pos = NULL;
  147. if (!data || !name || !*name)
  148. return;
  149. if (!data->stack) {
  150. cd_set_first_param(data, name, in, size);
  151. return;
  152. }
  153. if (cd_getparam(data, name, &pos)) {
  154. size_t cur_size = *(size_t*)pos;
  155. if (cur_size < size) {
  156. size_t offset = size - cur_size;
  157. size_t bytes = data->size;
  158. cd_ensure_capacity(data, &pos, bytes + offset);
  159. memmove(pos+offset, pos, bytes - (pos - data->stack));
  160. data->size += offset;
  161. } else if (cur_size > size) {
  162. size_t offset = cur_size - size;
  163. size_t bytes = data->size - offset;
  164. memmove(pos, pos+offset, bytes - (pos - data->stack));
  165. data->size -= offset;
  166. }
  167. cd_copy_data(&pos, in, size);
  168. } else {
  169. size_t name_len = strlen(name)+1;
  170. size_t offset = name_len + size + sizeof(size_t)*2;
  171. cd_ensure_capacity(data, &pos, data->size + offset);
  172. data->size += offset;
  173. cd_copy_string(&pos, name, 0);
  174. cd_copy_data(&pos, in, size);
  175. *(size_t*)pos = 0;
  176. }
  177. }
  178. bool calldata_get_string(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. }