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