1/*
2 * Copyright (c) 2005 Kungliga Tekniska H�gskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 *
17 * 3. Neither the name of the Institute nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#include "der_locl.h"
35#include <com_err.h>
36#include <sys/types.h>
37#include <sys/stat.h>
38#include <ctype.h>
39#include <getarg.h>
40#include <hex.h>
41#include <err.h>
42
43RCSID("$Id: asn1_gen.c 16666 2006-01-30 15:06:03Z lha $");
44
45static int
46doit(const char *fn)
47{
48    char buf[2048];
49    char *fnout;
50    const char *bname;
51    unsigned long line = 0;
52    FILE *f, *fout;
53    size_t offset = 0;
54
55    f = fopen(fn, "r");
56    if (f == NULL)
57	err(1, "fopen");
58
59    bname = strrchr(fn, '/');
60    if (bname)
61	bname++;
62    else
63	bname = fn;
64
65    asprintf(&fnout, "%s.out", bname);
66    if (fnout == NULL)
67	errx(1, "malloc");
68
69    fout = fopen(fnout, "w");
70    if (fout == NULL)
71	err(1, "fopen: output file");
72
73    while (fgets(buf, sizeof(buf), f) != NULL) {
74	char *ptr, *class, *type, *tag, *length, *data, *foo;
75	int ret, l, c, ty, ta;
76	unsigned char p[6], *pdata;
77	size_t sz;
78
79	line++;
80
81	buf[strcspn(buf, "\r\n")] = '\0';
82	if (buf[0] == '#' || buf[0] == '\0')
83	    continue;
84
85	ptr = buf;
86	while (isspace((unsigned char)*ptr))
87	       ptr++;
88
89	class = strtok_r(ptr, " \t\n", &foo);
90	if (class == NULL) errx(1, "class missing on line %lu", line);
91	type = strtok_r(NULL, " \t\n", &foo);
92	if (type == NULL) errx(1, "type missing on line %lu", line);
93	tag = strtok_r(NULL, " \t\n", &foo);
94	if (tag == NULL) errx(1, "tag missing on line %lu", line);
95	length = strtok_r(NULL, " \t\n", &foo);
96	if (length == NULL) errx(1, "length missing on line %lu", line);
97	data = strtok_r(NULL, " \t\n", &foo);
98
99	c = der_get_class_num(class);
100	if (c == -1) errx(1, "no valid class on line %lu", line);
101	ty = der_get_type_num(type);
102	if (ty == -1) errx(1, "no valid type on line %lu", line);
103	ta = der_get_tag_num(tag);
104	if (ta == -1)
105	    ta = atoi(tag);
106
107	l = atoi(length);
108
109	printf("line: %3lu offset: %3lu class: %d type: %d "
110	       "tag: %3d length: %3d %s\n",
111	       line, (unsigned long)offset, c, ty, ta, l,
112	       data ? "<have data>" : "<no data>");
113
114	ret = der_put_length_and_tag(p + sizeof(p) - 1, sizeof(p),
115				     l,
116				     c,
117				     ty,
118				     ta,
119				     &sz);
120	if (ret)
121	    errx(1, "der_put_length_and_tag: %d", ret);
122
123	if (fwrite(p + sizeof(p) - sz , sz, 1, fout) != 1)
124	    err(1, "fwrite length/tag failed");
125	offset += sz;
126
127	if (data) {
128	    size_t datalen;
129
130	    datalen = strlen(data) / 2;
131	    pdata = emalloc(sz);
132
133	    if (hex_decode(data, pdata, datalen) != datalen)
134		errx(1, "failed to decode data");
135
136	    if (fwrite(pdata, datalen, 1, fout) != 1)
137		err(1, "fwrite data failed");
138	    offset += datalen;
139
140	    free(pdata);
141	}
142    }
143    printf("line: eof offset: %lu\n", (unsigned long)offset);
144
145    fclose(fout);
146    fclose(f);
147    return 0;
148}
149
150
151static int version_flag;
152static int help_flag;
153struct getargs args[] = {
154    { "version", 0, arg_flag, &version_flag },
155    { "help", 0, arg_flag, &help_flag }
156};
157int num_args = sizeof(args) / sizeof(args[0]);
158
159static void
160usage(int code)
161{
162    arg_printusage(args, num_args, NULL, "parse-file");
163    exit(code);
164}
165
166int
167main(int argc, char **argv)
168{
169    int optidx = 0;
170
171    setprogname (argv[0]);
172
173    if(getarg(args, num_args, argc, argv, &optidx))
174	usage(1);
175    if(help_flag)
176	usage(0);
177    if(version_flag) {
178	print_version(NULL);
179	exit(0);
180    }
181    argv += optidx;
182    argc -= optidx;
183    if (argc != 1)
184	usage (1);
185
186    return doit (argv[0]);
187}
188