Kannel: Open Source WAP and SMS gateway  svn-r5335
wsp_strings.c File Reference
#include "gwlib/gwlib.h"
#include "wsp_strings.h"
#include "wsp_strings.def"

Go to the source code of this file.

Data Structures

struct  table
 
struct  numbered_element
 
struct  linear_element
 

Macros

#define TABLE_SIZE(table)   ((long)(sizeof(table) / sizeof(table[0])))
 
#define LINEAR(name, strings)
 
#define STRING(string)   { string, 0 },
 
#define VSTRING(version, string)   { string, version },
 
#define NUMBERED(name, strings)
 
#define ASSIGN(string, number)   { string, number, 0 },
 
#define VASSIGN(version, string, number)   { string, number, version },
 
#define LINEAR(name, strings)
 
#define LINEAR(name, strings)
 
#define LINEAR(name, strings)
 
#define LINEAR(name, strings)
 
#define LINEAR(name, strings)
 
#define NUMBERED(name, strings)
 
#define LINEAR(name, strings)   destroy_table(&name##_table);
 

Functions

static Octstrnumber_to_string (long number, struct table *table)
 
static unsigned char * number_to_cstr (long number, struct table *table)
 
static long string_to_number (Octstr *ostr, struct table *table)
 
static long string_to_versioned_number (Octstr *ostr, struct table *table, int version)
 
static void construct_linear_table (struct table *table, const struct linear_element *strings, long size)
 
static void construct_numbered_table (struct table *table, const struct numbered_element *strings, long size)
 
static void destroy_table (struct table *table)
 
void wsp_strings_init (void)
 
void wsp_strings_shutdown (void)
 

Variables

static int initialized
 

Macro Definition Documentation

◆ ASSIGN

#define ASSIGN (   string,
  number 
)    { string, number, 0 },

Definition at line 120 of file wsp_strings.c.

◆ LINEAR [1/7]

#define LINEAR (   name,
  strings 
)
Value:
static const struct linear_element name##_strings[] = { strings }; \
static struct table name##_table;
char * name
Definition: smsc_cimd2.c:212

Definition at line 144 of file wsp_strings.c.

◆ LINEAR [2/7]

#define LINEAR (   name,
  strings 
)
Value:
Octstr *wsp_##name##_to_string(long number) { \
return number_to_string(number, &name##_table); \
}
int number
Definition: smsc_cimd2.c:213
static Octstr * number_to_string(long number, struct table *table)
Definition: wsp_strings.c:150
char * name
Definition: smsc_cimd2.c:212
Definition: octstr.c:118

Definition at line 144 of file wsp_strings.c.

◆ LINEAR [3/7]

#define LINEAR (   name,
  strings 
)
Value:
unsigned char *wsp_##name##_to_cstr(long number) { \
return number_to_cstr(number, &name##_table); \
}
int number
Definition: smsc_cimd2.c:213
char * name
Definition: smsc_cimd2.c:212
static unsigned char * number_to_cstr(long number, struct table *table)
Definition: wsp_strings.c:168

Definition at line 144 of file wsp_strings.c.

◆ LINEAR [4/7]

#define LINEAR (   name,
  strings 
)
Value:
long wsp_string_to_##name(Octstr *ostr) { \
return string_to_number(ostr, &name##_table); \
}
char * name
Definition: smsc_cimd2.c:212
static long string_to_number(Octstr *ostr, struct table *table)
Definition: wsp_strings.c:187
Definition: octstr.c:118

Definition at line 144 of file wsp_strings.c.

◆ LINEAR [5/7]

#define LINEAR (   name,
  strings 
)
Value:
long wsp_string_to_versioned_##name(Octstr *ostr, int version) { \
return string_to_versioned_number(ostr, &name##_table, version); \
}
char * name
Definition: smsc_cimd2.c:212
Definition: octstr.c:118
static long string_to_versioned_number(Octstr *ostr, struct table *table, int version)
Definition: wsp_strings.c:203

Definition at line 144 of file wsp_strings.c.

◆ LINEAR [6/7]

#define LINEAR (   name,
  strings 
)
Value:
name##_strings, TABLE_SIZE(name##_strings));
#define TABLE_SIZE(table)
Definition: wsp_strings.c:73
char * name
Definition: smsc_cimd2.c:212
static void construct_linear_table(struct table *table, const struct linear_element *strings, long size)
Definition: wsp_strings.c:225

Definition at line 144 of file wsp_strings.c.

◆ LINEAR [7/7]

#define LINEAR (   name,
  strings 
)    destroy_table(&name##_table);

Definition at line 144 of file wsp_strings.c.

◆ NUMBERED [1/2]

#define NUMBERED (   name,
  strings 
)
Value:
static const struct numbered_element name##_strings[] = { strings }; \
static struct table name##_table;
char * name
Definition: smsc_cimd2.c:212

Definition at line 117 of file wsp_strings.c.

◆ NUMBERED [2/2]

#define NUMBERED (   name,
  strings 
)
Value:
name##_strings, TABLE_SIZE(name##_strings));
#define TABLE_SIZE(table)
Definition: wsp_strings.c:73
char * name
Definition: smsc_cimd2.c:212
static void construct_numbered_table(struct table *table, const struct numbered_element *strings, long size)
Definition: wsp_strings.c:242

Definition at line 117 of file wsp_strings.c.

◆ STRING

#define STRING (   string)    { string, 0 },

Definition at line 115 of file wsp_strings.c.

◆ TABLE_SIZE

#define TABLE_SIZE (   table)    ((long)(sizeof(table) / sizeof(table[0])))

Definition at line 73 of file wsp_strings.c.

◆ VASSIGN

#define VASSIGN (   version,
  string,
  number 
)    { string, number, version },

Definition at line 121 of file wsp_strings.c.

◆ VSTRING

#define VSTRING (   version,
  string 
)    { string, version },

Definition at line 116 of file wsp_strings.c.

Function Documentation

◆ construct_linear_table()

static void construct_linear_table ( struct table table,
const struct linear_element strings,
long  size 
)
static

Definition at line 225 of file wsp_strings.c.

References table::linear, table::numbers, octstr_imm(), table::size, size, table::strings, linear_element::version, and table::versions.

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 }
int * versions
Definition: wsp_strings.c:86
long * numbers
Definition: wsp_strings.c:85
int size
Definition: wsasm.c:84
long size
Definition: wsp_strings.c:83
Octstr * octstr_imm(const char *cstr)
Definition: octstr.c:283
Octstr ** strings
Definition: wsp_strings.c:84
int linear
Definition: wsp_strings.c:87

◆ construct_numbered_table()

static void construct_numbered_table ( struct table table,
const struct numbered_element strings,
long  size 
)
static

Definition at line 242 of file wsp_strings.c.

References table::linear, numbered_element::number, table::numbers, octstr_imm(), table::size, size, table::strings, numbered_element::version, and table::versions.

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 }
int * versions
Definition: wsp_strings.c:86
long * numbers
Definition: wsp_strings.c:85
int size
Definition: wsasm.c:84
long size
Definition: wsp_strings.c:83
Octstr * octstr_imm(const char *cstr)
Definition: octstr.c:283
Octstr ** strings
Definition: wsp_strings.c:84
int linear
Definition: wsp_strings.c:87

◆ destroy_table()

static void destroy_table ( struct table table)
static

Definition at line 260 of file wsp_strings.c.

References table::numbers, table::strings, and table::versions.

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 }
int * versions
Definition: wsp_strings.c:86
long * numbers
Definition: wsp_strings.c:85
Octstr ** strings
Definition: wsp_strings.c:84

◆ number_to_cstr()

static unsigned char * number_to_cstr ( long  number,
struct table table 
)
static

Definition at line 168 of file wsp_strings.c.

References gw_assert(), initialized, table::linear, number, table::numbers, octstr_get_cstr, table::size, size, and table::strings.

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 }
int number
Definition: smsc_cimd2.c:213
long * numbers
Definition: wsp_strings.c:85
int size
Definition: wsasm.c:84
gw_assert(wtls_machine->packet_to_send !=NULL)
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
long size
Definition: wsp_strings.c:83
static int initialized
Definition: wsp_strings.c:75
Octstr ** strings
Definition: wsp_strings.c:84
int linear
Definition: wsp_strings.c:87

◆ number_to_string()

static Octstr * number_to_string ( long  number,
struct table table 
)
static

Definition at line 150 of file wsp_strings.c.

References gw_assert(), initialized, table::linear, number, table::numbers, octstr_duplicate, table::size, size, and table::strings.

151 {
152  long i;
153 
155 
156  if (table->linear) {
157  if (number >= 0 && number < table->size)
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 }
int number
Definition: smsc_cimd2.c:213
long * numbers
Definition: wsp_strings.c:85
int size
Definition: wsasm.c:84
gw_assert(wtls_machine->packet_to_send !=NULL)
long size
Definition: wsp_strings.c:83
#define octstr_duplicate(ostr)
Definition: octstr.h:187
static int initialized
Definition: wsp_strings.c:75
Octstr ** strings
Definition: wsp_strings.c:84
int linear
Definition: wsp_strings.c:87

◆ string_to_number()

static long string_to_number ( Octstr ostr,
struct table table 
)
static

Definition at line 187 of file wsp_strings.c.

References gw_assert(), initialized, table::linear, table::numbers, octstr_case_compare(), table::size, and table::strings.

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 }
long * numbers
Definition: wsp_strings.c:85
gw_assert(wtls_machine->packet_to_send !=NULL)
long size
Definition: wsp_strings.c:83
int octstr_case_compare(const Octstr *os1, const Octstr *os2)
Definition: octstr.c:903
static int initialized
Definition: wsp_strings.c:75
Octstr ** strings
Definition: wsp_strings.c:84
int linear
Definition: wsp_strings.c:87

◆ string_to_versioned_number()

static long string_to_versioned_number ( Octstr ostr,
struct table table,
int  version 
)
static

Definition at line 203 of file wsp_strings.c.

References debug(), gw_assert(), initialized, table::linear, table::numbers, octstr_case_compare(), octstr_get_cstr, table::size, table::strings, and table::versions.

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 }
int * versions
Definition: wsp_strings.c:86
long * numbers
Definition: wsp_strings.c:85
gw_assert(wtls_machine->packet_to_send !=NULL)
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
long size
Definition: wsp_strings.c:83
int octstr_case_compare(const Octstr *os1, const Octstr *os2)
Definition: octstr.c:903
static int initialized
Definition: wsp_strings.c:75
Octstr ** strings
Definition: wsp_strings.c:84
void debug(const char *place, int err, const char *fmt,...)
Definition: log.c:726
int linear
Definition: wsp_strings.c:87

◆ wsp_strings_init()

void wsp_strings_init ( void  )

Definition at line 269 of file wsp_strings.c.

References initialized.

Referenced by main(), wsp_session_init(), and wsp_unit_init().

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 }
static int initialized
Definition: wsp_strings.c:75

◆ wsp_strings_shutdown()

void wsp_strings_shutdown ( void  )

Definition at line 286 of file wsp_strings.c.

References initialized.

Referenced by main(), wsp_session_shutdown(), and wsp_unit_shutdown().

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 }
static int initialized
Definition: wsp_strings.c:75

Variable Documentation

◆ initialized

See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.