1 1.1 christos /* $NetBSD: strnvisx.c,v 1.1 2016/05/02 19:18:29 christos Exp $ */ 2 1.1 christos 3 1.1 christos /*- 4 1.1 christos * Copyright (c) 1998, 1999, 2004 The NetBSD Foundation, Inc. 5 1.1 christos * All rights reserved. 6 1.1 christos * 7 1.1 christos * This code is derived from software contributed to The NetBSD Foundation 8 1.1 christos * by Charles M. Hannum; by Jason R. Thorpe of the Numerical Aerospace 9 1.1 christos * Simulation Facility, NASA Ames Research Center. 10 1.1 christos * 11 1.1 christos * Redistribution and use in source and binary forms, with or without 12 1.1 christos * modification, are permitted provided that the following conditions 13 1.1 christos * are met: 14 1.1 christos * 1. Redistributions of source code must retain the above copyright 15 1.1 christos * notice, this list of conditions and the following disclaimer. 16 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright 17 1.1 christos * notice, this list of conditions and the following disclaimer in the 18 1.1 christos * documentation and/or other materials provided with the distribution. 19 1.1 christos * 20 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 1.1 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 1.1 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 1.1 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 1.1 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 1.1 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 1.1 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 1.1 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 1.1 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 1.1 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 1.1 christos * POSSIBILITY OF SUCH DAMAGE. 31 1.1 christos */ 32 1.1 christos 33 1.1 christos /* 34 1.1 christos * from scsipiconf.c... 35 1.1 christos */ 36 1.1 christos #include <lib/libkern/libkern.h> 37 1.1 christos 38 1.1 christos #define STRVIS_ISWHITE(x) ((x) == ' ' || (x) == '\0' || (x) == '\377') 39 1.1 christos 40 1.1 christos int 41 1.1 christos strnvisx(char *dst, size_t dlen, const char *src, size_t slen, int flags) 42 1.1 christos { 43 1.1 christos if (dlen == 0) 44 1.1 christos return -1; 45 1.1 christos 46 1.1 christos if (flags & VIS_TRIM) { 47 1.1 christos /* Trim leading and trailing blanks and NULs. */ 48 1.1 christos while (slen > 0 && STRVIS_ISWHITE(src[0])) 49 1.1 christos ++src, --slen; 50 1.1 christos while (slen > 0 && STRVIS_ISWHITE(src[slen - 1])) 51 1.1 christos --slen; 52 1.1 christos } 53 1.1 christos 54 1.1 christos while (slen > 0) { 55 1.1 christos if ((flags & VIS_SAFE) && (*src < 0x20 || (*src & 0x80))) { 56 1.1 christos /* non-printable characters */ 57 1.1 christos if (dlen < 4) 58 1.1 christos goto out; 59 1.1 christos dlen -= 4; 60 1.1 christos *dst++ = '\\'; 61 1.1 christos *dst++ = ((*src & 0300) >> 6) + '0'; 62 1.1 christos *dst++ = ((*src & 0070) >> 3) + '0'; 63 1.1 christos *dst++ = ((*src & 0007) >> 0) + '0'; 64 1.1 christos } else if (*src == '\\') { 65 1.1 christos /* quote characters */ 66 1.1 christos if (dlen < 2) 67 1.1 christos goto out; 68 1.1 christos dlen -= 2; 69 1.1 christos *dst++ = '\\'; 70 1.1 christos *dst++ = '\\'; 71 1.1 christos } else { 72 1.1 christos /* normal characters */ 73 1.1 christos if (dlen < 1) 74 1.1 christos goto out; 75 1.1 christos *dst++ = *src; 76 1.1 christos } 77 1.1 christos ++src, --slen; 78 1.1 christos } 79 1.1 christos out: 80 1.1 christos if (dlen > 0) { 81 1.1 christos *dst++ = '\0'; 82 1.1 christos return slen ? -1 : 0; 83 1.1 christos } else { 84 1.1 christos *--dst = '\0'; 85 1.1 christos return -1; 86 1.1 christos } 87 1.1 christos } 88