fota_buf.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Dynamic data buffer
  3. * Copyright (c) 2007-2012, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #ifndef FOTABUF_H
  9. #define FOTABUF_H
  10. #include <stdint.h>
  11. /* fotabuf::buf is a pointer to external data */
  12. #define FOTABUF_FLAG_EXT_DATA BIT(0)
  13. /*
  14. * Internal data structure for fotabuf. Please do not touch this directly from
  15. * elsewhere. This is only defined in header file to allow inline functions
  16. * from this file to access data.
  17. */
  18. struct fotabuf {
  19. size_t size; /* total size of the allocated buffer */
  20. size_t used; /* length of data in the buffer */
  21. uint8_t *buf; /* pointer to the head of the buffer */
  22. unsigned int flags;
  23. /* optionally followed by the allocated buffer */
  24. };
  25. int fotabuf_resize(struct fotabuf **buf, size_t add_len);
  26. struct fotabuf *fotabuf_alloc(size_t len);
  27. struct fotabuf *fotabuf_alloc_ext_data(uint8_t *data, size_t len);
  28. struct fotabuf *fotabuf_alloc_copy(const void *data, size_t len);
  29. struct fotabuf *fotabuf_dup(const struct fotabuf *src);
  30. void fotabuf_free(struct fotabuf *buf);
  31. void fotabuf_clear_free(struct fotabuf *buf);
  32. void *fotabuf_put(struct fotabuf *buf, size_t len);
  33. struct fotabuf *fotabuf_concat(struct fotabuf *a, struct fotabuf *b);
  34. struct fotabuf *fotabuf_zeropad(struct fotabuf *buf, size_t len);
  35. void fotabuf_printf(struct fotabuf *buf, char *fmt, ...) PRINTF_FORMAT(2, 3);
  36. struct fotabuf *fotabuf_parse_bin(const char *buf);
  37. /**
  38. * fotabuf_size - Get the currently allocated size of a fotabuf buffer
  39. * @buf: fotabuf buffer
  40. * Returns: Currently allocated size of the buffer
  41. */
  42. static inline size_t fotabuf_size(const struct fotabuf *buf)
  43. {
  44. return buf->size;
  45. }
  46. /**
  47. * fotabuf_len - Get the current length of a fotabuf buffer data
  48. * @buf: fotabuf buffer
  49. * Returns: Currently used length of the buffer
  50. */
  51. static inline size_t fotabuf_len(const struct fotabuf *buf)
  52. {
  53. return buf->used;
  54. }
  55. /**
  56. * fotabuf_tailroom - Get size of available tail room in the end of the buffer
  57. * @buf: fotabuf buffer
  58. * Returns: Tail room (in bytes) of available space in the end of the buffer
  59. */
  60. static inline size_t fotabuf_tailroom(const struct fotabuf *buf)
  61. {
  62. return buf->size - buf->used;
  63. }
  64. /**
  65. * fotabuf_head - Get pointer to the head of the buffer data
  66. * @buf: fotabuf buffer
  67. * Returns: Pointer to the head of the buffer data
  68. */
  69. static inline const void *fotabuf_head(const struct fotabuf *buf)
  70. {
  71. return buf->buf;
  72. }
  73. static inline const u8 *fotabuf_head_u8(const struct fotabuf *buf)
  74. {
  75. return (const u8 *) fotabuf_head(buf);
  76. }
  77. /**
  78. * fotabuf_mhead - Get modifiable pointer to the head of the buffer data
  79. * @buf: fotabuf buffer
  80. * Returns: Pointer to the head of the buffer data
  81. */
  82. static inline void *fotabuf_mhead(struct fotabuf *buf)
  83. {
  84. return buf->buf;
  85. }
  86. static inline u8 *fotabuf_mhead_u8(struct fotabuf *buf)
  87. {
  88. return (u8 *) fotabuf_mhead(buf);
  89. }
  90. static inline void fotabuf_put_u8(struct fotabuf *buf, u8 data)
  91. {
  92. u8 *pos = (u8 *) fotabuf_put(buf, 1);
  93. *pos = data;
  94. }
  95. static inline void fotabuf_put_le16(struct fotabuf *buf, u16 data)
  96. {
  97. u8 *pos = (u8 *) fotabuf_put(buf, 2);
  98. FOTA_PUT_LE16(pos, data);
  99. }
  100. static inline void fotabuf_put_le32(struct fotabuf *buf, u32 data)
  101. {
  102. u8 *pos = (u8 *) fotabuf_put(buf, 4);
  103. FOTA_PUT_LE32(pos, data);
  104. }
  105. static inline void fotabuf_put_be16(struct fotabuf *buf, u16 data)
  106. {
  107. u8 *pos = (u8 *) fotabuf_put(buf, 2);
  108. FOTA_PUT_BE16(pos, data);
  109. }
  110. static inline void fotabuf_put_be24(struct fotabuf *buf, u32 data)
  111. {
  112. u8 *pos = (u8 *) fotabuf_put(buf, 3);
  113. FOTA_PUT_BE24(pos, data);
  114. }
  115. static inline void fotabuf_put_be32(struct fotabuf *buf, u32 data)
  116. {
  117. u8 *pos = (u8 *) fotabuf_put(buf, 4);
  118. FOTA_PUT_BE32(pos, data);
  119. }
  120. static inline void fotabuf_put_data(struct fotabuf *buf, const void *data,
  121. size_t len)
  122. {
  123. if (data) {
  124. os_memcpy(fotabuf_put(buf, len), data, len);
  125. }
  126. }
  127. static inline void fotabuf_put_buf(struct fotabuf *dst,
  128. const struct fotabuf *src)
  129. {
  130. fotabuf_put_data(dst, fotabuf_head(src), fotabuf_len(src));
  131. }
  132. static inline void fotabuf_set(struct fotabuf *buf, const void *data, size_t len)
  133. {
  134. buf->buf = (u8 *) data;
  135. buf->flags = FOTABUF_FLAG_EXT_DATA;
  136. buf->size = buf->used = len;
  137. }
  138. static inline void fotabuf_put_str(struct fotabuf *dst, const char *str)
  139. {
  140. fotabuf_put_data(dst, str, os_strlen(str));
  141. }
  142. #endif /* FOTABUF_H */