cpack.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*-
  2. * Copyright (c) 2003, 2004 David Young. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * 3. The name of David Young may not be used to endorse or promote
  13. * products derived from this software without specific prior
  14. * written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY DAVID YOUNG ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  18. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  19. * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DAVID
  20. * YOUNG BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  21. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
  22. * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  25. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  27. * OF SUCH DAMAGE.
  28. */
  29. #ifdef HAVE_CONFIG_H
  30. #include "config.h"
  31. #endif
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #include <netdissect-stdinc.h>
  35. #include "cpack.h"
  36. #include "extract.h"
  37. const uint8_t *
  38. cpack_next_boundary(const uint8_t *buf, const uint8_t *p, size_t alignment)
  39. {
  40. size_t misalignment = (size_t)(p - buf) % alignment;
  41. if (misalignment == 0)
  42. return p;
  43. return p + (alignment - misalignment);
  44. }
  45. /* Advance to the next wordsize boundary. Return NULL if fewer than
  46. * wordsize bytes remain in the buffer after the boundary. Otherwise,
  47. * return a pointer to the boundary.
  48. */
  49. const uint8_t *
  50. cpack_align_and_reserve(struct cpack_state *cs, size_t wordsize)
  51. {
  52. const uint8_t *next;
  53. /* Ensure alignment. */
  54. next = cpack_next_boundary(cs->c_buf, cs->c_next, wordsize);
  55. /* Too little space for wordsize bytes? */
  56. if (next - cs->c_buf + wordsize > cs->c_len)
  57. return NULL;
  58. return next;
  59. }
  60. /* Advance by N bytes without returning them. */
  61. int
  62. cpack_advance(struct cpack_state *cs, const size_t toskip)
  63. {
  64. /* No space left? */
  65. if (cs->c_next - cs->c_buf + toskip > cs->c_len)
  66. return -1;
  67. cs->c_next += toskip;
  68. return 0;
  69. }
  70. int
  71. cpack_init(struct cpack_state *cs, const uint8_t *buf, size_t buflen)
  72. {
  73. memset(cs, 0, sizeof(*cs));
  74. cs->c_buf = buf;
  75. cs->c_len = buflen;
  76. cs->c_next = cs->c_buf;
  77. return 0;
  78. }
  79. /* Unpack a 64-bit unsigned integer. */
  80. int
  81. cpack_uint64(struct cpack_state *cs, uint64_t *u)
  82. {
  83. const uint8_t *next;
  84. if ((next = cpack_align_and_reserve(cs, sizeof(*u))) == NULL)
  85. return -1;
  86. *u = EXTRACT_LE_64BITS(next);
  87. /* Move pointer past the uint64_t. */
  88. cs->c_next = next + sizeof(*u);
  89. return 0;
  90. }
  91. /* Unpack a 32-bit unsigned integer. */
  92. int
  93. cpack_uint32(struct cpack_state *cs, uint32_t *u)
  94. {
  95. const uint8_t *next;
  96. if ((next = cpack_align_and_reserve(cs, sizeof(*u))) == NULL)
  97. return -1;
  98. *u = EXTRACT_LE_32BITS(next);
  99. /* Move pointer past the uint32_t. */
  100. cs->c_next = next + sizeof(*u);
  101. return 0;
  102. }
  103. /* Unpack a 16-bit unsigned integer. */
  104. int
  105. cpack_uint16(struct cpack_state *cs, uint16_t *u)
  106. {
  107. const uint8_t *next;
  108. if ((next = cpack_align_and_reserve(cs, sizeof(*u))) == NULL)
  109. return -1;
  110. *u = EXTRACT_LE_16BITS(next);
  111. /* Move pointer past the uint16_t. */
  112. cs->c_next = next + sizeof(*u);
  113. return 0;
  114. }
  115. /* Unpack an 8-bit unsigned integer. */
  116. int
  117. cpack_uint8(struct cpack_state *cs, uint8_t *u)
  118. {
  119. /* No space left? */
  120. if ((size_t)(cs->c_next - cs->c_buf) >= cs->c_len)
  121. return -1;
  122. *u = *cs->c_next;
  123. /* Move pointer past the uint8_t. */
  124. cs->c_next++;
  125. return 0;
  126. }