Kannel: Open Source WAP and SMS gateway  svn-r5335
numhash.c File Reference
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include "gwlib/gwlib.h"
#include "numhash.h"

Go to the source code of this file.

Data Structures

struct  numhash_table
 
struct  numhash_number
 
struct  nh_entry
 
struct  numhashes
 

Macros

#define NUMHASH_AUTO_HASH   -1
 

Functions

static int add_item (Numhash *table, struct numhash_number *nro)
 
static int numhash_add_number (Numhash *table, char *nro)
 
static Numhashnumhash_init (int max_numbers, int hash_size)
 
int numhash_find_number (Numhash *table, Octstr *nro)
 
int numhash_find_key (Numhash *table, long long key)
 
long long numhash_get_key (Octstr *nro)
 
long long numhash_get_char_key (char *nro)
 
void numhash_destroy (Numhash *table)
 
double numhash_hash_fill (Numhash *table, int *longest)
 
int numhash_size (Numhash *table)
 
Numhashnumhash_create (const char *seek_url)
 

Variables

static int primes []
 
static int precision = 19
 

Macro Definition Documentation

◆ NUMHASH_AUTO_HASH

#define NUMHASH_AUTO_HASH   -1

Definition at line 79 of file numhash.c.

Referenced by numhash_create(), and numhash_init().

Function Documentation

◆ add_item()

static int add_item ( Numhash table,
struct numhash_number nro 
)
static

Definition at line 122 of file numhash.c.

References numhash_number::key, numhash_number::next, and warning().

Referenced by numhash_add_number().

123 {
124  if (table->hash[nro->key % table->hash_size]) { /* conflict */
125  struct numhash_number *ptr = table->hash[nro->key % table->hash_size];
126 
127  if (ptr->key == nro->key)
128  goto duplicate;
129 
130  while(ptr->next) {
131  ptr = ptr->next;
132  if (ptr->key == nro->key)
133  goto duplicate;
134  }
135  ptr->next = nro; /* put as last of the linkage */
136  } else
137  table->hash[nro->key % table->hash_size] = nro;
138 
139  return 0;
140 
141 duplicate:
142  warning(0, "Duplicate number %lld!", nro->key);
143  return -1;
144 }
struct numhash_number * next
Definition: numhash.c:102
long long key
Definition: numhash.c:101
void warning(int err, const char *fmt,...)
Definition: log.c:660

◆ numhash_add_number()

static int numhash_add_number ( Numhash table,
char *  nro 
)
static

Definition at line 149 of file numhash.c.

References add_item(), error(), numhash_number::key, numhash_number::next, table::numbers, and numhash_get_char_key().

Referenced by numhash_create().

150 {
151  struct numhash_number *newnro;
152 
153  if (table->number_total == table->table_size) {
154  error(0, "Table limit %ld reached, cannot add %s!",
155  table->table_size, nro);
156  return -1;
157  }
158  newnro = &table->numbers[table->number_total++]; /* take the next free */
159 
160  newnro->key = numhash_get_char_key(nro);
161  newnro->next = NULL;
162 
163  add_item(table, newnro);
164 
165  return 0;
166 }
void error(int err, const char *fmt,...)
Definition: log.c:648
long * numbers
Definition: wsp_strings.c:85
struct numhash_number * next
Definition: numhash.c:102
long long key
Definition: numhash.c:101
long long numhash_get_char_key(char *nro)
Definition: numhash.c:257
static int add_item(Numhash *table, struct numhash_number *nro)
Definition: numhash.c:122

◆ numhash_create()

Numhash* numhash_create ( const char *  seek_url)

Definition at line 313 of file numhash.c.

References charset, debug(), error(), http_create_empty_headers(), http_destroy_headers(), http_get_real(), http_header_get_content_type(), HTTP_METHOD_GET, HTTP_OK, info(), lines, numhash_add_number(), NUMHASH_AUTO_HASH, numhash_init(), octstr_create, octstr_destroy(), octstr_get_cstr, octstr_str_compare(), type, url, and warning().

Referenced by create_onetrans(), create_oneuser(), init_smsbox(), smsc2_reload_lists(), and smsc2_start().

314 {
315  int loc, lines = 0;
316  List *request_headers, *reply_headers;
317  Octstr *url, *final_url, *reply_body;
318  Octstr *type, *charset;
319 
320  char *data, *ptr, numbuf[100];
321  int status;
322  Numhash *table;
323 
324  url = octstr_create(seek_url);
325  request_headers = http_create_empty_headers();
326  status = http_get_real(HTTP_METHOD_GET, url, request_headers, &final_url,
327  &reply_headers, &reply_body);
329  octstr_destroy(final_url);
330  http_destroy_headers(request_headers);
331 
332  if (status != HTTP_OK) {
333  http_destroy_headers(reply_headers);
334  octstr_destroy(reply_body);
335  error(0, "Cannot load numhash!");
336  return NULL;
337  }
338  http_header_get_content_type(reply_headers, &type, &charset);
340  http_destroy_headers(reply_headers);
341 
342  if (octstr_str_compare(type, "text/plain") != 0) {
343  octstr_destroy(reply_body);
344  error(0, "Strange content type <%s> for numhash - expecting 'text/plain'"
345  ", operatiom fails", octstr_get_cstr(type));
347  return NULL;
348  }
350 
351  ptr = data = octstr_get_cstr(reply_body);
352  while(*ptr) {
353  if (*ptr == '\n') lines++;
354  ptr++;
355  }
356  debug("numhash", 0, "Total %d lines in %s", lines, seek_url);
357 
358  table = numhash_init(lines+10, NUMHASH_AUTO_HASH); /* automatic hash */
359 
360  /* now, parse the number information */
361 
362  lines = 0;
363 
364  while((ptr = strchr(data, '\n'))) { /* each line is ended with linefeed */
365  *ptr = '\0';
366  while(*data != '\0' && isspace(*data))
367  data++;
368  if (*data != '#') {
369  loc = 0;
370  while (*data != '\0') {
371  if (isdigit(*data))
372  numbuf[loc++] = *data;
373  else if (*data == ' ' || *data == '+' || *data == '-')
374  ;
375  else break;
376  data++;
377  }
378  if (loc) {
379  numbuf[loc] = '\0';
380  numhash_add_number(table, numbuf);
381  lines++;
382  }
383  else
384  warning(0, "Corrupted line '%s'", data);
385  }
386  data = ptr+1; /* next row... */
387  }
388  octstr_destroy(reply_body);
389 
390  info(0, "Read from <%s> total of %ld numbers", seek_url, table->number_total);
391  return table;
392 }
static Numhash * numhash_init(int max_numbers, int hash_size)
Definition: numhash.c:171
void error(int err, const char *fmt,...)
Definition: log.c:648
void info(int err, const char *fmt,...)
Definition: log.c:672
int type
Definition: smsc_cimd2.c:215
void http_header_get_content_type(List *headers, Octstr **type, Octstr **charset)
Definition: http.c:3225
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
Octstr * charset
Definition: test_ota.c:68
static List * lines
Definition: mtbatch.c:88
void http_destroy_headers(List *headers)
Definition: http.c:2879
Definition: http.h:142
List * http_create_empty_headers(void)
Definition: http.c:2872
void warning(int err, const char *fmt,...)
Definition: log.c:660
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:324
#define octstr_create(cstr)
Definition: octstr.h:125
#define NUMHASH_AUTO_HASH
Definition: numhash.c:79
int http_get_real(int method, Octstr *url, List *request_headers, Octstr **final_url, List **reply_headers, Octstr **reply_body)
Definition: http.c:1821
Definition: octstr.c:118
void debug(const char *place, int err, const char *fmt,...)
Definition: log.c:726
int octstr_str_compare(const Octstr *ostr, const char *str)
Definition: octstr.c:973
static int numhash_add_number(Numhash *table, char *nro)
Definition: numhash.c:149
static Octstr * url
Definition: test_xmlrpc.c:84
Definition: list.c:102

◆ numhash_destroy()

void numhash_destroy ( Numhash table)

Definition at line 275 of file numhash.c.

References table::numbers.

Referenced by destroy_onetrans(), destroy_oneuser(), main(), smsc2_cleanup(), and smsc2_reload_lists().

276 {
277  if (table == NULL)
278  return;
279  gw_free(table->numbers);
280  gw_free(table->hash);
281  gw_free(table);
282 }
long * numbers
Definition: wsp_strings.c:85

◆ numhash_find_key()

int numhash_find_key ( Numhash table,
long long  key 
)

Definition at line 228 of file numhash.c.

References numhash_number::key, and numhash_number::next.

Referenced by numhash_find_number().

229 {
230  struct numhash_number *ptr;
231 
232  ptr = table->hash[key % table->hash_size];
233  while (ptr) {
234  if (ptr->key == key) return 1;
235  ptr = ptr->next;
236  }
237  return 0; /* not found */
238 }
struct numhash_number * next
Definition: numhash.c:102
long long key
Definition: numhash.c:101

◆ numhash_find_number()

int numhash_find_number ( Numhash table,
Octstr nro 
)

Definition at line 218 of file numhash.c.

References numhash_number::key, numhash_find_key(), and numhash_get_key().

Referenced by bb_smscconn_receive(), blacklisted(), check_allowed_translation(), smsbox_req_handle(), smsc2_rout(), and whitelisted().

219 {
220  long long key = numhash_get_key(nro);
221  if (key < 0)
222  return key;
223 
224  return numhash_find_key(table, key);
225 }
long long key
Definition: numhash.c:101
int numhash_find_key(Numhash *table, long long key)
Definition: numhash.c:228
long long numhash_get_key(Octstr *nro)
Definition: numhash.c:242

◆ numhash_get_char_key()

long long numhash_get_char_key ( char *  nro)

Definition at line 257 of file numhash.c.

References numhash_number::key, and precision.

Referenced by numhash_add_number().

258 {
259  int len;
260  long long key;
261 
262  if (!nro) return -1;
263 
264  len = strlen(nro);
265 
266  if (len > precision)
267  key = strtoll(nro + len - precision, (char**) NULL, 10);
268  else
269  key = strtoll(nro, (char**) NULL, 10);
270 
271  return key;
272 }
long long key
Definition: numhash.c:101
static int precision
Definition: numhash.c:117

◆ numhash_get_key()

long long numhash_get_key ( Octstr nro)

Definition at line 242 of file numhash.c.

References numhash_number::key, octstr_get_cstr, octstr_len(), and precision.

Referenced by numhash_find_number().

243 {
244  long long key;
245 
246  if (!nro) return -1;
247 
248  if (octstr_len(nro) > precision)
249  key = strtoll(octstr_get_cstr(nro) + octstr_len(nro) - precision, (char**) NULL, 10);
250  else
251  key = strtoll(octstr_get_cstr(nro), (char**) NULL, 10);
252 
253  return key;
254 }
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
long long key
Definition: numhash.c:101
long octstr_len(const Octstr *ostr)
Definition: octstr.c:342
static int precision
Definition: numhash.c:117

◆ numhash_hash_fill()

double numhash_hash_fill ( Numhash table,
int *  longest 
)

Definition at line 285 of file numhash.c.

References numhash_number::next.

286 {
287  int i, l, max = 0, tot = 0;
288  struct numhash_number *ptr;
289 
290  for (i=0; i < table->hash_size; i++)
291  if (table->hash[i]) {
292  tot++;
293  ptr = table->hash[i];
294  for (l=0; ptr->next; ptr = ptr->next)
295  l++;
296  if (l > max)
297  max = l;
298  }
299 
300  if (longest != NULL)
301  *longest = max;
302 
303  return (double)(tot*100.0/(table->hash_size));
304 }
struct numhash_number * next
Definition: numhash.c:102

◆ numhash_init()

static Numhash* numhash_init ( int  max_numbers,
int  hash_size 
)
static

Definition at line 171 of file numhash.c.

References numhash_table::hash, numhash_table::hash_size, numhash_table::number_total, numhash_table::numbers, NUMHASH_AUTO_HASH, precision, primes, and numhash_table::table_size.

Referenced by numhash_create().

172 {
173  Numhash *ntable = NULL;
174 
175  ntable = gw_malloc(sizeof(Numhash));
176 
177  if (hash_size > 0)
178  ntable->hash_size = hash_size;
179  else if (hash_size == NUMHASH_AUTO_HASH) {
180  int i;
181  for(i=0 ; primes[i] > 0; i++) {
182  ntable->hash_size = primes[i];
183  if (ntable->hash_size > max_numbers)
184  break;
185  }
186  } else {
187  gw_free(ntable);
188  return NULL;
189  }
190  ntable->hash = gw_malloc(ntable->hash_size * sizeof(struct numhash_number *));
191  memset(ntable->hash, 0, sizeof(struct numhash_number *) * ntable->hash_size);
192 
193  ntable->table_size = max_numbers;
194  ntable->numbers = gw_malloc(ntable->table_size * sizeof(struct numhash_number));
195 
196  ntable->number_total = 0;
197 
198  /* set our accuracy according to the size of long int
199  * Ok, we call this many times if we use multiple tables, but
200  * that is not a problem...
201  */
202  if (sizeof(long long) >= 16)
203  precision = 38;
204  else if (sizeof(long long) >= 8)
205  precision = 19;
206 
207  return ntable;
208 }
static int primes[]
Definition: numhash.c:83
long number_total
Definition: numhash.c:94
struct numhash_number * numbers
Definition: numhash.c:93
#define NUMHASH_AUTO_HASH
Definition: numhash.c:79
long table_size
Definition: numhash.c:95
static int precision
Definition: numhash.c:117
long hash_size
Definition: numhash.c:97
struct numhash_number ** hash
Definition: numhash.c:96

◆ numhash_size()

int numhash_size ( Numhash table)

Definition at line 307 of file numhash.c.

308 {
309  return table->number_total;
310 }

Variable Documentation

◆ precision

int precision = 19
static

Definition at line 117 of file numhash.c.

Referenced by numhash_get_char_key(), numhash_get_key(), and numhash_init().

◆ primes

int primes[]
static
Initial value:
= {
101, 503, 1009, 2003, 3001, 5003, 7001,
10007, 20011, 30011, 40009, 50021, 60013, 70001, 80021, 90001,
100003, 150001, 200003, 300007, 400009, 500009, 600011, 700001,
800011, 900001, 1000003, 1100009, 1200007, 1300021, 1400017, 1500007,
1600033, 1700021, 1800017, 1900009, 2000003, 3000017, 4000037, 5000111,
6000101, 7000127, 8000051, -1
}

Definition at line 83 of file numhash.c.

Referenced by numhash_init().

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