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
|
/* This file is part of GNU Dico
Copyright (C) 2008-2024 Sergey Poznyakoff
GNU Dico 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 3, or (at your option)
any later version.
GNU Dico 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 GNU Dico. If not, see <http://www.gnu.org/licenses/>. */
#include <config.h>
#include <dico.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
struct hdr_buf
{
char *base;
size_t size;
size_t level;
};
#define HDR_BUF_SIZE 128
#define HDR_BUF_INIT { NULL, 0, 0 }
#define ISWS(c) ((c) == ' ' || (c) == '\t')
static int
hdr_buf_append(struct hdr_buf *buf, const char *str, size_t len)
{
if (len == 0)
return 0;
if (buf->size == 0) {
size_t size = HDR_BUF_SIZE * ((len + HDR_BUF_SIZE - 1) / HDR_BUF_SIZE);
buf->base = malloc(size);
if (!buf->base)
return 1;
buf->size = size;
} else if (buf->level + len > buf->size) {
char *p;
size_t diff = buf->level + len - buf->size;
size_t size = buf->size * ((diff + buf->size - 1) / buf->size + 1);
if (size < buf->size) {
errno = ENOMEM;
return 1;
}
p = realloc(buf->base, size);
if (!p)
return 1;
buf->base = p;
buf->size = size;
}
memcpy(buf->base + buf->level, str, len);
buf->level += len;
return 0;
}
#define hdr_buf_clear(b) do { (b)->level = 0; } while (0)
#define hdr_buf_free(b) do { free((b)->base); } while (0)
static int
collect_line(const char **ptext, dico_assoc_list_t asc, struct hdr_buf *hbuf)
{
const char *text = *ptext;
char c, *p;
size_t n;
hdr_buf_clear(hbuf);
do {
if (ISWS(*text)) {
while (*text && ISWS(*text))
text++;
text--;
}
n = strcspn(text, "\n");
if (n == 0) {
text += strlen(text);
break;
}
if (hdr_buf_append(hbuf, text, n))
return 1;
text += n;
if (!*text)
break;
text++;
} while (ISWS(*text));
c = 0;
if (hdr_buf_append(hbuf, &c, 1))
return 1;
p = strchr(hbuf->base, ':');
if (!p) {
errno = EINVAL;
return 1;
}
*p++ = 0;
while (*p && ISWS(*p))
p++;
if (dico_assoc_append(asc, hbuf->base, p))
return 1;
*ptext = text;
return 0;
}
int
dico_header_parse(dico_assoc_list_t *pasc, const char *text)
{
int rc = 0;
struct hdr_buf hbuf = HDR_BUF_INIT;
dico_assoc_list_t asc = dico_assoc_create(DICO_ASSOC_CI|DICO_ASSOC_MULT);
if (!asc)
return 1;
if (text) {
while (*text && *text != '\n'
&& (rc = collect_line(&text, asc, &hbuf)) == 0)
;
hdr_buf_free(&hbuf);
}
if (rc) {
int ec = errno;
dico_assoc_destroy(&asc);
errno = ec;
} else
*pasc = asc;
return rc;
}
|