Kannel: Open Source WAP and SMS gateway  $Revision: 5037 $
wsencode.c
Go to the documentation of this file.
1 /* ====================================================================
2  * The Kannel Software License, Version 1.0
3  *
4  * Copyright (c) 2001-2016 Kannel Group
5  * Copyright (c) 1998-2001 WapIT Ltd.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Kannel Group (http://www.kannel.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Kannel" and "Kannel Group" must not be used to
28  * endorse or promote products derived from this software without
29  * prior written permission. For written permission, please
30  * contact org@kannel.org.
31  *
32  * 5. Products derived from this software may not be called "Kannel",
33  * nor may "Kannel" appear in their name, without prior written
34  * permission of the Kannel Group.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE KANNEL GROUP OR ITS CONTRIBUTORS
40  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
41  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
42  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
43  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
44  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
45  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
46  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of the Kannel Group. For more information on
51  * the Kannel Group, please see <http://www.kannel.org/>.
52  *
53  * Portions of this software are based upon software originally written at
54  * WapIT Ltd., Helsinki, Finland for the Kannel project.
55  */
56 
57 /*
58  *
59  * wsencode.c
60  *
61  * Author: Markku Rossi <mtr@iki.fi>
62  *
63  * Copyright (c) 1999-2000 WAPIT OY LTD.
64  * All rights reserved.
65  *
66  * Encoding and decoding routines.
67  *
68  */
69 
70 #include "wsint.h"
71 
72 /********************* Types and definitions ****************************/
73 
74 #define WS_MB_CONT_BIT 0x80
75 #define WS_MB_DATA_MASK 0x7f
76 
77 /********************* Global functions *********************************/
78 
79 unsigned char *ws_encode_mb_uint32(WsUInt32 value, unsigned char *buffer,
80  size_t *len_return)
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 }
102 
103 
104 WsUInt32 ws_decode_mb_uint32(const unsigned char *buffer, size_t *len)
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 }
121 
122 
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 }
220 
221 
222 size_t ws_decode_buffer(const unsigned char *buffer, size_t buffer_len, ...)
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 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
#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
Definition: wsint.h:131
unsigned long WsUInt32
Definition: wsint.h:122
#define WS_MB_DATA_MASK
Definition: wsencode.c:75
#define WS_PUT_UINT16(buf, val)
Definition: wsencode.h:85
#define WS_PUT_UINT32(buf, val)
Definition: wsencode.h:92
#define WS_GET_UINT8(buf, var)
Definition: wsencode.h:101
#define WS_MB_UINT32_MAX_ENCODED_LEN
Definition: wsencode.h:133
size_t ws_decode_buffer(const unsigned char *buffer, size_t buffer_len,...)
Definition: wsencode.c:222
#define WS_MB_CONT_BIT
Definition: wsencode.c:74
unsigned char * ws_encode_mb_uint32(WsUInt32 value, unsigned char *buffer, size_t *len_return)
Definition: wsencode.c:79
unsigned char WsUInt8
Definition: wsint.h:116
unsigned short WsUInt16
Definition: wsint.h:119
size_t len
Definition: wsbuffer.h:79
WsBool ws_encode_buffer(WsBuffer *buffer,...)
Definition: wsencode.c:123
WsBool
Definition: wsint.h:128
WsBool ws_buffer_append_space(WsBuffer *buffer, unsigned char **p, size_t size)
Definition: wsbuffer.c:115
WsEncodingSpec
Definition: wsencode.h:136
unsigned char * data
Definition: wsbuffer.h:80
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.