2N/A/*
2N/A * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
2N/A * Copyright (c) 2001 by Internet Software Consortium.
2N/A *
2N/A * Permission to use, copy, modify, and distribute this software for any
2N/A * purpose with or without fee is hereby granted, provided that the above
2N/A * copyright notice and this permission notice appear in all copies.
2N/A *
2N/A * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
2N/A * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
2N/A * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
2N/A * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
2N/A * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
2N/A * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
2N/A * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2N/A */
2N/A
2N/A#include <port_before.h>
2N/A#include <ctype.h>
2N/A#include <stdio.h>
2N/A#include <string.h>
2N/A#include <isc/misc.h>
2N/A#include <port_after.h>
2N/A
2N/Astatic const char hex[17] = "0123456789abcdef";
2N/A
2N/Aint
2N/Aisc_gethexstring(unsigned char *buf, size_t len, int count, FILE *fp,
2N/A int *multiline)
2N/A{
2N/A int c, n;
2N/A unsigned char x;
2N/A char *s;
2N/A int result = count;
2N/A
2N/A x = 0; /*%< silence compiler */
2N/A n = 0;
2N/A while (count > 0) {
2N/A c = fgetc(fp);
2N/A
2N/A if ((c == EOF) ||
2N/A (c == '\n' && !*multiline) ||
2N/A (c == '(' && *multiline) ||
2N/A (c == ')' && !*multiline))
2N/A goto formerr;
2N/A /* comment */
2N/A if (c == ';') {
2N/A do {
2N/A c = fgetc(fp);
2N/A } while (c != EOF && c != '\n');
2N/A if (c == '\n' && *multiline)
2N/A continue;
2N/A goto formerr;
2N/A }
2N/A /* white space */
2N/A if (c == ' ' || c == '\t' || c == '\n' || c == '\r')
2N/A continue;
2N/A /* multiline */
2N/A if ('(' == c || c == ')') {
2N/A *multiline = (c == '(' /*)*/);
2N/A continue;
2N/A }
2N/A if ((s = strchr(hex, tolower(c))) == NULL)
2N/A goto formerr;
2N/A x = (x<<4) | (s - hex);
2N/A if (++n == 2) {
2N/A if (len > 0U) {
2N/A *buf++ = x;
2N/A len--;
2N/A } else
2N/A result = -1;
2N/A count--;
2N/A n = 0;
2N/A }
2N/A }
2N/A return (result);
2N/A
2N/A formerr:
2N/A if (c == '\n')
2N/A ungetc(c, fp);
2N/A return (-1);
2N/A}
2N/A
2N/Avoid
2N/Aisc_puthexstring(FILE *fp, const unsigned char *buf, size_t buflen,
2N/A size_t len1, size_t len2, const char *sep)
2N/A{
2N/A size_t i = 0;
2N/A
2N/A if (len1 < 4U)
2N/A len1 = 4;
2N/A if (len2 < 4U)
2N/A len2 = 4;
2N/A while (buflen > 0U) {
2N/A fputc(hex[(buf[0]>>4)&0xf], fp);
2N/A fputc(hex[buf[0]&0xf], fp);
2N/A i += 2;
2N/A buflen--;
2N/A buf++;
2N/A if (i >= len1 && sep != NULL) {
2N/A fputs(sep, fp);
2N/A i = 0;
2N/A len1 = len2;
2N/A }
2N/A }
2N/A}
2N/A
2N/Avoid
2N/Aisc_tohex(const unsigned char *buf, size_t buflen, char *t) {
2N/A while (buflen > 0U) {
2N/A *t++ = hex[(buf[0]>>4)&0xf];
2N/A *t++ = hex[buf[0]&0xf];
2N/A buf++;
2N/A buflen--;
2N/A }
2N/A *t = '\0';
2N/A}
2N/A
2N/A/*! \file */