forked from qmail/qmailadmin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
printh.c
211 lines (181 loc) · 5.5 KB
/
printh.c
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
/*
* $Id: printh.c,v 1.1.2.2 2004-11-14 18:05:55 tomcollins Exp $
* Copyright (C) 2004 Tom Logic LLC
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
#include "printh.h"
/* included from printh.h:
#include <stdlib.h>
#include <stdarg.h>
*/
#include <string.h>
#include <stdio.h>
#include <ctype.h>
/* vsnprinth - Custom version of sprintf for escaping strings for use on HTML
* pages and in cgi script parameters.
*
* (based on qnprintf from vpopmail)
*
* int vsnprinth (char *buffer, size_t size, const char *format, va_list ap)
* int snprinth (char *buffer, size_t size, const char *format, ...);
* int printh (const char *format, ...);
*
* buffer - buffer to print string to
* size - size of buffer
* format - a printf-style format string*
* ... - variable arguments for the format string
*
* NOTE: Currently supported formats: %%, %c, %s, %d/%i, %u, %ld/%li, %lu
* Since this function was designed to escape strings for use on HTML pages,
* the formats don't support any extended options.
*
* Extra formats: %C (like %s, but converts string to cgi-parameter safe
* version) and %H (like %s, but converts to HTML-safe version)
*
* Returns the number of characters that would have been printed to buffer
* if it was big enough. (i.e., if return value is larger than (size-1),
* buffer received an incomplete copy of the formatted string).
*
* It is possible to call snprinth with a NULL buffer of 0 bytes to determine
* how large the buffer needs to be. This is inefficient, as snprinth has
* to run twice.
*
* snprinth written November 2004 by Tom Collins <[email protected]>
* qnprintf written February 2004 by Tom Collins <[email protected]>
*/
int vsnprinth (char *buffer, size_t size, const char *format, va_list ap)
{
const char hex[] = "0123456789abcdef";
int printed; /* number of characters that would have been printed */
const char *f; /* current position in format string */
char *b; /* current position in output buffer */
char n[20]; /* buffer to hold string representation of number */
char *s; /* pointer to string to insert */
char *copy; /* pointer to replacement string for special char */
int stringtype;
#define SPRINTH_NORMAL 0
#define SPRINTH_HTML 1
#define SPRINTH_CGI 2
if (buffer == NULL && size > 0) return -1;
printed = 0;
b = buffer;
for (f = format; *f != '\0'; f++) {
if (*f != '%') {
if (++printed < size) *b++ = *f;
} else {
f++;
s = n;
stringtype = SPRINTH_NORMAL;
switch (*f) {
case '%':
strcpy (n, "%");
break;
case 'c':
snprintf (n, sizeof(n), "%c", va_arg (ap, int));
break;
case 'd':
case 'i':
snprintf (n, sizeof(n), "%d", va_arg (ap, int));
break;
case 'u':
snprintf (n, sizeof(n), "%u", va_arg (ap, unsigned int));
break;
case 'l':
f++;
switch (*f) {
case 'd':
case 'i':
snprintf (n, sizeof(n), "%ld", va_arg (ap, long));
break;
case 'u':
snprintf (n, sizeof(n), "%lu", va_arg (ap, unsigned long));
break;
default:
strcpy (n, "*");
}
break;
case 's':
s = va_arg (ap, char *);
break;
case 'H':
s = va_arg (ap, char *);
stringtype = SPRINTH_HTML;
break;
case 'C':
s = va_arg (ap, char *);
stringtype = SPRINTH_CGI;
break;
default:
strcpy (n, "*");
}
/* now copy the string parameter into the buffer */
while (*s != '\0') {
copy = NULL; /* default to no special handling */
if (stringtype == SPRINTH_HTML) {
switch (*s) {
case '"': copy = """; break;
case '<': copy = "<"; break;
case '>': copy = ">"; break;
case '&': copy = "&"; break;
}
} else if (stringtype == SPRINTH_CGI) {
if (*s == ' ')
copy = strcpy (n, "+");
else if (! isalnum(*s) && (strchr("._-", *s) == NULL)) {
copy = n;
sprintf (n, "%%%c%c", hex[*s >> 4 & 0x0F], hex[*s & 0x0F]);
}
}
if (copy == NULL) {
if (++printed < size) *b++ = *s;
} else {
/* replace *s with buffer pointed to by copy */
while (*copy != '\0') {
if (++printed < size) *b++ = *copy;
copy++;
}
}
s++;
}
}
}
*b = '\0';
/* If the formatted string doesn't fit in the buffer, zero out the buffer. */
if (printed >= size) {
memset (buffer, '\0', size);
}
return printed;
}
int snprinth (char *buffer, size_t size, const char *format, ...)
{
int ret;
va_list argp;
va_start (argp, format);
ret = vsnprinth (buffer, size, format, argp);
va_end (argp);
return ret;
}
int printh (const char *format, ...)
{
int ret;
char buffer[1024];
va_list argp;
va_start (argp, format);
ret = vsnprinth (buffer, sizeof(buffer), format, argp);
va_end (argp);
printf ("%s", buffer);
return ret;
}