xref: /titanic_52/usr/src/lib/libresolv2/common/isc/hex.c (revision 68c47f65208790c466e5e484f2293d3baed71c6a)
1 /*
2  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (c) 2001 by Internet Software Consortium.
4  *
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.
8  *
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.
16  */
17 
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>
24 
25 static const char hex[17] = "0123456789abcdef";
26 
27 int
28 isc_gethexstring(unsigned char *buf, size_t len, int count, FILE *fp,
29 		 int *multiline)
30 {
31 	int c, n;
32 	unsigned char x;
33 	char *s;
34 	int result = count;
35 
36 	x = 0; /*%< silence compiler */
37 	n = 0;
38 	while (count > 0) {
39 		c = fgetc(fp);
40 
41 		if ((c == EOF) ||
42 		    (c == '\n' && !*multiline) ||
43 		    (c == '(' && *multiline) ||
44 		    (c == ')' && !*multiline))
45 			goto formerr;
46 		/* comment */
47 		if (c == ';') {
48 			do {
49 				c = fgetc(fp);
50 			} while (c != EOF && c != '\n');
51 			if (c == '\n' && *multiline)
52 				continue;
53 			goto formerr;
54 		}
55 		/* white space */
56 		if (c == ' ' || c == '\t' || c == '\n' || c == '\r')
57 			continue;
58 		/* multiline */
59 		if ('(' == c || c == ')') {
60 			*multiline = (c == '(' /*)*/);
61 			continue;
62 		}
63 		if ((s = strchr(hex, tolower(c))) == NULL)
64 			goto formerr;
65 		x = (x<<4) | (s - hex);
66 		if (++n == 2) {
67 			if (len > 0U) {
68 				*buf++ = x;
69 				len--;
70 			} else
71 				result = -1;
72 			count--;
73 			n = 0;
74 		}
75 	}
76 	return (result);
77 
78  formerr:
79 	if (c == '\n')
80 		ungetc(c, fp);
81 	return (-1);
82 }
83 
84 void
85 isc_puthexstring(FILE *fp, const unsigned char *buf, size_t buflen,
86 		 size_t len1, size_t len2, const char *sep)
87 {
88 	size_t i = 0;
89 
90 	if (len1 < 4U)
91 		len1 = 4;
92 	if (len2 < 4U)
93 		len2 = 4;
94 	while (buflen > 0U) {
95 		fputc(hex[(buf[0]>>4)&0xf], fp);
96 		fputc(hex[buf[0]&0xf], fp);
97 		i += 2;
98 		buflen--;
99 		buf++;
100 		if (i >= len1 && sep != NULL) {
101 			fputs(sep, fp);
102 			i = 0;
103 			len1 = len2;
104 		}
105 	}
106 }
107 
108 void
109 isc_tohex(const unsigned char *buf, size_t buflen, char *t) {
110 	while (buflen > 0U) {
111 		*t++ = hex[(buf[0]>>4)&0xf];
112 		*t++ = hex[buf[0]&0xf];
113 		buf++;
114 		buflen--;
115 	}
116 	*t = '\0';
117 }
118 
119 /*! \file */
120