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