calldata.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 = 0;
  46. memcpy(&size, *pos, sizeof(size_t));
  47. *pos += sizeof(size_t);
  48. return size;
  49. }
  50. static inline const char *cd_serialize_string(uint8_t **pos)
  51. {
  52. size_t size = cd_serialize_size(pos);
  53. const char *str = (const char *)*pos;
  54. *pos += size;
  55. return (size != 0) ? str : NULL;
  56. }
  57. static bool cd_getparam(const calldata_t *data, const char *name,
  58. 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 void 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;
  120. offset = *pos - data->stack;
  121. new_capacity = data->capacity * 2;
  122. if (new_capacity < new_size)
  123. new_capacity = new_size;
  124. data->stack = brealloc(data->stack, new_capacity);
  125. data->capacity = new_capacity;
  126. *pos = data->stack + offset;
  127. }
  128. /* ------------------------------------------------------------------------- */
  129. bool calldata_get_data(const calldata_t *data, const char *name, void *out,
  130. size_t size)
  131. {
  132. uint8_t *pos;
  133. size_t data_size;
  134. if (!data || !name || !*name)
  135. return false;
  136. if (!cd_getparam(data, name, &pos))
  137. return false;
  138. data_size = cd_serialize_size(&pos);
  139. if (data_size != size)
  140. return false;
  141. memcpy(out, pos, size);
  142. return true;
  143. }
  144. void calldata_set_data(calldata_t *data, const char *name, const void *in,
  145. size_t size)
  146. {
  147. uint8_t *pos = NULL;
  148. if (!data || !name || !*name)
  149. return;
  150. if (!data->stack) {
  151. cd_set_first_param(data, name, in, size);
  152. return;
  153. }
  154. if (cd_getparam(data, name, &pos)) {
  155. size_t cur_size;
  156. memcpy(&cur_size, pos, sizeof(size_t));
  157. if (cur_size < size) {
  158. size_t offset = size - cur_size;
  159. size_t bytes = data->size;
  160. cd_ensure_capacity(data, &pos, bytes + offset);
  161. memmove(pos+offset, pos, bytes - (pos - data->stack));
  162. data->size += offset;
  163. } else if (cur_size > size) {
  164. size_t offset = cur_size - size;
  165. size_t bytes = data->size - offset;
  166. memmove(pos, pos+offset, bytes - (pos - data->stack));
  167. data->size -= offset;
  168. }
  169. cd_copy_data(&pos, in, size);
  170. } else {
  171. size_t name_len = strlen(name)+1;
  172. size_t offset = name_len + size + sizeof(size_t)*2;
  173. cd_ensure_capacity(data, &pos, data->size + offset);
  174. data->size += offset;
  175. cd_copy_string(&pos, name, 0);
  176. cd_copy_data(&pos, in, size);
  177. memset(pos, 0, sizeof(size_t));
  178. }
  179. }
  180. bool calldata_get_string(const calldata_t *data, const char *name,
  181. const char **str)
  182. {
  183. uint8_t *pos;
  184. if (!data || !name || !*name)
  185. return false;
  186. if (!cd_getparam(data, name, &pos))
  187. return false;
  188. *str = cd_serialize_string(&pos);
  189. return true;
  190. }