123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- #ifdef HAVE_CONFIG_H
- #include "config.h"
- #endif
- #include <stdlib.h>
- #include <string.h>
- #include <netdissect-stdinc.h>
- #include "cpack.h"
- #include "extract.h"
- const uint8_t *
- cpack_next_boundary(const uint8_t *buf, const uint8_t *p, size_t alignment)
- {
- size_t misalignment = (size_t)(p - buf) % alignment;
- if (misalignment == 0)
- return p;
- return p + (alignment - misalignment);
- }
- const uint8_t *
- cpack_align_and_reserve(struct cpack_state *cs, size_t wordsize)
- {
- const uint8_t *next;
-
- next = cpack_next_boundary(cs->c_buf, cs->c_next, wordsize);
-
- if (next - cs->c_buf + wordsize > cs->c_len)
- return NULL;
- return next;
- }
- int
- cpack_advance(struct cpack_state *cs, const size_t toskip)
- {
-
- if (cs->c_next - cs->c_buf + toskip > cs->c_len)
- return -1;
- cs->c_next += toskip;
- return 0;
- }
- int
- cpack_init(struct cpack_state *cs, const uint8_t *buf, size_t buflen)
- {
- memset(cs, 0, sizeof(*cs));
- cs->c_buf = buf;
- cs->c_len = buflen;
- cs->c_next = cs->c_buf;
- return 0;
- }
- int
- cpack_uint64(struct cpack_state *cs, uint64_t *u)
- {
- const uint8_t *next;
- if ((next = cpack_align_and_reserve(cs, sizeof(*u))) == NULL)
- return -1;
- *u = EXTRACT_LE_64BITS(next);
-
- cs->c_next = next + sizeof(*u);
- return 0;
- }
- int
- cpack_uint32(struct cpack_state *cs, uint32_t *u)
- {
- const uint8_t *next;
- if ((next = cpack_align_and_reserve(cs, sizeof(*u))) == NULL)
- return -1;
- *u = EXTRACT_LE_32BITS(next);
-
- cs->c_next = next + sizeof(*u);
- return 0;
- }
- int
- cpack_uint16(struct cpack_state *cs, uint16_t *u)
- {
- const uint8_t *next;
- if ((next = cpack_align_and_reserve(cs, sizeof(*u))) == NULL)
- return -1;
- *u = EXTRACT_LE_16BITS(next);
-
- cs->c_next = next + sizeof(*u);
- return 0;
- }
- int
- cpack_uint8(struct cpack_state *cs, uint8_t *u)
- {
-
- if ((size_t)(cs->c_next - cs->c_buf) >= cs->c_len)
- return -1;
- *u = *cs->c_next;
-
- cs->c_next++;
- return 0;
- }
|