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