1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
3 *
4 * Copyright (C) 2001 Cluster File Systems, Inc. <braam@clusterfs.com>
5 *
6 * This file is part of InterMezzo, http://www.inter-mezzo.org.
7 *
8 * InterMezzo is free software; you can redistribute it and/or
9 * modify it under the terms of version 2 of the GNU General Public
10 * License as published by the Free Software Foundation.
11 *
12 * InterMezzo is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with InterMezzo; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 *
21 * Data structures unpacking/packing macros & inlines
22 *
23 */
24
25 #ifndef _INTERMEZZO_LIB_H
26 #define _INTERMEZZO_LIB_H
27
28 #ifdef __KERNEL__
29 # include <linux/types.h>
30 #else
31 # include <string.h>
32 # include <sys/types.h>
33 #endif
34
35 #undef MIN
36 #define MIN(a,b) (((a)<(b)) ? (a): (b))
37 #undef MAX
38 #define MAX(a,b) (((a)>(b)) ? (a): (b))
39 #define MKSTR(ptr) ((ptr))? (ptr) : ""
40
size_round(int val)41 static inline int size_round (int val)
42 {
43 return (val + 3) & (~0x3);
44 }
45
size_round0(int val)46 static inline int size_round0(int val)
47 {
48 if (!val)
49 return 0;
50 return (val + 1 + 3) & (~0x3);
51 }
52
round_strlen(char * fset)53 static inline size_t round_strlen(char *fset)
54 {
55 return size_round(strlen(fset) + 1);
56 }
57
58 #ifdef __KERNEL__
59 # define NTOH__u32(var) le32_to_cpu(var)
60 # define NTOH__u64(var) le64_to_cpu(var)
61 # define HTON__u32(var) cpu_to_le32(var)
62 # define HTON__u64(var) cpu_to_le64(var)
63 #else
64 # include <glib.h>
65 # define NTOH__u32(var) GUINT32_FROM_LE(var)
66 # define NTOH__u64(var) GUINT64_FROM_LE(var)
67 # define HTON__u32(var) GUINT32_TO_LE(var)
68 # define HTON__u64(var) GUINT64_TO_LE(var)
69 #endif
70
71 /*
72 * copy sizeof(type) bytes from pointer to var and move ptr forward.
73 * return EFAULT if pointer goes beyond end
74 */
75 #define UNLOGV(var,type,ptr,end) \
76 do { \
77 var = *(type *)ptr; \
78 ptr += sizeof(type); \
79 if (ptr > end ) \
80 return -EFAULT; \
81 } while (0)
82
83 /* the following two macros convert to little endian */
84 /* type MUST be __u32 or __u64 */
85 #define LUNLOGV(var,type,ptr,end) \
86 do { \
87 var = NTOH##type(*(type *)ptr); \
88 ptr += sizeof(type); \
89 if (ptr > end ) \
90 return -EFAULT; \
91 } while (0)
92
93 /* now log values */
94 #define LOGV(var,type,ptr) \
95 do { \
96 *((type *)ptr) = var; \
97 ptr += sizeof(type); \
98 } while (0)
99
100 /* and in network order */
101 #define LLOGV(var,type,ptr) \
102 do { \
103 *((type *)ptr) = HTON##type(var); \
104 ptr += sizeof(type); \
105 } while (0)
106
107
108 /*
109 * set var to point at (type *)ptr, move ptr forward with sizeof(type)
110 * return from function with EFAULT if ptr goes beyond end
111 */
112 #define UNLOGP(var,type,ptr,end) \
113 do { \
114 var = (type *)ptr; \
115 ptr += sizeof(type); \
116 if (ptr > end ) \
117 return -EFAULT; \
118 } while (0)
119
120 #define LOGP(var,type,ptr) \
121 do { \
122 memcpy(ptr, var, sizeof(type)); \
123 ptr += sizeof(type); \
124 } while (0)
125
126 /*
127 * set var to point at (char *)ptr, move ptr forward by size_round(len);
128 * return from function with EFAULT if ptr goes beyond end
129 */
130 #define UNLOGL(var,type,len,ptr,end) \
131 do { \
132 if (len == 0) \
133 var = (type *)0; \
134 else { \
135 var = (type *)ptr; \
136 ptr += size_round(len * sizeof(type)); \
137 } \
138 if (ptr > end ) \
139 return -EFAULT; \
140 } while (0)
141
142 #define UNLOGL0(var,type,len,ptr,end) \
143 do { \
144 UNLOGL(var,type,len+1,ptr,end); \
145 if ( *((char *)ptr - size_round(len+1) + len) != '\0') \
146 return -EFAULT; \
147 } while (0)
148
149 #define LOGL(var,len,ptr) \
150 do { \
151 size_t __fill = size_round(len); \
152 /* Prevent data leakage. */ \
153 if (__fill > 0) \
154 memset((char *)ptr, 0, __fill); \
155 memcpy((char *)ptr, (const char *)var, len); \
156 ptr += __fill; \
157 } while (0)
158
159 #define LOGL0(var,len,ptr) \
160 do { \
161 if (!len) break; \
162 memcpy((char *)ptr, (const char *)var, len); \
163 *((char *)(ptr) + len) = 0; \
164 ptr += size_round(len + 1); \
165 } while (0)
166
167 #endif /* _INTERMEZZO_LIB_H */
168
169