Path: blob/main/crypto/heimdal/lib/asn1/asn1_gen.c
34878 views
/*1* Copyright (c) 2005 Kungliga Tekniska Högskolan2* (Royal Institute of Technology, Stockholm, Sweden).3* All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8*9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11*12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* 3. Neither the name of the Institute nor the names of its contributors17* may be used to endorse or promote products derived from this software18* without specific prior written permission.19*20* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND21* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE23* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE24* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL25* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS26* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)27* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT28* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY29* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF30* SUCH DAMAGE.31*/3233#include "der_locl.h"34#include <com_err.h>35#include <sys/types.h>36#include <sys/stat.h>37#include <ctype.h>38#include <getarg.h>39#include <hex.h>40#include <err.h>4142RCSID("$Id$");4344static int45doit(const char *fn)46{47char buf[2048];48char *fnout = NULL;49const char *bname;50unsigned long line = 0;51FILE *f, *fout;52size_t offset = 0;5354f = fopen(fn, "r");55if (f == NULL)56err(1, "fopen");5758bname = strrchr(fn, '/');59if (bname)60bname++;61else62bname = fn;6364if (asprintf(&fnout, "%s.out", bname) < 0 || fnout == NULL)65errx(1, "malloc");6667fout = fopen(fnout, "w");68if (fout == NULL)69err(1, "fopen: output file");7071while (fgets(buf, sizeof(buf), f) != NULL) {72char *ptr, *class, *type, *tag, *length, *data, *foo;73int ret, l, c, ty, ta;74unsigned char p[6], *pdata;75size_t sz;7677line++;7879buf[strcspn(buf, "\r\n")] = '\0';80if (buf[0] == '#' || buf[0] == '\0')81continue;8283ptr = buf;84while (isspace((unsigned char)*ptr))85ptr++;8687class = strtok_r(ptr, " \t\n", &foo);88if (class == NULL) errx(1, "class missing on line %lu", line);89type = strtok_r(NULL, " \t\n", &foo);90if (type == NULL) errx(1, "type missing on line %lu", line);91tag = strtok_r(NULL, " \t\n", &foo);92if (tag == NULL) errx(1, "tag missing on line %lu", line);93length = strtok_r(NULL, " \t\n", &foo);94if (length == NULL) errx(1, "length missing on line %lu", line);95data = strtok_r(NULL, " \t\n", &foo);9697c = der_get_class_num(class);98if (c == -1) errx(1, "no valid class on line %lu", line);99ty = der_get_type_num(type);100if (ty == -1) errx(1, "no valid type on line %lu", line);101ta = der_get_tag_num(tag);102if (ta == -1)103ta = atoi(tag);104105l = atoi(length);106107printf("line: %3lu offset: %3lu class: %d type: %d "108"tag: %3d length: %3d %s\n",109line, (unsigned long)offset, c, ty, ta, l,110data ? "<have data>" : "<no data>");111112ret = der_put_length_and_tag(p + sizeof(p) - 1, sizeof(p),113l,114c,115ty,116ta,117&sz);118if (ret)119errx(1, "der_put_length_and_tag: %d", ret);120121if (fwrite(p + sizeof(p) - sz , sz, 1, fout) != 1)122err(1, "fwrite length/tag failed");123offset += sz;124125if (data) {126size_t datalen;127128datalen = strlen(data) / 2;129pdata = emalloc(sz);130131if (hex_decode(data, pdata, datalen) != datalen)132errx(1, "failed to decode data");133134if (fwrite(pdata, datalen, 1, fout) != 1)135err(1, "fwrite data failed");136offset += datalen;137138free(pdata);139}140}141printf("line: eof offset: %lu\n", (unsigned long)offset);142143fclose(fout);144fclose(f);145return 0;146}147148149static int version_flag;150static int help_flag;151struct getargs args[] = {152{ "version", 0, arg_flag, &version_flag },153{ "help", 0, arg_flag, &help_flag }154};155int num_args = sizeof(args) / sizeof(args[0]);156157static void158usage(int code)159{160arg_printusage(args, num_args, NULL, "parse-file");161exit(code);162}163164int165main(int argc, char **argv)166{167int optidx = 0;168169setprogname (argv[0]);170171if(getarg(args, num_args, argc, argv, &optidx))172usage(1);173if(help_flag)174usage(0);175if(version_flag) {176print_version(NULL);177exit(0);178}179argv += optidx;180argc -= optidx;181if (argc != 1)182usage (1);183184return doit (argv[0]);185}186187188