Quoting.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9#include "squid.h"
10#include "html/Quoting.h"
11
12/*
13 * HTML defines these characters as special entities that should be quoted.
14 */
15static struct {
16 unsigned char code;
17 const char *quote;
19
20{
21 /* NOTE: The quoted form MUST not be larger than 6 character.
22 * see close to the MemPool commend below
23 */
24 {
25 '<', "&lt;"
26 },
27 {
28 '>', "&gt;"
29 },
30 {
31 '"', "&quot;"
32 },
33 {
34 '&', "&amp;"
35 },
36 {
37 '\'', "&#39;"
38 },
39 {
40 0, NULL
41 }
42};
43
44/*
45 * html_do_quote - Returns a static buffer containing the quoted
46 * string.
47 */
48char *
49html_quote(const char *string)
50{
51 static char *buf = nullptr;
52 static size_t bufsize = 0;
53 const char *src;
54 char *dst;
55 int i;
56
57 /* XXX This really should be implemented using a MemPool, but
58 * MemPools are not yet available in lib...
59 */
60 if (!buf || strlen(string) * 6 > bufsize) {
61 xfree(buf);
62 bufsize = strlen(string) * 6 + 1;
63 buf = static_cast<char *>(xcalloc(bufsize, 1));
64 }
65 for (src = string, dst = buf; *src; src++) {
66 const char *escape = NULL;
67 const unsigned char ch = *src;
68
69 /* Walk thru the list of HTML Entities that must be quoted to
70 * display safely
71 */
72 for (i = 0; htmlstandardentities[i].code; i++) {
73 if (ch == htmlstandardentities[i].code) {
74 escape = htmlstandardentities[i].quote;
75 break;
76 }
77 }
78 /* Encode control chars just to be on the safe side, and make
79 * sure all 8-bit characters are encoded to protect from buggy
80 * clients
81 */
82 if (!escape && (ch <= 0x1F || ch >= 0x7f) && ch != '\n' && ch != '\r' && ch != '\t') {
83 static char dec_encoded[7];
84 snprintf(dec_encoded, sizeof dec_encoded, "&#%3d;", (int) ch);
85 escape = dec_encoded;
86 }
87 if (escape) {
88 /* Ok, An escaped form was found above. Use it */
89 strncpy(dst, escape, 7);
90 dst += strlen(escape);
91 } else {
92 /* Apparently there is no need to escape this character */
93 *dst++ = ch;
94 }
95 }
96 /* Nullterminate and return the result */
97 *dst = '\0';
98 return (buf);
99}
100
char * html_quote(const char *string)
Definition: Quoting.cc:49
unsigned char code
Definition: Quoting.cc:16
const char * quote
Definition: Quoting.cc:17
static struct @72 htmlstandardentities[]
#define xfree
#define NULL
Definition: types.h:145
void * xcalloc(size_t n, size_t sz)
Definition: xalloc.cc:71

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors