#!/bin/sh

# Copyright (c) 2023 by DomainTools LLC
# Copyright (c) 2009, 2012, 2015 by Farsight Security, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

if [ $# -ne 1 ] ; then
  echo "ERROR: Single argument expected"
  exit 1
fi

header_fname="$1"

## Build array of the variables of interest, WDNS_CLASS_
## #define WDNS_CLASS_IN 1

defines=$(grep WDNS_CLASS_ "$header_fname" | awk '{print $2}')

## Sort based on name
sorted=$(echo "$defines" | sort)

cat <<EOF
/*
 * Copyright (c) Farsight Security, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

const char *
wdns_rrclass_to_str(uint16_t rrclass)
{
   switch (rrclass) {
EOF

for d in $defines ; do
	n=$(echo "$d" | sed -e 's/WDNS_[A-Z]*_//' -e 's/_/-/g')
	echo "   case $d: return (\"$n\");"
done

cat <<EOF
   }

   return (NULL);
}

static struct u16str {
    uint16_t u16;
    const char *str;
} rrclasses[] = {
EOF

for l in $sorted ; do
	n=$(echo "$l" | sed -e 's/WDNS_[A-Z]*_//' -e 's/_/-/g')
	echo "   { $l, \"$n\" },"
done

cat <<EOF
};

#define num_rrclasses (sizeof(rrclasses) / sizeof(struct u16str))

static int
cmp_u16str(const void *a, const void *b) {
    struct u16str *u1 = (struct u16str *) a;
    struct u16str *u2 = (struct u16str *) b;
    return (strcasecmp(u1->str, u2->str));
}

static bool
convert_generic_rrclass(const char *s, long int *val) {
    char *endptr = NULL;

    if (strlen(s) <= 5)
        return (false);
    if (strncasecmp(s, "CLASS", 5) != 0)
        return (false);

    s += 5; /* skip leading "CLASS" */

    *val = strtol(s, &endptr, 10);
    if (endptr != NULL && *endptr != '\\0')
        return (false);
    if (*val < 0 || *val > 65535)
        return (false);

    return (true);
}

uint16_t
wdns_str_to_rrclass(const char *str) {
    struct u16str key, *res;
    key.str = str;
    res = bsearch(&key, rrclasses, num_rrclasses, sizeof(struct u16str), cmp_u16str);
    if (res != NULL) {
        return (res->u16);
    } else {
        long int val = 0;
        if (convert_generic_rrclass(str, &val))
            return (val);
    }
    return (0);
}
EOF
