FreeCalypso > hg > themwi-nanp
changeset 0:159dd90eeafe
beginning, libnumutil compiles
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Tue, 12 Dec 2023 23:52:50 +0000 |
parents | |
children | 6534965175dd |
files | .hgignore build-inc/themwi/nanp include/number_db_v2.h include/number_lookup.h include/number_utils.h libnumutil/Makefile libnumutil/digit_groups.c libnumutil/nanp_valid.c libnumutil/numstring.c |
diffstat | 9 files changed, 192 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.hgignore Tue Dec 12 23:52:50 2023 +0000 @@ -0,0 +1,3 @@ +syntax: regexp + +\.[oa]$
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/build-inc/themwi/nanp Tue Dec 12 23:52:50 2023 +0000 @@ -0,0 +1,1 @@ +../../include \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/include/number_db_v2.h Tue Dec 12 23:52:50 2023 +0000 @@ -0,0 +1,40 @@ +/* + * This header file defines version 2 of ThemWi number database + * binary file format. + */ + +#pragma once + +#include <stdint.h> + +struct numdb_file_hdr { + uint32_t owned_number_count; + uint32_t short_number_count; +}; + +struct owned_number_rec { + uint16_t number[3]; /* NPA-exchange-suffix breakdown */ + uint8_t number_flags; /* properties of outside number */ + uint8_t usage; /* usage inside ThemWi */ + uint16_t remap[3]; /* secondary remap number */ +}; + +#define NUMBER_FLAG_SMSPROV 0x01 /* provisioned for outside SMS */ +#define NUMBER_FLAG_E911PROV 0x02 /* provisioned for E911 */ + +#define NUMBER_USAGE_MASK 0x0F +#define NUMBER_USAGE_TYPE_RSVD 0x00 +#define NUMBER_USAGE_TYPE_GSM_SUB 0x01 +#define NUMBER_USAGE_TYPE_ALIAS 0x02 +#define NUMBER_USAGE_FLAG_E911_VIA 0x10 + +struct short_number_rec { + uint16_t short_num; + uint8_t short_num_type; + uint8_t fullnum_flags; + uint16_t fullnum_prefix[2]; +}; + +#define SHORT_NUM_TYPE_ABBREV 0x01 +#define SHORT_NUM_TYPE_ITN 0x02 +#define SHORT_NUM_TYPE_TEST_SINK 0x03
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/include/number_lookup.h Tue Dec 12 23:52:50 2023 +0000 @@ -0,0 +1,12 @@ +/* + * This header file provides function prototype declarations + * for numdb lookup library. + */ + +#pragma once + +int read_number_db(void); +int refresh_number_db(void); + +const struct owned_number_rec *numdb_lookup_nanp(const char *numstr); +const struct short_number_rec *numdb_lookup_short(const char *numstr);
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/include/number_utils.h Tue Dec 12 23:52:50 2023 +0000 @@ -0,0 +1,17 @@ +/* + * This header file provides function prototype declarations + * for phone-number-related utility functions. + */ + +#pragma once + +#include <stdint.h> +#include <stdbool.h> + +uint16_t digits3_to_uint16(const char *numstr); +uint16_t digits4_to_uint16(const char *numstr); + +bool is_nanp_valid_prefix(const char *numstr); + +int grok_number_string(const char *str, bool allow_hyphen); +void dehyphen_number_string(const char *src, char *dest);
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libnumutil/Makefile Tue Dec 12 23:52:50 2023 +0000 @@ -0,0 +1,13 @@ +CC= gcc +CFLAGS= -O2 -I../build-inc +OBJS= digit_groups.o nanp_valid.o numstring.o +LIB= libnumutil.a + +all: ${LIB} + +${LIB}: ${OBJS} + ar rcu $@ ${OBJS} + ranlib $@ + +clean: + rm -f *.[oa] errs
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libnumutil/digit_groups.c Tue Dec 12 23:52:50 2023 +0000 @@ -0,0 +1,31 @@ +/* + * In version 2 of ThemWi owned number database, NANP numbers are stored as 3 + * uint16_t words: NPA, exchange and prefix, each uint16_t encoding a group of + * 3 or 4 digits of the full telephone number. This library module provides + * functions for turning groups of 3 or 4 digits into uint16_t words. + */ + +#include <stdint.h> + +#include <themwi/nanp/number_utils.h> + +uint16_t digits3_to_uint16(const char *str) +{ + int acc; + + acc = (str[0] - '0') * 100; + acc += (str[1] - '0') * 10; + acc += str[2] - '0'; + return acc; +} + +uint16_t digits4_to_uint16(const char *str) +{ + int acc; + + acc = (str[0] - '0') * 1000; + acc += (str[1] - '0') * 100; + acc += (str[2] - '0') * 10; + acc += str[3] - '0'; + return acc; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libnumutil/nanp_valid.c Tue Dec 12 23:52:50 2023 +0000 @@ -0,0 +1,23 @@ +/* + * Utility functions for NANP number validation. + */ + +#include <stdbool.h> + +#include <themwi/nanp/number_utils.h> + +bool is_nanp_valid_prefix(const char *s) +{ + /* validate NPA part */ + if (s[0] < '2') + return false; + if (s[1] == '1' && s[2] == '1') + return false; + /* validate exchange part */ + if (s[3] < '2') + return false; + if (s[4] == '1' && s[5] == '1') + return false; + /* all checks passed */ + return true; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libnumutil/numstring.c Tue Dec 12 23:52:50 2023 +0000 @@ -0,0 +1,52 @@ +/* + * Utility functions for number string initial parsing or preening. + * grok_number_string() checks whether or not a user-supplied string + * argument is fully numeric (with possibility of allowing hyphens), + * and returns the number of digits. dehyphen_number_string() copies + * a possibly-hyphenated number string to a new buffer with all hyphens + * taken out. + */ + +#include <ctype.h> + +#include <themwi/nanp/number_utils.h> + +int grok_number_string(const char *str, bool allow_hyphen) +{ + const char *cp; + int c, n; + bool last_hyphen; + + n = 0; + last_hyphen = false; + for (cp = str; *cp; ) { + c = *cp++; + if (isdigit(c)) { + n++; + last_hyphen = false; + } else if (c == '-') { + if (!allow_hyphen || !n || last_hyphen) + return(-1); + last_hyphen = true; + } else + return(-1); + } + if (last_hyphen) + return(-1); + return n; +} + +void dehyphen_number_string(const char *src, char *dest) +{ + const char *cp; + char *dp; + int c; + + dp = dest; + for (cp = src; *cp; ) { + c = *cp++; + if (isdigit(c)) + *dp++ = c; + } + *dp = '\0'; +}