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