comparison compat/vis.c @ 2:97097b4b6bfb

Add pwsdump utility The pwsdum utility can dump PasswordSafe database files to a plaintext format and convert this format back into a PasswordSafe database.
author Guido Berhoerster <guido+libpws@berhoerster.name>
date Wed, 01 Apr 2015 14:57:57 +0200
parents
children
comparison
equal deleted inserted replaced
1:e1309515d111 2:97097b4b6bfb
1 /* $OpenBSD: vis.c,v 1.25 2015/09/13 11:32:51 guenther Exp $ */
2 /*-
3 * Copyright (c) 1989, 1993
4 * The Regents of the University of California. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31 #include <sys/types.h>
32 #include <errno.h>
33 #include <ctype.h>
34 #include <limits.h>
35 #include <string.h>
36 #include <stdlib.h>
37
38 #include "pws-compat.h"
39 #include "vis.h"
40
41 #define isoctal(c) (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
42 #define isvisible(c,flag) \
43 (((c) == '\\' || (flag & VIS_ALL) == 0) && \
44 (((u_int)(c) <= UCHAR_MAX && isascii((u_char)(c)) && \
45 (((c) != '*' && (c) != '?' && (c) != '[' && (c) != '#') || \
46 (flag & VIS_GLOB) == 0) && isgraph((u_char)(c))) || \
47 ((flag & VIS_SP) == 0 && (c) == ' ') || \
48 ((flag & VIS_TAB) == 0 && (c) == '\t') || \
49 ((flag & VIS_NL) == 0 && (c) == '\n') || \
50 ((flag & VIS_SAFE) && ((c) == '\b' || \
51 (c) == '\007' || (c) == '\r' || \
52 isgraph((u_char)(c))))))
53
54 typedef unsigned int u_int;
55 typedef unsigned char u_char;
56
57 /*
58 * vis - visually encode characters
59 */
60 char *
61 vis(char *dst, int c, int flag, int nextc)
62 {
63 if (isvisible(c, flag)) {
64 if ((c == '"' && (flag & VIS_DQ) != 0) ||
65 (c == '\\' && (flag & VIS_NOSLASH) == 0))
66 *dst++ = '\\';
67 *dst++ = c;
68 *dst = '\0';
69 return (dst);
70 }
71
72 if (flag & VIS_CSTYLE) {
73 switch(c) {
74 case '\n':
75 *dst++ = '\\';
76 *dst++ = 'n';
77 goto done;
78 case '\r':
79 *dst++ = '\\';
80 *dst++ = 'r';
81 goto done;
82 case '\b':
83 *dst++ = '\\';
84 *dst++ = 'b';
85 goto done;
86 case '\a':
87 *dst++ = '\\';
88 *dst++ = 'a';
89 goto done;
90 case '\v':
91 *dst++ = '\\';
92 *dst++ = 'v';
93 goto done;
94 case '\t':
95 *dst++ = '\\';
96 *dst++ = 't';
97 goto done;
98 case '\f':
99 *dst++ = '\\';
100 *dst++ = 'f';
101 goto done;
102 case ' ':
103 *dst++ = '\\';
104 *dst++ = 's';
105 goto done;
106 case '\0':
107 *dst++ = '\\';
108 *dst++ = '0';
109 if (isoctal(nextc)) {
110 *dst++ = '0';
111 *dst++ = '0';
112 }
113 goto done;
114 }
115 }
116 if (((c & 0177) == ' ') || (flag & VIS_OCTAL) ||
117 ((flag & VIS_GLOB) && (c == '*' || c == '?' || c == '[' || c == '#'))) {
118 *dst++ = '\\';
119 *dst++ = ((u_char)c >> 6 & 07) + '0';
120 *dst++ = ((u_char)c >> 3 & 07) + '0';
121 *dst++ = ((u_char)c & 07) + '0';
122 goto done;
123 }
124 if ((flag & VIS_NOSLASH) == 0)
125 *dst++ = '\\';
126 if (c & 0200) {
127 c &= 0177;
128 *dst++ = 'M';
129 }
130 if (iscntrl((u_char)c)) {
131 *dst++ = '^';
132 if (c == 0177)
133 *dst++ = '?';
134 else
135 *dst++ = c + '@';
136 } else {
137 *dst++ = '-';
138 *dst++ = c;
139 }
140 done:
141 *dst = '\0';
142 return (dst);
143 }
144
145 /*
146 * strvis, strnvis, strvisx - visually encode characters from src into dst
147 *
148 * Dst must be 4 times the size of src to account for possible
149 * expansion. The length of dst, not including the trailing NULL,
150 * is returned.
151 *
152 * Strnvis will write no more than siz-1 bytes (and will NULL terminate).
153 * The number of bytes needed to fully encode the string is returned.
154 *
155 * Strvisx encodes exactly len bytes from src into dst.
156 * This is useful for encoding a block of data.
157 */
158 int
159 strvis(char *dst, const char *src, int flag)
160 {
161 char c;
162 char *start;
163
164 for (start = dst; (c = *src);)
165 dst = vis(dst, c, flag, *++src);
166 *dst = '\0';
167 return (dst - start);
168 }
169
170 int
171 strnvis(char *dst, const char *src, size_t siz, int flag)
172 {
173 char *start, *end;
174 char tbuf[5];
175 int c, i;
176
177 i = 0;
178 for (start = dst, end = start + siz - 1; (c = *src) && dst < end; ) {
179 if (isvisible(c, flag)) {
180 if ((c == '"' && (flag & VIS_DQ) != 0) ||
181 (c == '\\' && (flag & VIS_NOSLASH) == 0)) {
182 /* need space for the extra '\\' */
183 if (dst + 1 >= end) {
184 i = 2;
185 break;
186 }
187 *dst++ = '\\';
188 }
189 i = 1;
190 *dst++ = c;
191 src++;
192 } else {
193 i = vis(tbuf, c, flag, *++src) - tbuf;
194 if (dst + i <= end) {
195 memcpy(dst, tbuf, i);
196 dst += i;
197 } else {
198 src--;
199 break;
200 }
201 }
202 }
203 if (siz > 0)
204 *dst = '\0';
205 if (dst + i > end) {
206 /* adjust return value for truncation */
207 while ((c = *src))
208 dst += vis(tbuf, c, flag, *++src) - tbuf;
209 }
210 return (dst - start);
211 }
212
213 int
214 stravis(char **outp, const char *src, int flag)
215 {
216 char *buf;
217 int len, serrno;
218
219 buf = malloc(4 * strlen(src) + 1);
220 if (buf == NULL)
221 return -1;
222 len = strvis(buf, src, flag);
223 serrno = errno;
224 *outp = realloc(buf, len + 1);
225 if (*outp == NULL) {
226 *outp = buf;
227 errno = serrno;
228 }
229 return (len);
230 }
231
232 int
233 strvisx(char *dst, const char *src, size_t len, int flag)
234 {
235 char c;
236 char *start;
237
238 for (start = dst; len > 1; len--) {
239 c = *src;
240 dst = vis(dst, c, flag, *++src);
241 }
242 if (len)
243 dst = vis(dst, *src, flag, '\0');
244 *dst = '\0';
245 return (dst - start);
246 }