Kannel: Open Source WAP and SMS gateway  $Revision: 5037 $
wsencode.h File Reference

Go to the source code of this file.

Macros

#define WS_PUT_UINT8(buf, val)
 
#define WS_PUT_UINT16(buf, val)
 
#define WS_PUT_UINT32(buf, val)
 
#define WS_GET_UINT8(buf, var)
 
#define WS_GET_UINT16(buf, var)
 
#define WS_GET_UINT32(buf, var)
 
#define WS_MB_UINT32_MAX_ENCODED_LEN   5
 

Enumerations

enum  WsEncodingSpec {
  WS_ENC_END, WS_ENC_BYTE, WS_ENC_INT8, WS_ENC_UINT8,
  WS_ENC_INT16, WS_ENC_UINT16, WS_ENC_INT32, WS_ENC_UINT32,
  WS_ENC_MB_UINT16, WS_ENC_MB_UINT32, WS_ENC_DATA
}
 

Functions

unsigned char * ws_encode_mb_uint32 (WsUInt32 value, unsigned char *buffer, size_t *len_return)
 
WsUInt32 ws_decode_mb_uint32 (const unsigned char *buffer, size_t *len)
 
WsBool ws_encode_buffer (WsBuffer *buffer,...)
 
size_t ws_decode_buffer (const unsigned char *buffer, size_t buffer_len,...)
 

Macro Definition Documentation

#define WS_GET_UINT16 (   buf,
  var 
)
Value:
do { \
const unsigned char *_p = (buf); \
WsUInt16 _val; \
_val = _p[0]; \
_val <<= 8; \
_val |= _p[1]; \
(var) = _val; \
} while (0);

Definition at line 107 of file wsencode.h.

Referenced by ws_asm_dasm(), and ws_decode_buffer().

#define WS_GET_UINT32 (   buf,
  var 
)
Value:
do { \
const unsigned char *_p = (buf); \
WsUInt32 _val; \
_val = _p[0]; \
_val <<= 8; \
_val |= _p[1]; \
_val <<= 8; \
_val |= _p[2]; \
_val <<= 8; \
_val |= _p[3]; \
(var) = _val; \
} while (0);

Definition at line 117 of file wsencode.h.

Referenced by ws_decode_buffer().

#define WS_GET_UINT8 (   buf,
  var 
)
Value:
do { \
const unsigned char *_p = (buf); \
(var) = _p[0]; \
} while (0);

Definition at line 101 of file wsencode.h.

Referenced by ws_asm_dasm(), and ws_decode_buffer().

#define WS_MB_UINT32_MAX_ENCODED_LEN   5

Definition at line 133 of file wsencode.h.

Referenced by ws_bc_data_free(), and ws_encode_mb_uint32().

#define WS_PUT_UINT16 (   buf,
  val 
)
Value:
do { \
unsigned char *_p = (buf); \
_p[0] = (((val) & 0xff00) >> 8); \
_p[1] = ((val) & 0xff); \
} while (0)

Definition at line 85 of file wsencode.h.

Referenced by ws_encode_buffer().

#define WS_PUT_UINT32 (   buf,
  val 
)
Value:
do { \
unsigned char *_p = (buf); \
_p[0] = (((val) & 0xff000000) >> 24); \
_p[1] = (((val) & 0x00ff0000) >> 16); \
_p[2] = (((val) & 0x0000ff00) >> 8); \
_p[3] = ((val) & 0x000000ff); \
} while (0)

Definition at line 92 of file wsencode.h.

Referenced by ws_encode_buffer().

#define WS_PUT_UINT8 (   buf,
  val 
)
Value:
do { \
unsigned char *_p = (buf); \
_p[0] = ((val) & 0xff); \
} while (0)

Definition at line 78 of file wsencode.h.

Referenced by ws_bc_encode(), and ws_encode_buffer().

Enumeration Type Documentation

Enumerator
WS_ENC_END 
WS_ENC_BYTE 
WS_ENC_INT8 
WS_ENC_UINT8 
WS_ENC_INT16 
WS_ENC_UINT16 
WS_ENC_INT32 
WS_ENC_UINT32 
WS_ENC_MB_UINT16 
WS_ENC_MB_UINT32 
WS_ENC_DATA 

Definition at line 136 of file wsencode.h.

137 {
138  /* The terminator of the encoding list. This must be the last item
139  in all encoding and decoding function calls. */
140  WS_ENC_END,
141 
142  /* 8 bits of data. The value must be given as `WsByte'. */
143  WS_ENC_BYTE,
144 
145  /* A signed 8 bit integer. The value must be given as `WsInt8'. */
146  WS_ENC_INT8,
147 
148  /* An unsigned 8 bit integer. The value must be given as `WsUInt8'. */
149  WS_ENC_UINT8,
150 
151  /* A signed 16 bit integer. The value must be given as `WsInt16'. */
152  WS_ENC_INT16,
153 
154  /* An unsigned 16 bit integer. The value must be given as `WsUInt16'. */
156 
157  /* A signed 32 bit integer. The value must be given as `WsInt32'. */
158  WS_ENC_INT32,
159 
160  /* An unsigned 32 bit integer. The value must be given as `WsUInt32'. */
162 
163  /* An unsigned 16 bit integer in the multi-byte format. The value
164  must be given as `WsUInt16'. */
166 
167  /* An unsigned 32 bit integer in the multi-byte format. The value
168  must be given as `WsUInt32'. */
170 
171  /* Binary data specified with two arguments: unsigned char *, size_t */
WsEncodingSpec
Definition: wsencode.h:136

Function Documentation

size_t ws_decode_buffer ( const unsigned char *  buffer,
size_t  buffer_len,
  ... 
)

Definition at line 222 of file wsencode.c.

References WsBufferRec::len, ws_decode_mb_uint32(), WS_ENC_BYTE, WS_ENC_DATA, WS_ENC_END, WS_ENC_INT16, WS_ENC_INT32, WS_ENC_INT8, WS_ENC_MB_UINT16, WS_ENC_MB_UINT32, WS_ENC_UINT16, WS_ENC_UINT32, WS_ENC_UINT8, ws_fatal(), WS_GET_UINT16, WS_GET_UINT32, and WS_GET_UINT8.

Referenced by ws_bc_decode().

223 {
224  va_list ap;
225  WsEncodingSpec spec;
226  WsUInt8 *i8p;
227  WsUInt16 *i16p;
228  WsUInt32 *i32p;
229  unsigned char **cpp;
230  size_t len;
231  size_t orig_buffer_len = buffer_len;
232 
233  va_start(ap, buffer_len);
234 
235  while ((spec = va_arg(ap, int)) != WS_ENC_END) {
236  switch (spec) {
237  case WS_ENC_BYTE:
238  case WS_ENC_INT8:
239  case WS_ENC_UINT8:
240  if (buffer_len < 1)
241  goto too_short_buffer;
242 
243  i8p = va_arg(ap, WsUInt8 *);
244  WS_GET_UINT8(buffer, *i8p);
245 
246  buffer++;
247  buffer_len--;
248  break;
249 
250  case WS_ENC_INT16:
251  case WS_ENC_UINT16:
252  if (buffer_len < 2)
253  goto too_short_buffer;
254 
255  i16p = va_arg(ap, WsUInt16 *);
256  WS_GET_UINT16(buffer, *i16p);
257 
258  buffer += 2;
259  buffer_len -= 2;
260  break;
261 
262  case WS_ENC_INT32:
263  case WS_ENC_UINT32:
264  if (buffer_len < 4)
265  goto too_short_buffer;
266 
267  i32p = va_arg(ap, WsUInt32 *);
268  WS_GET_UINT32(buffer, *i32p);
269 
270  buffer += 4;
271  buffer_len -= 4;
272  break;
273 
274  case WS_ENC_MB_UINT16:
275  case WS_ENC_MB_UINT32:
276  {
277  size_t len = buffer_len;
278  WsUInt32 i32;
279 
280  if (buffer_len < 1)
281  goto too_short_buffer;
282 
283  i32 = ws_decode_mb_uint32(buffer, &len);
284 
285  if (spec == WS_ENC_MB_UINT16) {
286  i16p = va_arg(ap, WsUInt16 *);
287  *i16p = i32;
288  } else {
289  i32p = va_arg(ap, WsUInt32 *);
290  *i32p = i32;
291  }
292 
293  buffer += len;
294  buffer_len -= len;
295  }
296  break;
297 
298  case WS_ENC_DATA:
299  cpp = va_arg(ap, unsigned char **);
300  len = va_arg(ap, size_t);
301 
302  if (buffer_len < len)
303  goto too_short_buffer;
304 
305  *cpp = (unsigned char *) buffer;
306  buffer += len;
307  buffer_len -= len;
308  break;
309 
310  default:
311  ws_fatal("ws_encode_buffer(): unknown type %d: probably a missing "
312  "WS_ENC_END",
313  spec);
314  break;
315  }
316  }
317  va_end(ap);
318 
319  return orig_buffer_len - buffer_len;
320 
321 too_short_buffer:
322 
323  va_end(ap);
324 
325  return 0;
326 }
#define WS_GET_UINT32(buf, var)
Definition: wsencode.h:117
void ws_fatal(char *fmt,...)
Definition: wserror.c:91
#define WS_GET_UINT16(buf, var)
Definition: wsencode.h:107
WsUInt32 ws_decode_mb_uint32(const unsigned char *buffer, size_t *len)
Definition: wsencode.c:104
unsigned long WsUInt32
Definition: wsint.h:122
#define WS_GET_UINT8(buf, var)
Definition: wsencode.h:101
unsigned char WsUInt8
Definition: wsint.h:116
unsigned short WsUInt16
Definition: wsint.h:119
WsEncodingSpec
Definition: wsencode.h:136
WsUInt32 ws_decode_mb_uint32 ( const unsigned char *  buffer,
size_t *  len 
)

Definition at line 104 of file wsencode.c.

References WsBufferRec::len, WS_MB_CONT_BIT, and WS_MB_DATA_MASK.

Referenced by ws_bc_data_free(), and ws_decode_buffer().

105 {
106  WsUInt32 value = 0;
107  size_t i;
108 
109  for (i = 0; i < *len; i++) {
110  value <<= 7;
111  value |= buffer[i] & WS_MB_DATA_MASK;
112 
113  if ((buffer[i] & WS_MB_CONT_BIT) == 0)
114  break;
115  }
116 
117  *len = i + 1;
118 
119  return value;
120 }
unsigned long WsUInt32
Definition: wsint.h:122
#define WS_MB_DATA_MASK
Definition: wsencode.c:75
#define WS_MB_CONT_BIT
Definition: wsencode.c:74
WsBool ws_encode_buffer ( WsBuffer buffer,
  ... 
)

Definition at line 123 of file wsencode.c.

References WsBufferRec::data, error(), WsBufferRec::len, ws_buffer_append_space(), WS_ENC_BYTE, WS_ENC_DATA, WS_ENC_END, WS_ENC_INT16, WS_ENC_INT32, WS_ENC_INT8, WS_ENC_MB_UINT16, WS_ENC_MB_UINT32, WS_ENC_UINT16, WS_ENC_UINT32, WS_ENC_UINT8, ws_encode_mb_uint32(), WS_FALSE, ws_fatal(), WS_PUT_UINT16, WS_PUT_UINT32, WS_PUT_UINT8, and WS_TRUE.

Referenced by ws_asm_linearize(), and ws_bc_encode().

124 {
125  va_list ap;
126  WsEncodingSpec spec;
127  int ival;
128  unsigned char *p;
129  unsigned char *cp;
130  size_t len;
131  WsUInt32 ui32;
132  unsigned char data[64];
133 
134  va_start(ap, buffer);
135 
136  while ((spec = va_arg(ap, int)) != WS_ENC_END)
137  {
138  switch (spec)
139  {
140  case WS_ENC_BYTE:
141  case WS_ENC_INT8:
142  case WS_ENC_UINT8:
143  ival = va_arg(ap, int);
144 
145  if (!ws_buffer_append_space(buffer, &p, 1))
146  goto error;
147 
148  WS_PUT_UINT8(p, ival);
149  break;
150 
151  case WS_ENC_INT16:
152  case WS_ENC_UINT16:
153  ival = va_arg(ap, int);
154 
155  if (!ws_buffer_append_space(buffer, &p, 2))
156  goto error;
157 
158  WS_PUT_UINT16(p, ival);
159  break;
160 
161  case WS_ENC_INT32:
162  case WS_ENC_UINT32:
163  ival = va_arg(ap, long);
164 
165  if (!ws_buffer_append_space(buffer, &p, 4))
166  goto error;
167 
168  WS_PUT_UINT32(p, ival);
169  break;
170 
171  case WS_ENC_MB_UINT16:
172  case WS_ENC_MB_UINT32:
173  if (spec == WS_ENC_MB_UINT16)
174  ui32 = va_arg(ap, int);
175  else
176  ui32 = va_arg(ap, long);
177 
178  len = sizeof(data);
179  cp = ws_encode_mb_uint32(ui32, data, &len);
180 
181  if (!ws_buffer_append_space(buffer, &p, len))
182  goto error;
183 
184  memcpy(p, cp, len);
185  break;
186 
187  case WS_ENC_DATA:
188  cp = va_arg(ap, unsigned char *);
189  len = va_arg(ap, unsigned int);
190 
191  if (!ws_buffer_append_space(buffer, &p, len))
192  goto error;
193 
194  memcpy(p, cp, len);
195  break;
196 
197  default:
198  ws_fatal("ws_encode_buffer(): unknown type %d: probably a missing "
199  "WS_ENC_END",
200  spec);
201  break;
202  }
203  }
204 
205  va_end(ap);
206 
207  return WS_TRUE;
208 
209 
210  /*
211  * Error handling.
212  */
213 
214 error:
215 
216  va_end(ap);
217 
218  return WS_FALSE;
219 }
void error(int err, const char *fmt,...)
Definition: log.c:612
void ws_fatal(char *fmt,...)
Definition: wserror.c:91
#define WS_PUT_UINT8(buf, val)
Definition: wsencode.h:78
Definition: wsint.h:131
unsigned long WsUInt32
Definition: wsint.h:122
#define WS_PUT_UINT16(buf, val)
Definition: wsencode.h:85
#define WS_PUT_UINT32(buf, val)
Definition: wsencode.h:92
unsigned char * ws_encode_mb_uint32(WsUInt32 value, unsigned char *buffer, size_t *len_return)
Definition: wsencode.c:79
WsBool ws_buffer_append_space(WsBuffer *buffer, unsigned char **p, size_t size)
Definition: wsbuffer.c:115
WsEncodingSpec
Definition: wsencode.h:136
unsigned char* ws_encode_mb_uint32 ( WsUInt32  value,
unsigned char *  buffer,
size_t *  len_return 
)

Definition at line 79 of file wsencode.c.

References WsBufferRec::len, WS_MB_CONT_BIT, WS_MB_DATA_MASK, and WS_MB_UINT32_MAX_ENCODED_LEN.

Referenced by ws_bc_encode(), and ws_encode_buffer().

81 {
82  unsigned char *p = buffer + WS_MB_UINT32_MAX_ENCODED_LEN;
83  size_t len = 1;
84 
85  /* Set the terminator byte. */
86  *p = value & WS_MB_DATA_MASK;
87  value >>= 7;
88  p--;
89 
90  /* And add the data. */
91  while (value > 0) {
92  *p = (value & WS_MB_DATA_MASK) | WS_MB_CONT_BIT;
93  value >>= 7;
94  p--;
95  len++;
96  }
97 
98  *len_return = len;
99 
100  return p + 1;
101 }
#define WS_MB_DATA_MASK
Definition: wsencode.c:75
#define WS_MB_UINT32_MAX_ENCODED_LEN
Definition: wsencode.h:133
#define WS_MB_CONT_BIT
Definition: wsencode.c:74
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.