Hideaki Tai / msgpack-embedded

Dependents:   hello_message_pack

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers pack.h Source File

pack.h

00001 /*
00002  * MessagePack for C packing routine
00003  *
00004  * Copyright (C) 2008-2009 FURUHASHI Sadayuki
00005  *
00006  *    Distributed under the Boost Software License, Version 1.0.
00007  *    (See accompanying file LICENSE_1_0.txt or copy at
00008  *    http://www.boost.org/LICENSE_1_0.txt)
00009  */
00010 #ifndef MSGPACK_PACK_H
00011 #define MSGPACK_PACK_H
00012 
00013 #include "pack_define.h"
00014 #include "object.h"
00015 #include <stdlib.h>
00016 
00017 #ifdef __cplusplus
00018 extern "C" {
00019 #endif
00020 
00021 
00022 /**
00023  * @defgroup msgpack_buffer Buffers
00024  * @ingroup msgpack
00025  * @{
00026  * @}
00027  */
00028 
00029 /**
00030  * @defgroup msgpack_pack Serializer
00031  * @ingroup msgpack
00032  * @{
00033  */
00034 
00035 typedef int (*msgpack_packer_write)(void* data, const char* buf, size_t len);
00036 
00037 typedef struct msgpack_packer {
00038     void* data;
00039     msgpack_packer_write callback;
00040 } msgpack_packer;
00041 
00042 static void msgpack_packer_init(msgpack_packer* pk, void* data, msgpack_packer_write callback);
00043 
00044 static msgpack_packer* msgpack_packer_new(void* data, msgpack_packer_write callback);
00045 static void msgpack_packer_free(msgpack_packer* pk);
00046 
00047 static int msgpack_pack_char(msgpack_packer* pk, char d);
00048 
00049 static int msgpack_pack_signed_char(msgpack_packer* pk, signed char d);
00050 static int msgpack_pack_short(msgpack_packer* pk, short d);
00051 static int msgpack_pack_int(msgpack_packer* pk, int d);
00052 static int msgpack_pack_long(msgpack_packer* pk, long d);
00053 static int msgpack_pack_long_long(msgpack_packer* pk, long long d);
00054 static int msgpack_pack_unsigned_char(msgpack_packer* pk, unsigned char d);
00055 static int msgpack_pack_unsigned_short(msgpack_packer* pk, unsigned short d);
00056 static int msgpack_pack_unsigned_int(msgpack_packer* pk, unsigned int d);
00057 static int msgpack_pack_unsigned_long(msgpack_packer* pk, unsigned long d);
00058 static int msgpack_pack_unsigned_long_long(msgpack_packer* pk, unsigned long long d);
00059 
00060 static int msgpack_pack_uint8(msgpack_packer* pk, uint8_t d);
00061 static int msgpack_pack_uint16(msgpack_packer* pk, uint16_t d);
00062 static int msgpack_pack_uint32(msgpack_packer* pk, uint32_t d);
00063 static int msgpack_pack_uint64(msgpack_packer* pk, uint64_t d);
00064 static int msgpack_pack_int8(msgpack_packer* pk, int8_t d);
00065 static int msgpack_pack_int16(msgpack_packer* pk, int16_t d);
00066 static int msgpack_pack_int32(msgpack_packer* pk, int32_t d);
00067 static int msgpack_pack_int64(msgpack_packer* pk, int64_t d);
00068 
00069 static int msgpack_pack_fix_uint8(msgpack_packer* pk, uint8_t d);
00070 static int msgpack_pack_fix_uint16(msgpack_packer* pk, uint16_t d);
00071 static int msgpack_pack_fix_uint32(msgpack_packer* pk, uint32_t d);
00072 static int msgpack_pack_fix_uint64(msgpack_packer* pk, uint64_t d);
00073 static int msgpack_pack_fix_int8(msgpack_packer* pk, int8_t d);
00074 static int msgpack_pack_fix_int16(msgpack_packer* pk, int16_t d);
00075 static int msgpack_pack_fix_int32(msgpack_packer* pk, int32_t d);
00076 static int msgpack_pack_fix_int64(msgpack_packer* pk, int64_t d);
00077 
00078 static int msgpack_pack_float(msgpack_packer* pk, float d);
00079 static int msgpack_pack_double(msgpack_packer* pk, double d);
00080 
00081 static int msgpack_pack_nil(msgpack_packer* pk);
00082 static int msgpack_pack_true(msgpack_packer* pk);
00083 static int msgpack_pack_false(msgpack_packer* pk);
00084 
00085 static int msgpack_pack_array(msgpack_packer* pk, size_t n);
00086 
00087 static int msgpack_pack_map(msgpack_packer* pk, size_t n);
00088 
00089 static int msgpack_pack_str(msgpack_packer* pk, size_t l);
00090 static int msgpack_pack_str_body(msgpack_packer* pk, const void* b, size_t l);
00091 
00092 static int msgpack_pack_v4raw(msgpack_packer* pk, size_t l);
00093 static int msgpack_pack_v4raw_body(msgpack_packer* pk, const void* b, size_t l);
00094 
00095 static int msgpack_pack_bin(msgpack_packer* pk, size_t l);
00096 static int msgpack_pack_bin_body(msgpack_packer* pk, const void* b, size_t l);
00097 
00098 static int msgpack_pack_ext(msgpack_packer* pk, size_t l, int8_t type);
00099 static int msgpack_pack_ext_body(msgpack_packer* pk, const void* b, size_t l);
00100 
00101 int msgpack_pack_object(msgpack_packer* pk, msgpack_object d);
00102 
00103 
00104 /** @} */
00105 
00106 
00107 #define msgpack_pack_inline_func(name) \
00108     inline int msgpack_pack ## name
00109 
00110 #define msgpack_pack_inline_func_cint(name) \
00111     inline int msgpack_pack ## name
00112 
00113 #define msgpack_pack_inline_func_fixint(name) \
00114     inline int msgpack_pack_fix ## name
00115 
00116 #define msgpack_pack_user msgpack_packer*
00117 
00118 #define msgpack_pack_append_buffer(user, buf, len) \
00119     return (*(user)->callback)((user)->data, (const char*)buf, len)
00120 
00121 #include "pack_template.h"
00122 
00123 inline void msgpack_packer_init(msgpack_packer* pk, void* data, msgpack_packer_write callback)
00124 {
00125     pk->data = data;
00126     pk->callback = callback;
00127 }
00128 
00129 inline msgpack_packer* msgpack_packer_new(void* data, msgpack_packer_write callback)
00130 {
00131     msgpack_packer* pk = (msgpack_packer*)calloc(1, sizeof(msgpack_packer));
00132     if(!pk) { return NULL; }
00133     msgpack_packer_init(pk, data, callback);
00134     return pk;
00135 }
00136 
00137 inline void msgpack_packer_free(msgpack_packer* pk)
00138 {
00139     free(pk);
00140 }
00141 
00142 
00143 #ifdef __cplusplus
00144 }
00145 #endif
00146 
00147 #endif /* msgpack/pack.h */