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