Refactor string functions, add str_fromprint()

Move lots of non-Serval-specific string functions from "serval.h"/dataformats.c
to "str.h"/str.c.

Add str_fromprint() function that reverses the effect of toprint(): interprets
escape sequences in the source string "\n\t\r\0\xHH\\" and replaces them with a
single char in the destination string.
This commit is contained in:
Andrew Bettison 2012-11-07 16:42:45 +10:30
parent a29f685bdd
commit 4450116472
18 changed files with 174 additions and 122 deletions

View File

@ -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"

View File

@ -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 <ctype.h>
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 <andrew@servalproject.com>
*/
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 <andrew@servalproject.com>
*/
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 <andrew@servalproject.com>
*/
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 <andrew@servalproject.com>
*/
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;

View File

@ -13,6 +13,7 @@
*/
#include "serval.h"
#include "str.h"
#include "overlay_address.h"
struct subscriber *directory_service;

View File

@ -22,6 +22,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include <sys/stat.h>
#include <signal.h>
#include "serval.h"
#include "str.h"
#include "strbuf.h"
#include "strbuf_helpers.h"
#include "overlay_address.h"

View File

@ -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"

View File

@ -18,6 +18,7 @@
#include <sys/stat.h>
#include "serval.h"
#include "str.h"
#include "strbuf.h"
#include "overlay_buffer.h"
#include "overlay_address.h"

View File

@ -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"

View File

@ -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"

View File

@ -18,6 +18,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include <sys/stat.h>
#include "serval.h"
#include "str.h"
#include "strbuf.h"
#include "overlay_buffer.h"
#include "overlay_address.h"

View File

@ -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"

View File

@ -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"

View File

@ -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"

View File

@ -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 <stdlib.h>

View File

@ -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 <stdlib.h>
#include <ctype.h>

View File

@ -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)

98
str.c
View File

@ -17,12 +17,72 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#define __STR_INLINE
#include "str.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#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 <andrew@servalproject.com>
*/
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 <andrew@servalproject.com>
*/
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;
}

54
str.h
View File

@ -20,6 +20,60 @@
#ifndef __STR_H__
#define __STR_H__
#include <string.h>
#include <ctype.h>
#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 <andrew@servalproject.com>
*/
__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 <andrew@servalproject.com>
*/
__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.

1
vomp.c
View File

@ -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"