PageRenderTime 58ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/bind-9.9.1-P1/contrib/idn/idnkit-1.0-src/lib/debug.c

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