summaryrefslogtreecommitdiff
blob: 07c22d55689ab2a657513be9807d9d4f484df612 (plain)
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
#include "../include/extract_alloc.h"

#include "astring.h"
#include "mem.h"
#include "memento.h"

#include <assert.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


void extract_astring_init(extract_astring_t* string)
{
    string->chars = NULL;
    string->chars_num = 0;
}

void extract_astring_free(extract_alloc_t* alloc, extract_astring_t* string)
{
    extract_free(alloc, &string->chars);
    extract_astring_init(string);
}


int extract_astring_catl(extract_alloc_t* alloc, extract_astring_t* string, const char* s, size_t s_len)
{
    if (extract_realloc2(alloc, &string->chars, string->chars_num+1, string->chars_num + s_len + 1)) return -1;
    /* Coverity doesn't seem to realise that extract_realloc2() modifies
    string->chars. */
    /* coverity[deref_parm_field_in_call] */
    memcpy(string->chars + string->chars_num, s, s_len);
    string->chars[string->chars_num + s_len] = 0;
    string->chars_num += s_len;
    return 0;
}

int extract_astring_catc(extract_alloc_t* alloc, extract_astring_t* string, char c)
{
    return extract_astring_catl(alloc, string, &c, 1);
}

int extract_astring_cat(extract_alloc_t* alloc, extract_astring_t* string, const char* s)
{
    return extract_astring_catl(alloc, string, s, strlen(s));
}

int extract_astring_catf(extract_alloc_t* alloc, extract_astring_t* string, const char* format, ...)
{
    char* buffer = NULL;
    int e;
    va_list va;
    va_start(va, format);
    e = extract_vasprintf(alloc, &buffer, format, va);
    va_end(va);
    if (e < 0) return e;
    e = extract_astring_cat(alloc, string, buffer);
    extract_free(alloc, &buffer);
    return e;
}

int extract_astring_truncate(extract_astring_t* content, int len)
{
    assert((size_t) len <= content->chars_num);
    content->chars_num -= len;
    content->chars[content->chars_num] = 0;
    return 0;
}

int extract_astring_char_truncate_if(extract_astring_t* content, char c)
{
    if (content->chars_num && content->chars[content->chars_num-1] == c) {
        extract_astring_truncate(content, 1);
    }
    return 0;
}

int extract_astring_catc_unicode(
        extract_alloc_t* alloc,
        extract_astring_t* string,
        int c,
        int xml,
        int ascii_ligatures,
        int ascii_dash,
        int ascii_apostrophe
        )
{
    int ret = -1;

    if (0) {}

    /* Escape XML special characters. */
    else if (xml && c == '<')  extract_astring_cat(alloc, string, "&lt;");
    else if (xml && c == '>')  extract_astring_cat(alloc, string, "&gt;");
    else if (xml && c == '&')  extract_astring_cat(alloc, string, "&amp;");
    else if (xml && c == '"')  extract_astring_cat(alloc, string, "&quot;");
    else if (xml && c == '\'') extract_astring_cat(alloc, string, "&apos;");

    /* Expand ligatures. */
    else if (ascii_ligatures && c == 0xFB00)
    {
        if (extract_astring_cat(alloc, string, "ff")) goto end;
    }
    else if (ascii_ligatures && c == 0xFB01)
    {
        if (extract_astring_cat(alloc, string, "fi")) goto end;
    }
    else if (ascii_ligatures && c == 0xFB02)
    {
        if (extract_astring_cat(alloc, string, "fl")) goto end;
    }
    else if (ascii_ligatures && c == 0xFB03)
    {
        if (extract_astring_cat(alloc, string, "ffi")) goto end;
    }
    else if (ascii_ligatures && c == 0xFB04)
    {
        if (extract_astring_cat(alloc, string, "ffl")) goto end;
    }

    /* Convert some special characters to ascii. */
    else if (ascii_dash && c == 0x2212)
    {
        if (extract_astring_catc(alloc, string, '-')) goto end;
    }
    else if (ascii_apostrophe && c == 0x2019)
    {
        if (extract_astring_catc(alloc, string, '\'')) goto end;
    }

    /* Output ASCII verbatim. */
    else if (c >= 32 && c <= 127)
    {
        if (extract_astring_catc(alloc, string, (char) c)) goto end;
    }

    /* Escape all other characters. */
    else
    {
        if (xml)
        {
            char    buffer[32];
            if (c < 32
                    && (c != 0x9 && c != 0xa && c != 0xd)
                    )
            {
                /* Illegal xml character; see
                https://www.w3.org/TR/xml/#charsets. We replace with
                0xfffd, the unicode replacement character. */
                c = 0xfffd;
            }
            snprintf(buffer, sizeof(buffer), "&#x%x;", c);
            if (extract_astring_cat(alloc, string, buffer)) goto end;
        }
        else
        {
            /* Use utf8. */
            if (c < 0x80)
            {
                if (extract_astring_catc(alloc, string, (char) c)) return -1;
            }
            else if (c < 0x0800)
            {
                char cc[2] =
                {
                    (char) (((c >> 6) & 0x1f) | 0xc0),
                    (char) (((c >> 0) & 0x3f) | 0x80)
                };
                if (extract_astring_catl(alloc, string, cc, sizeof(cc))) return -1;
            }
            else if (c < 0x10000)
            {
                char cc[3] =
                {
                    (char) (((c >> 12) & 0x0f) | 0xe0),
                    (char) (((c >>  6) & 0x3f) | 0x80),
                    (char) (((c >>  0) & 0x3f) | 0x80)
                };
                if (extract_astring_catl(alloc, string, cc, sizeof(cc))) return -1;
            }
            else if (c < 0x110000)
            {
                char cc[4] =
                {
                    (char) (((c >> 18) & 0x07) | 0xf0),
                    (char) (((c >> 12) & 0x3f) | 0x80),
                    (char) (((c >>  6) & 0x3f) | 0x80),
                    (char) (((c >>  0) & 0x3f) | 0x80)
                };
                if (extract_astring_catl(alloc, string, cc, sizeof(cc))) return -1;
            }
            else
            {
                /* Use replacement character. */
                char cc[4] = { (char) 0xef, (char) 0xbf, (char) 0xbd, 0};
                if (extract_astring_catl(alloc, string, cc, sizeof(cc))) return -1;
            }
        }
    }

    ret = 0;

    end:
    return ret;
}

int extract_astring_catc_unicode_xml(extract_alloc_t* alloc, extract_astring_t* string, int c)
{
    /* Fixme, better to use ascii_ligatures=0, but that requires updates to
    expected output files. */
    return extract_astring_catc_unicode(
            alloc,
            string,
            c,
            1 /*xml*/,
            1 /*ascii_ligatures*/,
            0 /*ascii_dash*/,
            0 /*ascii_apostrophe*/
            );
}