libisdn
msgb.h
Go to the documentation of this file.
00001 /*
00002  *
00003  *
00004  *
00005  */
00006 #ifndef _MSGB_H_
00007 #define _MSGB_H_
00008 
00009 #include <stdint.h>
00010 
00011 #include "dlist.h"
00012 
00013 struct msgb {
00014         struct dlist_head list;         
00015         struct msgb *next;              
00017         unsigned int flags;
00018         unsigned int size;
00019 
00020         unsigned int head;              
00021         unsigned int tail;              
00022         unsigned int end;               
00024         uint8_t *buf;
00025 };
00026 
00027 #define MSGB_STATIC_SIZE(_size) \
00028         (sizeof(struct msgb) + _size)
00029 
00030 #define MSGB_INIT_STATIC(_msgb, _size)  \
00031         msgb_init((struct msgb *)(_msgb), (uint8_t *)(_msgb) + sizeof(struct msgb), (_size) - sizeof(struct msgb))
00032 
00041 int msgb_init(struct msgb *msgb, const uint8_t *buf, const int size);
00042 
00048 struct msgb *msgb_alloc(const int size);
00049 
00057 struct msgb *msgb_alloc_reserve(const int size, const int reserve);
00058 
00065 int msgb_destroy(struct msgb *msgb);
00066 
00074 int msgb_reserve(struct msgb *msgb, const int reserve);
00075 
00083 int msgb_unreserve(struct msgb *msgb, const int size);
00084 
00090 int msgb_size(const struct msgb *msgb);
00091 
00097 int msgb_length(const struct msgb *msgb);
00098 
00104 int msgb_space(const struct msgb *msgb);
00105 
00111 int msgb_reserved_space(const struct msgb *msgb);
00112 
00118 int msgb_empty(const struct msgb *msgb);
00119 
00125 int msgb_full(const struct msgb *msgb);
00126 
00132 int msgb_left(const struct msgb *msgb);
00133 
00134 
00140 uint8_t *msgb_head_ptr(const struct msgb *msgb);
00141 
00147 uint8_t *msgb_tail_ptr(const struct msgb *msgb);
00148 
00149 
00156 uint8_t *msgb_put(struct msgb *msgb, const int size);
00157 
00158 static inline int msgb_put_u8(struct msgb *msgb, const uint8_t val)
00159 {
00160         uint8_t *tmp = msgb_put(msgb, 1);
00161         if (!tmp)
00162                 return -1;
00163         tmp[0] = val;
00164         return 0;
00165 }
00166 
00167 static inline int msgb_put_s8(struct msgb *msgb, const int8_t val)
00168 {
00169         uint8_t *tmp = msgb_put(msgb, 1);
00170         if (!tmp)
00171                 return -1;
00172         tmp[0] = val;
00173         return 0;
00174 }
00175 
00176 static inline int msgb_put_u16(struct msgb *msgb, const uint16_t val)
00177 {
00178         uint8_t *tmp = msgb_put(msgb, 2);
00179         if (!tmp)
00180                 return -1;
00181         tmp[0] = (val >> 8) & 0xff;
00182         tmp[1] =  val       & 0xff;
00183         return 0;
00184 }
00185 
00186 static inline int msgb_put_s16(struct msgb *msgb, const int16_t val)
00187 {
00188         uint8_t *tmp = msgb_put(msgb, 2);
00189         if (!tmp)
00190                 return -1;
00191         tmp[0] = (val >> 8) & 0xff;
00192         tmp[1] =  val       & 0xff;
00193         return 0;
00194 }
00195 
00196 static inline int msgb_put_u32(struct msgb *msgb, const uint32_t val)
00197 {
00198         uint8_t *tmp = msgb_put(msgb, 4);
00199         if (!tmp)
00200                 return -1;
00201         tmp[0] = (val >> 24) & 0xff;
00202         tmp[1] = (val >> 16) & 0xff;
00203         tmp[2] = (val >>  8) & 0xff;
00204         tmp[3] =  val        & 0xff;
00205         return 0;
00206 }
00207 
00208 static inline int msgb_put_s32(struct msgb *msgb, const int32_t val)
00209 {
00210         uint8_t *tmp = msgb_put(msgb, 4);
00211         if (!tmp)
00212                 return -1;
00213         tmp[0] = (val >> 24) & 0xff;
00214         tmp[1] = (val >> 16) & 0xff;
00215         tmp[2] = (val >>  8) & 0xff;
00216         tmp[3] =  val        & 0xff;
00217         return 0;
00218 }
00219 
00220 static inline int msgb_put_u64(struct msgb *msgb, const uint64_t val)
00221 {
00222         uint8_t *tmp = msgb_put(msgb, 8);
00223         if (!tmp)
00224                 return -1;
00225         tmp[0] = (val >> 56) & 0xff;
00226         tmp[1] = (val >> 48) & 0xff;
00227         tmp[2] = (val >> 40) & 0xff;
00228         tmp[3] = (val >> 32) & 0xff;
00229         tmp[4] = (val >> 24) & 0xff;
00230         tmp[5] = (val >> 16) & 0xff;
00231         tmp[6] = (val >>  8) & 0xff;
00232         tmp[7] =  val        & 0xff;
00233         return 0;
00234 }
00235 
00236 static inline int msgb_put_s64(struct msgb *msgb, const int64_t val)
00237 {
00238         uint8_t *tmp = msgb_put(msgb, 8);
00239         if (!tmp)
00240                 return -1;
00241         tmp[0] = (val >> 56) & 0xff;
00242         tmp[1] = (val >> 48) & 0xff;
00243         tmp[2] = (val >> 40) & 0xff;
00244         tmp[3] = (val >> 32) & 0xff;
00245         tmp[4] = (val >> 24) & 0xff;
00246         tmp[5] = (val >> 16) & 0xff;
00247         tmp[6] = (val >>  8) & 0xff;
00248         tmp[7] =  val        & 0xff;
00249         return 0;
00250 }
00251 
00260 int msgb_write(struct msgb *msgb, const void *buf, const int size);
00261 
00270 int msgb_fill(struct msgb *msgb, const int c, const int size);
00271 
00272 
00279 uint8_t *msgb_get(struct msgb *msgb, const int size);
00280 
00281 static inline uint8_t msgb_get_u8(struct msgb *msgb)
00282 {
00283         uint8_t *tmp = msgb_get(msgb, 1);
00284         if (!tmp)
00285                 return 0;
00286         return tmp[0];
00287 }
00288 
00289 static inline int8_t msgb_get_s8(struct msgb *msgb)
00290 {
00291         uint8_t *tmp = msgb_get(msgb, 1);
00292         if (!tmp)
00293                 return 0;
00294         return tmp[0];
00295 }
00296 
00297 static inline uint16_t msgb_get_u16(struct msgb *msgb)
00298 {
00299         uint8_t *tmp = msgb_get(msgb, 2);
00300         if (!tmp)
00301                 return 0;
00302         return tmp[0] << 8 | tmp[1];
00303 }
00304 
00305 static inline int16_t msgb_get_s16(struct msgb *msgb)
00306 {
00307         uint8_t *tmp = msgb_get(msgb, 2);
00308         if (!tmp)
00309                 return 0;
00310         return tmp[0] << 8 | tmp[1];
00311 }
00312 
00313 static inline uint32_t msgb_get_u32(struct msgb *msgb)
00314 {
00315         uint8_t *tmp = msgb_get(msgb, 4);
00316         if (!tmp)
00317                 return 0;
00318         return tmp[0] << 24 | tmp[1] << 16 | tmp[2] << 8 | tmp[3];
00319 }
00320 
00321 static inline int32_t msgb_get_s32(struct msgb *msgb)
00322 {
00323         uint8_t *tmp = msgb_get(msgb, 4);
00324         if (!tmp)
00325                 return 0;
00326         return tmp[0] << 24 | tmp[1] << 16 | tmp[2] << 8 | tmp[3];
00327 }
00328 
00329 static inline uint64_t msgb_get_u64(struct msgb *msgb)
00330 {
00331         uint8_t *tmp = msgb_get(msgb, 8);
00332         uint64_t val = 0;
00333         if (!tmp)
00334                 return 0;
00335         val |= tmp[0]; val <<= 8;
00336         val |= tmp[1]; val <<= 8;
00337         val |= tmp[2]; val <<= 8;
00338         val |= tmp[3]; val <<= 8;
00339         val |= tmp[4]; val <<= 8;
00340         val |= tmp[5]; val <<= 8;
00341         val |= tmp[6]; val <<= 8;
00342         val |= tmp[7];
00343         return val;
00344 }
00345 
00346 static inline int64_t msgb_get_s64(struct msgb *msgb)
00347 {
00348         uint8_t *tmp = msgb_get(msgb, 8);
00349         uint64_t val = 0;
00350         if (!tmp)
00351                 return 0;
00352         val |= tmp[0]; val <<= 8;
00353         val |= tmp[1]; val <<= 8;
00354         val |= tmp[2]; val <<= 8;
00355         val |= tmp[3]; val <<= 8;
00356         val |= tmp[4]; val <<= 8;
00357         val |= tmp[5]; val <<= 8;
00358         val |= tmp[6]; val <<= 8;
00359         val |= tmp[7];
00360         return val;
00361 }
00362 
00371 int msgb_read(struct msgb *msgb, void *buf, const int size);
00372 
00378 int msgb_rewind(struct msgb *msgb);
00379 
00383 typedef enum {
00384         MSGB_SEEK_CUR = 0,      
00385         MSGB_SEEK_SET,          
00386         MSGB_SEEK_END,          
00387 } msgb_seek_t;
00388 
00396 int msgb_seek(struct msgb *msgb, const msgb_seek_t type, const int offset);
00397 
00404 int msgb_tell(const struct msgb *msgb);
00405 
00412 int msgb_clear(struct msgb *msgb);
00413 
00420 int msgb_reset(struct msgb *msgb);
00421 
00427 struct msgb *msgb_linearize(struct msgb *msgb);
00428 
00436 struct msgb *msgb_linearize_reserve(struct msgb *msgb, const int reserve);
00437 
00446 int msgb_merge(struct msgb *msgb, struct msgb *outb);
00447 
00454 struct msgb *msgb_split(struct msgb *msgb, const int size);
00455 
00465 struct msgb *msgb_split_reserve(struct msgb *msgb, const int size, const int reserve);
00466 
00473 int msgb_chain_append(struct msgb *chain, struct msgb *msgb);
00474 
00481 struct msgb *msgb_chain_split(struct msgb *chain, struct msgb *where);
00482 
00489 struct msgb *msgb_chain_split_at(struct msgb *chain, const int pos);
00490 
00496 int msgb_chain_count(const struct msgb *chain);
00497 
00503 int msgb_chain_length(const struct msgb *chain);
00504 
00510 int msgb_chain_size(const struct msgb *chain);
00511 
00512 #endif /* _MSGB_H_ */