17c478bd9Sstevel@tonic-gate /* 2*9525b14bSRao Shoaib * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") 37c478bd9Sstevel@tonic-gate * Copyright (c) 2001 by Internet Software Consortium. 47c478bd9Sstevel@tonic-gate * 57c478bd9Sstevel@tonic-gate * Permission to use, copy, modify, and distribute this software for any 67c478bd9Sstevel@tonic-gate * purpose with or without fee is hereby granted, provided that the above 77c478bd9Sstevel@tonic-gate * copyright notice and this permission notice appear in all copies. 87c478bd9Sstevel@tonic-gate * 9*9525b14bSRao Shoaib * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES 10*9525b14bSRao Shoaib * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11*9525b14bSRao Shoaib * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR 12*9525b14bSRao Shoaib * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13*9525b14bSRao Shoaib * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14*9525b14bSRao Shoaib * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT 15*9525b14bSRao Shoaib * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 167c478bd9Sstevel@tonic-gate */ 177c478bd9Sstevel@tonic-gate 187c478bd9Sstevel@tonic-gate #include <port_before.h> 197c478bd9Sstevel@tonic-gate #include <ctype.h> 207c478bd9Sstevel@tonic-gate #include <stdio.h> 217c478bd9Sstevel@tonic-gate #include <string.h> 227c478bd9Sstevel@tonic-gate #include <isc/misc.h> 237c478bd9Sstevel@tonic-gate #include <port_after.h> 247c478bd9Sstevel@tonic-gate 257c478bd9Sstevel@tonic-gate static const char hex[17] = "0123456789abcdef"; 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate int 287c478bd9Sstevel@tonic-gate isc_gethexstring(unsigned char *buf, size_t len, int count, FILE *fp, 297c478bd9Sstevel@tonic-gate int *multiline) 307c478bd9Sstevel@tonic-gate { 317c478bd9Sstevel@tonic-gate int c, n; 327c478bd9Sstevel@tonic-gate unsigned char x; 337c478bd9Sstevel@tonic-gate char *s; 347c478bd9Sstevel@tonic-gate int result = count; 357c478bd9Sstevel@tonic-gate 36*9525b14bSRao Shoaib x = 0; /*%< silence compiler */ 377c478bd9Sstevel@tonic-gate n = 0; 387c478bd9Sstevel@tonic-gate while (count > 0) { 397c478bd9Sstevel@tonic-gate c = fgetc(fp); 407c478bd9Sstevel@tonic-gate 417c478bd9Sstevel@tonic-gate if ((c == EOF) || 427c478bd9Sstevel@tonic-gate (c == '\n' && !*multiline) || 437c478bd9Sstevel@tonic-gate (c == '(' && *multiline) || 447c478bd9Sstevel@tonic-gate (c == ')' && !*multiline)) 457c478bd9Sstevel@tonic-gate goto formerr; 467c478bd9Sstevel@tonic-gate /* comment */ 477c478bd9Sstevel@tonic-gate if (c == ';') { 48*9525b14bSRao Shoaib do { 49*9525b14bSRao Shoaib c = fgetc(fp); 50*9525b14bSRao Shoaib } while (c != EOF && c != '\n'); 517c478bd9Sstevel@tonic-gate if (c == '\n' && *multiline) 527c478bd9Sstevel@tonic-gate continue; 537c478bd9Sstevel@tonic-gate goto formerr; 547c478bd9Sstevel@tonic-gate } 557c478bd9Sstevel@tonic-gate /* white space */ 567c478bd9Sstevel@tonic-gate if (c == ' ' || c == '\t' || c == '\n' || c == '\r') 577c478bd9Sstevel@tonic-gate continue; 587c478bd9Sstevel@tonic-gate /* multiline */ 597c478bd9Sstevel@tonic-gate if ('(' == c || c == ')') { 607c478bd9Sstevel@tonic-gate *multiline = (c == '(' /*)*/); 617c478bd9Sstevel@tonic-gate continue; 627c478bd9Sstevel@tonic-gate } 637c478bd9Sstevel@tonic-gate if ((s = strchr(hex, tolower(c))) == NULL) 647c478bd9Sstevel@tonic-gate goto formerr; 657c478bd9Sstevel@tonic-gate x = (x<<4) | (s - hex); 667c478bd9Sstevel@tonic-gate if (++n == 2) { 67*9525b14bSRao Shoaib if (len > 0U) { 687c478bd9Sstevel@tonic-gate *buf++ = x; 697c478bd9Sstevel@tonic-gate len--; 707c478bd9Sstevel@tonic-gate } else 717c478bd9Sstevel@tonic-gate result = -1; 727c478bd9Sstevel@tonic-gate count--; 737c478bd9Sstevel@tonic-gate n = 0; 747c478bd9Sstevel@tonic-gate } 757c478bd9Sstevel@tonic-gate } 767c478bd9Sstevel@tonic-gate return (result); 777c478bd9Sstevel@tonic-gate 787c478bd9Sstevel@tonic-gate formerr: 797c478bd9Sstevel@tonic-gate if (c == '\n') 807c478bd9Sstevel@tonic-gate ungetc(c, fp); 817c478bd9Sstevel@tonic-gate return (-1); 827c478bd9Sstevel@tonic-gate } 837c478bd9Sstevel@tonic-gate 847c478bd9Sstevel@tonic-gate void 857c478bd9Sstevel@tonic-gate isc_puthexstring(FILE *fp, const unsigned char *buf, size_t buflen, 867c478bd9Sstevel@tonic-gate size_t len1, size_t len2, const char *sep) 877c478bd9Sstevel@tonic-gate { 887c478bd9Sstevel@tonic-gate size_t i = 0; 897c478bd9Sstevel@tonic-gate 90*9525b14bSRao Shoaib if (len1 < 4U) 917c478bd9Sstevel@tonic-gate len1 = 4; 92*9525b14bSRao Shoaib if (len2 < 4U) 937c478bd9Sstevel@tonic-gate len2 = 4; 94*9525b14bSRao Shoaib while (buflen > 0U) { 957c478bd9Sstevel@tonic-gate fputc(hex[(buf[0]>>4)&0xf], fp); 967c478bd9Sstevel@tonic-gate fputc(hex[buf[0]&0xf], fp); 977c478bd9Sstevel@tonic-gate i += 2; 987c478bd9Sstevel@tonic-gate buflen--; 997c478bd9Sstevel@tonic-gate buf++; 1007c478bd9Sstevel@tonic-gate if (i >= len1 && sep != NULL) { 1017c478bd9Sstevel@tonic-gate fputs(sep, fp); 1027c478bd9Sstevel@tonic-gate i = 0; 1037c478bd9Sstevel@tonic-gate len1 = len2; 1047c478bd9Sstevel@tonic-gate } 1057c478bd9Sstevel@tonic-gate } 1067c478bd9Sstevel@tonic-gate } 1077c478bd9Sstevel@tonic-gate 1087c478bd9Sstevel@tonic-gate void 1097c478bd9Sstevel@tonic-gate isc_tohex(const unsigned char *buf, size_t buflen, char *t) { 110*9525b14bSRao Shoaib while (buflen > 0U) { 1117c478bd9Sstevel@tonic-gate *t++ = hex[(buf[0]>>4)&0xf]; 1127c478bd9Sstevel@tonic-gate *t++ = hex[buf[0]&0xf]; 1137c478bd9Sstevel@tonic-gate buf++; 1147c478bd9Sstevel@tonic-gate buflen--; 1157c478bd9Sstevel@tonic-gate } 1167c478bd9Sstevel@tonic-gate *t = '\0'; 1177c478bd9Sstevel@tonic-gate } 118*9525b14bSRao Shoaib 119*9525b14bSRao Shoaib /*! \file */ 120