-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstring.c
More file actions
423 lines (339 loc) · 9.01 KB
/
string.c
File metadata and controls
423 lines (339 loc) · 9.01 KB
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/*
* Gophernicus - Copyright (c) 2009-2014 Kim Holviala <kim@holviala.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "ugopherserver.h"
/*
* Repeat a character num times and zero-terminate
*/
void strrepeat(char *dest, char c, size_t num)
{
memset(dest, c, num);
dest[num] = '\0';
}
/*
* Replace characters in-place
*/
void strreplace(char *str, char from, char to)
{
while (*str) {
if (*str == from) *str = to;
str++;
}
}
/*
* Cut string to width, return resulting width (UTF-8 aware)
*/
size_t strcut(char *str, size_t width)
{
unsigned char c = '\0';
int w = 0;
int i;
while (width-- && (c = *str++)) {
if (c >= 0x80 && (*str & 0xc0) == 0x80) {
i = 0;
if ((c & 0xf8) == 0xf0) i = 3;
else if ((c & 0xf0) == 0xe0) i = 2;
else if ((c & 0xe0) == 0xc0) i = 1;
while (i--) if (!*str++) break;
}
w++;
}
if (c) *str = '\0';
return w;
}
/*
* Match key and return value (key: value)
*/
char *strkey(char *header, char *key)
{
char *c;
size_t len;
if ((len = strlen(key)) == 0) return NULL;
if (strncasecmp(header, key, len) == MATCH) {
c = header + len;
do { c++; } while (*c == ' ' || *c == '\t');
if (*c != ':') return NULL;
do { c++; } while (*c == ' ' || *c == '\t');
return c;
}
return NULL;
}
/*
* Return last character of a string
*/
char strlast(char *str)
{
int len;
if ((len = (int)strlen(str) - 1) >= 0) return str[len];
else return 0;
}
/*
* Remove CRLF from a string
*/
void chomp(char *str)
{
char *c;
if ((c = strrchr(str, '\n'))) *c = '\0';
if ((c = strrchr(str, '\r'))) *c = '\0';
}
/*
* Return charset name
*/
char *strcharset(int charset)
{
if (charset == AUTO) return "auto";
if (charset == US_ASCII) return "US-ASCII";
if (charset == ISO_8859_1) return "ISO-8859-1";
if (charset == UTF_8) return "UTF-8";
return "(unknown)";
}
/*
* Convert a string between UTF-8, ISO-8859-1 and US-ASCII
*/
void strniconv(int charset, char *out, char *in, size_t outsize)
{
char ascii[] = ASCII;
unsigned long c;
size_t len;
int i;
/* Loop through the input string */
len = strlen(in);
while (--outsize && len > 0) {
/* Get one input char */
c = (unsigned char) *in++;
len--;
/* 7-bit chars are the same in all three charsets */
if (c < 0x80) {
*out++ = (unsigned char) c;
continue;
}
/* Assume ISO-8859-1 which requires 0 extra bytes */
i = 0;
/* UTF-8? (We'll actually check the next char here, not current) */
if ((*in & 0xc0) == 0x80) {
/* Four-byte UTF-8? */
if ((c & 0xf8) == 0xf0 && len >= 3) { c &= 0x07; i = 3; }
/* Three-byte UTF-8? */
else if ((c & 0xf0) == 0xe0 && len >= 2) { c &= 0x0f; i = 2; }
/* Two-byte UTF-8? */
else if ((c & 0xe0) == 0xc0 && len >= 1) { c &= 0x1f; i = 1; }
/* Parse rest of the UTF-8 bytes */
while (i--) {
c <<= 6;
c |= *in++ & 0x3f;
len--;
}
}
/*
* At this point we've got one 32bit UTF character in c and
* we're ready to convert it to the specified output charset
*/
/* Handle UTF-8 */
if (charset == UTF_8) {
i = 0;
/* Two-byte encoding? */
if (c < 0x800 && outsize > 2) { *out++ = (c >> 6) | 0xc0; i = 1; }
/* Three-byte encoding? */
else if (c < 0x10000 && outsize > 3) { *out++ = (c >> 12) | 0xe0; i = 2; }
/* Four-byte encoding? */
else if (c < 0x110000 && outsize > 4) { *out++ = (c >> 18) | 0xf0; i = 3; }
/* Encode rest of the UTF-8 bytes */
while (i--) {
*out++ = ((c >> (i * 6)) & 0x3f) | 0x80;
outsize--;
}
continue;
}
/* Handle ISO-8859-1 */
if (charset == ISO_8859_1) {
if (c >= 0xa0 && c <= 0xff)
*out++ = (unsigned char) c;
else
*out++ = UNKNOWN;
continue;
}
/* Handle all other charsets as 7-bit US-ASCII */
if (c >= 0x80 && c <= 0xff)
*out++ = ascii[c - 0x80];
else
*out++ = UNKNOWN;
}
/* Zero-terminate output */
*out = '\0';
}
/*
* Encode string with =OCT encoding
*/
void strnencode(char *out, const char *in, size_t outsize)
{
unsigned char c;
/* Loop through the input string */
while (--outsize) {
/* End of source? */
if (!(c = *in++)) break;
/* Need to encode the char? */
if (c < '+' || c > '~') {
/* Can we fit the encoded version into outbuffer? */
if (outsize < 5) break;
/* Output encoded char */
snprintf(out, outsize, "=%.3o", c);
out += 4;
}
/* Copy regular chars */
else *out++ = c;
}
/* Zero-terminate output */
*out = '\0';
}
/*
* Decode both %HEX and #OCT encodings
*/
void strndecode(char *out, char *in, size_t outsize)
{
unsigned char c;
unsigned int i;
/* Loop through the input string */
while (--outsize) {
/* End of source? */
if (!(c = *in++)) break;
/* Parse %hex encoding */
if (c == '%' && strlen(in) >= 2) {
sscanf(in, "%2x", &i);
*out++ = i;
in += 2;
continue;
}
/* Parse #octal encoding */
if (c == '#' && strlen(in) >= 3) {
sscanf(in, "%3o", &i);
*out++ = i;
in += 3;
continue;
}
/* Parse #octal encoding */
if (c == '=' && strlen(in) >= 3) {
sscanf(in, "%3o", &i);
*out++ = i;
in += 3;
continue;
}
/* Copy non-encoded chars */
*out++ = c;
}
/* Zero-terminate output */
*out = '\0';
}
/*
* Format number to human-readable filesize with unit
*/
void strfsize(char *out, off_t size, size_t outsize)
{
static char *unit[] = { UNITS };
int u;
float s;
/* Start with kilobytes */
s = ((float) size) / 1024;
u = 0;
/* Loop through the units until the size is small enough */
while (s >= 1000 && unit[(u + 1)]) {
s = s / 1024;
u++;
}
/* Format size */
snprintf(out, outsize, "%7.1f %s", s, unit[u]);
}
#ifndef HAVE_STRLCPY
/*
* Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <string.h>
/*
* Copy src to string dst of size siz. At most siz-1 characters
* will be copied. Always NUL terminates (unless siz == 0).
* Returns strlen(src); if retval >= siz, truncation occurred.
*/
size_t strlcpy(char *dst, const char *src, size_t siz)
{
char *d = dst;
const char *s = src;
size_t n = siz;
/* Copy as many bytes as will fit */
if (n != 0) {
while (--n != 0) {
if ((*d++ = *s++) == '\0')
break;
}
}
/* Not enough room in dst, add NUL and traverse rest of src */
if (n == 0) {
if (siz != 0)
*d = '\0'; /* NUL-terminate dst */
while (*s++)
;
}
return(s - src - 1); /* count does not include NUL */
}
/*
* Appends src to string dst of size siz (unlike strncat, siz is the
* full size of dst, not space left). At most siz-1 characters
* will be copied. Always NUL terminates (unless siz <= strlen(dst)).
* Returns strlen(src) + MIN(siz, strlen(initial dst)).
* If retval >= siz, truncation occurred.
*/
size_t strlcat(char *dst, const char *src, size_t siz)
{
char *d = dst;
const char *s = src;
size_t n = siz;
size_t dlen;
/* Find the end of dst and adjust bytes left but don't go past end */
while (n-- != 0 && *d != '\0')
d++;
dlen = d - dst;
n = siz - dlen;
if (n == 0)
return(dlen + strlen(s));
while (*s != '\0') {
if (n != 1) {
*d++ = *s;
n--;
}
s++;
}
*d = '\0';
return(dlen + (s - src)); /* count does not include NUL */
}
#endif