1 #ifndef INCLUDE_AL_SERIALIZE_H
2 #define INCLUDE_AL_SERIALIZE_H
83 #define SER_HEADER_SIZE 5
100 static uint32_t serCopy1(
void * dst,
const void * src, uint32_t num);
103 static uint32_t serCopy2(
void * dst,
const void * src, uint32_t num);
106 static uint32_t serCopy4(
void * dst,
const void * src, uint32_t num);
109 static uint32_t serCopy8(
void * dst,
const void * src, uint32_t num);
112 uint32_t serDecode(
const char * b,
void * data);
115 struct SerHeader serGetHeader(const char * buf);
118 static inline int serHeaderSize(){
return SER_HEADER_SIZE; }
121 int serTypeSize(uint8_t t);
123 uint32_t serElementsSize(
const struct SerHeader * h);
125 static void serSwap(
char * a,
char * b);
126 static void serSwapBytes2(
void * v);
127 static void serSwapBytes4(
void * v);
128 static void serSwapBytes8(
void * v);
131 const char * serStringifyHeader(
const struct SerHeader * h);
134 const char * serStringifyType(uint8_t t);
144 #define SOH serHeaderSize()
146 static inline void serHeaderWrite(
char * b, uint8_t type, uint32_t num){
148 serCopy4(b+1, &num, 1);
152 serHeaderWrite(b, type, N);\
153 return SOH + serCopy##B(b+SOH, v, N)
154 static inline uint32_t serEncode1(uint8_t type,
char * b,
const void * v, uint32_t n){ DO(1,n); }
155 static inline uint32_t serEncode2(uint8_t type,
char * b,
const void * v, uint32_t n){ DO(2,n); }
156 static inline uint32_t serEncode4(uint8_t type,
char * b,
const void * v, uint32_t n){ DO(4,n); }
157 static inline uint32_t serEncode8(uint8_t type,
char * b,
const void * v, uint32_t n){ DO(8,n); }
160 static inline uint32_t serEncodeFloat32(
char * b,
const float * v , uint32_t n){
return serEncode4(SER_FLOAT32, b,v,n); }
161 static inline uint32_t serEncodeFloat64(
char * b,
const double * v , uint32_t n){
return serEncode8(SER_FLOAT64, b,v,n); }
162 static inline uint32_t serEncodeInt8 (
char * b,
const int8_t * v , uint32_t n){
return serEncode1(SER_INT8 , b,v,n); }
163 static inline uint32_t serEncodeInt16 (
char * b,
const int16_t * v , uint32_t n){
return serEncode2(SER_INT16 , b,v,n); }
164 static inline uint32_t serEncodeInt32 (
char * b,
const int32_t * v , uint32_t n){
return serEncode4(SER_INT32 , b,v,n); }
165 static inline uint32_t serEncodeInt64 (
char * b,
const int64_t * v , uint32_t n){
return serEncode8(SER_INT64 , b,v,n); }
166 static inline uint32_t serEncodeUInt8 (
char * b,
const uint8_t * v , uint32_t n){
return serEncode1(SER_UINT8 , b,v,n); }
167 static inline uint32_t serEncodeUInt16 (
char * b,
const uint16_t * v, uint32_t n){
return serEncode2(SER_UINT16 , b,v,n); }
168 static inline uint32_t serEncodeUInt32 (
char * b,
const uint32_t * v, uint32_t n){
return serEncode4(SER_UINT32 , b,v,n); }
169 static inline uint32_t serEncodeUInt64 (
char * b,
const uint64_t * v, uint32_t n){
return serEncode8(SER_UINT64 , b,v,n); }
174 static inline void serSwap(
char * a,
char * b){
char t=*a; *a=*b; *b=t; }
176 static inline void serSwapBytes2(
void * v){
177 char * b = (
char *)v;
181 static inline void serSwapBytes4(
void * v){
182 char * b = (
char *)v;
187 static inline void serSwapBytes8(
void * v){
188 char * b = (
char *)v;
196 static inline uint32_t serCopy1(
void * d,
const void * s, uint32_t n){
197 memcpy(d,s,n);
return n;
200 #define DEF_LE(B, S)\
201 static inline uint32_t serCopy##B(void * d, const void * s, uint32_t n){\
207 #define DEF_BE(p, B, S)\
208 static inline uint32_t serCopy##B(void * d, const void * s, uint32_t n){\
211 char * t = (char *)d;\
212 for(uint32_t i=0; i<n; i+=B) swapBytes##B(t+i);\
216 #ifdef SER_IS_BIG_ENDIAN
217 DEF_BE(2,1) DEF_BE(4,2) DEF_BE(8,3)
219 DEF_LE(2,1) DEF_LE(4,2) DEF_LE(8,3)