# HG changeset patch # User Mychaela Falconia # Date 1702425170 0 # Node ID 159dd90eeafefe2a031e0db20cd22dcccbd10771 beginning, libnumutil compiles diff -r 000000000000 -r 159dd90eeafe .hgignore --- /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]$ diff -r 000000000000 -r 159dd90eeafe build-inc/themwi/nanp --- /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 diff -r 000000000000 -r 159dd90eeafe include/number_db_v2.h --- /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 + +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 diff -r 000000000000 -r 159dd90eeafe include/number_lookup.h --- /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); diff -r 000000000000 -r 159dd90eeafe include/number_utils.h --- /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 +#include + +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); diff -r 000000000000 -r 159dd90eeafe libnumutil/Makefile --- /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 diff -r 000000000000 -r 159dd90eeafe libnumutil/digit_groups.c --- /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 + +#include + +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; +} diff -r 000000000000 -r 159dd90eeafe libnumutil/nanp_valid.c --- /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 + +#include + +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; +} diff -r 000000000000 -r 159dd90eeafe libnumutil/numstring.c --- /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 + +#include + +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'; +}