diff --git a/commandline.c b/commandline.c index ba778bec..e21307e3 100644 --- a/commandline.c +++ b/commandline.c @@ -37,6 +37,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include "serval.h" #include "rhizome.h" #include "strbuf.h" +#include "str.h" #include "mdp_client.h" #include "cli.h" diff --git a/dataformats.c b/dataformats.c index 4f6b9012..436f4e4c 100644 --- a/dataformats.c +++ b/dataformats.c @@ -19,93 +19,9 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include "serval.h" #include "rhizome.h" +#include "str.h" #include -char hexdigit[16] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; - -static inline int _is_xsubstring(const char *text, int len) -{ - while (len--) - if (!isxdigit(*text++)) - return 0; - return 1; -} - -static inline int _is_xstring(const char *text, int len) -{ - while (len--) - if (!isxdigit(*text++)) - return 0; - return *text == '\0'; -} - -/* Return true iff 'len' bytes starting at 'text' are hex digits, upper or lower case. - Does not check the following byte. - @author Andrew Bettison - */ -int is_xsubstring(const char *text, int len) -{ - return _is_xsubstring(text, len); -} - -/* Return true iff the nul-terminated string 'text' has length 'len' and consists only of hex - digits, upper or lower case. - @author Andrew Bettison - */ -int is_xstring(const char *text, int len) -{ - return _is_xstring(text, len); -} - -/* Does this whole buffer contain the same value? */ -int is_all_matching(const unsigned char *ptr, size_t len, unsigned char value) -{ - while (len--) - if (*ptr++ != value) - return 0; - return 1; -} - -char *tohex(char *dstHex, const unsigned char *srcBinary, size_t bytes) -{ - char *p; - for (p = dstHex; bytes--; ++srcBinary) { - *p++ = hexdigit[*srcBinary >> 4]; - *p++ = hexdigit[*srcBinary & 0xf]; - } - *p = '\0'; - return dstHex; -} - -/* Convert nbinary*2 ASCII hex characters [0-9A-Fa-f] to nbinary bytes of data. Can be used to - perform the conversion in-place, eg, fromhex(buf, (char*)buf, n); Returns -1 if a non-hex-digit - character is encountered, otherwise returns the number of binary bytes produced (= nbinary). - @author Andrew Bettison - */ -size_t fromhex(unsigned char *dstBinary, const char *srcHex, size_t nbinary) -{ - size_t count = 0; - while (count != nbinary) { - unsigned char high = hexvalue(*srcHex++); - if (high & 0xf0) return -1; - unsigned char low = hexvalue(*srcHex++); - if (low & 0xf0) return -1; - dstBinary[count++] = (high << 4) + low; - } - return count; -} - -/* Convert nbinary*2 ASCII hex characters [0-9A-Fa-f] followed by a nul '\0' character to nbinary bytes of data. Can be used to - perform the conversion in-place, eg, fromhex(buf, (char*)buf, n); Returns -1 if a non-hex-digit - character is encountered or the character immediately following the last hex digit is not a nul, - otherwise returns zero. - @author Andrew Bettison - */ -int fromhexstr(unsigned char *dstBinary, const char *srcHex, size_t nbinary) -{ - return (fromhex(dstBinary, srcHex, nbinary) == nbinary && srcHex[nbinary * 2] == '\0') ? 0 : -1; -} - int str_is_subscriber_id(const char *sid) { size_t len = 0; @@ -119,7 +35,7 @@ int strn_is_subscriber_id(const char *sid, size_t *lenp) *lenp = 9; return 1; } - if (_is_xsubstring(sid, SID_STRLEN)) { + if (is_xsubstring(sid, SID_STRLEN)) { if (lenp) *lenp = SID_STRLEN; return 1; @@ -129,42 +45,42 @@ int strn_is_subscriber_id(const char *sid, size_t *lenp) int rhizome_strn_is_manifest_id(const char *id) { - return _is_xsubstring(id, RHIZOME_MANIFEST_ID_STRLEN); + return is_xsubstring(id, RHIZOME_MANIFEST_ID_STRLEN); } int rhizome_str_is_manifest_id(const char *id) { - return _is_xstring(id, RHIZOME_MANIFEST_ID_STRLEN); + return is_xstring(id, RHIZOME_MANIFEST_ID_STRLEN); } int rhizome_strn_is_bundle_key(const char *key) { - return _is_xsubstring(key, RHIZOME_BUNDLE_KEY_STRLEN); + return is_xsubstring(key, RHIZOME_BUNDLE_KEY_STRLEN); } int rhizome_str_is_bundle_key(const char *key) { - return _is_xstring(key, RHIZOME_BUNDLE_KEY_STRLEN); + return is_xstring(key, RHIZOME_BUNDLE_KEY_STRLEN); } int rhizome_strn_is_bundle_crypt_key(const char *key) { - return _is_xsubstring(key, RHIZOME_CRYPT_KEY_STRLEN); + return is_xsubstring(key, RHIZOME_CRYPT_KEY_STRLEN); } int rhizome_str_is_bundle_crypt_key(const char *key) { - return _is_xstring(key, RHIZOME_CRYPT_KEY_STRLEN); + return is_xstring(key, RHIZOME_CRYPT_KEY_STRLEN); } int rhizome_strn_is_file_hash(const char *hash) { - return _is_xsubstring(hash, RHIZOME_FILEHASH_STRLEN); + return is_xsubstring(hash, RHIZOME_FILEHASH_STRLEN); } int rhizome_str_is_file_hash(const char *hash) { - return _is_xstring(hash, RHIZOME_FILEHASH_STRLEN); + return is_xstring(hash, RHIZOME_FILEHASH_STRLEN); } int str_is_did(const char *did) @@ -278,22 +194,6 @@ int stowSid(unsigned char *packet, int ofs, const char *sid) return 0; } -char *str_toupper_inplace(char *str) -{ - register char *s; - for (s = str; *s; ++s) - *s = toupper(*s); - return str; -} - -int hexvalue(char c) -{ - if (c >= '0' && c <= '9') return c - '0'; - if (c >= 'A' && c <= 'F') return c - 'A' + 10; - if (c >= 'a' && c <= 'f') return c - 'a' + 10; - return -1; -} - int packetGetID(unsigned char *packet,int len,char *did,char *sid) { int ofs=HEADERFIELDS_LEN; diff --git a/directory_client.c b/directory_client.c index bbffe763..0146eb4b 100644 --- a/directory_client.c +++ b/directory_client.c @@ -13,6 +13,7 @@ */ #include "serval.h" +#include "str.h" #include "overlay_address.h" struct subscriber *directory_service; diff --git a/dna_helper.c b/dna_helper.c index 49a9d83f..fc02bac9 100644 --- a/dna_helper.c +++ b/dna_helper.c @@ -22,6 +22,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include #include #include "serval.h" +#include "str.h" #include "strbuf.h" #include "strbuf_helpers.h" #include "overlay_address.h" diff --git a/keyring.c b/keyring.c index 23ee1884..c56496ce 100644 --- a/keyring.c +++ b/keyring.c @@ -17,6 +17,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "serval.h" +#include "str.h" #include "rhizome.h" #include "nacl.h" #include "overlay_address.h" diff --git a/mdp_client.c b/mdp_client.c index ccf2ee91..d5bfabdb 100644 --- a/mdp_client.c +++ b/mdp_client.c @@ -18,6 +18,7 @@ #include #include "serval.h" +#include "str.h" #include "strbuf.h" #include "overlay_buffer.h" #include "overlay_address.h" diff --git a/overlay_address.c b/overlay_address.c index 9f8fec7f..348f347b 100644 --- a/overlay_address.c +++ b/overlay_address.c @@ -26,6 +26,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "serval.h" +#include "str.h" #include "overlay_address.h" #include "overlay_buffer.h" #include "overlay_packet.h" diff --git a/overlay_advertise.c b/overlay_advertise.c index 81158b31..a59a51cf 100644 --- a/overlay_advertise.c +++ b/overlay_advertise.c @@ -18,6 +18,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "serval.h" +#include "str.h" #include "overlay_address.h" #include "overlay_buffer.h" #include "overlay_packet.h" diff --git a/overlay_mdp.c b/overlay_mdp.c index 71262bac..f7614d7b 100644 --- a/overlay_mdp.c +++ b/overlay_mdp.c @@ -18,6 +18,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include #include "serval.h" +#include "str.h" #include "strbuf.h" #include "overlay_buffer.h" #include "overlay_address.h" diff --git a/overlay_packetformats.c b/overlay_packetformats.c index e036a8b8..093738f8 100644 --- a/overlay_packetformats.c +++ b/overlay_packetformats.c @@ -18,6 +18,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "serval.h" +#include "str.h" #include "strbuf.h" #include "overlay_buffer.h" #include "overlay_packet.h" diff --git a/overlay_payload.c b/overlay_payload.c index a1ee3a2f..2dab22d3 100644 --- a/overlay_payload.c +++ b/overlay_payload.c @@ -18,6 +18,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "serval.h" +#include "str.h" #include "overlay_buffer.h" #include "overlay_packet.h" diff --git a/overlay_route.c b/overlay_route.c index 997cc9bf..b6213fb5 100644 --- a/overlay_route.c +++ b/overlay_route.c @@ -18,6 +18,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "serval.h" +#include "str.h" #include "strbuf.h" #include "overlay_buffer.h" #include "overlay_address.h" diff --git a/rhizome.c b/rhizome.c index d3c06c36..04cf7300 100644 --- a/rhizome.c +++ b/rhizome.c @@ -18,6 +18,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "serval.h" +#include "str.h" #include "rhizome.h" #include diff --git a/rhizome_crypto.c b/rhizome_crypto.c index 2e4defcd..e7c32c21 100644 --- a/rhizome_crypto.c +++ b/rhizome_crypto.c @@ -18,6 +18,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "serval.h" +#include "str.h" #include "rhizome.h" #include #include diff --git a/serval.h b/serval.h index 223b3f7a..1f8fae02 100644 --- a/serval.h +++ b/serval.h @@ -429,16 +429,6 @@ extern overlay_txqueue overlay_tx[OQ_MAX]; ssize_t recvwithttl(int sock, unsigned char *buffer, size_t bufferlen, int *ttl, struct sockaddr *recvaddr, socklen_t *recvaddrlen); -int is_xsubstring(const char *text, int len); -int is_xstring(const char *text, int len); -char *tohex(char *dstHex, const unsigned char *srcBinary, size_t bytes); -size_t fromhex(unsigned char *dstBinary, const char *srcHex, size_t bytes); -int fromhexstr(unsigned char *dstBinary, const char *srcHex, size_t bytes); -int hexvalue(char c); -char *str_toupper_inplace(char *s); - -int is_all_matching(const unsigned char *ptr, size_t len, unsigned char value); - // is the SID entirely 0xFF? #define is_sid_broadcast(SID) is_all_matching(SID, SID_SIZE, 0xFF) diff --git a/str.c b/str.c index e400babc..74c41b79 100644 --- a/str.c +++ b/str.c @@ -17,12 +17,72 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ +#define __STR_INLINE +#include "str.h" + #include #include #include #include -#include "str.h" -#include "log.h" + +char hexdigit[16] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; + +char *tohex(char *dstHex, const unsigned char *srcBinary, size_t bytes) +{ + char *p; + for (p = dstHex; bytes--; ++srcBinary) { + *p++ = hexdigit[*srcBinary >> 4]; + *p++ = hexdigit[*srcBinary & 0xf]; + } + *p = '\0'; + return dstHex; +} + +/* Convert nbinary*2 ASCII hex characters [0-9A-Fa-f] to nbinary bytes of data. Can be used to + perform the conversion in-place, eg, fromhex(buf, (char*)buf, n); Returns -1 if a non-hex-digit + character is encountered, otherwise returns the number of binary bytes produced (= nbinary). + @author Andrew Bettison + */ +size_t fromhex(unsigned char *dstBinary, const char *srcHex, size_t nbinary) +{ + size_t count = 0; + while (count != nbinary) { + unsigned char high = hexvalue(*srcHex++); + if (high & 0xf0) return -1; + unsigned char low = hexvalue(*srcHex++); + if (low & 0xf0) return -1; + dstBinary[count++] = (high << 4) + low; + } + return count; +} + +/* Convert nbinary*2 ASCII hex characters [0-9A-Fa-f] followed by a nul '\0' character to nbinary bytes of data. Can be used to + perform the conversion in-place, eg, fromhex(buf, (char*)buf, n); Returns -1 if a non-hex-digit + character is encountered or the character immediately following the last hex digit is not a nul, + otherwise returns zero. + @author Andrew Bettison + */ +int fromhexstr(unsigned char *dstBinary, const char *srcHex, size_t nbinary) +{ + return (fromhex(dstBinary, srcHex, nbinary) == nbinary && srcHex[nbinary * 2] == '\0') ? 0 : -1; +} + +/* Does this whole buffer contain the same value? */ +int is_all_matching(const unsigned char *ptr, size_t len, unsigned char value) +{ + while (len--) + if (*ptr++ != value) + return 0; + return 1; +} + +char *str_toupper_inplace(char *str) +{ + register char *s; + for (s = str; *s; ++s) + *s = toupper(*s); + return str; +} int str_startswith(char *str, const char *substring, char **afterp) { @@ -103,3 +163,37 @@ int str_to_ll_scaled(const char *str, int base, long long *result, char **afterp *result = value; return 1; } + +size_t str_fromprint(unsigned char *dst, const char *src) +{ + unsigned char *const odst = dst; + while (*src) { + switch (*src) { + case '\\': + ++src; + switch (*src) { + case '\0': *dst++ = '\\'; break; + case '0': *dst++ = '\0'; ++src; break; + case 'n': *dst++ = '\n'; ++src; break; + case 'r': *dst++ = '\r'; ++src; break; + case 't': *dst++ = '\t'; ++src; break; + case 'x': + if (isxdigit(src[1]) && isxdigit(src[2])) { + ++src; + fromhex(dst++, src, 1); + src += 2; + break; + } + // fall through + default: + *dst++ = *src++; + break; + } + break; + default: + *dst++ = *src++; + break; + } + } + return dst - odst; +} diff --git a/str.h b/str.h index b35854d1..35f0667d 100644 --- a/str.h +++ b/str.h @@ -20,6 +20,60 @@ #ifndef __STR_H__ #define __STR_H__ +#include +#include + +#ifndef __STR_INLINE +# if __GNUC__ && !__GNUC_STDC_INLINE__ +# define __STR_INLINE extern inline +# else +# define __STR_INLINE inline +# endif +#endif + +/* Return true iff 'len' bytes starting at 'text' are hex digits, upper or lower case. + * Does not check the following byte. + * + * @author Andrew Bettison + */ +__STR_INLINE int is_xsubstring(const char *text, int len) +{ + while (len--) + if (!isxdigit(*text++)) + return 0; + return 1; +} + +/* Return true iff the nul-terminated string 'text' has length 'len' and consists only of hex + * digits, upper or lower case. + * + * @author Andrew Bettison + */ +__STR_INLINE int is_xstring(const char *text, int len) +{ + while (len--) + if (!isxdigit(*text++)) + return 0; + return *text == '\0'; +} + +extern char hexdigit[16]; +char *tohex(char *dstHex, const unsigned char *srcBinary, size_t bytes); +size_t fromhex(unsigned char *dstBinary, const char *srcHex, size_t nbinary); +int fromhexstr(unsigned char *dstBinary, const char *srcHex, size_t nbinary); +int is_all_matching(const unsigned char *ptr, size_t len, unsigned char value); +char *str_toupper_inplace(char *s); + +__STR_INLINE int hexvalue(char c) +{ + if (c >= '0' && c <= '9') return c - '0'; + if (c >= 'A' && c <= 'F') return c - 'A' + 10; + if (c >= 'a' && c <= 'f') return c - 'a' + 10; + return -1; +} + +size_t str_fromprint(unsigned char *dst, const char *src); + /* Check if a given string starts with a given sub-string. If so, return 1 and, if afterp is not * NULL, set *afterp to point to the character immediately following the substring. Otherwise * return 0. diff --git a/vomp.c b/vomp.c index 37c029ec..266dbda8 100644 --- a/vomp.c +++ b/vomp.c @@ -25,6 +25,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "serval.h" +#include "str.h" #include "strbuf.h" #include "strlcpy.h"