Kannel: Open Source WAP and SMS gateway  $Revision: 5037 $
wsp_strings.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 /* wsp_strings.c: lookup code for various tables defined by WSP standard
58  *
59  * This file provides functions to translate strings to numbers and numbers
60  * to strings according to the Assigned Numbers tables in appendix A
61  * of the WSP specification.
62  *
63  * The tables are in wsp_strings.def, in a special format suitable for
64  * use with the C preprocessor, which we abuse liberally to get the
65  * interface we want.
66  *
67  * Richard Braakman
68  */
69 
70 #include "gwlib/gwlib.h"
71 #include "wsp_strings.h"
72 
73 #define TABLE_SIZE(table) ((long)(sizeof(table) / sizeof(table[0])))
74 
75 static int initialized;
76 
77 /* The arrays in a table structure are all of equal length, and their
78  * elements correspond. The number for string 0 is in numbers[0], etc.
79  * Table structures are initialized dynamically.
80  */
81 struct table
82 {
83  long size; /* Nr of entries in each of the tables below */
84  Octstr **strings; /* Immutable octstrs */
85  long *numbers; /* Assigned numbers, or NULL for linear tables */
86  int *versions; /* WSP Encoding-versions, or NULL if non-versioned */
87  int linear; /* True for tables defined as LINEAR */
88 };
89 
91 {
92  char *str;
93  long number;
94  int version;
95 };
96 
98 {
99  char *str;
100  int version;
101 };
102 
103 /* Local functions */
104 static Octstr *number_to_string(long number, struct table *table);
105 static unsigned char *number_to_cstr(long number, struct table *table);
106 static long string_to_number(Octstr *ostr, struct table *table);
107 static long string_to_versioned_number(Octstr *ostr, struct table *table, int version);
108 
109 
110 /* Declare the data. For each table "foo", create a foo_strings array
111  * to hold the data, and a (still empty) foo_table structure. */
112 #define LINEAR(name, strings) \
113  static const struct linear_element name##_strings[] = { strings }; \
114  static struct table name##_table;
115 #define STRING(string) { string, 0 },
116 #define VSTRING(version, string) { string, version },
117 #define NUMBERED(name, strings) \
118  static const struct numbered_element name##_strings[] = { strings }; \
119  static struct table name##_table;
120 #define ASSIGN(string, number) { string, number, 0 },
121 #define VASSIGN(version, string, number) { string, number, version },
122 #include "wsp_strings.def"
123 
124 /* Define the functions for translating number to Octstr */
125 #define LINEAR(name, strings) \
126 Octstr *wsp_##name##_to_string(long number) { \
127  return number_to_string(number, &name##_table); \
128 }
129 #include "wsp_strings.def"
130 
131 /* Define the functions for translating number to constant string */
132 #define LINEAR(name, strings) \
133 unsigned char *wsp_##name##_to_cstr(long number) { \
134  return number_to_cstr(number, &name##_table); \
135 }
136 #include "wsp_strings.def"
137 
138 #define LINEAR(name, strings) \
139 long wsp_string_to_##name(Octstr *ostr) { \
140  return string_to_number(ostr, &name##_table); \
141 }
142 #include "wsp_strings.def"
143 
144 #define LINEAR(name, strings) \
145 long wsp_string_to_versioned_##name(Octstr *ostr, int version) { \
146  return string_to_versioned_number(ostr, &name##_table, version); \
147 }
148 #include "wsp_strings.def"
149 
150 static Octstr *number_to_string(long number, struct table *table)
151 {
152  long i;
153 
155 
156  if (table->linear) {
157  if (number >= 0 && number < table->size)
158  return octstr_duplicate(table->strings[number]);
159  } else {
160  for (i = 0; i < table->size; i++) {
161  if (table->numbers[i] == number)
162  return octstr_duplicate(table->strings[i]);
163  }
164  }
165  return NULL;
166 }
167 
168 static unsigned char *number_to_cstr(long number, struct table *table)
169 {
170  long i;
171 
173 
174  if (table->linear) {
175  if (number >= 0 && number < table->size)
176  return (unsigned char *)octstr_get_cstr(table->strings[number]);
177  } else {
178  for (i = 0; i < table->size; i++) {
179  if (table->numbers[i] == number)
180  return (unsigned char *)octstr_get_cstr(table->strings[i]);
181  }
182  }
183  return NULL;
184 }
185 
186 /* Case-insensitive string lookup */
187 static long string_to_number(Octstr *ostr, struct table *table)
188 {
189  long i;
190 
192 
193  for (i = 0; i < table->size; i++) {
194  if (octstr_case_compare(ostr, table->strings[i]) == 0) {
195  return table->linear ? i : table->numbers[i];
196  }
197  }
198 
199  return -1;
200 }
201 
202 /* Case-insensitive string lookup according to passed WSP encoding version */
203 static long string_to_versioned_number(Octstr *ostr, struct table *table,
204  int version)
205 {
206  long i, ret;
207 
209 
210  /* walk the whole table and pick the highest versioned token */
211  ret = -1;
212  for (i = 0; i < table->size; i++) {
213  if (octstr_case_compare(ostr, table->strings[i]) == 0 &&
214  table->versions[i] <= version) {
215  ret = table->linear ? i : table->numbers[i];
216  }
217  }
218 
219  debug("wsp.strings",0,"WSP: Mapping `%s', WSP 1.%d to 0x%04lx.",
220  octstr_get_cstr(ostr), version, ret);
221 
222  return ret;
223 }
224 
225 static void construct_linear_table(struct table *table, const struct linear_element *strings,
226  long size)
227 {
228  long i;
229 
230  table->size = size;
231  table->strings = gw_malloc(size * (sizeof table->strings[0]));
232  table->numbers = NULL;
233  table->versions = gw_malloc(size * (sizeof table->versions[0]));
234  table->linear = 1;
235 
236  for (i = 0; i < size; i++) {
237  table->strings[i] = octstr_imm(strings[i].str);
238  table->versions[i] = strings[i].version;
239  }
240 }
241 
242 static void construct_numbered_table(struct table *table, const struct numbered_element *strings,
243  long size)
244 {
245  long i;
246 
247  table->size = size;
248  table->strings = gw_malloc(size * (sizeof table->strings[0]));
249  table->numbers = gw_malloc(size * (sizeof table->numbers[0]));
250  table->versions = gw_malloc(size * (sizeof table->versions[0]));
251  table->linear = 0;
252 
253  for (i = 0; i < size; i++) {
254  table->strings[i] = octstr_imm(strings[i].str);
255  table->numbers[i] = strings[i].number;
256  table->versions[i] = strings[i].version;
257  }
258 }
259 
260 static void destroy_table(struct table *table)
261 {
262  /* No need to call octstr_destroy on immutable octstrs */
263 
264  gw_free(table->strings);
265  gw_free(table->numbers);
266  gw_free(table->versions);
267 }
268 
270 {
271  if (initialized > 0) {
272  initialized++;
273  return;
274  }
275 
276 #define LINEAR(name, strings) \
277  construct_linear_table(&name##_table, \
278  name##_strings, TABLE_SIZE(name##_strings));
279 #define NUMBERED(name, strings) \
280  construct_numbered_table(&name##_table, \
281  name##_strings, TABLE_SIZE(name##_strings));
282 #include "wsp_strings.def"
283  initialized++;
284 }
285 
287 {
288  /* If we were initialized more than once, then wait for more than
289  * one shutdown. */
290  if (initialized > 1) {
291  initialized--;
292  return;
293  }
294 
295 #define LINEAR(name, strings) \
296  destroy_table(&name##_table);
297 #include "wsp_strings.def"
298 
299  initialized = 0;
300 }
int * versions
Definition: wsp_strings.c:86
int number
Definition: smsc_cimd2.c:213
long * numbers
Definition: wsp_strings.c:85
int size
Definition: wsasm.c:84
static Octstr * number_to_string(long number, struct table *table)
Definition: wsp_strings.c:150
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
static void destroy_table(struct table *table)
Definition: wsp_strings.c:260
long size
Definition: wsp_strings.c:83
Octstr * octstr_imm(const char *cstr)
Definition: octstr.c:281
#define octstr_duplicate(ostr)
Definition: octstr.h:187
int octstr_case_compare(const Octstr *os1, const Octstr *os2)
Definition: octstr.c:901
gw_assert(wtls_machine->packet_to_send!=NULL)
static int initialized
Definition: wsp_strings.c:75
Octstr ** strings
Definition: wsp_strings.c:84
static long string_to_number(Octstr *ostr, struct table *table)
Definition: wsp_strings.c:187
static unsigned char * number_to_cstr(long number, struct table *table)
Definition: wsp_strings.c:168
Definition: octstr.c:118
void wsp_strings_shutdown(void)
Definition: wsp_strings.c:286
void wsp_strings_init(void)
Definition: wsp_strings.c:269
static long string_to_versioned_number(Octstr *ostr, struct table *table, int version)
Definition: wsp_strings.c:203
void debug(const char *place, int err, const char *fmt,...)
Definition: log.c:690
int linear
Definition: wsp_strings.c:87
static void construct_linear_table(struct table *table, const struct linear_element *strings, long size)
Definition: wsp_strings.c:225
static void construct_numbered_table(struct table *table, const struct numbered_element *strings, long size)
Definition: wsp_strings.c:242
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.