1 /* $NetBSD: vis.c,v 1.34 2005/11/18 08:32:46 martin Exp $ */ 2 3 /*- 4 * Copyright (c) 1989, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 /*- 33 * Copyright (c) 1999, 2005 The NetBSD Foundation, Inc. 34 * All rights reserved. 35 * 36 * Redistribution and use in source and binary forms, with or without 37 * modification, are permitted provided that the following conditions 38 * are met: 39 * 1. Redistributions of source code must retain the above copyright 40 * notice, this list of conditions and the following disclaimer. 41 * 2. Redistributions in binary form must reproduce the above copyright 42 * notice, this list of conditions and the following disclaimer in the 43 * documentation and/or other materials provided with the distribution. 44 * 3. All advertising materials mentioning features or use of this software 45 * must display the following acknowledgement: 46 * This product includes software developed by the NetBSD 47 * Foundation, Inc. and its contributors. 48 * 4. Neither the name of The NetBSD Foundation nor the names of its 49 * contributors may be used to endorse or promote products derived 50 * from this software without specific prior written permission. 51 * 52 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 53 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 54 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 55 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 56 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 57 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 58 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 59 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 60 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 61 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 62 * POSSIBILITY OF SUCH DAMAGE. 63 */ 64 65 #include <sys/cdefs.h> 66 #if defined(LIBC_SCCS) && !defined(lint) 67 __RCSID("$NetBSD: vis.c,v 1.34 2005/11/18 08:32:46 martin Exp $"); 68 #endif /* LIBC_SCCS and not lint */ 69 70 #include "namespace.h" 71 #include <sys/types.h> 72 73 #include <assert.h> 74 #include <vis.h> 75 #include <stdlib.h> 76 77 #ifdef __weak_alias 78 __weak_alias(strsvis,_strsvis) 79 __weak_alias(strsvisx,_strsvisx) 80 __weak_alias(strvis,_strvis) 81 __weak_alias(strvisx,_strvisx) 82 __weak_alias(svis,_svis) 83 __weak_alias(vis,_vis) 84 #endif 85 86 #if !HAVE_VIS || !HAVE_SVIS 87 #include <ctype.h> 88 #include <limits.h> 89 #include <stdio.h> 90 #include <string.h> 91 92 #undef BELL 93 #define BELL '\a' 94 95 #define isoctal(c) (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7') 96 #define iswhite(c) (c == ' ' || c == '\t' || c == '\n') 97 #define issafe(c) (c == '\b' || c == BELL || c == '\r') 98 #define xtoa(c) "0123456789abcdef"[c] 99 100 #define MAXEXTRAS 5 101 102 103 #define MAKEEXTRALIST(flag, extra, orig_str) \ 104 do { \ 105 const char *orig = orig_str; \ 106 const char *o = orig; \ 107 char *e; \ 108 while (*o++) \ 109 continue; \ 110 extra = malloc((size_t)((o - orig) + MAXEXTRAS)); \ 111 if (!extra) break; \ 112 for (o = orig, e = extra; (*e++ = *o++) != '\0';) \ 113 continue; \ 114 e--; \ 115 if (flag & VIS_SP) *e++ = ' '; \ 116 if (flag & VIS_TAB) *e++ = '\t'; \ 117 if (flag & VIS_NL) *e++ = '\n'; \ 118 if ((flag & VIS_NOSLASH) == 0) *e++ = '\\'; \ 119 *e = '\0'; \ 120 } while (/*CONSTCOND*/0) 121 122 123 /* 124 * This is HVIS, the macro of vis used to HTTP style (RFC 1808) 125 */ 126 #define HVIS(dst, c, flag, nextc, extra) \ 127 do \ 128 if (!isascii(c) || !isalnum(c) || strchr("$-_.+!*'(),", c) != NULL) { \ 129 *dst++ = '%'; \ 130 *dst++ = xtoa(((unsigned int)c >> 4) & 0xf); \ 131 *dst++ = xtoa((unsigned int)c & 0xf); \ 132 } else { \ 133 SVIS(dst, c, flag, nextc, extra); \ 134 } \ 135 while (/*CONSTCOND*/0) 136 137 /* 138 * This is SVIS, the central macro of vis. 139 * dst: Pointer to the destination buffer 140 * c: Character to encode 141 * flag: Flag word 142 * nextc: The character following 'c' 143 * extra: Pointer to the list of extra characters to be 144 * backslash-protected. 145 */ 146 #define SVIS(dst, c, flag, nextc, extra) \ 147 do { \ 148 int isextra; \ 149 isextra = strchr(extra, c) != NULL; \ 150 if (!isextra && isascii(c) && (isgraph(c) || iswhite(c) || \ 151 ((flag & VIS_SAFE) && issafe(c)))) { \ 152 *dst++ = c; \ 153 break; \ 154 } \ 155 if (flag & VIS_CSTYLE) { \ 156 switch (c) { \ 157 case '\n': \ 158 *dst++ = '\\'; *dst++ = 'n'; \ 159 continue; \ 160 case '\r': \ 161 *dst++ = '\\'; *dst++ = 'r'; \ 162 continue; \ 163 case '\b': \ 164 *dst++ = '\\'; *dst++ = 'b'; \ 165 continue; \ 166 case BELL: \ 167 *dst++ = '\\'; *dst++ = 'a'; \ 168 continue; \ 169 case '\v': \ 170 *dst++ = '\\'; *dst++ = 'v'; \ 171 continue; \ 172 case '\t': \ 173 *dst++ = '\\'; *dst++ = 't'; \ 174 continue; \ 175 case '\f': \ 176 *dst++ = '\\'; *dst++ = 'f'; \ 177 continue; \ 178 case ' ': \ 179 *dst++ = '\\'; *dst++ = 's'; \ 180 continue; \ 181 case '\0': \ 182 *dst++ = '\\'; *dst++ = '0'; \ 183 if (isoctal(nextc)) { \ 184 *dst++ = '0'; \ 185 *dst++ = '0'; \ 186 } \ 187 continue; \ 188 default: \ 189 if (isgraph(c)) { \ 190 *dst++ = '\\'; *dst++ = c; \ 191 continue; \ 192 } \ 193 } \ 194 } \ 195 if (isextra || ((c & 0177) == ' ') || (flag & VIS_OCTAL)) { \ 196 *dst++ = '\\'; \ 197 *dst++ = (u_char)(((u_int32_t)(u_char)c >> 6) & 03) + '0'; \ 198 *dst++ = (u_char)(((u_int32_t)(u_char)c >> 3) & 07) + '0'; \ 199 *dst++ = (c & 07) + '0'; \ 200 } else { \ 201 if ((flag & VIS_NOSLASH) == 0) *dst++ = '\\'; \ 202 if (c & 0200) { \ 203 c &= 0177; *dst++ = 'M'; \ 204 } \ 205 if (iscntrl(c)) { \ 206 *dst++ = '^'; \ 207 if (c == 0177) \ 208 *dst++ = '?'; \ 209 else \ 210 *dst++ = c + '@'; \ 211 } else { \ 212 *dst++ = '-'; *dst++ = c; \ 213 } \ 214 } \ 215 } while (/*CONSTCOND*/0) 216 217 218 /* 219 * svis - visually encode characters, also encoding the characters 220 * pointed to by `extra' 221 */ 222 char * 223 svis(char *dst, int c, int flag, int nextc, const char *extra) 224 { 225 char *nextra = NULL; 226 227 _DIAGASSERT(dst != NULL); 228 _DIAGASSERT(extra != NULL); 229 MAKEEXTRALIST(flag, nextra, extra); 230 if (!nextra) { 231 *dst = '\0'; /* can't create nextra, return "" */ 232 return dst; 233 } 234 if (flag & VIS_HTTPSTYLE) 235 HVIS(dst, c, flag, nextc, nextra); 236 else 237 SVIS(dst, c, flag, nextc, nextra); 238 free(nextra); 239 *dst = '\0'; 240 return dst; 241 } 242 243 244 /* 245 * strsvis, strsvisx - visually encode characters from src into dst 246 * 247 * Extra is a pointer to a \0-terminated list of characters to 248 * be encoded, too. These functions are useful e. g. to 249 * encode strings in such a way so that they are not interpreted 250 * by a shell. 251 * 252 * Dst must be 4 times the size of src to account for possible 253 * expansion. The length of dst, not including the trailing NULL, 254 * is returned. 255 * 256 * Strsvisx encodes exactly len bytes from src into dst. 257 * This is useful for encoding a block of data. 258 */ 259 int 260 strsvis(char *dst, const char *csrc, int flag, const char *extra) 261 { 262 int c; 263 char *start; 264 char *nextra = NULL; 265 const unsigned char *src = (const unsigned char *)csrc; 266 267 _DIAGASSERT(dst != NULL); 268 _DIAGASSERT(src != NULL); 269 _DIAGASSERT(extra != NULL); 270 MAKEEXTRALIST(flag, nextra, extra); 271 if (!nextra) { 272 *dst = '\0'; /* can't create nextra, return "" */ 273 return 0; 274 } 275 if (flag & VIS_HTTPSTYLE) { 276 for (start = dst; (c = *src++) != '\0'; /* empty */) 277 HVIS(dst, c, flag, *src, nextra); 278 } else { 279 for (start = dst; (c = *src++) != '\0'; /* empty */) 280 SVIS(dst, c, flag, *src, nextra); 281 } 282 free(nextra); 283 *dst = '\0'; 284 return (dst - start); 285 } 286 287 288 int 289 strsvisx(char *dst, const char *csrc, size_t len, int flag, const char *extra) 290 { 291 unsigned char c; 292 char *start; 293 char *nextra = NULL; 294 const unsigned char *src = (const unsigned char *)csrc; 295 296 _DIAGASSERT(dst != NULL); 297 _DIAGASSERT(src != NULL); 298 _DIAGASSERT(extra != NULL); 299 MAKEEXTRALIST(flag, nextra, extra); 300 if (! nextra) { 301 *dst = '\0'; /* can't create nextra, return "" */ 302 return 0; 303 } 304 305 if (flag & VIS_HTTPSTYLE) { 306 for (start = dst; len > 0; len--) { 307 c = *src++; 308 HVIS(dst, c, flag, len ? *src : '\0', nextra); 309 } 310 } else { 311 for (start = dst; len > 0; len--) { 312 c = *src++; 313 SVIS(dst, c, flag, len ? *src : '\0', nextra); 314 } 315 } 316 free(nextra); 317 *dst = '\0'; 318 return (dst - start); 319 } 320 #endif 321 322 #if !HAVE_VIS 323 /* 324 * vis - visually encode characters 325 */ 326 char * 327 vis(char *dst, int c, int flag, int nextc) 328 { 329 char *extra = NULL; 330 unsigned char uc = (unsigned char)c; 331 332 _DIAGASSERT(dst != NULL); 333 334 MAKEEXTRALIST(flag, extra, ""); 335 if (! extra) { 336 *dst = '\0'; /* can't create extra, return "" */ 337 return dst; 338 } 339 if (flag & VIS_HTTPSTYLE) 340 HVIS(dst, uc, flag, nextc, extra); 341 else 342 SVIS(dst, uc, flag, nextc, extra); 343 free(extra); 344 *dst = '\0'; 345 return dst; 346 } 347 348 349 /* 350 * strvis, strvisx - visually encode characters from src into dst 351 * 352 * Dst must be 4 times the size of src to account for possible 353 * expansion. The length of dst, not including the trailing NULL, 354 * is returned. 355 * 356 * Strvisx encodes exactly len bytes from src into dst. 357 * This is useful for encoding a block of data. 358 */ 359 int 360 strvis(char *dst, const char *src, int flag) 361 { 362 char *extra = NULL; 363 int rv; 364 365 MAKEEXTRALIST(flag, extra, ""); 366 if (!extra) { 367 *dst = '\0'; /* can't create extra, return "" */ 368 return 0; 369 } 370 rv = strsvis(dst, src, flag, extra); 371 free(extra); 372 return rv; 373 } 374 375 376 int 377 strvisx(char *dst, const char *src, size_t len, int flag) 378 { 379 char *extra = NULL; 380 int rv; 381 382 MAKEEXTRALIST(flag, extra, ""); 383 if (!extra) { 384 *dst = '\0'; /* can't create extra, return "" */ 385 return 0; 386 } 387 rv = strsvisx(dst, src, len, flag, extra); 388 free(extra); 389 return rv; 390 } 391 #endif 392