Add BIND 9.2.4rc7.
[dragonfly.git] / contrib / bind-9.2.4rc7 / lib / bind / isc / hex.c
blobc177ca0fa3280e27b33790f7abe163fb411de23c
1 /*
2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (c) 2001 by Internet Software Consortium.
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <port_before.h>
19 #include <ctype.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <isc/misc.h>
23 #include <port_after.h>
25 static const char hex[17] = "0123456789abcdef";
27 int
28 isc_gethexstring(unsigned char *buf, size_t len, int count, FILE *fp,
29 int *multiline)
31 int c, n;
32 unsigned char x;
33 char *s;
34 int result = count;
36 x = 0; /* silence compiler */
37 n = 0;
38 while (count > 0) {
39 c = fgetc(fp);
41 if ((c == EOF) ||
42 (c == '\n' && !*multiline) ||
43 (c == '(' && *multiline) ||
44 (c == ')' && !*multiline))
45 goto formerr;
46 /* comment */
47 if (c == ';') {
48 while ((c = fgetc(fp)) != EOF && c != '\n')
49 /* empty */
50 if (c == '\n' && *multiline)
51 continue;
52 goto formerr;
54 /* white space */
55 if (c == ' ' || c == '\t' || c == '\n' || c == '\r')
56 continue;
57 /* multiline */
58 if ('(' == c || c == ')') {
59 *multiline = (c == '(' /*)*/);
60 continue;
62 if ((s = strchr(hex, tolower(c))) == NULL)
63 goto formerr;
64 x = (x<<4) | (s - hex);
65 if (++n == 2) {
66 if (len > 0U) {
67 *buf++ = x;
68 len--;
69 } else
70 result = -1;
71 count--;
72 n = 0;
75 return (result);
77 formerr:
78 if (c == '\n')
79 ungetc(c, fp);
80 return (-1);
83 void
84 isc_puthexstring(FILE *fp, const unsigned char *buf, size_t buflen,
85 size_t len1, size_t len2, const char *sep)
87 size_t i = 0;
89 if (len1 < 4U)
90 len1 = 4;
91 if (len2 < 4U)
92 len2 = 4;
93 while (buflen > 0U) {
94 fputc(hex[(buf[0]>>4)&0xf], fp);
95 fputc(hex[buf[0]&0xf], fp);
96 i += 2;
97 buflen--;
98 buf++;
99 if (i >= len1 && sep != NULL) {
100 fputs(sep, fp);
101 i = 0;
102 len1 = len2;
107 void
108 isc_tohex(const unsigned char *buf, size_t buflen, char *t) {
109 while (buflen > 0U) {
110 *t++ = hex[(buf[0]>>4)&0xf];
111 *t++ = hex[buf[0]&0xf];
112 buf++;
113 buflen--;
115 *t = '\0';