Kannel: Open Source WAP and SMS gateway  svn-r5335
wsencode.h
Go to the documentation of this file.
1 /* ====================================================================
2  * The Kannel Software License, Version 1.0
3  *
4  * Copyright (c) 2001-2018 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.h
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 to store different types of data to
67  * the format, specified by the WMLScript specification.
68  *
69  */
70 
71 #ifndef WSENCODE_H
72 #define WSENCODE_H
73 
74 /********************* Types and defintions *****************************/
75 
76 /* Macros to store and restore integers from data buffers. */
77 
78 #define WS_PUT_UINT8(buf, val) \
79  do { \
80  unsigned char *_p = (buf); \
81  _p[0] = ((val) & 0xff); \
82  } while (0)
83 
84 
85 #define WS_PUT_UINT16(buf, val) \
86  do { \
87  unsigned char *_p = (buf); \
88  _p[0] = (((val) & 0xff00) >> 8); \
89  _p[1] = ((val) & 0xff); \
90  } while (0)
91 
92 #define WS_PUT_UINT32(buf, val) \
93  do { \
94  unsigned char *_p = (buf); \
95  _p[0] = (((val) & 0xff000000) >> 24); \
96  _p[1] = (((val) & 0x00ff0000) >> 16); \
97  _p[2] = (((val) & 0x0000ff00) >> 8); \
98  _p[3] = ((val) & 0x000000ff); \
99  } while (0)
100 
101 #define WS_GET_UINT8(buf, var) \
102  do { \
103  const unsigned char *_p = (buf); \
104  (var) = _p[0]; \
105  } while (0);
106 
107 #define WS_GET_UINT16(buf, var) \
108  do { \
109  const unsigned char *_p = (buf); \
110  WsUInt16 _val; \
111  _val = _p[0]; \
112  _val <<= 8; \
113  _val |= _p[1]; \
114  (var) = _val; \
115  } while (0);
116 
117 #define WS_GET_UINT32(buf, var) \
118  do { \
119  const unsigned char *_p = (buf); \
120  WsUInt32 _val; \
121  _val = _p[0]; \
122  _val <<= 8; \
123  _val |= _p[1]; \
124  _val <<= 8; \
125  _val |= _p[2]; \
126  _val <<= 8; \
127  _val |= _p[3]; \
128  (var) = _val; \
129  } while (0);
130 
131 /* The maximum length of a multi-byte encoded WsUInt32 integer (in
132  bytes). */
133 #define WS_MB_UINT32_MAX_ENCODED_LEN 5
134 
135 /* Type specifiers for the ws_{encode,decode}_buffer() functions. */
136 typedef enum
137 {
138  /* The terminator of the encoding list. This must be the last item
139  in all encoding and decoding function calls. */
141 
142  /* 8 bits of data. The value must be given as `WsByte'. */
144 
145  /* A signed 8 bit integer. The value must be given as `WsInt8'. */
147 
148  /* An unsigned 8 bit integer. The value must be given as `WsUInt8'. */
150 
151  /* A signed 16 bit integer. The value must be given as `WsInt16'. */
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'. */
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 */
174 
175 /********************* Global functions *********************************/
176 
177 /* Encode the unsigned 32 bit integer `value' to the multi-byte format
178  to the buffer `buffer'. The buffer `buffer' must have at least
179  WS_MB_UINT32_MAX_ENCODED_LEN bytes of data. The function returns a
180  pointer, pointing to the beginning of the encoded data. Note that
181  the returned pointer does not necessarily point to the beginning of
182  the buffer `buffer'. The size of the encoded multi-byte value is
183  returned in `len_return'. */
184 unsigned char *ws_encode_mb_uint32(WsUInt32 value, unsigned char *buffer,
185  size_t *len_return);
186 
187 /* Decode a multi-byte encoded unsigned integer from the buffer
188  `buffer'. The function returns the decoded value. The argument
189  `len' must contain the length of the buffer `buffer'. It is set to
190  contain the length of the encoded value in the buffer. The value,
191  stored in `len', can be used to skip the multi-byte encoded value
192  from the buffer `buffer'. */
193 WsUInt32 ws_decode_mb_uint32(const unsigned char *buffer, size_t *len);
194 
195 /* Encode data as specified in the WsEncodingSpec encoded argument
196  list `...' into the buffer `buffer'. The function returns WS_TRUE
197  if the encoding was successful or WS_FALSE otherwise (out of
198  memory). */
199 WsBool ws_encode_buffer(WsBuffer *buffer, ...);
200 
201 /* Decode data from the buffer `buffer', `buffer_len' according to the
202  WsEncodingSpec encoded argument list `...'. The argument list
203  `...' must be encoded as in ws_encode_buffer() but the values must
204  be replaced with pointers to variables of the type. The function
205  returns the number of bytes decoded from the buffer or 0 if the
206  decoding failed. */
207 size_t ws_decode_buffer(const unsigned char *buffer, size_t buffer_len, ...);
208 
209 #endif /* not WSENCODE_H */
unsigned long WsUInt32
Definition: wsint.h:122
WsUInt32 ws_decode_mb_uint32(const unsigned char *buffer, size_t *len)
Definition: wsencode.c:104
WsBool ws_encode_buffer(WsBuffer *buffer,...)
Definition: wsencode.c:123
size_t len
Definition: wsbuffer.h:79
WsBool
Definition: wsint.h:128
WsEncodingSpec
Definition: wsencode.h:136
unsigned char * ws_encode_mb_uint32(WsUInt32 value, unsigned char *buffer, size_t *len_return)
Definition: wsencode.c:79
size_t ws_decode_buffer(const unsigned char *buffer, size_t buffer_len,...)
Definition: wsencode.c:222
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.