2 * $Id: cdr.h,v 0.0.0.1 2004/11/26
4 * -------------------------------------------------------------------
6 * Open Real-Time Ethernet
8 * Copyright (C) 2001-2006
9 * Department of Control Engineering FEE CTU Prague, Czech Republic
10 * http://dce.felk.cvut.cz
11 * http://www.ocera.org
13 * Author: Petr Smolik petr.smolik@wo.cz
15 * Project Responsible: Zdenek Hanzalek
17 * Modified for FORB by Michal Sojka <sojkam1@fel.cvut.cz>, 2008
18 * --------------------------------------------------------------------
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2 of the License, or
23 * (at your option) any later version.
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
30 * Original of source was from ORBit: A CORBA v2.2 ORB
34 * @author Michal Sojka <sojkam1@fel.cvut.cz>
35 * @date Sun Oct 12 16:55:44 2008
37 * @brief Handling of serialization/deserialization.
39 * Forked from ORTE project.
46 #include "basic_types.h"
47 #include "cdr_codec.h"
50 * Alignment of CORBA types mapped to C.
51 * These have *nothing* to do with CDR alignment.
53 #define ORTE_ALIGNOF_CORBA_OCTET 1
54 #define ORTE_ALIGNOF_CORBA_BOOLEAN 1
55 #define ORTE_ALIGNOF_CORBA_CHAR 1
56 #define ORTE_ALIGNOF_CORBA_WCHAR 2
57 #define ORTE_ALIGNOF_CORBA_SHORT 2
58 #define ORTE_ALIGNOF_CORBA_LONG 4
59 #define ORTE_ALIGNOF_CORBA_LONG_LONG 8
60 #define ORTE_ALIGNOF_CORBA_FLOAT 4
61 #define ORTE_ALIGNOF_CORBA_DOUBLE 8
62 #define ORTE_ALIGNOF_CORBA_LONG_DOUBLE 16
63 #define ORTE_ALIGNOF_CORBA_STRUCT 1
64 #define ORTE_ALIGNOF_CORBA_POINTER 4
72 * struct CDR_Codec - used for serialization/deserialization
75 * @buffer: buffer for data
76 * @buf_len: buffer length
77 * @wptr_max: maximal size of writing data
79 * @wptr_last: index of the data after the last written data
81 * @readonly: readonly attribute
82 * @release_buffer: use CORBA_TRUE if is necessary to free buffer memory after destruction of structure
84 * Struct @CDR_Codec is used by serialization and deserialization functions.
87 CDR_Endianness host_endian;
88 CDR_Endianness data_endian;
91 unsigned int wptr_max;
93 unsigned int wptr_last;
95 CORBA_boolean readonly;
96 CORBA_boolean release_buffer;
99 #define HEXDIGIT(c) (isdigit((guchar)(c))?(c)-'0':tolower((guchar)(c))-'a'+10)
100 #define HEXOCTET(a,b) ((HEXDIGIT((a)) << 4) | HEXDIGIT((b)))
102 static inline CORBA_long CDR_data_size(CDR_Codec *codec)
104 return codec->wptr_last - codec->rptr;
107 extern CORBA_boolean CDR_buffer_init(CDR_Codec *codec, const unsigned int size, const unsigned int first);
108 extern CORBA_boolean CDR_buffer_reset(CDR_Codec *codec, const unsigned int first);
109 extern CORBA_boolean CDR_buffer_prepend(CDR_Codec *codec, const unsigned int len);
110 extern CDR_Codec *CDR_codec_init(void);
111 extern CDR_Codec *CDR_codec_init_static(CDR_Codec *codec);
112 extern void CDR_codec_release_buffer(CDR_Codec *codec);
113 extern void CDR_codec_free(CDR_Codec *);
115 extern CORBA_boolean CDR_buffer_puts(CDR_Codec *codec, const void *data, const unsigned int len);
116 extern CORBA_boolean CDR_buffer_gets(CDR_Codec *codec, void *dest, const unsigned int len);
118 extern CORBA_boolean CDR_put_short(CDR_Codec *codec, CORBA_short s);
119 extern CORBA_boolean CDR_put_ushort(CDR_Codec *codec, CORBA_unsigned_short us);
120 extern CORBA_boolean CDR_put_long(CDR_Codec *codec, CORBA_long l);
121 extern CORBA_boolean CDR_put_ulong(CDR_Codec *codec, CORBA_unsigned_long ul);
122 /* #ifdef HAVE_CORBA_LONG_LONG */
123 extern CORBA_boolean CDR_put_long_long(CDR_Codec *codec, CORBA_long_long ll);
124 extern CORBA_boolean CDR_put_ulong_long(CDR_Codec *codec, CORBA_unsigned_long_long ull);
125 extern CORBA_boolean CDR_get_ulong_long(CDR_Codec *codec, CORBA_unsigned_long_long *ul);
126 extern CORBA_boolean CDR_get_long_long(CDR_Codec *codec, CORBA_long_long *ul);
128 extern CORBA_boolean CDR_put_float(CDR_Codec *codec, CORBA_float f);
129 extern CORBA_boolean CDR_put_double(CDR_Codec *codec, CORBA_double d);
130 extern CORBA_boolean CDR_put_long_double(CDR_Codec *codec, CORBA_long_double ld);
131 extern CORBA_boolean CDR_put_octet(CDR_Codec *codec, CORBA_octet datum);
132 extern CORBA_boolean CDR_put_octets(CDR_Codec *codec, void *data, unsigned long len);
133 extern CORBA_boolean CDR_put_char(CDR_Codec *codec, CORBA_char c);
134 extern CORBA_boolean CDR_put_boolean(CDR_Codec *codec, CORBA_boolean datum);
135 extern CORBA_boolean CDR_put_string(CDR_Codec *codec, const CORBA_char *str);
136 extern CORBA_boolean CDR_buffer_gets(CDR_Codec *codec, void *dest, const unsigned int len);
137 extern CORBA_boolean CDR_get_short(CDR_Codec *codec, CORBA_short *us);
138 extern CORBA_boolean CDR_get_ushort(CDR_Codec *codec, CORBA_unsigned_short *us);
139 extern CORBA_boolean CDR_get_long(CDR_Codec *codec, CORBA_long *l);
140 extern CORBA_boolean CDR_get_ulong(CDR_Codec *codec, CORBA_unsigned_long *ul);
141 extern CORBA_boolean CDR_get_octet(CDR_Codec *codec, CORBA_octet *datum);
142 extern CORBA_boolean CDR_get_boolean(CDR_Codec *codec, CORBA_boolean *b);
143 extern CORBA_boolean CDR_get_char(CDR_Codec *codec, CORBA_char *c);
144 extern CORBA_boolean CDR_get_string(CDR_Codec *codec, CORBA_char **str);
145 extern CORBA_boolean CDR_get_string_buff(CDR_Codec *codec, CORBA_char *str);
146 extern CORBA_boolean CDR_get_string_static(CDR_Codec *codec, CORBA_char **str);
147 extern CORBA_boolean CDR_get_seq_begin(CDR_Codec *codec, CORBA_unsigned_long *ul);
148 extern CORBA_boolean CDR_get_float(CDR_Codec *codec, CORBA_float *f);
149 extern CORBA_boolean CDR_get_double(CDR_Codec *codec, CORBA_double *d);
151 extern CORBA_boolean CDR_put_align(CDR_Codec *codec, unsigned bsize);
152 extern CORBA_boolean CDR_get_align(CDR_Codec *codec, unsigned bsize);
155 /* serialization functions */
156 #define CORBA_short_serialize(x,y) CDR_put_short((x),*(y))
157 #define CORBA_long_serialize(x,y) CDR_put_long((x),*(y))
158 #define CORBA_long_long_serialize(x,y) CDR_put_long_long((x),*(y))
159 #define CORBA_unsigned_short_serialize(x,y) CDR_put_ushort((x),*(y))
160 #define CORBA_unsigned_long_serialize(x,y) CDR_put_ulong((x),*(y))
161 #define CORBA_unsigned_long_long_serialize(x,y) CDR_put_ulong_long((x),*(y))
162 #define CORBA_float_serialize(x,y) CDR_put_float((x),*(y))
163 #define CORBA_double_serialize(x,y) CDR_put_double((x),*(y))
164 #define CORBA_char_serialize(x,y) CDR_put_char((x),*(y))
165 #define CORBA_boolean_serialize(x,y) CDR_put_boolean((x),*(y))
166 #define CORBA_octet_serialize(x,y) CDR_put_octet((x),*(y))
167 #define CORBA_long_double_serialize(x,y) CDR_put_long_double((x),*(y))
168 #define CORBA_string_serialize(x,y) CDR_put_string((x),*(y))
170 /* deserialization functions */
171 #define CORBA_short_deserialize(x,y) CDR_get_short((x),(y))
172 #define CORBA_long_deserialize(x,y) CDR_get_long((x),(y))
173 #define CORBA_long_long_deserialize(x,y) CDR_get_long_long((x),(y))
174 #define CORBA_unsigned_short_deserialize(x,y) CDR_get_ushort((x),(y))
175 #define CORBA_unsigned_long_deserialize(x,y) CDR_get_ulong((x),(y))
176 #define CORBA_unsigned_long_long_deserialize(x,y) CDR_get_ulong_long((x),(y))
177 #define CORBA_float_deserialize(x,y) CDR_get_float((x),(y))
178 #define CORBA_double_deserialize(x,y) CDR_get_double((x),(y))
179 #define CORBA_char_deserialize(x,y) CDR_get_char((x),(y))
180 #define CORBA_boolean_deserialize(x,y) CDR_get_boolean((x),(y))
181 #define CORBA_octet_deserialize(x,y) CDR_get_octet((x),(y))
182 #define CORBA_long_double_deserialize(x,y) CDR_get_long_double((x),(y))
183 #define CORBA_string_deserialize(x,y) CDR_get_string((x),(y))
185 /* get_max_size functions */
186 #define CORBA_short_get_max_size(x) ((x)->csize=\
187 (unsigned long)ALIGN_ADDRESS((x)->csize,ORTE_ALIGNOF_CORBA_SHORT)+ORTE_ALIGNOF_CORBA_SHORT)
188 #define CORBA_long_get_max_size(x) ((x)->csize=\
189 (unsigned long)ALIGN_ADDRESS((x)->csize,ORTE_ALIGNOF_CORBA_LONG)+ORTE_ALIGNOF_CORBA_LONG)
190 #define CORBA_unsigned_short_get_max_size(x) ((x)->csize=\
191 (unsigned long)ALIGN_ADDRESS((x)->csize,ORTE_ALIGNOF_CORBA_SHORT)+ORTE_ALIGNOF_CORBA_SHORT)
192 #define CORBA_unsigned_long_get_max_size(x) ((x)->csize=\
193 (unsigned long)ALIGN_ADDRESS((x)->csize,ORTE_ALIGNOF_CORBA_LONG)+ORTE_ALIGNOF_CORBA_LONG)
194 #define CORBA_float_get_max_size(x) ((x)->csize=\
195 (unsigned long)ALIGN_ADDRESS((x)->csize,ORTE_ALIGNOF_CORBA_FLOAT)+ORTE_ALIGNOF_CORBA_FLOAT)
196 #define CORBA_double_get_max_size(x) ((x)->csize=\
197 (unsigned long)ALIGN_ADDRESS((x)->csize,ORTE_ALIGNOF_CORBA_DOUBLE)+ORTE_ALIGNOF_CORBA_DOUBLE)
198 #define CORBA_char_get_max_size(x) ((x)->csize=\
199 (unsigned long)ALIGN_ADDRESS((x)->csize,ORTE_ALIGNOF_CORBA_CHAR)+ORTE_ALIGNOF_CORBA_CHAR)
200 #define CORBA_boolean_get_max_size(x) ((x)->csize=\
201 (unsigned long)ALIGN_ADDRESS((x)->csize,ORTE_ALIGNOF_CORBA_BOOLEAN)+ORTE_ALIGNOF_CORBA_BOOLEAN)
202 #define CORBA_octet_get_max_size(x) ((x)->csize=\
203 (unsigned long)ALIGN_ADDRESS((x)->csize,ORTE_ALIGNOF_CORBA_OCTET)+ORTE_ALIGNOF_CORBA_OCTET)
204 #define CORBA_long_double_get_max_size(x) ((x)->csize=\
205 (unsigned long)ALIGN_ADDRESS((x)->csize,ORTE_ALIGNOF_CORBA_LONG_DOUBLE)+ORTE_ALIGNOF_CORBA_DOUBLE)
206 #define CORBA_string_get_max_size(x,y) \
207 ((x)->csize=(unsigned long)ALIGN_ADDRESS((x)->csize,ORTE_ALIGNOF_CORBA_LONG) + ORTE_ALIGNOF_CORBA_LONG + y + 1)
209 #endif /* !_ORTE_CDR_H_ */