]>
git.ipfire.org Git - thirdparty/squid.git/blob - lib/html_quote.c
2 * Copyright (C) 1996-2014 The Squid Software Foundation and contributors
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.
10 #include "html_quote.h"
17 * HTML defines these characters as special entities that should be quoted.
22 } htmlstandardentities
[] =
25 /* NOTE: The quoted form MUST not be larger than 6 character.
26 * see close to the MemPool commend below
49 * html_do_quote - Returns a static buffer containing the quoted
53 html_quote(const char *string
)
56 static size_t bufsize
= 0;
61 /* XXX This really should be implemented using a MemPool, but
62 * MemPools are not yet available in lib...
64 if (buf
== NULL
|| strlen(string
) * 6 > bufsize
) {
66 bufsize
= strlen(string
) * 6 + 1;
67 buf
= xcalloc(bufsize
, 1);
69 for (src
= string
, dst
= buf
; *src
; src
++) {
70 const char *escape
= NULL
;
71 const unsigned char ch
= *src
;
73 /* Walk thru the list of HTML Entities that must be quoted to
76 for (i
= 0; htmlstandardentities
[i
].code
; i
++) {
77 if (ch
== htmlstandardentities
[i
].code
) {
78 escape
= htmlstandardentities
[i
].quote
;
82 /* Encode control chars just to be on the safe side, and make
83 * sure all 8-bit characters are encoded to protect from buggy
86 if (!escape
&& (ch
<= 0x1F || ch
>= 0x7f) && ch
!= '\n' && ch
!= '\r' && ch
!= '\t') {
87 static char dec_encoded
[7];
88 snprintf(dec_encoded
, sizeof dec_encoded
, "&#%3d;", (int) ch
);
92 /* Ok, An escaped form was found above. Use it */
93 strncpy(dst
, escape
, 6);
94 dst
+= strlen(escape
);
96 /* Apparently there is no need to escape this character */
100 /* Nullterminate and return the result */