forked from servalproject/serval-dna
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataformats.c
309 lines (271 loc) · 7.54 KB
/
dataformats.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
Serval DNA data interchange formats
Copyright (C) 2010-2013 Serval Project Inc.
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.
*/
#define __RHIZOME_TYPES_INLINE
#include <ctype.h>
#include "serval_types.h"
#include "rhizome_types.h"
#include "os.h"
#include "str.h"
#include "dataformats.h"
int cmp_sid_t(const sid_t *a, const sid_t *b)
{
return memcmp(a, b, sizeof a->binary);
}
int str_to_sid_t(sid_t *sidp, const char *hex)
{
return parse_sid_t(sidp, hex, -1, NULL); // checks for nul terminator
}
int strn_to_sid_t(sid_t *sidp, const char *hex, size_t hexlen)
{
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;
if (sidp)
*sidp = SID_BROADCAST;
return 0;
}
if (hexlen != -1 && hexlen != SID_STRLEN)
return -1;
sid_t tmp;
int n = fromhex(tmp.binary, hex, sizeof tmp.binary);
if (n != sizeof tmp.binary)
return -1;
if (endp)
*endp = hex + SID_STRLEN;
else if (hexlen == -1 && hex[SID_STRLEN] != '\0')
return -1;
if (sidp)
*sidp = tmp;
return 0;
}
int str_is_subscriber_id(const char *sid)
{
size_t len = 0;
return strn_is_subscriber_id(sid, &len) && sid[len] == '\0';
}
int strn_is_subscriber_id(const char *sid, size_t *lenp)
{
if (strncasecmp(sid, "broadcast", 9) == 0) {
if (lenp)
*lenp = 9;
return 1;
}
if (is_xsubstring(sid, SID_STRLEN)) {
if (lenp)
*lenp = SID_STRLEN;
return 1;
}
return 0;
}
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)
{
return parse_rhizome_bid_t(bid, hex, -1, NULL); // checks for nul terminator
}
int strn_to_rhizome_bid_t(rhizome_bid_t *bid, const char *hex, size_t hexlen)
{
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;
rhizome_bid_t tmp;
int n = fromhex(tmp.binary, hex, sizeof tmp.binary);
if (n != sizeof tmp.binary)
return -1;
if (endp)
*endp = hex + RHIZOME_BUNDLE_ID_STRLEN;
else if (hexlen == -1 && hex[RHIZOME_BUNDLE_ID_STRLEN] != '\0')
return -1;
if (bid)
*bid = tmp;
return 0;
}
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)
{
return parse_rhizome_filehash_t(hashp, hex, -1, NULL); // checks for nul terminator
}
int strn_to_rhizome_filehash_t(rhizome_filehash_t *hashp, const char *hex, size_t hexlen)
{
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;
rhizome_filehash_t tmp;
int n = fromhex(tmp.binary, hex, sizeof tmp.binary);
if (n != sizeof tmp.binary)
return -1;
if (endp)
*endp = hex + RHIZOME_FILEHASH_STRLEN;
else if (hexlen == -1 && hex[RHIZOME_FILEHASH_STRLEN] != '\0')
return -1;
if (hashp)
*hashp = tmp;
return 0;
}
int str_to_rhizome_bk_t(rhizome_bk_t *bkp, const char *hex)
{
return parse_rhizome_bk_t(bkp, hex, -1, NULL); // checks for nul terminator
}
int strn_to_rhizome_bk_t(rhizome_bk_t *bkp, const char *hex, size_t hexlen)
{
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;
rhizome_bk_t tmp;
int n = fromhex(tmp.binary, hex, sizeof tmp.binary);
if (n != sizeof tmp.binary)
return -1;
if (endp)
*endp = hex + RHIZOME_BUNDLE_KEY_STRLEN;
else if (hexlen == -1 && hex[RHIZOME_BUNDLE_KEY_STRLEN] != '\0')
return -1;
if (bkp)
*bkp = tmp;
return 0;
}
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;
}
return strn_to_rhizome_bk_t(bskp, text, textlen);
}
int rhizome_strn_is_bundle_crypt_key(const char *key)
{
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);
}
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';
}
/* 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 <[email protected]>
*/
int rhizome_str_is_manifest_name(const char *text)
{
while (*text && *text != '\n' && *text != '\r')
++text;
return *text == '\0';
}
int str_is_did(const char *did)
{
size_t len = 0;
return strn_is_did(did, &len) && did[len] == '\0';
}
int is_didchar(char c)
{
return isdigit(c) || c == '*' || c == '#' || c == '+';
}
int strn_is_did(const char *did, size_t *lenp)
{
int i;
for (i = 0; i < DID_MAXSIZE && is_didchar(did[i]); ++i)
;
if (i < DID_MINSIZE)
return 0;
if (lenp)
*lenp = i;
return 1;
}
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; }
}
uint64_t read_uint64(const unsigned char *o)
{
int i;
uint64_t v=0;
for(i=0;i<8;i++) v=(v<<8)|o[8-1-i];
return v;
}
uint32_t read_uint32(const unsigned char *o)
{
int i;
uint32_t v=0;
for(i=0;i<4;i++) v=(v<<8)|o[4-1-i];
return v;
}
uint16_t read_uint16(const unsigned char *o)
{
int i;
uint16_t v=0;
for(i=0;i<2;i++) v=(v<<8)|o[2-1-i];
return v;
}
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);
}