MCPcopy Index your code
hub / github.com/git/git / cmd__xml_encode

Function cmd__xml_encode

t/helper/test-xml-encode.c:9–80  ·  view source on GitHub ↗

* Encodes (possibly incorrect) UTF-8 on to , to be embedded * in an XML file. */

Source from the content-addressed store, hash-verified

7 * in an XML file.
8 */
9int cmd__xml_encode(int argc UNUSED, const char **argv UNUSED)
10{
11 unsigned char buf[1024], tmp[4], *tmp2 = NULL;
12 ssize_t cur = 0, len = 1, remaining = 0;
13 unsigned char ch;
14
15 for (;;) {
16 if (++cur == len) {
17 len = xread(0, buf, sizeof(buf));
18 if (!len)
19 return 0;
20 if (len < 0)
21 die_errno("Could not read <stdin>");
22 cur = 0;
23 }
24 ch = buf[cur];
25
26 if (tmp2) {
27 if ((ch & 0xc0) != 0x80) {
28 fputs(utf8_replace_character, stdout);
29 tmp2 = NULL;
30 cur--;
31 continue;
32 }
33 *tmp2 = ch;
34 tmp2++;
35 if (--remaining == 0) {
36 fwrite(tmp, tmp2 - tmp, 1, stdout);
37 tmp2 = NULL;
38 }
39 continue;
40 }
41
42 if (!(ch & 0x80)) {
43 /* 0xxxxxxx */
44 if (ch == '&')
45 fputs("&amp;", stdout);
46 else if (ch == '\'')
47 fputs("&apos;", stdout);
48 else if (ch == '"')
49 fputs("&quot;", stdout);
50 else if (ch == '<')
51 fputs("&lt;", stdout);
52 else if (ch == '>')
53 fputs("&gt;", stdout);
54 else if (ch >= 0x20)
55 fputc(ch, stdout);
56 else if (ch == 0x09 || ch == 0x0a || ch == 0x0d)
57 fprintf(stdout, "&#x%02x;", ch);
58 else
59 fputs(utf8_replace_character, stdout);
60 } else if ((ch & 0xe0) == 0xc0) {
61 /* 110XXXXx 10xxxxxx */
62 tmp[0] = ch;
63 remaining = 1;
64 tmp2 = tmp + 1;
65 } else if ((ch & 0xf0) == 0xe0) {
66 /* 1110XXXX 10Xxxxxx 10xxxxxx */

Callers

nothing calls this directly

Calls 2

xreadFunction · 0.85
die_errnoFunction · 0.85

Tested by

no test coverage detected