decode.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. #ifndef __CEPH_DECODE_H
  2. #define __CEPH_DECODE_H
  3. #include <linux/err.h>
  4. #include <linux/bug.h>
  5. #include <linux/time.h>
  6. #include <asm/unaligned.h>
  7. #include <linux/ceph/types.h>
  8. /* This seemed to be the easiest place to define these */
  9. #define U8_MAX ((u8)(~0U))
  10. #define U16_MAX ((u16)(~0U))
  11. #define U32_MAX ((u32)(~0U))
  12. #define U64_MAX ((u64)(~0ULL))
  13. #define S8_MAX ((s8)(U8_MAX >> 1))
  14. #define S16_MAX ((s16)(U16_MAX >> 1))
  15. #define S32_MAX ((s32)(U32_MAX >> 1))
  16. #define S64_MAX ((s64)(U64_MAX >> 1LL))
  17. #define S8_MIN ((s8)(-S8_MAX - 1))
  18. #define S16_MIN ((s16)(-S16_MAX - 1))
  19. #define S32_MIN ((s32)(-S32_MAX - 1))
  20. #define S64_MIN ((s64)(-S64_MAX - 1LL))
  21. /*
  22. * in all cases,
  23. * void **p pointer to position pointer
  24. * void *end pointer to end of buffer (last byte + 1)
  25. */
  26. static inline u64 ceph_decode_64(void **p)
  27. {
  28. u64 v = get_unaligned_le64(*p);
  29. *p += sizeof(u64);
  30. return v;
  31. }
  32. static inline u32 ceph_decode_32(void **p)
  33. {
  34. u32 v = get_unaligned_le32(*p);
  35. *p += sizeof(u32);
  36. return v;
  37. }
  38. static inline u16 ceph_decode_16(void **p)
  39. {
  40. u16 v = get_unaligned_le16(*p);
  41. *p += sizeof(u16);
  42. return v;
  43. }
  44. static inline u8 ceph_decode_8(void **p)
  45. {
  46. u8 v = *(u8 *)*p;
  47. (*p)++;
  48. return v;
  49. }
  50. static inline void ceph_decode_copy(void **p, void *pv, size_t n)
  51. {
  52. memcpy(pv, *p, n);
  53. *p += n;
  54. }
  55. /*
  56. * bounds check input.
  57. */
  58. static inline int ceph_has_room(void **p, void *end, size_t n)
  59. {
  60. return end >= *p && n <= end - *p;
  61. }
  62. #define ceph_decode_need(p, end, n, bad) \
  63. do { \
  64. if (!likely(ceph_has_room(p, end, n))) \
  65. goto bad; \
  66. } while (0)
  67. #define ceph_decode_64_safe(p, end, v, bad) \
  68. do { \
  69. ceph_decode_need(p, end, sizeof(u64), bad); \
  70. v = ceph_decode_64(p); \
  71. } while (0)
  72. #define ceph_decode_32_safe(p, end, v, bad) \
  73. do { \
  74. ceph_decode_need(p, end, sizeof(u32), bad); \
  75. v = ceph_decode_32(p); \
  76. } while (0)
  77. #define ceph_decode_16_safe(p, end, v, bad) \
  78. do { \
  79. ceph_decode_need(p, end, sizeof(u16), bad); \
  80. v = ceph_decode_16(p); \
  81. } while (0)
  82. #define ceph_decode_8_safe(p, end, v, bad) \
  83. do { \
  84. ceph_decode_need(p, end, sizeof(u8), bad); \
  85. v = ceph_decode_8(p); \
  86. } while (0)
  87. #define ceph_decode_copy_safe(p, end, pv, n, bad) \
  88. do { \
  89. ceph_decode_need(p, end, n, bad); \
  90. ceph_decode_copy(p, pv, n); \
  91. } while (0)
  92. /*
  93. * Allocate a buffer big enough to hold the wire-encoded string, and
  94. * decode the string into it. The resulting string will always be
  95. * terminated with '\0'. If successful, *p will be advanced
  96. * past the decoded data. Also, if lenp is not a null pointer, the
  97. * length (not including the terminating '\0') will be recorded in
  98. * *lenp. Note that a zero-length string is a valid return value.
  99. *
  100. * Returns a pointer to the newly-allocated string buffer, or a
  101. * pointer-coded errno if an error occurs. Neither *p nor *lenp
  102. * will have been updated if an error is returned.
  103. *
  104. * There are two possible failures:
  105. * - converting the string would require accessing memory at or
  106. * beyond the "end" pointer provided (-ERANGE)
  107. * - memory could not be allocated for the result (-ENOMEM)
  108. */
  109. static inline char *ceph_extract_encoded_string(void **p, void *end,
  110. size_t *lenp, gfp_t gfp)
  111. {
  112. u32 len;
  113. void *sp = *p;
  114. char *buf;
  115. ceph_decode_32_safe(&sp, end, len, bad);
  116. if (!ceph_has_room(&sp, end, len))
  117. goto bad;
  118. buf = kmalloc(len + 1, gfp);
  119. if (!buf)
  120. return ERR_PTR(-ENOMEM);
  121. if (len)
  122. memcpy(buf, sp, len);
  123. buf[len] = '\0';
  124. *p = (char *) *p + sizeof (u32) + len;
  125. if (lenp)
  126. *lenp = (size_t) len;
  127. return buf;
  128. bad:
  129. return ERR_PTR(-ERANGE);
  130. }
  131. /*
  132. * struct ceph_timespec <-> struct timespec
  133. */
  134. static inline void ceph_decode_timespec(struct timespec *ts,
  135. const struct ceph_timespec *tv)
  136. {
  137. ts->tv_sec = (__kernel_time_t)le32_to_cpu(tv->tv_sec);
  138. ts->tv_nsec = (long)le32_to_cpu(tv->tv_nsec);
  139. }
  140. static inline void ceph_encode_timespec(struct ceph_timespec *tv,
  141. const struct timespec *ts)
  142. {
  143. tv->tv_sec = cpu_to_le32((u32)ts->tv_sec);
  144. tv->tv_nsec = cpu_to_le32((u32)ts->tv_nsec);
  145. }
  146. /*
  147. * sockaddr_storage <-> ceph_sockaddr
  148. */
  149. static inline void ceph_encode_addr(struct ceph_entity_addr *a)
  150. {
  151. __be16 ss_family = htons(a->in_addr.ss_family);
  152. a->in_addr.ss_family = *(__u16 *)&ss_family;
  153. }
  154. static inline void ceph_decode_addr(struct ceph_entity_addr *a)
  155. {
  156. __be16 ss_family = *(__be16 *)&a->in_addr.ss_family;
  157. a->in_addr.ss_family = ntohs(ss_family);
  158. WARN_ON(a->in_addr.ss_family == 512);
  159. }
  160. /*
  161. * encoders
  162. */
  163. static inline void ceph_encode_64(void **p, u64 v)
  164. {
  165. put_unaligned_le64(v, (__le64 *)*p);
  166. *p += sizeof(u64);
  167. }
  168. static inline void ceph_encode_32(void **p, u32 v)
  169. {
  170. put_unaligned_le32(v, (__le32 *)*p);
  171. *p += sizeof(u32);
  172. }
  173. static inline void ceph_encode_16(void **p, u16 v)
  174. {
  175. put_unaligned_le16(v, (__le16 *)*p);
  176. *p += sizeof(u16);
  177. }
  178. static inline void ceph_encode_8(void **p, u8 v)
  179. {
  180. *(u8 *)*p = v;
  181. (*p)++;
  182. }
  183. static inline void ceph_encode_copy(void **p, const void *s, int len)
  184. {
  185. memcpy(*p, s, len);
  186. *p += len;
  187. }
  188. /*
  189. * filepath, string encoders
  190. */
  191. static inline void ceph_encode_filepath(void **p, void *end,
  192. u64 ino, const char *path)
  193. {
  194. u32 len = path ? strlen(path) : 0;
  195. BUG_ON(*p + 1 + sizeof(ino) + sizeof(len) + len > end);
  196. ceph_encode_8(p, 1);
  197. ceph_encode_64(p, ino);
  198. ceph_encode_32(p, len);
  199. if (len)
  200. memcpy(*p, path, len);
  201. *p += len;
  202. }
  203. static inline void ceph_encode_string(void **p, void *end,
  204. const char *s, u32 len)
  205. {
  206. BUG_ON(*p + sizeof(len) + len > end);
  207. ceph_encode_32(p, len);
  208. if (len)
  209. memcpy(*p, s, len);
  210. *p += len;
  211. }
  212. #define ceph_encode_need(p, end, n, bad) \
  213. do { \
  214. if (!likely(ceph_has_room(p, end, n))) \
  215. goto bad; \
  216. } while (0)
  217. #define ceph_encode_64_safe(p, end, v, bad) \
  218. do { \
  219. ceph_encode_need(p, end, sizeof(u64), bad); \
  220. ceph_encode_64(p, v); \
  221. } while (0)
  222. #define ceph_encode_32_safe(p, end, v, bad) \
  223. do { \
  224. ceph_encode_need(p, end, sizeof(u32), bad); \
  225. ceph_encode_32(p, v); \
  226. } while (0)
  227. #define ceph_encode_16_safe(p, end, v, bad) \
  228. do { \
  229. ceph_encode_need(p, end, sizeof(u16), bad); \
  230. ceph_encode_16(p, v); \
  231. } while (0)
  232. #define ceph_encode_8_safe(p, end, v, bad) \
  233. do { \
  234. ceph_encode_need(p, end, sizeof(u8), bad); \
  235. ceph_encode_8(p, v); \
  236. } while (0)
  237. #define ceph_encode_copy_safe(p, end, pv, n, bad) \
  238. do { \
  239. ceph_encode_need(p, end, n, bad); \
  240. ceph_encode_copy(p, pv, n); \
  241. } while (0)
  242. #define ceph_encode_string_safe(p, end, s, n, bad) \
  243. do { \
  244. ceph_encode_need(p, end, n, bad); \
  245. ceph_encode_string(p, end, s, n); \
  246. } while (0)
  247. #endif