blob: 91179fb2cc3fd7a6a9f07ecc80b1638ebf5a3a4b [file] [log] [blame]
Sage Weil31b80062009-10-06 11:31:13 -07001#ifndef __CEPH_DECODE_H
2#define __CEPH_DECODE_H
3
4#include <asm/unaligned.h>
5
Sage Weilc89136e2009-10-14 09:59:09 -07006#include "types.h"
7
Sage Weil31b80062009-10-06 11:31:13 -07008/*
9 * in all cases,
10 * void **p pointer to position pointer
11 * void *end pointer to end of buffer (last byte + 1)
12 */
13
Sage Weilc89136e2009-10-14 09:59:09 -070014static inline u64 ceph_decode_64(void **p)
15{
16 u64 v = get_unaligned_le64(*p);
17 *p += sizeof(u64);
18 return v;
19}
20static inline u32 ceph_decode_32(void **p)
21{
22 u32 v = get_unaligned_le32(*p);
23 *p += sizeof(u32);
24 return v;
25}
26static inline u16 ceph_decode_16(void **p)
27{
28 u16 v = get_unaligned_le16(*p);
29 *p += sizeof(u16);
30 return v;
31}
32static inline u8 ceph_decode_8(void **p)
33{
34 u8 v = *(u8 *)*p;
35 (*p)++;
36 return v;
37}
38static inline void ceph_decode_copy(void **p, void *pv, size_t n)
39{
40 memcpy(pv, *p, n);
41 *p += n;
42}
43
Sage Weil31b80062009-10-06 11:31:13 -070044/*
45 * bounds check input.
46 */
47#define ceph_decode_need(p, end, n, bad) \
48 do { \
49 if (unlikely(*(p) + (n) > (end))) \
50 goto bad; \
51 } while (0)
52
Sage Weil31b80062009-10-06 11:31:13 -070053#define ceph_decode_64_safe(p, end, v, bad) \
54 do { \
55 ceph_decode_need(p, end, sizeof(u64), bad); \
Sage Weilc89136e2009-10-14 09:59:09 -070056 v = ceph_decode_64(p); \
Sage Weil31b80062009-10-06 11:31:13 -070057 } while (0)
58#define ceph_decode_32_safe(p, end, v, bad) \
59 do { \
60 ceph_decode_need(p, end, sizeof(u32), bad); \
Sage Weilc89136e2009-10-14 09:59:09 -070061 v = ceph_decode_32(p); \
Sage Weil31b80062009-10-06 11:31:13 -070062 } while (0)
63#define ceph_decode_16_safe(p, end, v, bad) \
64 do { \
65 ceph_decode_need(p, end, sizeof(u16), bad); \
Sage Weilc89136e2009-10-14 09:59:09 -070066 v = ceph_decode_16(p); \
Sage Weil31b80062009-10-06 11:31:13 -070067 } while (0)
68
69#define ceph_decode_copy_safe(p, end, pv, n, bad) \
70 do { \
71 ceph_decode_need(p, end, n, bad); \
72 ceph_decode_copy(p, pv, n); \
73 } while (0)
74
75/*
76 * struct ceph_timespec <-> struct timespec
77 */
Sage Weilc89136e2009-10-14 09:59:09 -070078static inline void ceph_decode_timespec(struct timespec *ts,
79 struct ceph_timespec *tv)
80{
81 ts->tv_sec = le32_to_cpu(tv->tv_sec);
82 ts->tv_nsec = le32_to_cpu(tv->tv_nsec);
83}
84static inline void ceph_encode_timespec(struct ceph_timespec *tv,
85 struct timespec *ts)
86{
87 tv->tv_sec = cpu_to_le32(ts->tv_sec);
88 tv->tv_nsec = cpu_to_le32(ts->tv_nsec);
89}
Sage Weil31b80062009-10-06 11:31:13 -070090
91/*
92 * encoders
93 */
Sage Weilc89136e2009-10-14 09:59:09 -070094static inline void ceph_encode_64(void **p, u64 v)
95{
96 put_unaligned_le64(v, (__le64 *)*p);
97 *p += sizeof(u64);
98}
99static inline void ceph_encode_32(void **p, u32 v)
100{
101 put_unaligned_le32(v, (__le32 *)*p);
102 *p += sizeof(u32);
103}
104static inline void ceph_encode_16(void **p, u16 v)
105{
106 put_unaligned_le16(v, (__le16 *)*p);
107 *p += sizeof(u16);
108}
109static inline void ceph_encode_8(void **p, u8 v)
110{
111 *(u8 *)*p = v;
112 (*p)++;
113}
Sage Weil31b80062009-10-06 11:31:13 -0700114
115/*
116 * filepath, string encoders
117 */
118static inline void ceph_encode_filepath(void **p, void *end,
119 u64 ino, const char *path)
120{
121 u32 len = path ? strlen(path) : 0;
122 BUG_ON(*p + sizeof(ino) + sizeof(len) + len > end);
123 ceph_encode_64(p, ino);
124 ceph_encode_32(p, len);
125 if (len)
126 memcpy(*p, path, len);
127 *p += len;
128}
129
130static inline void ceph_encode_string(void **p, void *end,
131 const char *s, u32 len)
132{
133 BUG_ON(*p + sizeof(len) + len > end);
134 ceph_encode_32(p, len);
135 if (len)
136 memcpy(*p, s, len);
137 *p += len;
138}
139
140
141#endif