xref: /titanic_52/usr/src/lib/libresolv2/common/isc/hex.c (revision fa9e4066f08beec538e775443c5be79dd423fcab)
1 /*
2  * Copyright 2002 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 /*
7  * Copyright (c) 2001 by Internet Software Consortium.
8  *
9  * Permission to use, copy, modify, and distribute this software for any
10  * purpose with or without fee is hereby granted, provided that the above
11  * copyright notice and this permission notice appear in all copies.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
14  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
15  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
16  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
17  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
18  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
19  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
20  * SOFTWARE.
21  */
22 
23 #pragma ident	"%Z%%M%	%I%	%E% SMI"
24 
25 #include <port_before.h>
26 #include <ctype.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <isc/misc.h>
30 #include <port_after.h>
31 
32 static const char hex[17] = "0123456789abcdef";
33 
34 int
35 isc_gethexstring(unsigned char *buf, size_t len, int count, FILE *fp,
36 		 int *multiline)
37 {
38 	int c, n;
39 	unsigned char x;
40 	char *s;
41 	int result = count;
42 
43 	x = 0; /* silence compiler */
44 	n = 0;
45 	while (count > 0) {
46 		c = fgetc(fp);
47 
48 		if ((c == EOF) ||
49 		    (c == '\n' && !*multiline) ||
50 		    (c == '(' && *multiline) ||
51 		    (c == ')' && !*multiline))
52 			goto formerr;
53 		/* comment */
54 		if (c == ';') {
55 			while ((c = fgetc(fp)) != EOF && c != '\n')
56 				/* empty */
57 			if (c == '\n' && *multiline)
58 				continue;
59 			goto formerr;
60 		}
61 		/* white space */
62 		if (c == ' ' || c == '\t' || c == '\n' || c == '\r')
63 			continue;
64 		/* multiline */
65 		if ('(' == c || c == ')') {
66 			*multiline = (c == '(' /*)*/);
67 			continue;
68 		}
69 		if ((s = strchr(hex, tolower(c))) == NULL)
70 			goto formerr;
71 		x = (x<<4) | (s - hex);
72 		if (++n == 2) {
73 			if (len > 0) {
74 				*buf++ = x;
75 				len--;
76 			} else
77 				result = -1;
78 			count--;
79 			n = 0;
80 		}
81 	}
82 	return (result);
83 
84  formerr:
85 	if (c == '\n')
86 		ungetc(c, fp);
87 	return (-1);
88 }
89 
90 void
91 isc_puthexstring(FILE *fp, const unsigned char *buf, size_t buflen,
92 		 size_t len1, size_t len2, const char *sep)
93 {
94 	size_t i = 0;
95 
96 	if (len1 < 4)
97 		len1 = 4;
98 	if (len2 < 4)
99 		len2 = 4;
100 	while (buflen > 0) {
101 		fputc(hex[(buf[0]>>4)&0xf], fp);
102 		fputc(hex[buf[0]&0xf], fp);
103 		i += 2;
104 		buflen--;
105 		buf++;
106 		if (i >= len1 && sep != NULL) {
107 			fputs(sep, fp);
108 			i = 0;
109 			len1 = len2;
110 		}
111 	}
112 }
113 
114 void
115 isc_tohex(const unsigned char *buf, size_t buflen, char *t) {
116 	while (buflen > 0) {
117 		*t++ = hex[(buf[0]>>4)&0xf];
118 		*t++ = hex[buf[0]&0xf];
119 		buf++;
120 		buflen--;
121 	}
122 	*t = '\0';
123 }
124