2012-07-23 05:47:21 +00:00
|
|
|
/*
|
2013-12-04 06:26:55 +00:00
|
|
|
Serval DNA data interchange formats
|
|
|
|
Copyright (C) 2010-2013 Serval Project Inc.
|
2010-07-13 12:15:46 +00:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
2014-05-23 08:19:00 +00:00
|
|
|
#define __RHIZOME_TYPES_INLINE
|
2013-11-25 02:39:54 +00:00
|
|
|
#include <ctype.h>
|
2014-05-23 08:19:00 +00:00
|
|
|
#include "serval_types.h"
|
|
|
|
#include "rhizome_types.h"
|
|
|
|
#include "os.h"
|
2012-11-07 06:12:45 +00:00
|
|
|
#include "str.h"
|
2013-11-25 02:39:54 +00:00
|
|
|
#include "dataformats.h"
|
2010-07-13 12:15:46 +00:00
|
|
|
|
2013-10-03 07:14:06 +00:00
|
|
|
int cmp_sid_t(const sid_t *a, const sid_t *b)
|
|
|
|
{
|
|
|
|
return memcmp(a, b, sizeof a->binary);
|
|
|
|
}
|
|
|
|
|
2014-04-29 04:09:21 +00:00
|
|
|
int str_to_sid_t(sid_t *sidp, const char *hex)
|
2013-11-21 01:46:26 +00:00
|
|
|
{
|
2015-03-27 18:23:09 +00:00
|
|
|
return parse_sid_t(sidp, hex, -1, NULL); // checks for nul terminator
|
2013-02-14 05:36:01 +00:00
|
|
|
}
|
|
|
|
|
2015-03-27 18:23:09 +00:00
|
|
|
int strn_to_sid_t(sid_t *sidp, const char *hex, size_t hexlen)
|
2013-02-14 05:36:01 +00:00
|
|
|
{
|
2015-03-27 18:23:09 +00:00
|
|
|
return parse_sid_t(sidp, hex, hexlen, NULL); // does not check for nul terminator
|
|
|
|
}
|
|
|
|
|
|
|
|
int parse_sid_t(sid_t *sidp, const char *hex, ssize_t hexlen, const char **endp)
|
|
|
|
{
|
|
|
|
const char *end = NULL;
|
|
|
|
if (strn_startswith(hex, hexlen, "broadcast", &end)) {
|
|
|
|
if (endp)
|
|
|
|
*endp = end;
|
|
|
|
else if (hexlen == -1 && *end != '\0')
|
|
|
|
return -1;
|
2014-04-29 04:09:21 +00:00
|
|
|
if (sidp)
|
|
|
|
*sidp = SID_BROADCAST;
|
2013-02-14 05:36:01 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2015-03-27 18:23:09 +00:00
|
|
|
if (hexlen != -1 && hexlen != SID_STRLEN)
|
2014-04-29 04:09:21 +00:00
|
|
|
return -1;
|
2015-03-27 18:23:09 +00:00
|
|
|
sid_t tmp;
|
2013-02-14 05:36:01 +00:00
|
|
|
int n = fromhex(tmp.binary, hex, sizeof tmp.binary);
|
|
|
|
if (n != sizeof tmp.binary)
|
|
|
|
return -1;
|
2015-03-27 18:23:09 +00:00
|
|
|
if (endp)
|
|
|
|
*endp = hex + SID_STRLEN;
|
|
|
|
else if (hexlen == -1 && hex[SID_STRLEN] != '\0')
|
|
|
|
return -1;
|
2014-04-29 04:09:21 +00:00
|
|
|
if (sidp)
|
|
|
|
*sidp = tmp;
|
2013-02-14 05:36:01 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-07-12 06:44:25 +00:00
|
|
|
int str_is_subscriber_id(const char *sid)
|
|
|
|
{
|
2012-07-23 02:53:17 +00:00
|
|
|
size_t len = 0;
|
|
|
|
return strn_is_subscriber_id(sid, &len) && sid[len] == '\0';
|
2012-07-12 06:44:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int strn_is_subscriber_id(const char *sid, size_t *lenp)
|
|
|
|
{
|
|
|
|
if (strncasecmp(sid, "broadcast", 9) == 0) {
|
|
|
|
if (lenp)
|
|
|
|
*lenp = 9;
|
|
|
|
return 1;
|
|
|
|
}
|
2012-11-07 06:12:45 +00:00
|
|
|
if (is_xsubstring(sid, SID_STRLEN)) {
|
2012-07-12 06:44:25 +00:00
|
|
|
if (lenp)
|
|
|
|
*lenp = SID_STRLEN;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-10-03 13:46:45 +00:00
|
|
|
int cmp_rhizome_bid_t(const rhizome_bid_t *a, const rhizome_bid_t *b)
|
|
|
|
{
|
|
|
|
return memcmp(a, b, sizeof a->binary);
|
|
|
|
}
|
|
|
|
|
|
|
|
int str_to_rhizome_bid_t(rhizome_bid_t *bid, const char *hex)
|
|
|
|
{
|
2015-03-27 18:23:09 +00:00
|
|
|
return parse_rhizome_bid_t(bid, hex, -1, NULL); // checks for nul terminator
|
2013-10-03 13:46:45 +00:00
|
|
|
}
|
|
|
|
|
2015-03-27 18:23:09 +00:00
|
|
|
int strn_to_rhizome_bid_t(rhizome_bid_t *bid, const char *hex, size_t hexlen)
|
2013-10-03 13:46:45 +00:00
|
|
|
{
|
2015-03-27 18:23:09 +00:00
|
|
|
return parse_rhizome_bid_t(bid, hex, hexlen, NULL); // does not check for nul terminator
|
|
|
|
}
|
|
|
|
|
|
|
|
int parse_rhizome_bid_t(rhizome_bid_t *bid, const char *hex, ssize_t hexlen, const char **endp)
|
|
|
|
{
|
|
|
|
if (hexlen != -1 && hexlen != RHIZOME_BUNDLE_ID_STRLEN)
|
|
|
|
return -1;
|
2013-10-03 13:46:45 +00:00
|
|
|
rhizome_bid_t tmp;
|
|
|
|
int n = fromhex(tmp.binary, hex, sizeof tmp.binary);
|
|
|
|
if (n != sizeof tmp.binary)
|
|
|
|
return -1;
|
2015-03-27 18:23:09 +00:00
|
|
|
if (endp)
|
|
|
|
*endp = hex + RHIZOME_BUNDLE_ID_STRLEN;
|
|
|
|
else if (hexlen == -1 && hex[RHIZOME_BUNDLE_ID_STRLEN] != '\0')
|
|
|
|
return -1;
|
2013-12-18 07:05:05 +00:00
|
|
|
if (bid)
|
|
|
|
*bid = tmp;
|
2013-10-03 13:46:45 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-10-10 07:53:06 +00:00
|
|
|
int cmp_rhizome_filehash_t(const rhizome_filehash_t *a, const rhizome_filehash_t *b)
|
|
|
|
{
|
|
|
|
return memcmp(a, b, sizeof a->binary);
|
|
|
|
}
|
|
|
|
|
|
|
|
int str_to_rhizome_filehash_t(rhizome_filehash_t *hashp, const char *hex)
|
|
|
|
{
|
2015-03-27 18:23:09 +00:00
|
|
|
return parse_rhizome_filehash_t(hashp, hex, -1, NULL); // checks for nul terminator
|
2013-10-10 07:53:06 +00:00
|
|
|
}
|
|
|
|
|
2015-03-27 18:23:09 +00:00
|
|
|
int strn_to_rhizome_filehash_t(rhizome_filehash_t *hashp, const char *hex, size_t hexlen)
|
2013-10-10 07:53:06 +00:00
|
|
|
{
|
2015-03-27 18:23:09 +00:00
|
|
|
return parse_rhizome_filehash_t(hashp, hex, hexlen, NULL); // does not check for nul terminator
|
|
|
|
}
|
|
|
|
|
|
|
|
int parse_rhizome_filehash_t(rhizome_filehash_t *hashp, const char *hex, ssize_t hexlen, const char **endp)
|
|
|
|
{
|
|
|
|
if (hexlen != -1 && hexlen != RHIZOME_FILEHASH_STRLEN)
|
|
|
|
return -1;
|
2013-10-10 07:53:06 +00:00
|
|
|
rhizome_filehash_t tmp;
|
|
|
|
int n = fromhex(tmp.binary, hex, sizeof tmp.binary);
|
|
|
|
if (n != sizeof tmp.binary)
|
|
|
|
return -1;
|
2015-03-27 18:23:09 +00:00
|
|
|
if (endp)
|
|
|
|
*endp = hex + RHIZOME_FILEHASH_STRLEN;
|
|
|
|
else if (hexlen == -1 && hex[RHIZOME_FILEHASH_STRLEN] != '\0')
|
|
|
|
return -1;
|
2013-12-18 07:05:05 +00:00
|
|
|
if (hashp)
|
|
|
|
*hashp = tmp;
|
2013-10-10 07:53:06 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
int str_to_rhizome_bk_t(rhizome_bk_t *bkp, const char *hex)
|
|
|
|
{
|
2015-03-27 18:23:09 +00:00
|
|
|
return parse_rhizome_bk_t(bkp, hex, -1, NULL); // checks for nul terminator
|
2012-06-25 05:20:38 +00:00
|
|
|
}
|
|
|
|
|
2015-03-27 18:23:09 +00:00
|
|
|
int strn_to_rhizome_bk_t(rhizome_bk_t *bkp, const char *hex, size_t hexlen)
|
2013-12-18 07:05:05 +00:00
|
|
|
{
|
2015-03-27 18:23:09 +00:00
|
|
|
return parse_rhizome_bk_t(bkp, hex, hexlen, NULL); // does not check for nul terminator
|
|
|
|
}
|
|
|
|
|
|
|
|
int parse_rhizome_bk_t(rhizome_bk_t *bkp, const char *hex, ssize_t hexlen, const char **endp)
|
|
|
|
{
|
|
|
|
if (hexlen != -1 && hexlen != RHIZOME_BUNDLE_KEY_STRLEN)
|
|
|
|
return -1;
|
2013-12-18 07:05:05 +00:00
|
|
|
rhizome_bk_t tmp;
|
|
|
|
int n = fromhex(tmp.binary, hex, sizeof tmp.binary);
|
|
|
|
if (n != sizeof tmp.binary)
|
|
|
|
return -1;
|
2015-03-27 18:23:09 +00:00
|
|
|
if (endp)
|
|
|
|
*endp = hex + RHIZOME_BUNDLE_KEY_STRLEN;
|
|
|
|
else if (hexlen == -1 && hex[RHIZOME_BUNDLE_KEY_STRLEN] != '\0')
|
|
|
|
return -1;
|
2013-12-18 07:05:05 +00:00
|
|
|
if (bkp)
|
|
|
|
*bkp = tmp;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-12 23:33:07 +00:00
|
|
|
int str_to_rhizome_bsk_t(rhizome_bk_t *bskp, const char *text)
|
|
|
|
{
|
|
|
|
return strn_to_rhizome_bsk_t(bskp, text, strlen(text));
|
|
|
|
}
|
|
|
|
|
|
|
|
int strn_to_rhizome_bsk_t(rhizome_bk_t *bskp, const char *text, size_t textlen)
|
|
|
|
{
|
|
|
|
if (textlen > 0 && text[0] == '#') {
|
|
|
|
if (textlen <= 1)
|
|
|
|
return -1; // missing pass phrase
|
|
|
|
if (textlen > RHIZOME_BUNDLE_SECRET_MAX_STRLEN + 1)
|
|
|
|
return -1; // pass phrase too long
|
|
|
|
if (bskp)
|
|
|
|
strn_digest_passphrase(bskp->binary, sizeof bskp->binary, text, textlen);
|
|
|
|
return 0;
|
|
|
|
}
|
2015-03-27 18:23:09 +00:00
|
|
|
return strn_to_rhizome_bk_t(bskp, text, textlen);
|
2014-11-12 23:33:07 +00:00
|
|
|
}
|
|
|
|
|
2012-06-25 05:20:38 +00:00
|
|
|
int rhizome_strn_is_bundle_crypt_key(const char *key)
|
|
|
|
{
|
2012-11-07 06:12:45 +00:00
|
|
|
return is_xsubstring(key, RHIZOME_CRYPT_KEY_STRLEN);
|
2012-06-25 05:20:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int rhizome_str_is_bundle_crypt_key(const char *key)
|
|
|
|
{
|
2012-11-07 06:12:45 +00:00
|
|
|
return is_xstring(key, RHIZOME_CRYPT_KEY_STRLEN);
|
2012-06-25 05:20:38 +00:00
|
|
|
}
|
|
|
|
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
int rhizome_str_is_manifest_service(const char *text)
|
|
|
|
{
|
|
|
|
if (text[0] == '\0')
|
|
|
|
return 0;
|
|
|
|
while (*text && (isalnum(*text) || *text == '_' || *text == '.'))
|
|
|
|
++text;
|
|
|
|
return *text == '\0';
|
|
|
|
}
|
|
|
|
|
2013-12-02 00:42:31 +00:00
|
|
|
/* A name cannot contain a LF because that is the Rhizome text manifest field terminator. For the
|
|
|
|
* time being, CR is not allowed either, because the Rhizome field terminator includes an optional
|
|
|
|
* CR. See rhizome_manifest_parse().
|
|
|
|
*
|
|
|
|
* @author Andrew Bettison <andrew@servalproject.com>
|
|
|
|
*/
|
|
|
|
int rhizome_str_is_manifest_name(const char *text)
|
|
|
|
{
|
|
|
|
while (*text && *text != '\n' && *text != '\r')
|
|
|
|
++text;
|
|
|
|
return *text == '\0';
|
|
|
|
}
|
|
|
|
|
2012-07-23 02:53:17 +00:00
|
|
|
int str_is_did(const char *did)
|
|
|
|
{
|
|
|
|
size_t len = 0;
|
|
|
|
return strn_is_did(did, &len) && did[len] == '\0';
|
|
|
|
}
|
|
|
|
|
2012-07-23 05:47:21 +00:00
|
|
|
int is_didchar(char c)
|
|
|
|
{
|
|
|
|
return isdigit(c) || c == '*' || c == '#' || c == '+';
|
|
|
|
}
|
|
|
|
|
2012-07-23 02:53:17 +00:00
|
|
|
int strn_is_did(const char *did, size_t *lenp)
|
|
|
|
{
|
|
|
|
int i;
|
2012-07-23 05:47:21 +00:00
|
|
|
for (i = 0; i < DID_MAXSIZE && is_didchar(did[i]); ++i)
|
|
|
|
;
|
2012-07-23 02:53:17 +00:00
|
|
|
if (i < DID_MINSIZE)
|
|
|
|
return 0;
|
|
|
|
if (lenp)
|
|
|
|
*lenp = i;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-12-03 03:40:32 +00:00
|
|
|
void write_uint64(unsigned char *o,uint64_t v)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for(i=0;i<8;i++)
|
|
|
|
{ *(o++)=v&0xff; v=v>>8; }
|
|
|
|
}
|
|
|
|
|
|
|
|
void write_uint32(unsigned char *o,uint32_t v)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for(i=0;i<4;i++)
|
|
|
|
{ *(o++)=v&0xff; v=v>>8; }
|
|
|
|
}
|
|
|
|
|
|
|
|
void write_uint16(unsigned char *o,uint16_t v)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for(i=0;i<2;i++)
|
|
|
|
{ *(o++)=v&0xff; v=v>>8; }
|
|
|
|
}
|
|
|
|
|
2013-11-25 02:39:54 +00:00
|
|
|
uint64_t read_uint64(const unsigned char *o)
|
2012-12-03 03:40:32 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
uint64_t v=0;
|
|
|
|
for(i=0;i<8;i++) v=(v<<8)|o[8-1-i];
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2013-11-25 02:39:54 +00:00
|
|
|
uint32_t read_uint32(const unsigned char *o)
|
2012-12-03 03:40:32 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
uint32_t v=0;
|
|
|
|
for(i=0;i<4;i++) v=(v<<8)|o[4-1-i];
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2013-11-25 02:39:54 +00:00
|
|
|
uint16_t read_uint16(const unsigned char *o)
|
2012-12-03 03:40:32 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
uint16_t v=0;
|
|
|
|
for(i=0;i<2;i++) v=(v<<8)|o[2-1-i];
|
|
|
|
return v;
|
|
|
|
}
|
2013-11-28 06:23:12 +00:00
|
|
|
|
|
|
|
int compare_wrapped_uint8(uint8_t one, uint8_t two)
|
|
|
|
{
|
|
|
|
return (int8_t)(one - two);
|
|
|
|
}
|
|
|
|
|
|
|
|
int compare_wrapped_uint16(uint16_t one, uint16_t two)
|
|
|
|
{
|
|
|
|
return (int16_t)(one - two);
|
|
|
|
}
|