1178825Sdfr/*
2178825Sdfr * Copyright (c) 2005 Kungliga Tekniska H�gskolan
3178825Sdfr * (Royal Institute of Technology, Stockholm, Sweden).
4178825Sdfr * All rights reserved.
5178825Sdfr *
6178825Sdfr * Redistribution and use in source and binary forms, with or without
7178825Sdfr * modification, are permitted provided that the following conditions
8178825Sdfr * are met:
9178825Sdfr *
10178825Sdfr * 1. Redistributions of source code must retain the above copyright
11178825Sdfr *    notice, this list of conditions and the following disclaimer.
12178825Sdfr *
13178825Sdfr * 2. Redistributions in binary form must reproduce the above copyright
14178825Sdfr *    notice, this list of conditions and the following disclaimer in the
15178825Sdfr *    documentation and/or other materials provided with the distribution.
16178825Sdfr *
17178825Sdfr * 3. Neither the name of the Institute nor the names of its contributors
18178825Sdfr *    may be used to endorse or promote products derived from this software
19178825Sdfr *    without specific prior written permission.
20178825Sdfr *
21178825Sdfr * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22178825Sdfr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23178825Sdfr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24178825Sdfr * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25178825Sdfr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26178825Sdfr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27178825Sdfr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28178825Sdfr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29178825Sdfr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30178825Sdfr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31178825Sdfr * SUCH DAMAGE.
32178825Sdfr */
33178825Sdfr
34178825Sdfr#include "der_locl.h"
35178825Sdfr#include <com_err.h>
36178825Sdfr#include <sys/types.h>
37178825Sdfr#include <sys/stat.h>
38178825Sdfr#include <ctype.h>
39178825Sdfr#include <getarg.h>
40178825Sdfr#include <hex.h>
41178825Sdfr#include <err.h>
42178825Sdfr
43178825SdfrRCSID("$Id: asn1_gen.c 16666 2006-01-30 15:06:03Z lha $");
44178825Sdfr
45178825Sdfrstatic int
46178825Sdfrdoit(const char *fn)
47178825Sdfr{
48178825Sdfr    char buf[2048];
49178825Sdfr    char *fnout;
50178825Sdfr    const char *bname;
51178825Sdfr    unsigned long line = 0;
52178825Sdfr    FILE *f, *fout;
53178825Sdfr    size_t offset = 0;
54178825Sdfr
55178825Sdfr    f = fopen(fn, "r");
56178825Sdfr    if (f == NULL)
57178825Sdfr	err(1, "fopen");
58178825Sdfr
59178825Sdfr    bname = strrchr(fn, '/');
60178825Sdfr    if (bname)
61178825Sdfr	bname++;
62178825Sdfr    else
63178825Sdfr	bname = fn;
64178825Sdfr
65178825Sdfr    asprintf(&fnout, "%s.out", bname);
66178825Sdfr    if (fnout == NULL)
67178825Sdfr	errx(1, "malloc");
68178825Sdfr
69178825Sdfr    fout = fopen(fnout, "w");
70178825Sdfr    if (fout == NULL)
71178825Sdfr	err(1, "fopen: output file");
72178825Sdfr
73178825Sdfr    while (fgets(buf, sizeof(buf), f) != NULL) {
74178825Sdfr	char *ptr, *class, *type, *tag, *length, *data, *foo;
75178825Sdfr	int ret, l, c, ty, ta;
76178825Sdfr	unsigned char p[6], *pdata;
77178825Sdfr	size_t sz;
78178825Sdfr
79178825Sdfr	line++;
80178825Sdfr
81178825Sdfr	buf[strcspn(buf, "\r\n")] = '\0';
82178825Sdfr	if (buf[0] == '#' || buf[0] == '\0')
83178825Sdfr	    continue;
84178825Sdfr
85178825Sdfr	ptr = buf;
86178825Sdfr	while (isspace((unsigned char)*ptr))
87178825Sdfr	       ptr++;
88178825Sdfr
89178825Sdfr	class = strtok_r(ptr, " \t\n", &foo);
90178825Sdfr	if (class == NULL) errx(1, "class missing on line %lu", line);
91178825Sdfr	type = strtok_r(NULL, " \t\n", &foo);
92178825Sdfr	if (type == NULL) errx(1, "type missing on line %lu", line);
93178825Sdfr	tag = strtok_r(NULL, " \t\n", &foo);
94178825Sdfr	if (tag == NULL) errx(1, "tag missing on line %lu", line);
95178825Sdfr	length = strtok_r(NULL, " \t\n", &foo);
96178825Sdfr	if (length == NULL) errx(1, "length missing on line %lu", line);
97178825Sdfr	data = strtok_r(NULL, " \t\n", &foo);
98178825Sdfr
99178825Sdfr	c = der_get_class_num(class);
100178825Sdfr	if (c == -1) errx(1, "no valid class on line %lu", line);
101178825Sdfr	ty = der_get_type_num(type);
102178825Sdfr	if (ty == -1) errx(1, "no valid type on line %lu", line);
103178825Sdfr	ta = der_get_tag_num(tag);
104178825Sdfr	if (ta == -1)
105178825Sdfr	    ta = atoi(tag);
106178825Sdfr
107178825Sdfr	l = atoi(length);
108178825Sdfr
109178825Sdfr	printf("line: %3lu offset: %3lu class: %d type: %d "
110178825Sdfr	       "tag: %3d length: %3d %s\n",
111178825Sdfr	       line, (unsigned long)offset, c, ty, ta, l,
112178825Sdfr	       data ? "<have data>" : "<no data>");
113178825Sdfr
114178825Sdfr	ret = der_put_length_and_tag(p + sizeof(p) - 1, sizeof(p),
115178825Sdfr				     l,
116178825Sdfr				     c,
117178825Sdfr				     ty,
118178825Sdfr				     ta,
119178825Sdfr				     &sz);
120178825Sdfr	if (ret)
121178825Sdfr	    errx(1, "der_put_length_and_tag: %d", ret);
122178825Sdfr
123178825Sdfr	if (fwrite(p + sizeof(p) - sz , sz, 1, fout) != 1)
124178825Sdfr	    err(1, "fwrite length/tag failed");
125178825Sdfr	offset += sz;
126178825Sdfr
127178825Sdfr	if (data) {
128178825Sdfr	    size_t datalen;
129178825Sdfr
130178825Sdfr	    datalen = strlen(data) / 2;
131178825Sdfr	    pdata = emalloc(sz);
132178825Sdfr
133178825Sdfr	    if (hex_decode(data, pdata, datalen) != datalen)
134178825Sdfr		errx(1, "failed to decode data");
135178825Sdfr
136178825Sdfr	    if (fwrite(pdata, datalen, 1, fout) != 1)
137178825Sdfr		err(1, "fwrite data failed");
138178825Sdfr	    offset += datalen;
139178825Sdfr
140178825Sdfr	    free(pdata);
141178825Sdfr	}
142178825Sdfr    }
143178825Sdfr    printf("line: eof offset: %lu\n", (unsigned long)offset);
144178825Sdfr
145178825Sdfr    fclose(fout);
146178825Sdfr    fclose(f);
147178825Sdfr    return 0;
148178825Sdfr}
149178825Sdfr
150178825Sdfr
151178825Sdfrstatic int version_flag;
152178825Sdfrstatic int help_flag;
153178825Sdfrstruct getargs args[] = {
154178825Sdfr    { "version", 0, arg_flag, &version_flag },
155178825Sdfr    { "help", 0, arg_flag, &help_flag }
156178825Sdfr};
157178825Sdfrint num_args = sizeof(args) / sizeof(args[0]);
158178825Sdfr
159178825Sdfrstatic void
160178825Sdfrusage(int code)
161178825Sdfr{
162178825Sdfr    arg_printusage(args, num_args, NULL, "parse-file");
163178825Sdfr    exit(code);
164178825Sdfr}
165178825Sdfr
166178825Sdfrint
167178825Sdfrmain(int argc, char **argv)
168178825Sdfr{
169178825Sdfr    int optidx = 0;
170178825Sdfr
171178825Sdfr    setprogname (argv[0]);
172178825Sdfr
173178825Sdfr    if(getarg(args, num_args, argc, argv, &optidx))
174178825Sdfr	usage(1);
175178825Sdfr    if(help_flag)
176178825Sdfr	usage(0);
177178825Sdfr    if(version_flag) {
178178825Sdfr	print_version(NULL);
179178825Sdfr	exit(0);
180178825Sdfr    }
181178825Sdfr    argv += optidx;
182178825Sdfr    argc -= optidx;
183178825Sdfr    if (argc != 1)
184178825Sdfr	usage (1);
185178825Sdfr
186178825Sdfr    return doit (argv[0]);
187178825Sdfr}
188