10Sstevel@tonic-gate /*
2*11038SRao.Shoaib@Sun.COM * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
30Sstevel@tonic-gate * Copyright (c) 2001 by Internet Software Consortium.
40Sstevel@tonic-gate *
50Sstevel@tonic-gate * Permission to use, copy, modify, and distribute this software for any
60Sstevel@tonic-gate * purpose with or without fee is hereby granted, provided that the above
70Sstevel@tonic-gate * copyright notice and this permission notice appear in all copies.
80Sstevel@tonic-gate *
9*11038SRao.Shoaib@Sun.COM * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10*11038SRao.Shoaib@Sun.COM * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11*11038SRao.Shoaib@Sun.COM * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
12*11038SRao.Shoaib@Sun.COM * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13*11038SRao.Shoaib@Sun.COM * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14*11038SRao.Shoaib@Sun.COM * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15*11038SRao.Shoaib@Sun.COM * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
160Sstevel@tonic-gate */
170Sstevel@tonic-gate
180Sstevel@tonic-gate #include <port_before.h>
190Sstevel@tonic-gate #include <ctype.h>
200Sstevel@tonic-gate #include <stdio.h>
210Sstevel@tonic-gate #include <string.h>
220Sstevel@tonic-gate #include <isc/misc.h>
230Sstevel@tonic-gate #include <port_after.h>
240Sstevel@tonic-gate
250Sstevel@tonic-gate static const char hex[17] = "0123456789abcdef";
260Sstevel@tonic-gate
270Sstevel@tonic-gate int
isc_gethexstring(unsigned char * buf,size_t len,int count,FILE * fp,int * multiline)280Sstevel@tonic-gate isc_gethexstring(unsigned char *buf, size_t len, int count, FILE *fp,
290Sstevel@tonic-gate int *multiline)
300Sstevel@tonic-gate {
310Sstevel@tonic-gate int c, n;
320Sstevel@tonic-gate unsigned char x;
330Sstevel@tonic-gate char *s;
340Sstevel@tonic-gate int result = count;
350Sstevel@tonic-gate
36*11038SRao.Shoaib@Sun.COM x = 0; /*%< silence compiler */
370Sstevel@tonic-gate n = 0;
380Sstevel@tonic-gate while (count > 0) {
390Sstevel@tonic-gate c = fgetc(fp);
400Sstevel@tonic-gate
410Sstevel@tonic-gate if ((c == EOF) ||
420Sstevel@tonic-gate (c == '\n' && !*multiline) ||
430Sstevel@tonic-gate (c == '(' && *multiline) ||
440Sstevel@tonic-gate (c == ')' && !*multiline))
450Sstevel@tonic-gate goto formerr;
460Sstevel@tonic-gate /* comment */
470Sstevel@tonic-gate if (c == ';') {
48*11038SRao.Shoaib@Sun.COM do {
49*11038SRao.Shoaib@Sun.COM c = fgetc(fp);
50*11038SRao.Shoaib@Sun.COM } while (c != EOF && c != '\n');
510Sstevel@tonic-gate if (c == '\n' && *multiline)
520Sstevel@tonic-gate continue;
530Sstevel@tonic-gate goto formerr;
540Sstevel@tonic-gate }
550Sstevel@tonic-gate /* white space */
560Sstevel@tonic-gate if (c == ' ' || c == '\t' || c == '\n' || c == '\r')
570Sstevel@tonic-gate continue;
580Sstevel@tonic-gate /* multiline */
590Sstevel@tonic-gate if ('(' == c || c == ')') {
600Sstevel@tonic-gate *multiline = (c == '(' /*)*/);
610Sstevel@tonic-gate continue;
620Sstevel@tonic-gate }
630Sstevel@tonic-gate if ((s = strchr(hex, tolower(c))) == NULL)
640Sstevel@tonic-gate goto formerr;
650Sstevel@tonic-gate x = (x<<4) | (s - hex);
660Sstevel@tonic-gate if (++n == 2) {
67*11038SRao.Shoaib@Sun.COM if (len > 0U) {
680Sstevel@tonic-gate *buf++ = x;
690Sstevel@tonic-gate len--;
700Sstevel@tonic-gate } else
710Sstevel@tonic-gate result = -1;
720Sstevel@tonic-gate count--;
730Sstevel@tonic-gate n = 0;
740Sstevel@tonic-gate }
750Sstevel@tonic-gate }
760Sstevel@tonic-gate return (result);
770Sstevel@tonic-gate
780Sstevel@tonic-gate formerr:
790Sstevel@tonic-gate if (c == '\n')
800Sstevel@tonic-gate ungetc(c, fp);
810Sstevel@tonic-gate return (-1);
820Sstevel@tonic-gate }
830Sstevel@tonic-gate
840Sstevel@tonic-gate void
isc_puthexstring(FILE * fp,const unsigned char * buf,size_t buflen,size_t len1,size_t len2,const char * sep)850Sstevel@tonic-gate isc_puthexstring(FILE *fp, const unsigned char *buf, size_t buflen,
860Sstevel@tonic-gate size_t len1, size_t len2, const char *sep)
870Sstevel@tonic-gate {
880Sstevel@tonic-gate size_t i = 0;
890Sstevel@tonic-gate
90*11038SRao.Shoaib@Sun.COM if (len1 < 4U)
910Sstevel@tonic-gate len1 = 4;
92*11038SRao.Shoaib@Sun.COM if (len2 < 4U)
930Sstevel@tonic-gate len2 = 4;
94*11038SRao.Shoaib@Sun.COM while (buflen > 0U) {
950Sstevel@tonic-gate fputc(hex[(buf[0]>>4)&0xf], fp);
960Sstevel@tonic-gate fputc(hex[buf[0]&0xf], fp);
970Sstevel@tonic-gate i += 2;
980Sstevel@tonic-gate buflen--;
990Sstevel@tonic-gate buf++;
1000Sstevel@tonic-gate if (i >= len1 && sep != NULL) {
1010Sstevel@tonic-gate fputs(sep, fp);
1020Sstevel@tonic-gate i = 0;
1030Sstevel@tonic-gate len1 = len2;
1040Sstevel@tonic-gate }
1050Sstevel@tonic-gate }
1060Sstevel@tonic-gate }
1070Sstevel@tonic-gate
1080Sstevel@tonic-gate void
isc_tohex(const unsigned char * buf,size_t buflen,char * t)1090Sstevel@tonic-gate isc_tohex(const unsigned char *buf, size_t buflen, char *t) {
110*11038SRao.Shoaib@Sun.COM while (buflen > 0U) {
1110Sstevel@tonic-gate *t++ = hex[(buf[0]>>4)&0xf];
1120Sstevel@tonic-gate *t++ = hex[buf[0]&0xf];
1130Sstevel@tonic-gate buf++;
1140Sstevel@tonic-gate buflen--;
1150Sstevel@tonic-gate }
1160Sstevel@tonic-gate *t = '\0';
1170Sstevel@tonic-gate }
118*11038SRao.Shoaib@Sun.COM
119*11038SRao.Shoaib@Sun.COM /*! \file */
120