xref: /minix3/external/bsd/bind/dist/contrib/idn/idnkit-1.0-src/lib/debug.c (revision 00b67f09dd46474d133c95011a48590a8e8f94c7)
1 /*	$NetBSD: debug.c,v 1.4 2014/12/10 04:37:55 christos Exp $	*/
2 
3 #ifndef lint
4 static char *rcsid = "Id: debug.c,v 1.1 2003/06/04 00:25:51 marka Exp ";
5 #endif
6 
7 /*
8  * Copyright (c) 2000,2002 Japan Network Information Center.
9  * All rights reserved.
10  *
11  * By using this file, you agree to the terms and conditions set forth bellow.
12  *
13  * 			LICENSE TERMS AND CONDITIONS
14  *
15  * The following License Terms and Conditions apply, unless a different
16  * license is obtained from Japan Network Information Center ("JPNIC"),
17  * a Japanese association, Kokusai-Kougyou-Kanda Bldg 6F, 2-3-4 Uchi-Kanda,
18  * Chiyoda-ku, Tokyo 101-0047, Japan.
19  *
20  * 1. Use, Modification and Redistribution (including distribution of any
21  *    modified or derived work) in source and/or binary forms is permitted
22  *    under this License Terms and Conditions.
23  *
24  * 2. Redistribution of source code must retain the copyright notices as they
25  *    appear in each source code file, this License Terms and Conditions.
26  *
27  * 3. Redistribution in binary form must reproduce the Copyright Notice,
28  *    this License Terms and Conditions, in the documentation and/or other
29  *    materials provided with the distribution.  For the purposes of binary
30  *    distribution the "Copyright Notice" refers to the following language:
31  *    "Copyright (c) 2000-2002 Japan Network Information Center.  All rights reserved."
32  *
33  * 4. The name of JPNIC may not be used to endorse or promote products
34  *    derived from this Software without specific prior written approval of
35  *    JPNIC.
36  *
37  * 5. Disclaimer/Limitation of Liability: THIS SOFTWARE IS PROVIDED BY JPNIC
38  *    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
39  *    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
40  *    PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL JPNIC BE LIABLE
41  *    FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
42  *    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
43  *    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
44  *    BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
45  *    WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
46  *    OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
47  *    ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
48  */
49 
50 #include <config.h>
51 
52 #include <stdio.h>
53 #include <stdarg.h>
54 #include <stdlib.h>
55 #include <string.h>
56 
57 #include <idn/debug.h>
58 
59 static char *hex = "0123456789abcdef";
60 
61 #define STRING_MAXBYTES	200
62 #define STRING_NBUFS	4
63 static char bufs[STRING_NBUFS][STRING_MAXBYTES + 16];	/* +16 for margin */
64 static int bufno = 0;
65 
66 char *
idn__debug_hexstring(const char * s,int maxbytes)67 idn__debug_hexstring(const char *s, int maxbytes) {
68 	char *buf = bufs[bufno];
69 	char *p;
70 	int i;
71 
72 	if (maxbytes > STRING_MAXBYTES)
73 		maxbytes = STRING_MAXBYTES;
74 
75 	for (i = 0, p = buf; i < maxbytes; i += 3, s++) {
76 		int c = *(unsigned char *)s;
77 
78 		if (c == '\0')
79 			break;
80 		*p++ = hex[c >> 4];
81 		*p++ = hex[c & 15];
82 		*p++ = ' ';
83 	}
84 
85 	if (i >= maxbytes)
86 		strcpy(p, "...");
87 	else
88 		*p = '\0';
89 
90 	bufno = (bufno + 1) % STRING_NBUFS;
91 	return (buf);
92 }
93 
94 char *
idn__debug_xstring(const char * s,int maxbytes)95 idn__debug_xstring(const char *s, int maxbytes) {
96 	char *buf = bufs[bufno];
97 	char *p;
98 	int i;
99 
100 	if (maxbytes > STRING_MAXBYTES)
101 		maxbytes = STRING_MAXBYTES;
102 
103 	i = 0;
104 	p = buf;
105 	while (i < maxbytes) {
106 		int c = *(unsigned char *)s;
107 
108 		if (c == '\0') {
109 			break;
110 		} else if (0x20 <= c && c <= 0x7e) {
111 			*p++ = c;
112 			i++;
113 		} else {
114 			*p++ = '\\';
115 			*p++ = 'x';
116 			*p++ = hex[c >> 4];
117 			*p++ = hex[c & 15];
118 			i += 4;
119 		}
120 		s++;
121 	}
122 
123 	if (i >= maxbytes)
124 		strcpy(p, "...");
125 	else
126 		*p = '\0';
127 
128 	bufno = (bufno + 1) % STRING_NBUFS;
129 	return (buf);
130 }
131 
132 char *
idn__debug_ucs4xstring(const unsigned long * s,int maxbytes)133 idn__debug_ucs4xstring(const unsigned long *s, int maxbytes) {
134 	char *buf = bufs[bufno];
135 	char *p;
136 	int i;
137 
138 	if (maxbytes > STRING_MAXBYTES)
139 		maxbytes = STRING_MAXBYTES;
140 
141 	i = 0;
142 	p = buf;
143 	while (i < maxbytes) {
144 		if (*s == '\0') {
145 			break;
146 		} else if (0x20 <= *s && *s <= 0x7e) {
147 			*p++ = *s;
148 			i++;
149 		} else {
150 			*p++ = '\\';
151 			*p++ = 'x';
152 			i += 2;
153 			if (*s >= 0x1000000UL) {
154 				*p++ = hex[(*s >> 28) & 0x0f];
155 				*p++ = hex[(*s >> 24) & 0x0f];
156 				i += 2;
157 			}
158 			if (*s >= 0x10000UL) {
159 				*p++ = hex[(*s >> 20) & 0x0f];
160 				*p++ = hex[(*s >> 16) & 0x0f];
161 				i += 2;
162 		    	}
163 			if (*s >= 0x100UL) {
164 				*p++ = hex[(*s >> 12) & 0x0f];
165 				*p++ = hex[(*s >>  8) & 0x0f];
166 				i += 2;
167 			}
168 			*p++ = hex[(*s >> 4) & 0x0f];
169 			*p++ = hex[ *s       & 0x0f];
170 			i += 2;
171 		}
172 		s++;
173 	}
174 
175 	if (i >= maxbytes)
176 		strcpy(p, "...");
177 	else
178 		*p = '\0';
179 
180 	bufno = (bufno + 1) % STRING_NBUFS;
181 	return (buf);
182 }
183 
184 char *
idn__debug_utf16xstring(const unsigned short * s,int maxbytes)185 idn__debug_utf16xstring(const unsigned short *s, int maxbytes) {
186 	char *buf = bufs[bufno];
187 	char *p;
188 	int i;
189 
190 	if (maxbytes > STRING_MAXBYTES)
191 		maxbytes = STRING_MAXBYTES;
192 
193 	i = 0;
194 	p = buf;
195 	while (i < maxbytes) {
196 		if (*s == '\0') {
197 			break;
198 		} else if (0x20 <= *s && *s <= 0x7e) {
199 			*p++ = *s;
200 			i++;
201 		} else {
202 			*p++ = '\\';
203 			*p++ = 'x';
204 			*p++ = hex[(*s >> 12) & 0x0f];
205 			*p++ = hex[(*s >>  8) & 0x0f];
206 			*p++ = hex[(*s >> 4)  & 0x0f];
207 			*p++ = hex[ *s        & 0x0f];
208 			i += 6;
209 		}
210 		s++;
211 	}
212 
213 	if (i >= maxbytes)
214 		strcpy(p, "...");
215 	else
216 		*p = '\0';
217 
218 	bufno = (bufno + 1) % STRING_NBUFS;
219 	return (buf);
220 }
221 
222 char *
idn__debug_hexdata(const char * s,int length,int maxbytes)223 idn__debug_hexdata(const char *s, int length, int maxbytes) {
224 	char *buf = bufs[bufno];
225 	char *p;
226 	int i;
227 
228 	if (maxbytes > STRING_MAXBYTES)
229 		maxbytes = STRING_MAXBYTES;
230 
231 	i = 0;
232 	p = buf;
233 	while (length > 0 && i < maxbytes) {
234 		int c = *(const unsigned char *)s;
235 
236 		*p++ = hex[c >> 4];
237 		*p++ = hex[c & 15];
238 		*p++ = ' ';
239 		i += 3;
240 		length--;
241 		s++;
242 	}
243 
244 	if (i >= maxbytes)
245 		strcpy(p, "...");
246 	else
247 		*p = '\0';
248 
249 	bufno = (bufno + 1) % STRING_NBUFS;
250 	return (buf);
251 }
252 
253 void
idn__debug_hexdump(const char * s,int length)254 idn__debug_hexdump(const char *s, int length) {
255 	int i;
256 	const unsigned char *p = (const unsigned char *)s;
257 
258 	i = 0;
259 	while (length-- > 0) {
260 		if (i % 16 == 0) {
261 			if (i > 0)
262 				fprintf(stderr, "\n");
263 			fprintf(stderr, "%4x:", i);
264 		}
265 		fprintf(stderr, " %02x", p[i]);
266 		i++;
267 	}
268 	fprintf(stderr, "\n");
269 }
270