-
Notifications
You must be signed in to change notification settings - Fork 446
/
textutils.c
497 lines (418 loc) · 11.7 KB
/
textutils.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/* textutils.c -- non-bioinformatics utility routines for text etc.
Copyright (C) 2016, 2018-2020 Genome Research Ltd.
Author: John Marshall <jm18@sanger.ac.uk>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE. */
#define HTS_BUILDING_LIBRARY // Enables HTSLIB_EXPORT, see htslib/hts_defs.h
#include <config.h>
#include <stdio.h>
#include <string.h>
#include "htslib/hfile.h"
#include "htslib/kstring.h"
#include "htslib/sam.h" // For stringify_argv() declaration
#include "hts_internal.h"
static int dehex(char c)
{
if (c >= 'a' && c <= 'f') return c - 'a' + 10;
else if (c >= 'A' && c <= 'F') return c - 'A' + 10;
else if (c >= '0' && c <= '9') return c - '0';
else return -1; // Hence dehex('\0') = -1
}
int hts_decode_percent(char *dest, size_t *destlen, const char *s)
{
char *d = dest;
int hi, lo;
while (*s) {
if (*s == '%' && (hi = dehex(s[1])) >= 0 && (lo = dehex(s[2])) >= 0) {
*d++ = (hi << 4) | lo;
s += 3;
}
else *d++ = *s++;
}
*d = '\0';
*destlen = d - dest;
return 0;
}
static int debase64(char c)
{
if (c >= 'a' && c <= 'z') return c - 'a' + 26;
else if (c >= 'A' && c <= 'Z') return c - 'A';
else if (c >= '0' && c <= '9') return c - '0' + 52;
else if (c == '/') return 63;
else if (c == '+') return 62;
else return -1; // Hence debase64('\0') = -1
}
size_t hts_base64_decoded_length(size_t len)
{
size_t nquartets = (len + 2) / 4;
return 3 * nquartets;
}
int hts_decode_base64(char *dest, size_t *destlen, const char *s)
{
char *d = dest;
int x0, x1, x2, x3;
while (1) {
x0 = debase64(*s++);
x1 = (x0 >= 0)? debase64(*s++) : -1;
x2 = (x1 >= 0)? debase64(*s++) : -1;
x3 = (x2 >= 0)? debase64(*s++) : -1;
if (x3 < 0) break;
*d++ = (x0 << 2) | (x1 >> 4);
*d++ = (x1 << 4) | (x2 >> 2);
*d++ = (x2 << 6) | x3;
}
if (x1 >= 0) *d++ = (x0 << 2) | (x1 >> 4);
if (x2 >= 0) *d++ = (x1 << 4) | (x2 >> 2);
*destlen = d - dest;
return 0;
}
static char *encode_utf8(char *s, unsigned x)
{
if (x >= 0x10000) {
*s++ = 0xF0 | (x >> 18);
*s++ = 0x80 | ((x >> 12) & 0x3F);
*s++ = 0x80 | ((x >> 6) & 0x3F);
*s++ = 0x80 | (x & 0x3F);
}
else if (x >= 0x800) {
*s++ = 0xE0 | (x >> 12);
*s++ = 0x80 | ((x >> 6) & 0x3F);
*s++ = 0x80 | (x & 0x3F);
}
else if (x >= 0x80) {
*s++ = 0xC0 | (x >> 6);
*s++ = 0x80 | (x & 0x3F);
}
else *s++ = x;
return s;
}
static char *sscan_string(char *s)
{
char *d = s;
int d1, d2, d3, d4;
for (;;) switch (*s) {
case '\\':
switch (s[1]) {
case '\0': *d = '\0'; return s+1;
case 'b': *d++ = '\b'; s += 2; break;
case 'f': *d++ = '\f'; s += 2; break;
case 'n': *d++ = '\n'; s += 2; break;
case 'r': *d++ = '\r'; s += 2; break;
case 't': *d++ = '\t'; s += 2; break;
default: *d++ = s[1]; s += 2; break;
case 'u':
if ((d1 = dehex(s[2])) >= 0 && (d2 = dehex(s[3])) >= 0 &&
(d3 = dehex(s[4])) >= 0 && (d4 = dehex(s[5])) >= 0) {
d = encode_utf8(d, d1 << 12 | d2 << 8 | d3 << 4 | d4);
s += 6;
}
break;
}
break;
case '"':
*d = '\0';
return s+1;
case '\0':
*d = '\0';
return s;
default:
*d++ = *s++;
break;
}
}
static int fscan_string(hFILE *fp, kstring_t *d)
{
int c, d1, d2, d3, d4;
uint32_t e = 0;
while ((c = hgetc(fp)) != EOF) switch (c) {
case '\\':
if ((c = hgetc(fp)) == EOF) return e == 0 ? 0 : -1;
switch (c) {
case 'b': e |= kputc('\b', d) < 0; break;
case 'f': e |= kputc('\f', d) < 0; break;
case 'n': e |= kputc('\n', d) < 0; break;
case 'r': e |= kputc('\r', d) < 0; break;
case 't': e |= kputc('\t', d) < 0; break;
default: e |= kputc(c, d) < 0; break;
case 'u':
if ((c = hgetc(fp)) != EOF && (d1 = dehex(c)) >= 0 &&
(c = hgetc(fp)) != EOF && (d2 = dehex(c)) >= 0 &&
(c = hgetc(fp)) != EOF && (d3 = dehex(c)) >= 0 &&
(c = hgetc(fp)) != EOF && (d4 = dehex(c)) >= 0) {
char buf[8];
char *lim = encode_utf8(buf, d1 << 12 | d2 << 8 | d3 << 4 | d4);
e |= kputsn(buf, lim - buf, d) < 0;
}
break;
}
break;
case '"':
return e == 0 ? 0 : -1;
default:
e |= kputc(c, d) < 0;
break;
}
return e == 0 ? 0 : -1;
}
static char token_type(hts_json_token *token)
{
const char *s = token->str;
switch (*s) {
case 'f':
return (strcmp(s, "false") == 0)? 'b' : '?';
case 'n':
return (strcmp(s, "null") == 0)? '.' : '?';
case 't':
return (strcmp(s, "true") == 0)? 'b' : '?';
case '-':
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
return 'n';
default:
return '?';
}
}
HTSLIB_EXPORT
hts_json_token * hts_json_alloc_token(void) {
return calloc(1, sizeof(hts_json_token));
}
HTSLIB_EXPORT
char hts_json_token_type(hts_json_token *token) {
return token->type;
}
HTSLIB_EXPORT
void hts_json_free_token(hts_json_token *token) {
free(token);
}
HTSLIB_EXPORT
char *hts_json_token_str(hts_json_token *token) {
return token->str;
}
HTSLIB_EXPORT
char hts_json_snext(char *str, size_t *state, hts_json_token *token)
{
char *s = &str[*state >> 2];
int hidden = *state & 3;
if (hidden) {
*state &= ~3;
return token->type = "?}]?"[hidden];
}
#define STATE(s,h) (((s) - str) << 2 | (h))
for (;;) switch (*s) {
case ' ':
case '\t':
case '\r':
case '\n':
case ',':
case ':':
s++;
continue;
case '\0':
return token->type = '\0';
case '{':
case '[':
case '}':
case ']':
*state = STATE(s+1, 0);
return token->type = *s;
case '"':
token->str = s+1;
*state = STATE(sscan_string(s+1), 0);
return token->type = 's';
default:
token->str = s;
s += strcspn(s, " \t\r\n,]}");
hidden = (*s == '}')? 1 : (*s == ']')? 2 : 0;
if (*s != '\0') *s++ = '\0';
*state = STATE(s, hidden);
return token->type = token_type(token);
}
#undef STATE
}
HTSLIB_EXPORT
char hts_json_fnext(struct hFILE *fp, hts_json_token *token, kstring_t *kstr)
{
char peek;
int c;
for (;;) switch (c = hgetc(fp)) {
case ' ':
case '\t':
case '\r':
case '\n':
case ',':
case ':':
continue;
case EOF:
return token->type = '\0';
case '{':
case '[':
case '}':
case ']':
return token->type = c;
case '"':
kstr->l = 0;
fscan_string(fp, kstr);
if (kstr->l == 0) kputsn("", 0, kstr);
token->str = kstr->s;
return token->type = 's';
default:
kstr->l = 0;
kputc(c, kstr);
while (hpeek(fp, &peek, 1) == 1 && !strchr(" \t\r\n,]}", peek)) {
if ((c = hgetc(fp)) == EOF) break;
kputc(c, kstr);
}
token->str = kstr->s;
return token->type = token_type(token);
}
}
typedef char hts_json_nextfn(void *arg1, void *arg2, hts_json_token *token);
static char skip_value(char type, hts_json_nextfn *next, void *arg1, void *arg2)
{
hts_json_token token;
int level;
switch (type? type : next(arg1, arg2, &token)) {
case '\0':
return '\0';
case '?':
case '}':
case ']':
return '?';
case '{':
case '[':
level = 1;
break;
default:
return 'v';
}
while (level > 0)
switch (next(arg1, arg2, &token)) {
case '\0':
return '\0';
case '?':
return '?';
case '{':
case '[':
level++;
break;
case '}':
case ']':
--level;
break;
default:
break;
}
return 'v';
}
static char snext(void *arg1, void *arg2, hts_json_token *token)
{
return hts_json_snext(arg1, arg2, token);
}
HTSLIB_EXPORT
char hts_json_sskip_value(char *str, size_t *state, char type)
{
return skip_value(type, snext, str, state);
}
static char fnext(void *arg1, void *arg2, hts_json_token *token)
{
return hts_json_fnext(arg1, token, arg2);
}
HTSLIB_EXPORT
char hts_json_fskip_value(struct hFILE *fp, char type)
{
kstring_t str = { 0, 0, NULL };
char ret = skip_value(type, fnext, fp, &str);
free(str.s);
return ret;
}
/*
* A function to help with construction of CL tags in @PG records.
* Takes an argc, argv pair and returns a single space-separated string.
* This string should be deallocated by the calling function.
*
* Returns malloced char * on success
* NULL on failure
*/
char *stringify_argv(int argc, char *argv[]) {
char *str, *cp;
size_t nbytes = 1;
int i, j;
/* Allocate */
for (i = 0; i < argc; i++) {
if (i > 0) nbytes += 1;
nbytes += strlen(argv[i]);
}
if (!(str = malloc(nbytes)))
return NULL;
/* Copy */
cp = str;
for (i = 0; i < argc; i++) {
if (i > 0) *cp++ = ' ';
j = 0;
while (argv[i][j]) {
if (argv[i][j] == '\t')
*cp++ = ' ';
else
*cp++ = argv[i][j];
j++;
}
}
*cp++ = 0;
return str;
}
/* Utility function for printing possibly malicious text data
*/
const char *
hts_strprint(char *buf, size_t buflen, char quote, const char *s, size_t len)
{
const char *slim = (len < SIZE_MAX)? &s[len] : NULL;
char *t = buf, *bufend = buf + buflen;
size_t qlen = quote? 1 : 0;
if (quote) *t++ = quote;
for (; slim? (s < slim) : (*s); s++) {
char c;
size_t clen;
switch (*s) {
case '\n': c = 'n'; clen = 2; break;
case '\r': c = 'r'; clen = 2; break;
case '\t': c = 't'; clen = 2; break;
case '\0': c = '0'; clen = 2; break;
case '\\': c = '\\'; clen = 2; break;
default:
c = *s;
if (c == quote) clen = 2;
else clen = isprint_c(c)? 1 : 4;
break;
}
if (t-buf + clen + qlen >= buflen) {
while (t-buf + 3 + qlen >= buflen) t--;
if (quote) *t++ = quote;
strcpy(t, "...");
return buf;
}
if (clen == 4) {
snprintf(t, bufend - t, "\\x%02X", (unsigned char) c);
t += clen;
}
else {
if (clen == 2) *t++ = '\\';
*t++ = c;
}
}
if (quote) *t++ = quote;
*t = '\0';
return buf;
}