/contrib/bind9/lib/lwres/lwbuffer.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 361 lines · 175 code · 61 blank · 125 comment · 3 complexity · 66289fe7b291d4622f87258c046dfd14 MD5 · raw file

  1. /*
  2. * Copyright (C) 2004, 2005, 2007 Internet Systems Consortium, Inc. ("ISC")
  3. * Copyright (C) 2000, 2001 Internet Software Consortium.
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  10. * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  11. * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  12. * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  13. * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  14. * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  15. * PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. /* $Id: lwbuffer.c,v 1.15 2007/06/19 23:47:22 tbox Exp $ */
  18. /*! \file */
  19. /**
  20. * These functions provide bounds checked access to a region of memory
  21. * where data is being read or written. They are based on, and similar
  22. * to, the isc_buffer_ functions in the ISC library.
  23. *
  24. * A buffer is a region of memory, together with a set of related
  25. * subregions. The used region and the available region are disjoint, and
  26. * their union is the buffer's region. The used region extends from the
  27. * beginning of the buffer region to the last used byte. The available
  28. * region extends from one byte greater than the last used byte to the
  29. * end of the buffer's region. The size of the used region can be changed
  30. * using various buffer commands. Initially, the used region is empty.
  31. *
  32. * The used region is further subdivided into two disjoint regions: the
  33. * consumed region and the remaining region. The union of these two
  34. * regions is the used region. The consumed region extends from the
  35. * beginning of the used region to the byte before the current offset (if
  36. * any). The remaining region the current pointer to the end of the used
  37. * region. The size of the consumed region can be changed using various
  38. * buffer commands. Initially, the consumed region is empty.
  39. *
  40. * The active region is an (optional) subregion of the remaining region.
  41. * It extends from the current offset to an offset in the remaining
  42. * region. Initially, the active region is empty. If the current offset
  43. * advances beyond the chosen offset, the active region will also be
  44. * empty.
  45. *
  46. *
  47. * \verbatim
  48. * /------------entire length---------------\\
  49. * /----- used region -----\\/-- available --\\
  50. * +----------------------------------------+
  51. * | consumed | remaining | |
  52. * +----------------------------------------+
  53. * a b c d e
  54. *
  55. * a == base of buffer.
  56. * b == current pointer. Can be anywhere between a and d.
  57. * c == active pointer. Meaningful between b and d.
  58. * d == used pointer.
  59. * e == length of buffer.
  60. *
  61. * a-e == entire length of buffer.
  62. * a-d == used region.
  63. * a-b == consumed region.
  64. * b-d == remaining region.
  65. * b-c == optional active region.
  66. * \endverbatim
  67. *
  68. * lwres_buffer_init() initializes the lwres_buffer_t *b and assocates it
  69. * with the memory region of size length bytes starting at location base.
  70. *
  71. * lwres_buffer_invalidate() marks the buffer *b as invalid. Invalidating
  72. * a buffer after use is not required, but makes it possible to catch its
  73. * possible accidental use.
  74. *
  75. * The functions lwres_buffer_add() and lwres_buffer_subtract()
  76. * respectively increase and decrease the used space in buffer *b by n
  77. * bytes. lwres_buffer_add() checks for buffer overflow and
  78. * lwres_buffer_subtract() checks for underflow. These functions do not
  79. * allocate or deallocate memory. They just change the value of used.
  80. *
  81. * A buffer is re-initialised by lwres_buffer_clear(). The function sets
  82. * used , current and active to zero.
  83. *
  84. * lwres_buffer_first() makes the consumed region of buffer *p empty by
  85. * setting current to zero (the start of the buffer).
  86. *
  87. * lwres_buffer_forward() increases the consumed region of buffer *b by n
  88. * bytes, checking for overflow. Similarly, lwres_buffer_back() decreases
  89. * buffer b's consumed region by n bytes and checks for underflow.
  90. *
  91. * lwres_buffer_getuint8() reads an unsigned 8-bit integer from *b and
  92. * returns it. lwres_buffer_putuint8() writes the unsigned 8-bit integer
  93. * val to buffer *b.
  94. *
  95. * lwres_buffer_getuint16() and lwres_buffer_getuint32() are identical to
  96. * lwres_buffer_putuint8() except that they respectively read an unsigned
  97. * 16-bit or 32-bit integer in network byte order from b. Similarly,
  98. * lwres_buffer_putuint16() and lwres_buffer_putuint32() writes the
  99. * unsigned 16-bit or 32-bit integer val to buffer b, in network byte
  100. * order.
  101. *
  102. * Arbitrary amounts of data are read or written from a lightweight
  103. * resolver buffer with lwres_buffer_getmem() and lwres_buffer_putmem()
  104. * respectively. lwres_buffer_putmem() copies length bytes of memory at
  105. * base to b. Conversely, lwres_buffer_getmem() copies length bytes of
  106. * memory from b to base.
  107. */
  108. #include <config.h>
  109. #include <string.h>
  110. #include <lwres/lwbuffer.h>
  111. #include "assert_p.h"
  112. void
  113. lwres_buffer_init(lwres_buffer_t *b, void *base, unsigned int length)
  114. {
  115. /*
  116. * Make 'b' refer to the 'length'-byte region starting at base.
  117. */
  118. REQUIRE(b != NULL);
  119. b->magic = LWRES_BUFFER_MAGIC;
  120. b->base = base;
  121. b->length = length;
  122. b->used = 0;
  123. b->current = 0;
  124. b->active = 0;
  125. }
  126. /* Make 'b' an invalid buffer. */
  127. void
  128. lwres_buffer_invalidate(lwres_buffer_t *b)
  129. {
  130. REQUIRE(LWRES_BUFFER_VALID(b));
  131. b->magic = 0;
  132. b->base = NULL;
  133. b->length = 0;
  134. b->used = 0;
  135. b->current = 0;
  136. b->active = 0;
  137. }
  138. /* Increase the 'used' region of 'b' by 'n' bytes. */
  139. void
  140. lwres_buffer_add(lwres_buffer_t *b, unsigned int n)
  141. {
  142. REQUIRE(LWRES_BUFFER_VALID(b));
  143. REQUIRE(b->used + n <= b->length);
  144. b->used += n;
  145. }
  146. /* Decrease the 'used' region of 'b' by 'n' bytes. */
  147. void
  148. lwres_buffer_subtract(lwres_buffer_t *b, unsigned int n)
  149. {
  150. REQUIRE(LWRES_BUFFER_VALID(b));
  151. REQUIRE(b->used >= n);
  152. b->used -= n;
  153. if (b->current > b->used)
  154. b->current = b->used;
  155. if (b->active > b->used)
  156. b->active = b->used;
  157. }
  158. /* Make the used region empty. */
  159. void
  160. lwres_buffer_clear(lwres_buffer_t *b)
  161. {
  162. REQUIRE(LWRES_BUFFER_VALID(b));
  163. b->used = 0;
  164. b->current = 0;
  165. b->active = 0;
  166. }
  167. /* Make the consumed region empty. */
  168. void
  169. lwres_buffer_first(lwres_buffer_t *b)
  170. {
  171. REQUIRE(LWRES_BUFFER_VALID(b));
  172. b->current = 0;
  173. }
  174. /* Increase the 'consumed' region of 'b' by 'n' bytes. */
  175. void
  176. lwres_buffer_forward(lwres_buffer_t *b, unsigned int n)
  177. {
  178. REQUIRE(LWRES_BUFFER_VALID(b));
  179. REQUIRE(b->current + n <= b->used);
  180. b->current += n;
  181. }
  182. /* Decrease the 'consumed' region of 'b' by 'n' bytes. */
  183. void
  184. lwres_buffer_back(lwres_buffer_t *b, unsigned int n)
  185. {
  186. REQUIRE(LWRES_BUFFER_VALID(b));
  187. REQUIRE(n <= b->current);
  188. b->current -= n;
  189. }
  190. /* Read an unsigned 8-bit integer from 'b' and return it. */
  191. lwres_uint8_t
  192. lwres_buffer_getuint8(lwres_buffer_t *b)
  193. {
  194. unsigned char *cp;
  195. lwres_uint8_t result;
  196. REQUIRE(LWRES_BUFFER_VALID(b));
  197. REQUIRE(b->used - b->current >= 1);
  198. cp = b->base;
  199. cp += b->current;
  200. b->current += 1;
  201. result = ((unsigned int)(cp[0]));
  202. return (result);
  203. }
  204. /* Put an unsigned 8-bit integer */
  205. void
  206. lwres_buffer_putuint8(lwres_buffer_t *b, lwres_uint8_t val)
  207. {
  208. unsigned char *cp;
  209. REQUIRE(LWRES_BUFFER_VALID(b));
  210. REQUIRE(b->used + 1 <= b->length);
  211. cp = b->base;
  212. cp += b->used;
  213. b->used += 1;
  214. cp[0] = (val & 0x00ff);
  215. }
  216. /* Read an unsigned 16-bit integer in network byte order from 'b', convert it to host byte order, and return it. */
  217. lwres_uint16_t
  218. lwres_buffer_getuint16(lwres_buffer_t *b)
  219. {
  220. unsigned char *cp;
  221. lwres_uint16_t result;
  222. REQUIRE(LWRES_BUFFER_VALID(b));
  223. REQUIRE(b->used - b->current >= 2);
  224. cp = b->base;
  225. cp += b->current;
  226. b->current += 2;
  227. result = ((unsigned int)(cp[0])) << 8;
  228. result |= ((unsigned int)(cp[1]));
  229. return (result);
  230. }
  231. /* Put an unsigned 16-bit integer. */
  232. void
  233. lwres_buffer_putuint16(lwres_buffer_t *b, lwres_uint16_t val)
  234. {
  235. unsigned char *cp;
  236. REQUIRE(LWRES_BUFFER_VALID(b));
  237. REQUIRE(b->used + 2 <= b->length);
  238. cp = b->base;
  239. cp += b->used;
  240. b->used += 2;
  241. cp[0] = (val & 0xff00) >> 8;
  242. cp[1] = (val & 0x00ff);
  243. }
  244. /* Read an unsigned 32-bit integer in network byte order from 'b', convert it to host byte order, and return it. */
  245. lwres_uint32_t
  246. lwres_buffer_getuint32(lwres_buffer_t *b)
  247. {
  248. unsigned char *cp;
  249. lwres_uint32_t result;
  250. REQUIRE(LWRES_BUFFER_VALID(b));
  251. REQUIRE(b->used - b->current >= 4);
  252. cp = b->base;
  253. cp += b->current;
  254. b->current += 4;
  255. result = ((unsigned int)(cp[0])) << 24;
  256. result |= ((unsigned int)(cp[1])) << 16;
  257. result |= ((unsigned int)(cp[2])) << 8;
  258. result |= ((unsigned int)(cp[3]));
  259. return (result);
  260. }
  261. /* Put an unsigned 32-bit integer. */
  262. void
  263. lwres_buffer_putuint32(lwres_buffer_t *b, lwres_uint32_t val)
  264. {
  265. unsigned char *cp;
  266. REQUIRE(LWRES_BUFFER_VALID(b));
  267. REQUIRE(b->used + 4 <= b->length);
  268. cp = b->base;
  269. cp += b->used;
  270. b->used += 4;
  271. cp[0] = (unsigned char)((val & 0xff000000) >> 24);
  272. cp[1] = (unsigned char)((val & 0x00ff0000) >> 16);
  273. cp[2] = (unsigned char)((val & 0x0000ff00) >> 8);
  274. cp[3] = (unsigned char)(val & 0x000000ff);
  275. }
  276. /* copies length bytes of memory at base to b */
  277. void
  278. lwres_buffer_putmem(lwres_buffer_t *b, const unsigned char *base,
  279. unsigned int length)
  280. {
  281. unsigned char *cp;
  282. REQUIRE(LWRES_BUFFER_VALID(b));
  283. REQUIRE(b->used + length <= b->length);
  284. cp = (unsigned char *)b->base + b->used;
  285. memcpy(cp, base, length);
  286. b->used += length;
  287. }
  288. /* copies length bytes of memory at b to base */
  289. void
  290. lwres_buffer_getmem(lwres_buffer_t *b, unsigned char *base,
  291. unsigned int length)
  292. {
  293. unsigned char *cp;
  294. REQUIRE(LWRES_BUFFER_VALID(b));
  295. REQUIRE(b->used - b->current >= length);
  296. cp = b->base;
  297. cp += b->current;
  298. b->current += length;
  299. memcpy(base, cp, length);
  300. }