1 /* $NetBSD: debug.c,v 1.4 2014/12/10 04:37:55 christos Exp $ */
4 static char *rcsid
= "Id: debug.c,v 1.1 2003/06/04 00:25:51 marka Exp ";
8 * Copyright (c) 2000,2002 Japan Network Information Center.
11 * By using this file, you agree to the terms and conditions set forth bellow.
13 * LICENSE TERMS AND CONDITIONS
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.
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.
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.
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."
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
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.
57 #include <idn/debug.h>
59 static char *hex
= "0123456789abcdef";
61 #define STRING_MAXBYTES 200
62 #define STRING_NBUFS 4
63 static char bufs
[STRING_NBUFS
][STRING_MAXBYTES
+ 16]; /* +16 for margin */
67 idn__debug_hexstring(const char *s
, int maxbytes
) {
68 char *buf
= bufs
[bufno
];
72 if (maxbytes
> STRING_MAXBYTES
)
73 maxbytes
= STRING_MAXBYTES
;
75 for (i
= 0, p
= buf
; i
< maxbytes
; i
+= 3, s
++) {
76 int c
= *(unsigned char *)s
;
90 bufno
= (bufno
+ 1) % STRING_NBUFS
;
95 idn__debug_xstring(const char *s
, int maxbytes
) {
96 char *buf
= bufs
[bufno
];
100 if (maxbytes
> STRING_MAXBYTES
)
101 maxbytes
= STRING_MAXBYTES
;
105 while (i
< maxbytes
) {
106 int c
= *(unsigned char *)s
;
110 } else if (0x20 <= c
&& c
<= 0x7e) {
128 bufno
= (bufno
+ 1) % STRING_NBUFS
;
133 idn__debug_ucs4xstring(const unsigned long *s
, int maxbytes
) {
134 char *buf
= bufs
[bufno
];
138 if (maxbytes
> STRING_MAXBYTES
)
139 maxbytes
= STRING_MAXBYTES
;
143 while (i
< maxbytes
) {
146 } else if (0x20 <= *s
&& *s
<= 0x7e) {
153 if (*s
>= 0x1000000UL
) {
154 *p
++ = hex
[(*s
>> 28) & 0x0f];
155 *p
++ = hex
[(*s
>> 24) & 0x0f];
158 if (*s
>= 0x10000UL
) {
159 *p
++ = hex
[(*s
>> 20) & 0x0f];
160 *p
++ = hex
[(*s
>> 16) & 0x0f];
164 *p
++ = hex
[(*s
>> 12) & 0x0f];
165 *p
++ = hex
[(*s
>> 8) & 0x0f];
168 *p
++ = hex
[(*s
>> 4) & 0x0f];
169 *p
++ = hex
[ *s
& 0x0f];
180 bufno
= (bufno
+ 1) % STRING_NBUFS
;
185 idn__debug_utf16xstring(const unsigned short *s
, int maxbytes
) {
186 char *buf
= bufs
[bufno
];
190 if (maxbytes
> STRING_MAXBYTES
)
191 maxbytes
= STRING_MAXBYTES
;
195 while (i
< maxbytes
) {
198 } else if (0x20 <= *s
&& *s
<= 0x7e) {
204 *p
++ = hex
[(*s
>> 12) & 0x0f];
205 *p
++ = hex
[(*s
>> 8) & 0x0f];
206 *p
++ = hex
[(*s
>> 4) & 0x0f];
207 *p
++ = hex
[ *s
& 0x0f];
218 bufno
= (bufno
+ 1) % STRING_NBUFS
;
223 idn__debug_hexdata(const char *s
, int length
, int maxbytes
) {
224 char *buf
= bufs
[bufno
];
228 if (maxbytes
> STRING_MAXBYTES
)
229 maxbytes
= STRING_MAXBYTES
;
233 while (length
> 0 && i
< maxbytes
) {
234 int c
= *(const unsigned char *)s
;
249 bufno
= (bufno
+ 1) % STRING_NBUFS
;
254 idn__debug_hexdump(const char *s
, int length
) {
256 const unsigned char *p
= (const unsigned char *)s
;
259 while (length
-- > 0) {
262 fprintf(stderr
, "\n");
263 fprintf(stderr
, "%4x:", i
);
265 fprintf(stderr
, " %02x", p
[i
]);
268 fprintf(stderr
, "\n");