xx
git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@879 ec53bebd-3082-4978-b11e-865c3cabbd6b
This commit is contained in:
@@ -1,28 +0,0 @@
|
|||||||
# $Id$
|
|
||||||
|
|
||||||
AUTOMAKE_OPTIONS = no-dependencies
|
|
||||||
|
|
||||||
lib_LIBRARIES = asn1
|
|
||||||
|
|
||||||
asn1_SOURCES = asn1.c der_get.c der_put.c
|
|
||||||
|
|
||||||
|
|
||||||
noinst_PROGRAMS = asn1_compile
|
|
||||||
|
|
||||||
asn1_compile_SOURCES = parse.y lex.l main.c hash.c symbol.c gen.c
|
|
||||||
|
|
||||||
parse.c: parse.y
|
|
||||||
$(YACC) -d $(YFLAGS) $(srcdir)/parse.y
|
|
||||||
mv y.tab.c parse.c
|
|
||||||
|
|
||||||
parse.h: parse.c
|
|
||||||
mv y.tab.h parse.h
|
|
||||||
|
|
||||||
lex.c: lex.l
|
|
||||||
$(LEX) $(LFLAGS) -t $(srcdir)/lex.l > lex.c
|
|
||||||
|
|
||||||
lex.o: parse.h
|
|
||||||
|
|
||||||
asn1.c: asn1_compile k5.asn1
|
|
||||||
./asn1_compile $(srcdir)/k5.asn1
|
|
||||||
|
|
41
asn1/der.h
41
asn1/der.h
@@ -1,41 +0,0 @@
|
|||||||
/* $Id$ */
|
|
||||||
|
|
||||||
#ifndef DER_H
|
|
||||||
|
|
||||||
#define DER_H
|
|
||||||
|
|
||||||
typedef enum {UNIV = 0, APPL = 1, CONTEXT = 2 , PRIVATE = 3} Der_class;
|
|
||||||
|
|
||||||
typedef enum {PRIM = 0, CONS = 1} Der_type;
|
|
||||||
|
|
||||||
/* Universal tags */
|
|
||||||
|
|
||||||
enum {
|
|
||||||
UT_Integer = 2,
|
|
||||||
UT_BitString = 3,
|
|
||||||
UT_OctetString = 4,
|
|
||||||
UT_Null = 5,
|
|
||||||
UT_ObjID = 6,
|
|
||||||
UT_Sequence = 16,
|
|
||||||
UT_Set = 17,
|
|
||||||
UT_PrintableString = 19,
|
|
||||||
UT_IA5String = 22,
|
|
||||||
UT_UTCTime = 23,
|
|
||||||
UT_GeneralizedTime = 24,
|
|
||||||
UT_GeneralString = 27,
|
|
||||||
};
|
|
||||||
|
|
||||||
/**/
|
|
||||||
|
|
||||||
struct krb5_data {
|
|
||||||
unsigned len;
|
|
||||||
unsigned char *data;
|
|
||||||
};
|
|
||||||
|
|
||||||
typedef struct krb5_data krb5_data;
|
|
||||||
|
|
||||||
krb5_data string_make (char *);
|
|
||||||
krb5_data string_make_n (int len, char *);
|
|
||||||
void string_free (krb5_data);
|
|
||||||
|
|
||||||
#endif /* DER_H */
|
|
240
asn1/der_get.c
240
asn1/der_get.c
@@ -1,240 +0,0 @@
|
|||||||
/* $Id$ */
|
|
||||||
|
|
||||||
#ifdef HAVE_CONFIG_H
|
|
||||||
#include <config.h>
|
|
||||||
#endif
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <time.h>
|
|
||||||
#include "der.h"
|
|
||||||
|
|
||||||
/*
|
|
||||||
* All decoding functions take a pointer `p' to first position in
|
|
||||||
* which to read, from the left, `len' which means the maximum
|
|
||||||
* number of characters we are able to read and return an int
|
|
||||||
* indicating how many actually got read, or <0 in case of errors.
|
|
||||||
*/
|
|
||||||
|
|
||||||
int
|
|
||||||
der_get_int (unsigned char *p, int len, int *ret)
|
|
||||||
{
|
|
||||||
int val = 0;
|
|
||||||
int oldlen = len;
|
|
||||||
|
|
||||||
while (len--)
|
|
||||||
val = val * 256 + *p++;
|
|
||||||
*ret = val;
|
|
||||||
return oldlen;
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
der_get_length (unsigned char *p, int len, int *ret)
|
|
||||||
{
|
|
||||||
int val;
|
|
||||||
|
|
||||||
if (--len < 0)
|
|
||||||
return -1;
|
|
||||||
val = *p++;
|
|
||||||
if (val < 128) {
|
|
||||||
*ret = val;
|
|
||||||
return 1;
|
|
||||||
} else {
|
|
||||||
int l;
|
|
||||||
|
|
||||||
val &= 0x7F;
|
|
||||||
if (len < val)
|
|
||||||
return -1;
|
|
||||||
l = der_get_int (p, val, ret);
|
|
||||||
if (l < 0)
|
|
||||||
return l;
|
|
||||||
else
|
|
||||||
return l+1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
der_get_general_string (unsigned char *p, int len, char **str)
|
|
||||||
{
|
|
||||||
int l, slen;
|
|
||||||
char *s;
|
|
||||||
|
|
||||||
l = der_get_length (p, len, &slen);
|
|
||||||
if (l < 0)
|
|
||||||
return l;
|
|
||||||
p += l;
|
|
||||||
len -= l;
|
|
||||||
if (len < slen)
|
|
||||||
return -1;
|
|
||||||
s = malloc (slen + 1);
|
|
||||||
if (s == NULL)
|
|
||||||
return -1;
|
|
||||||
memcpy (s, p, slen);
|
|
||||||
s[slen] = '\0';
|
|
||||||
*str = s;
|
|
||||||
return slen + l;
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
der_get_octet_string (unsigned char *p, int len, krb5_data *data)
|
|
||||||
{
|
|
||||||
int l, slen;
|
|
||||||
|
|
||||||
l = der_get_length (p, len, &slen);
|
|
||||||
if (l < 0)
|
|
||||||
return l;
|
|
||||||
p += l;
|
|
||||||
len -= l;
|
|
||||||
if (len < slen)
|
|
||||||
return -1;
|
|
||||||
data->len = slen;
|
|
||||||
data->data = malloc(slen);
|
|
||||||
if (data->data == NULL)
|
|
||||||
return -1;
|
|
||||||
memcpy (data->data, p, slen);
|
|
||||||
return slen + l;
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
der_get_tag (unsigned char *p, int len, Der_class *class, Der_type *type,
|
|
||||||
int *tag)
|
|
||||||
{
|
|
||||||
if (len < 1)
|
|
||||||
return -1;
|
|
||||||
*class = ((*p) >> 6) & 0x03;
|
|
||||||
*type = ((*p) >> 5) & 0x01;
|
|
||||||
*tag = (*p) & 0x1F;
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
der_match_tag (unsigned char *p, int len, Der_class class, Der_type type,
|
|
||||||
int tag)
|
|
||||||
{
|
|
||||||
int l;
|
|
||||||
Der_class thisclass;
|
|
||||||
Der_type thistype;
|
|
||||||
int thistag;
|
|
||||||
|
|
||||||
l = der_get_tag (p, len, &thisclass, &thistype, &thistag);
|
|
||||||
if (l < 0)
|
|
||||||
return l;
|
|
||||||
if (class == thisclass && type == thistype && tag == thistag)
|
|
||||||
return l;
|
|
||||||
else
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
decode_integer (unsigned char *p, int len, void *data)
|
|
||||||
{
|
|
||||||
int *num = (int *)data;
|
|
||||||
int ret = 0;
|
|
||||||
int l, reallen;
|
|
||||||
|
|
||||||
l = der_match_tag (p, len, UNIV, PRIM, UT_Integer);
|
|
||||||
if (l < 0)
|
|
||||||
return l;
|
|
||||||
p += l;
|
|
||||||
len -= l;
|
|
||||||
ret += l;
|
|
||||||
l = der_get_length (p, len, &reallen);
|
|
||||||
if (l < 0)
|
|
||||||
return l;
|
|
||||||
p += l;
|
|
||||||
len -= l;
|
|
||||||
ret += l;
|
|
||||||
l = der_get_int (p, reallen, num);
|
|
||||||
if (l < 0)
|
|
||||||
return l;
|
|
||||||
p += l;
|
|
||||||
len -= l;
|
|
||||||
ret += l;
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
decode_general_string (unsigned char *p, int len, void *data)
|
|
||||||
{
|
|
||||||
char **str = (char **)data;
|
|
||||||
int ret = 0;
|
|
||||||
int l;
|
|
||||||
|
|
||||||
l = der_match_tag (p, len, UNIV, PRIM, UT_GeneralString);
|
|
||||||
if (l < 0)
|
|
||||||
return l;
|
|
||||||
p += l;
|
|
||||||
len -= l;
|
|
||||||
ret += l;
|
|
||||||
l = der_get_general_string (p, len, str);
|
|
||||||
if (l < 0)
|
|
||||||
return l;
|
|
||||||
p += l;
|
|
||||||
len -= l;
|
|
||||||
ret += l;
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
decode_octet_string (unsigned char *p, int len, void *data)
|
|
||||||
{
|
|
||||||
krb5_data *k = (krb5_data *)data;
|
|
||||||
int ret = 0;
|
|
||||||
int l;
|
|
||||||
|
|
||||||
l = der_match_tag (p, len, UNIV, PRIM, UT_OctetString);
|
|
||||||
if (l < 0)
|
|
||||||
return l;
|
|
||||||
p += l;
|
|
||||||
len -= l;
|
|
||||||
ret += l;
|
|
||||||
l = der_get_octet_string (p, len, k);
|
|
||||||
if (l < 0)
|
|
||||||
return l;
|
|
||||||
p += l;
|
|
||||||
len -= l;
|
|
||||||
ret += l;
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
generalizedtime2time (char *s, time_t *t)
|
|
||||||
{
|
|
||||||
struct tm tm;
|
|
||||||
|
|
||||||
sscanf (s, "%04d%02d%02d%02d%02d%02dZ",
|
|
||||||
&tm.tm_year, &tm.tm_mon, &tm.tm_mday, &tm.tm_hour,
|
|
||||||
&tm.tm_min, &tm.tm_sec);
|
|
||||||
tm.tm_year -= 1900;
|
|
||||||
tm.tm_mon -= 1;
|
|
||||||
tm.tm_isdst = 0;
|
|
||||||
|
|
||||||
*t = mktime(&tm);
|
|
||||||
#if 0 /* XXX */
|
|
||||||
*t -= timezone;
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
decode_generalized_time (unsigned char *p, int len, void *data)
|
|
||||||
{
|
|
||||||
time_t *t = (time_t *)data;
|
|
||||||
krb5_data k;
|
|
||||||
int ret = 0;
|
|
||||||
int l;
|
|
||||||
|
|
||||||
l = der_match_tag (p, len, UNIV, PRIM, UT_GeneralizedTime);
|
|
||||||
if (l < 0)
|
|
||||||
return l;
|
|
||||||
p += l;
|
|
||||||
len -= l;
|
|
||||||
ret += l;
|
|
||||||
l = der_get_octet_string (p, len, &k);
|
|
||||||
if (l < 0)
|
|
||||||
return l;
|
|
||||||
p += l;
|
|
||||||
len -= l;
|
|
||||||
ret += l;
|
|
||||||
generalizedtime2time (k.data, t);
|
|
||||||
free (k.data);
|
|
||||||
return ret;
|
|
||||||
}
|
|
214
asn1/der_put.c
214
asn1/der_put.c
@@ -1,214 +0,0 @@
|
|||||||
/* $Id$ */
|
|
||||||
|
|
||||||
#ifdef HAVE_CONFIG_H
|
|
||||||
#include <config.h>
|
|
||||||
#endif
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <time.h>
|
|
||||||
#include "der.h"
|
|
||||||
|
|
||||||
/*
|
|
||||||
* All encoding functions take a pointer `p' to first position in
|
|
||||||
* which to write, from the right, `len' which means the maximum
|
|
||||||
* number of characters we are able to write and return an int
|
|
||||||
* indicating how many actually got written, or <0 in case of errors.
|
|
||||||
*/
|
|
||||||
|
|
||||||
int
|
|
||||||
der_put_int (unsigned char *p, int len, int val)
|
|
||||||
{
|
|
||||||
unsigned char *base = p;
|
|
||||||
|
|
||||||
if (val) {
|
|
||||||
while (len > 0 && val) {
|
|
||||||
*p-- = val % 256;
|
|
||||||
val /= 256;
|
|
||||||
--len;
|
|
||||||
}
|
|
||||||
if (val)
|
|
||||||
return -1;
|
|
||||||
else
|
|
||||||
return base - p;
|
|
||||||
} else if (len < 1)
|
|
||||||
return -1;
|
|
||||||
else {
|
|
||||||
*p = 0;
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
der_put_length (unsigned char *p, int len, int val)
|
|
||||||
{
|
|
||||||
if (val < 128) {
|
|
||||||
if (len < 1)
|
|
||||||
return -1;
|
|
||||||
else {
|
|
||||||
*p = val;
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
int l;
|
|
||||||
|
|
||||||
l = der_put_int (p, len - 1,val);
|
|
||||||
if (l < 0)
|
|
||||||
return l;
|
|
||||||
p -= l;
|
|
||||||
*p = 0x80 | l;
|
|
||||||
return l + 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
der_put_general_string (unsigned char *p, int len, char *str)
|
|
||||||
{
|
|
||||||
int slen = strlen(str);
|
|
||||||
int l;
|
|
||||||
|
|
||||||
if (len < slen)
|
|
||||||
return -1;
|
|
||||||
p -= slen;
|
|
||||||
len -= slen;
|
|
||||||
memcpy (p+1, str, slen);
|
|
||||||
l = der_put_length (p, len, slen);
|
|
||||||
if(l < 0)
|
|
||||||
return l;
|
|
||||||
return slen + l;
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
der_put_octet_string (unsigned char *p, int len, krb5_data *data)
|
|
||||||
{
|
|
||||||
int l;
|
|
||||||
|
|
||||||
if (len < data->len)
|
|
||||||
return -1;
|
|
||||||
p -= data->len;
|
|
||||||
len -= data->len;
|
|
||||||
memcpy (p+1, data->data, data->len);
|
|
||||||
l = der_put_length (p, len, data->len);
|
|
||||||
if (l < 0)
|
|
||||||
return l;
|
|
||||||
return l + data->len;
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
der_put_tag (unsigned char *p, int len, Der_class class, Der_type type,
|
|
||||||
int tag)
|
|
||||||
{
|
|
||||||
if (len < 1)
|
|
||||||
return -1;
|
|
||||||
*p = (class << 6) | (type << 5) | tag; /* XXX */
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
encode_integer (unsigned char *p, int len, void *data)
|
|
||||||
{
|
|
||||||
int num = *((int *)data);
|
|
||||||
int ret = 0;
|
|
||||||
int l;
|
|
||||||
|
|
||||||
l = der_put_int (p, len, num);
|
|
||||||
if (l < 0)
|
|
||||||
return l;
|
|
||||||
p -= l;
|
|
||||||
len -= l;
|
|
||||||
ret += l;
|
|
||||||
l = der_put_length (p, len, l);
|
|
||||||
if (l < 0)
|
|
||||||
return l;
|
|
||||||
p -= l;
|
|
||||||
len -= l;
|
|
||||||
ret += l;
|
|
||||||
l = der_put_tag (p, len, UNIV, PRIM, UT_Integer);
|
|
||||||
if (l < 0)
|
|
||||||
return l;
|
|
||||||
p -= l;
|
|
||||||
len -= l;
|
|
||||||
ret += l;
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
encode_general_string (unsigned char *p, int len, void *data)
|
|
||||||
{
|
|
||||||
char *str = *((char **)data);
|
|
||||||
int ret = 0;
|
|
||||||
int l;
|
|
||||||
|
|
||||||
l = der_put_general_string (p, len, str);
|
|
||||||
if (l < 0)
|
|
||||||
return l;
|
|
||||||
p -= l;
|
|
||||||
len -= l;
|
|
||||||
ret += l;
|
|
||||||
l = der_put_tag (p, len, UNIV, PRIM, UT_GeneralString);
|
|
||||||
if (l < 0)
|
|
||||||
return l;
|
|
||||||
p -= l;
|
|
||||||
len -= l;
|
|
||||||
ret += l;
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
encode_octet_string (unsigned char *p, int len, void *data)
|
|
||||||
{
|
|
||||||
krb5_data *k = (krb5_data *)data;
|
|
||||||
int ret = 0;
|
|
||||||
int l;
|
|
||||||
|
|
||||||
l = der_put_octet_string (p, len, k);
|
|
||||||
if (l < 0)
|
|
||||||
return l;
|
|
||||||
p -= l;
|
|
||||||
len -= l;
|
|
||||||
ret += l;
|
|
||||||
l = der_put_tag (p, len, UNIV, PRIM, UT_OctetString);
|
|
||||||
if (l < 0)
|
|
||||||
return l;
|
|
||||||
p -= l;
|
|
||||||
len -= l;
|
|
||||||
ret += l;
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
time2generalizedtime (time_t t, krb5_data *s)
|
|
||||||
{
|
|
||||||
struct tm *tm;
|
|
||||||
|
|
||||||
s->data = malloc(16);
|
|
||||||
s->len = 15;
|
|
||||||
tm = gmtime (&t);
|
|
||||||
sprintf (s->data, "%04d%02d%02d%02d%02d%02dZ", tm->tm_year + 1900,
|
|
||||||
tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min,
|
|
||||||
tm->tm_sec);
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
encode_generalized_time (unsigned char *p, int len, void *data)
|
|
||||||
{
|
|
||||||
time_t *t = (time_t *)data;
|
|
||||||
krb5_data k;
|
|
||||||
int l;
|
|
||||||
int ret = 0;
|
|
||||||
|
|
||||||
time2generalizedtime (*t, &k);
|
|
||||||
l = der_put_octet_string (p, len, &k);
|
|
||||||
free (k.data);
|
|
||||||
if (l < 0)
|
|
||||||
return l;
|
|
||||||
p -= l;
|
|
||||||
len -= l;
|
|
||||||
ret += l;
|
|
||||||
l = der_put_tag (p, len, UNIV, PRIM, UT_GeneralizedTime);
|
|
||||||
if (l < 0)
|
|
||||||
return l;
|
|
||||||
p -= l;
|
|
||||||
len -= l;
|
|
||||||
ret += l;
|
|
||||||
return ret;
|
|
||||||
}
|
|
573
asn1/gen.c
573
asn1/gen.c
@@ -1,573 +0,0 @@
|
|||||||
/* $Id$ */
|
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include "der.h"
|
|
||||||
#include "gen.h"
|
|
||||||
|
|
||||||
FILE *headerfile, *codefile;
|
|
||||||
|
|
||||||
#define STEM "asn1"
|
|
||||||
|
|
||||||
void
|
|
||||||
init_generate (char *filename)
|
|
||||||
{
|
|
||||||
headerfile = fopen (STEM ".h", "w");
|
|
||||||
fprintf (headerfile,
|
|
||||||
"/* Genereated from %s */\n"
|
|
||||||
"/* Do not edit */\n\n",
|
|
||||||
filename);
|
|
||||||
codefile = fopen (STEM ".c", "w");
|
|
||||||
fprintf (codefile,
|
|
||||||
"/* Generated from %s */\n"
|
|
||||||
"/* Do not edit */\n\n"
|
|
||||||
"#include <stdio.h>\n"
|
|
||||||
"#include <stdlib.h>\n"
|
|
||||||
"#include <time.h>\n"
|
|
||||||
"#include <der.h>\n"
|
|
||||||
"#include <" STEM ".h>\n\n",
|
|
||||||
filename);
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
close_generate ()
|
|
||||||
{
|
|
||||||
fclose (headerfile);
|
|
||||||
fclose (codefile);
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
generate_constant (Symbol *s)
|
|
||||||
{
|
|
||||||
fprintf (headerfile, "static const int %s = %d;\n\n",
|
|
||||||
s->gen_name, s->constant);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
define_type (char *name, Type *t)
|
|
||||||
{
|
|
||||||
switch (t->type) {
|
|
||||||
case TType:
|
|
||||||
fprintf (headerfile, "%s %s;\n", t->symbol->gen_name, name);
|
|
||||||
break;
|
|
||||||
case TInteger:
|
|
||||||
fprintf (headerfile, "int %s;\n", name);
|
|
||||||
break;
|
|
||||||
case TOctetString:
|
|
||||||
fprintf (headerfile, "krb5_data %s;\n", name);
|
|
||||||
break;
|
|
||||||
case TBitString: {
|
|
||||||
Member *m;
|
|
||||||
Type i;
|
|
||||||
int tag = -1;
|
|
||||||
|
|
||||||
i.type = TInteger;
|
|
||||||
fprintf (headerfile, "struct {\n");
|
|
||||||
for (m = t->members; m && m->val != tag; m = m->next) {
|
|
||||||
char *n = malloc(strlen(m->gen_name) + 3);
|
|
||||||
strcpy (n, m->gen_name);
|
|
||||||
strcat (n, ":1");
|
|
||||||
define_type (n, &i);
|
|
||||||
free (n);
|
|
||||||
if (tag == -1)
|
|
||||||
tag = m->val;
|
|
||||||
}
|
|
||||||
fprintf (headerfile, "} %s;\n\n", name);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case TSequence: {
|
|
||||||
Member *m;
|
|
||||||
int tag = -1;
|
|
||||||
|
|
||||||
fprintf (headerfile, "struct {\n");
|
|
||||||
for (m = t->members; m && m->val != tag; m = m->next) {
|
|
||||||
if (m->optional) {
|
|
||||||
char *n = malloc(strlen(m->gen_name) + 2);
|
|
||||||
|
|
||||||
*n = '*';
|
|
||||||
strcpy (n+1, m->gen_name);
|
|
||||||
define_type (n, m->type);
|
|
||||||
free (n);
|
|
||||||
} else
|
|
||||||
define_type (m->gen_name, m->type);
|
|
||||||
if (tag == -1)
|
|
||||||
tag = m->val;
|
|
||||||
}
|
|
||||||
fprintf (headerfile, "} %s;\n\n", name);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case TSequenceOf: {
|
|
||||||
Type i;
|
|
||||||
|
|
||||||
i.type = TInteger;
|
|
||||||
i.application = 0;
|
|
||||||
|
|
||||||
fprintf (headerfile, "struct {\n");
|
|
||||||
define_type ("len", &i);
|
|
||||||
define_type ("*val", t->subtype);
|
|
||||||
fprintf (headerfile, "} %s;\n\n", name);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case TGeneralizedTime:
|
|
||||||
fprintf (headerfile, "time_t %s;\n", name);
|
|
||||||
break;
|
|
||||||
case TGeneralString:
|
|
||||||
fprintf (headerfile, "char *%s;\n", name);
|
|
||||||
break;
|
|
||||||
case TApplication:
|
|
||||||
define_type (name, t->subtype);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
abort ();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
generate_type_header (Symbol *s)
|
|
||||||
{
|
|
||||||
fprintf (headerfile, "typedef ");
|
|
||||||
define_type (s->gen_name, s->type);
|
|
||||||
fprintf (headerfile, "\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
encode_primitive (char *typename, char *name)
|
|
||||||
{
|
|
||||||
fprintf (codefile,
|
|
||||||
"l = encode_%s(p, len, %s);\n"
|
|
||||||
"if(l < 0)\n"
|
|
||||||
"return l;\n"
|
|
||||||
"p -= l;\n"
|
|
||||||
"len -= l;\n"
|
|
||||||
"ret += l;\n\n",
|
|
||||||
typename,
|
|
||||||
name);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
encode_type (char *name, Type *t)
|
|
||||||
{
|
|
||||||
switch (t->type) {
|
|
||||||
case TType:
|
|
||||||
#if 0
|
|
||||||
encode_type (name, t->symbol->type);
|
|
||||||
#endif
|
|
||||||
fprintf (codefile,
|
|
||||||
"l = encode_%s(p, len, %s);\n"
|
|
||||||
"if(l < 0)\n"
|
|
||||||
"return l;\n"
|
|
||||||
"p -= l;\n"
|
|
||||||
"len -= l;\n"
|
|
||||||
"ret += l;\n\n",
|
|
||||||
t->symbol->gen_name, name);
|
|
||||||
break;
|
|
||||||
case TInteger:
|
|
||||||
encode_primitive ("integer", name);
|
|
||||||
break;
|
|
||||||
case TOctetString:
|
|
||||||
encode_primitive ("octet_string", name);
|
|
||||||
break;
|
|
||||||
case TBitString: {
|
|
||||||
Member *m;
|
|
||||||
int pos;
|
|
||||||
int rest;
|
|
||||||
int tag = -1;
|
|
||||||
|
|
||||||
if (t->members == NULL)
|
|
||||||
break;
|
|
||||||
|
|
||||||
fprintf (codefile, "{\n"
|
|
||||||
"unsigned char c = 0;\n");
|
|
||||||
pos = t->members->prev->val;
|
|
||||||
rest = 7 - (t->members->prev->val % 8);
|
|
||||||
|
|
||||||
for (m = t->members->prev; m && tag != m->val; m = m->prev) {
|
|
||||||
while (m->val / 8 < pos / 8) {
|
|
||||||
fprintf (codefile,
|
|
||||||
"*p-- = c; len--; ret++;\n"
|
|
||||||
"c = 0;\n");
|
|
||||||
pos -= 8;
|
|
||||||
}
|
|
||||||
fprintf (codefile,
|
|
||||||
"if(%s->%s) c |= 1<<%d;\n", name, m->gen_name,
|
|
||||||
m->val % 8);
|
|
||||||
|
|
||||||
if (tag == -1)
|
|
||||||
tag = m->val;
|
|
||||||
}
|
|
||||||
|
|
||||||
fprintf (codefile,
|
|
||||||
"*p-- = c;\n"
|
|
||||||
"*p-- = %d;\n"
|
|
||||||
"len -= 2;\n"
|
|
||||||
"ret += 2;\n"
|
|
||||||
"}\n\n"
|
|
||||||
"l = der_put_length (p, len, ret);\n"
|
|
||||||
"if(l < 0)\n"
|
|
||||||
" return l;\n"
|
|
||||||
"p -= l;\n"
|
|
||||||
"len -= l;\n"
|
|
||||||
"ret += l;\n\n"
|
|
||||||
"l = der_put_tag (p, len, UNIV, PRIM, UT_BitString);\n"
|
|
||||||
"if(l < 0)\n"
|
|
||||||
"return l;\n"
|
|
||||||
"p -= l;\n"
|
|
||||||
"len -= l;\n"
|
|
||||||
"ret += l;\n\n",
|
|
||||||
rest);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case TSequence: {
|
|
||||||
Member *m;
|
|
||||||
int tag = -1;
|
|
||||||
|
|
||||||
if (t->members == NULL)
|
|
||||||
break;
|
|
||||||
|
|
||||||
for (m = t->members->prev; m && tag != m->val; m = m->prev) {
|
|
||||||
char *s = malloc(2 + strlen(name) + 1 + strlen(m->gen_name) + 3);
|
|
||||||
|
|
||||||
sprintf (s, "%s(%s)->%s", m->optional ? "" : "&", name, m->gen_name);
|
|
||||||
if (m->optional)
|
|
||||||
fprintf (codefile,
|
|
||||||
"if(%s)\n",
|
|
||||||
s);
|
|
||||||
fprintf (codefile, "{\n"
|
|
||||||
"int oldret = ret;\n"
|
|
||||||
"ret = 0;\n");
|
|
||||||
encode_type (s, m->type);
|
|
||||||
fprintf (codefile,
|
|
||||||
"l = der_put_length (p, len, ret);\n"
|
|
||||||
"if (l < 0)\n"
|
|
||||||
"return l;\n"
|
|
||||||
"p -= l;\n"
|
|
||||||
"len -= l;\n"
|
|
||||||
"ret += l;\n\n"
|
|
||||||
"l = der_put_tag (p, len, CONTEXT, CONS, %d);\n"
|
|
||||||
"if(l < 0)\n"
|
|
||||||
"return l;\n"
|
|
||||||
"p -= l;\n"
|
|
||||||
"len -= l;\n"
|
|
||||||
"ret += l;\n\n",
|
|
||||||
m->val);
|
|
||||||
fprintf (codefile,
|
|
||||||
"ret += oldret;\n"
|
|
||||||
"}\n");
|
|
||||||
if (tag == -1)
|
|
||||||
tag = m->val;
|
|
||||||
free (s);
|
|
||||||
}
|
|
||||||
fprintf (codefile,
|
|
||||||
"l = der_put_length (p, len, ret);\n"
|
|
||||||
"if(l < 0)\n"
|
|
||||||
" return l;\n"
|
|
||||||
"p -= l;\n"
|
|
||||||
"len -= l;\n"
|
|
||||||
"ret += l;\n\n"
|
|
||||||
"l = der_put_tag (p, len, UNIV, CONS, UT_Sequence);\n"
|
|
||||||
"if(l < 0)\n"
|
|
||||||
"return l;\n"
|
|
||||||
"p -= l;\n"
|
|
||||||
"len -= l;\n"
|
|
||||||
"ret += l;\n\n");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case TSequenceOf: {
|
|
||||||
char *n = malloc(strlen(name) + 12);
|
|
||||||
|
|
||||||
fprintf (codefile,
|
|
||||||
"for(i = (%s)->len - 1; i >= 0; --i) {\n"
|
|
||||||
"int oldret = ret;\n"
|
|
||||||
"ret = 0;\n",
|
|
||||||
name);
|
|
||||||
sprintf (n, "&(%s)->val[i]", name);
|
|
||||||
encode_type (n, t->subtype);
|
|
||||||
fprintf (codefile,
|
|
||||||
"ret += oldret;\n"
|
|
||||||
"}\n"
|
|
||||||
"l = der_put_length (p, len, ret);\n"
|
|
||||||
"if(l < 0)\n"
|
|
||||||
" return l;\n"
|
|
||||||
"p -= l;\n"
|
|
||||||
"len -= l;\n"
|
|
||||||
"ret += l;\n\n"
|
|
||||||
"l = der_put_tag (p, len, UNIV, CONS, UT_Sequence);\n"
|
|
||||||
"if(l < 0)\n"
|
|
||||||
"return l;\n"
|
|
||||||
"p -= l;\n"
|
|
||||||
"len -= l;\n"
|
|
||||||
"ret += l;\n\n");
|
|
||||||
free (n);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case TGeneralizedTime:
|
|
||||||
encode_primitive ("generalized_time", name);
|
|
||||||
break;
|
|
||||||
case TGeneralString:
|
|
||||||
encode_primitive ("general_string", name);
|
|
||||||
break;
|
|
||||||
case TApplication:
|
|
||||||
encode_type (name, t->subtype);
|
|
||||||
fprintf (codefile,
|
|
||||||
"l = der_put_length (p, len, ret);\n"
|
|
||||||
"if(l < 0)\n"
|
|
||||||
" return l;\n"
|
|
||||||
"p -= l;\n"
|
|
||||||
"len -= l;\n"
|
|
||||||
"ret += l;\n\n"
|
|
||||||
"l = der_put_tag (p, len, APPL, CONS, %d);\n"
|
|
||||||
"if(l < 0)\n"
|
|
||||||
"return l;\n"
|
|
||||||
"p -= l;\n"
|
|
||||||
"len -= l;\n"
|
|
||||||
"ret += l;\n\n",
|
|
||||||
t->application);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
abort ();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
generate_type_encode (Symbol *s)
|
|
||||||
{
|
|
||||||
fprintf (headerfile,
|
|
||||||
"int encode_%s(unsigned char *, int, void *);\n",
|
|
||||||
s->gen_name);
|
|
||||||
|
|
||||||
fprintf (codefile, "int\n"
|
|
||||||
"encode_%s(unsigned char *p, int len, void *d)\n"
|
|
||||||
"{\n"
|
|
||||||
"%s *data = (%s *)d;\n"
|
|
||||||
"int ret = 0;\n"
|
|
||||||
"int l, i;\n\n",
|
|
||||||
s->gen_name, s->gen_name, s->gen_name);
|
|
||||||
|
|
||||||
encode_type ("data", s->type);
|
|
||||||
fprintf (codefile, "return ret;\n"
|
|
||||||
"}\n\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
decode_primitive (char *typename, char *name)
|
|
||||||
{
|
|
||||||
fprintf (codefile,
|
|
||||||
"l = decode_%s(p, len, %s);\n"
|
|
||||||
"if(l < 0)\n"
|
|
||||||
"return l;\n"
|
|
||||||
"p += l;\n"
|
|
||||||
"len -= l;\n"
|
|
||||||
"ret += l;\n\n",
|
|
||||||
typename,
|
|
||||||
name);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
decode_type (char *name, Type *t)
|
|
||||||
{
|
|
||||||
switch (t->type) {
|
|
||||||
case TType:
|
|
||||||
#if 0
|
|
||||||
decode_type (name, t->symbol->type);
|
|
||||||
#endif
|
|
||||||
fprintf (codefile,
|
|
||||||
"l = decode_%s(p, len, %s);\n"
|
|
||||||
"if(l < 0)\n"
|
|
||||||
"return l;\n"
|
|
||||||
"p += l;\n"
|
|
||||||
"len -= l;\n"
|
|
||||||
"ret += l;\n\n",
|
|
||||||
t->symbol->gen_name, name);
|
|
||||||
break;
|
|
||||||
case TInteger:
|
|
||||||
decode_primitive ("integer", name);
|
|
||||||
break;
|
|
||||||
case TOctetString:
|
|
||||||
decode_primitive ("octet_string", name);
|
|
||||||
break;
|
|
||||||
case TBitString:
|
|
||||||
/* XXX */
|
|
||||||
fprintf (codefile,
|
|
||||||
"l = der_match_tag (p, len, UNIV, PRIM, UT_BitString);\n"
|
|
||||||
"if(l < 0)\n"
|
|
||||||
"return l;\n"
|
|
||||||
"p += l;\n"
|
|
||||||
"len -= l;\n"
|
|
||||||
"ret += l;\n"
|
|
||||||
"l = der_get_length (p, len, &reallen);\n"
|
|
||||||
"if(l < 0)\n"
|
|
||||||
"return l;\n"
|
|
||||||
"p += l;\n"
|
|
||||||
"len -= l;\n"
|
|
||||||
"ret += l;\n"
|
|
||||||
"if(len < reallen)\n"
|
|
||||||
"return -1;\n"
|
|
||||||
"p += reallen;\n"
|
|
||||||
"len -= reallen;\n"
|
|
||||||
"ret += reallen;\n");
|
|
||||||
break;
|
|
||||||
case TSequence: {
|
|
||||||
Member *m;
|
|
||||||
int tag = -1;
|
|
||||||
|
|
||||||
if (t->members == NULL)
|
|
||||||
break;
|
|
||||||
|
|
||||||
fprintf (codefile,
|
|
||||||
"l = der_match_tag (p, len, UNIV, CONS, UT_Sequence);\n"
|
|
||||||
"if(l < 0)\n"
|
|
||||||
"return l;\n"
|
|
||||||
"p += l;\n"
|
|
||||||
"len -= l;\n"
|
|
||||||
"ret += l;\n"
|
|
||||||
"l = der_get_length (p, len, &reallen);\n"
|
|
||||||
"if(l < 0)\n"
|
|
||||||
"return l;\n"
|
|
||||||
"p += l;\n"
|
|
||||||
"len -= l;\n"
|
|
||||||
"ret += l;\n"
|
|
||||||
"if(len < reallen)\n"
|
|
||||||
"return -1;\n"
|
|
||||||
"len = reallen;\n");
|
|
||||||
|
|
||||||
for (m = t->members; m && tag != m->val; m = m->next) {
|
|
||||||
char *s = malloc(2 + strlen(name) + 1 + strlen(m->gen_name) + 3);
|
|
||||||
|
|
||||||
sprintf (s, "%s(%s)->%s", m->optional ? "" : "&", name, m->gen_name);
|
|
||||||
fprintf (codefile, "{\n"
|
|
||||||
"int newlen, oldlen;\n\n"
|
|
||||||
"l = der_match_tag (p, len, CONTEXT, CONS, %d);\n",
|
|
||||||
m->val);
|
|
||||||
fprintf (codefile,
|
|
||||||
"if(l >= 0) {\n"
|
|
||||||
"p += l;\n"
|
|
||||||
"len -= l;\n"
|
|
||||||
"ret += l;\n"
|
|
||||||
"l = der_get_length (p, len, &newlen);\n"
|
|
||||||
"if(l < 0)\n"
|
|
||||||
"return l;\n"
|
|
||||||
"p += l;\n"
|
|
||||||
"len -= l;\n"
|
|
||||||
"ret += l;\n"
|
|
||||||
"if(len < newlen)\n"
|
|
||||||
"return -1;\n"
|
|
||||||
"oldlen = len;\n"
|
|
||||||
"len = newlen;\n");
|
|
||||||
if (m->optional)
|
|
||||||
fprintf (codefile,
|
|
||||||
"%s = malloc(sizeof(*%s));\n",
|
|
||||||
s, s);
|
|
||||||
decode_type (s, m->type);
|
|
||||||
fprintf (codefile,
|
|
||||||
"len = oldlen - newlen;\n"
|
|
||||||
"}\n"
|
|
||||||
"else {\n");
|
|
||||||
if(m->optional)
|
|
||||||
fprintf (codefile,
|
|
||||||
"%s = NULL;\n"
|
|
||||||
"}\n", s);
|
|
||||||
else
|
|
||||||
fprintf (codefile,
|
|
||||||
"return l;\n"
|
|
||||||
"}\n");
|
|
||||||
fprintf (codefile,
|
|
||||||
"}\n");
|
|
||||||
if (tag == -1)
|
|
||||||
tag = m->val;
|
|
||||||
free (s);
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case TSequenceOf: {
|
|
||||||
char *n = malloc(2*strlen(name) + 20);
|
|
||||||
|
|
||||||
fprintf (codefile,
|
|
||||||
"l = der_match_tag (p, len, UNIV, CONS, UT_Sequence);\n"
|
|
||||||
"if(l < 0)\n"
|
|
||||||
"return l;\n"
|
|
||||||
"p += l;\n"
|
|
||||||
"len -= l;\n"
|
|
||||||
"ret += l;\n"
|
|
||||||
"l = der_get_length (p, len, &reallen);\n"
|
|
||||||
"if(l < 0)\n"
|
|
||||||
"return l;\n"
|
|
||||||
"p += l;\n"
|
|
||||||
"len -= l;\n"
|
|
||||||
"ret += l;\n"
|
|
||||||
"if(len < reallen)\n"
|
|
||||||
"return -1;\n"
|
|
||||||
"len = reallen;\n");
|
|
||||||
|
|
||||||
fprintf (codefile,
|
|
||||||
"(%s)->len = 0;\n"
|
|
||||||
"(%s)->val = NULL;\n"
|
|
||||||
"while(len > 0) {\n"
|
|
||||||
"(%s)->len++;\n"
|
|
||||||
"(%s)->val = realloc((%s)->val, sizeof(*((%s)->val)) * (%s)->len);\n",
|
|
||||||
name, name, name, name, name, name, name);
|
|
||||||
sprintf (n, "&(%s)->val[(%s)->len-1]", name, name);
|
|
||||||
decode_type (n, t->subtype);
|
|
||||||
fprintf (codefile,
|
|
||||||
"}\n");
|
|
||||||
free (n);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case TGeneralizedTime:
|
|
||||||
decode_primitive ("generalized_time", name);
|
|
||||||
break;
|
|
||||||
case TGeneralString:
|
|
||||||
decode_primitive ("general_string", name);
|
|
||||||
break;
|
|
||||||
case TApplication:
|
|
||||||
fprintf (codefile,
|
|
||||||
"l = der_match_tag (p, len, APPL, CONS, %d);\n"
|
|
||||||
"if(l < 0)\n"
|
|
||||||
"return l;\n"
|
|
||||||
"p += l;\n"
|
|
||||||
"len -= l;\n"
|
|
||||||
"ret += l;\n"
|
|
||||||
"l = der_get_length(p, len, &reallen);\n"
|
|
||||||
"if(l < 0)\n"
|
|
||||||
"return l;\n"
|
|
||||||
"p += l;\n"
|
|
||||||
"len -= l;\n"
|
|
||||||
"ret += l;\n"
|
|
||||||
"if(len < reallen)\n"
|
|
||||||
"return -1;\n"
|
|
||||||
"len = reallen;\n",
|
|
||||||
t->application);
|
|
||||||
decode_type (name, t->subtype);
|
|
||||||
break;
|
|
||||||
default :
|
|
||||||
abort ();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
generate_type_decode (Symbol *s)
|
|
||||||
{
|
|
||||||
fprintf (headerfile,
|
|
||||||
"int decode_%s(unsigned char *, int, void *);\n",
|
|
||||||
s->gen_name);
|
|
||||||
|
|
||||||
fprintf (codefile, "int\n"
|
|
||||||
"decode_%s(unsigned char *p, int len, void *d)\n"
|
|
||||||
"{\n"
|
|
||||||
"%s *data = (%s *)d;\n"
|
|
||||||
"int ret = 0, reallen;\n"
|
|
||||||
"int l, i;\n\n",
|
|
||||||
s->gen_name, s->gen_name, s->gen_name);
|
|
||||||
|
|
||||||
decode_type ("data", s->type);
|
|
||||||
fprintf (codefile, "return ret;\n"
|
|
||||||
"}\n\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
generate_type (Symbol *s)
|
|
||||||
{
|
|
||||||
generate_type_header (s);
|
|
||||||
generate_type_encode (s);
|
|
||||||
generate_type_decode (s);
|
|
||||||
}
|
|
11
asn1/gen.h
11
asn1/gen.h
@@ -1,11 +0,0 @@
|
|||||||
/* $Id$ */
|
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
#include "symbol.h"
|
|
||||||
|
|
||||||
void init_generate (char *);
|
|
||||||
void close_generate ();
|
|
||||||
void generate_constant (Symbol *);
|
|
||||||
void generate_type (Symbol *);
|
|
||||||
|
|
||||||
extern FILE *headerfile, *codefile;
|
|
181
asn1/hash.c
181
asn1/hash.c
@@ -1,181 +0,0 @@
|
|||||||
/*
|
|
||||||
* Hash table functions
|
|
||||||
*/
|
|
||||||
|
|
||||||
/* $Id$ */
|
|
||||||
|
|
||||||
static char rcsid[] = "$Id$";
|
|
||||||
|
|
||||||
#include <assert.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <limits.h>
|
|
||||||
#include <ctype.h>
|
|
||||||
#include "hash.h"
|
|
||||||
|
|
||||||
static Hashentry *_search(Hashtab * htab, /* The hash table */
|
|
||||||
void *ptr); /* And key */
|
|
||||||
|
|
||||||
Hashtab *
|
|
||||||
hashtabnew(int sz,
|
|
||||||
int (*cmp) (void *, void *),
|
|
||||||
unsigned (*hash) (void *))
|
|
||||||
{
|
|
||||||
Hashtab *htab;
|
|
||||||
int i;
|
|
||||||
|
|
||||||
assert(sz > 0);
|
|
||||||
|
|
||||||
htab = (Hashtab *) malloc(sizeof(Hashtab) + (sz - 1) * sizeof(Hashentry *));
|
|
||||||
for (i = 0; i < sz; ++i)
|
|
||||||
htab->tab[i] = NULL;
|
|
||||||
|
|
||||||
if (htab == NULL) {
|
|
||||||
return NULL;
|
|
||||||
} else {
|
|
||||||
htab->cmp = cmp;
|
|
||||||
htab->hash = hash;
|
|
||||||
htab->sz = sz;
|
|
||||||
return htab;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Intern search function */
|
|
||||||
|
|
||||||
static Hashentry *
|
|
||||||
_search(Hashtab * htab, void *ptr)
|
|
||||||
{
|
|
||||||
Hashentry *hptr;
|
|
||||||
|
|
||||||
assert(htab && ptr);
|
|
||||||
|
|
||||||
for (hptr = htab->tab[(*htab->hash) (ptr) % htab->sz];
|
|
||||||
hptr;
|
|
||||||
hptr = hptr->next)
|
|
||||||
if ((*htab->cmp) (ptr, hptr->ptr) == 0)
|
|
||||||
break;
|
|
||||||
return hptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Search for element in hash table */
|
|
||||||
|
|
||||||
void *
|
|
||||||
hashtabsearch(Hashtab * htab, void *ptr)
|
|
||||||
{
|
|
||||||
Hashentry *tmp;
|
|
||||||
|
|
||||||
tmp = _search(htab, ptr);
|
|
||||||
return tmp ? tmp->ptr : tmp;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* add element to hash table */
|
|
||||||
/* if already there, set new value */
|
|
||||||
/* !NULL if succesful */
|
|
||||||
|
|
||||||
void *
|
|
||||||
hashtabadd(Hashtab * htab, void *ptr)
|
|
||||||
{
|
|
||||||
Hashentry *h = _search(htab, ptr);
|
|
||||||
Hashentry **tabptr;
|
|
||||||
|
|
||||||
assert(htab && ptr);
|
|
||||||
|
|
||||||
if (h)
|
|
||||||
free((void *) h->ptr);
|
|
||||||
else {
|
|
||||||
h = (Hashentry *) malloc(sizeof(Hashentry));
|
|
||||||
if (h == NULL) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
tabptr = &htab->tab[(*htab->hash) (ptr) % htab->sz];
|
|
||||||
h->next = *tabptr;
|
|
||||||
*tabptr = h;
|
|
||||||
h->prev = tabptr;
|
|
||||||
if (h->next)
|
|
||||||
h->next->prev = &h->next;
|
|
||||||
}
|
|
||||||
h->ptr = ptr;
|
|
||||||
return h;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* delete element with key key. Iff freep, free Hashentry->ptr */
|
|
||||||
|
|
||||||
int
|
|
||||||
_hashtabdel(Hashtab * htab, void *ptr, int freep)
|
|
||||||
{
|
|
||||||
Hashentry *h;
|
|
||||||
|
|
||||||
assert(htab && ptr);
|
|
||||||
|
|
||||||
h = _search(htab, ptr);
|
|
||||||
if (h) {
|
|
||||||
if (freep)
|
|
||||||
free(h->ptr);
|
|
||||||
if (*(h->prev) = h->next)
|
|
||||||
h->next->prev = h->prev;
|
|
||||||
free(h);
|
|
||||||
return 0;
|
|
||||||
} else
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Do something for each element */
|
|
||||||
|
|
||||||
void
|
|
||||||
hashtabforeach(Hashtab * htab, int (*func) (void *ptr, void *arg),
|
|
||||||
void *arg)
|
|
||||||
{
|
|
||||||
Hashentry **h, *g;
|
|
||||||
|
|
||||||
assert(htab);
|
|
||||||
|
|
||||||
for (h = htab->tab; h < &htab->tab[htab->sz]; ++h)
|
|
||||||
for (g = *h; g; g = g->next)
|
|
||||||
if ((*func) (g->ptr, arg))
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* standard hash-functions for strings */
|
|
||||||
|
|
||||||
unsigned
|
|
||||||
hashadd(const char *s)
|
|
||||||
{ /* Standard hash function */
|
|
||||||
unsigned i;
|
|
||||||
|
|
||||||
assert(s);
|
|
||||||
|
|
||||||
for (i = 0; *s; ++s)
|
|
||||||
i += *s;
|
|
||||||
return i;
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned
|
|
||||||
hashcaseadd(const char *s)
|
|
||||||
{ /* Standard hash function */
|
|
||||||
unsigned i;
|
|
||||||
|
|
||||||
assert(s);
|
|
||||||
|
|
||||||
for (i = 0; *s; ++s)
|
|
||||||
i += toupper(*s);
|
|
||||||
return i;
|
|
||||||
}
|
|
||||||
|
|
||||||
#define TWELVE (sizeof(unsigned))
|
|
||||||
#define SEVENTYFIVE (6*sizeof(unsigned))
|
|
||||||
#define HIGH_BITS (~((unsigned)(~0) >> TWELVE))
|
|
||||||
|
|
||||||
unsigned
|
|
||||||
hashjpw(const char *ss)
|
|
||||||
{ /* another hash function */
|
|
||||||
unsigned h = 0;
|
|
||||||
unsigned g;
|
|
||||||
unsigned const char *s = ss;
|
|
||||||
|
|
||||||
for (; *s; ++s) {
|
|
||||||
h = (h << TWELVE) + *s;
|
|
||||||
if (g = h & HIGH_BITS)
|
|
||||||
h = (h ^ (g >> SEVENTYFIVE)) & ~HIGH_BITS;
|
|
||||||
}
|
|
||||||
return h;
|
|
||||||
}
|
|
54
asn1/hash.h
54
asn1/hash.h
@@ -1,54 +0,0 @@
|
|||||||
/*
|
|
||||||
* hash.h. Header file for hash table functions
|
|
||||||
*/
|
|
||||||
|
|
||||||
/* $Id$ */
|
|
||||||
|
|
||||||
struct hashentry { /* Entry in bucket */
|
|
||||||
struct hashentry **prev;
|
|
||||||
struct hashentry *next;
|
|
||||||
void *ptr;
|
|
||||||
};
|
|
||||||
|
|
||||||
typedef struct hashentry Hashentry;
|
|
||||||
|
|
||||||
struct hashtab { /* Hash table */
|
|
||||||
int (*cmp)(void *, void *); /* Compare function */
|
|
||||||
unsigned (*hash)(void *); /* hash function */
|
|
||||||
int sz; /* Size */
|
|
||||||
Hashentry *tab[1]; /* The table */
|
|
||||||
};
|
|
||||||
|
|
||||||
typedef struct hashtab Hashtab;
|
|
||||||
|
|
||||||
/* prototypes */
|
|
||||||
|
|
||||||
Hashtab *hashtabnew(int sz,
|
|
||||||
int (*cmp)(void *, void *),
|
|
||||||
unsigned (*hash)(void *)); /* Make new hash table */
|
|
||||||
|
|
||||||
void *hashtabsearch(Hashtab *htab, /* The hash table */
|
|
||||||
void *ptr); /* The key */
|
|
||||||
|
|
||||||
|
|
||||||
void *hashtabadd(Hashtab *htab, /* The hash table */
|
|
||||||
void *ptr); /* The element */
|
|
||||||
|
|
||||||
int _hashtabdel(Hashtab *htab, /* The table */
|
|
||||||
void *ptr, /* Key */
|
|
||||||
int freep); /* Free data part? */
|
|
||||||
|
|
||||||
void hashtabforeach(Hashtab *htab,
|
|
||||||
int (*func)(void *ptr, void *arg),
|
|
||||||
void *arg);
|
|
||||||
|
|
||||||
unsigned hashadd(const char *s); /* Standard hash function */
|
|
||||||
unsigned hashcaseadd(const char *s); /* Standard hash function */
|
|
||||||
unsigned hashjpw(const char *s); /* another hash function */
|
|
||||||
|
|
||||||
/* macros */
|
|
||||||
|
|
||||||
/* Don't free space */
|
|
||||||
#define hashtabdel(htab,key) _hashtabdel(htab,key,FALSE)
|
|
||||||
|
|
||||||
#define hashtabfree(htab,key) _hashtabdel(htab,key,TRUE) /* Do! */
|
|
328
asn1/k5.asn1
328
asn1/k5.asn1
@@ -1,328 +0,0 @@
|
|||||||
KERBEROS5 DEFINITIONS ::=
|
|
||||||
BEGIN
|
|
||||||
|
|
||||||
nt-unknown INTEGER ::= 0 -- Name type not known
|
|
||||||
nt-principal INTEGER ::= 1 -- Just the name of the principal as in
|
|
||||||
nt-srv-inst INTEGER ::= 2 -- Service and other unique instance (krbtgt)
|
|
||||||
nt-srv-hst INTEGER ::= 3 -- Service with host name as instance
|
|
||||||
nt-srv-xhst INTEGER ::= 4 -- Service with host as remaining components
|
|
||||||
nt-uid INTEGER ::= 5 -- Unique ID
|
|
||||||
|
|
||||||
|
|
||||||
Realm ::= GeneralString
|
|
||||||
PrincipalName ::= SEQUENCE {
|
|
||||||
name-type[0] INTEGER,
|
|
||||||
name-string[1] SEQUENCE OF GeneralString
|
|
||||||
}
|
|
||||||
|
|
||||||
HostAddress ::= SEQUENCE {
|
|
||||||
addr-type[0] INTEGER,
|
|
||||||
address[1] OCTET STRING
|
|
||||||
}
|
|
||||||
|
|
||||||
HostAddresses ::= SEQUENCE OF SEQUENCE {
|
|
||||||
addr-type[0] INTEGER,
|
|
||||||
address[1] OCTET STRING
|
|
||||||
}
|
|
||||||
|
|
||||||
KerberosTime ::= GeneralizedTime -- Specifying UTC time zone (Z)
|
|
||||||
|
|
||||||
AuthorizationData ::= SEQUENCE OF SEQUENCE {
|
|
||||||
ad-type[0] INTEGER,
|
|
||||||
ad-data[1] OCTET STRING
|
|
||||||
}
|
|
||||||
|
|
||||||
APOptions ::= BIT STRING {
|
|
||||||
reserved(0),
|
|
||||||
use-session-key(1),
|
|
||||||
mutual-required(2)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
TicketFlags ::= BIT STRING {
|
|
||||||
reserved(0),
|
|
||||||
forwardable(1),
|
|
||||||
forwarded(2),
|
|
||||||
proxiable(3),
|
|
||||||
proxy(4),
|
|
||||||
may-postdate(5),
|
|
||||||
postdated(6),
|
|
||||||
invalid(7),
|
|
||||||
renewable(8),
|
|
||||||
initial(9),
|
|
||||||
pre-authent(10),
|
|
||||||
hw-authent(11)
|
|
||||||
}
|
|
||||||
|
|
||||||
KDCOptions ::= BIT STRING {
|
|
||||||
reserved(0),
|
|
||||||
forwardable(1),
|
|
||||||
forwarded(2),
|
|
||||||
proxiable(3),
|
|
||||||
proxy(4),
|
|
||||||
allow-postdate(5),
|
|
||||||
postdated(6),
|
|
||||||
unused7(7),
|
|
||||||
renewable(8),
|
|
||||||
unused9(9),
|
|
||||||
unused10(10),
|
|
||||||
unused11(11),
|
|
||||||
renewable-ok(27),
|
|
||||||
enc-tkt-in-skey(28),
|
|
||||||
renew(30),
|
|
||||||
validate(31)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
LastReq ::= SEQUENCE OF SEQUENCE {
|
|
||||||
lr-type[0] INTEGER,
|
|
||||||
lr-value[1] KerberosTime
|
|
||||||
}
|
|
||||||
|
|
||||||
EncryptedData ::= SEQUENCE {
|
|
||||||
etype[0] INTEGER, -- EncryptionType
|
|
||||||
kvno[1] INTEGER OPTIONAL,
|
|
||||||
cipher[2] OCTET STRING -- ciphertext
|
|
||||||
}
|
|
||||||
|
|
||||||
EncryptionKey ::= SEQUENCE {
|
|
||||||
keytype[0] INTEGER,
|
|
||||||
keyvalue[1] OCTET STRING
|
|
||||||
}
|
|
||||||
|
|
||||||
-- encoded Transited field
|
|
||||||
TransitedEncoding ::= SEQUENCE {
|
|
||||||
tr-type[0] INTEGER, -- must be registered
|
|
||||||
contents[1] OCTET STRING
|
|
||||||
}
|
|
||||||
|
|
||||||
Ticket ::= [APPLICATION 1] SEQUENCE {
|
|
||||||
tkt-vno[0] INTEGER,
|
|
||||||
realm[1] Realm,
|
|
||||||
sname[2] PrincipalName,
|
|
||||||
enc-part[3] EncryptedData
|
|
||||||
}
|
|
||||||
-- Encrypted part of ticket
|
|
||||||
EncTicketPart ::= [APPLICATION 3] SEQUENCE {
|
|
||||||
flags[0] TicketFlags,
|
|
||||||
key[1] EncryptionKey,
|
|
||||||
crealm[2] Realm,
|
|
||||||
cname[3] PrincipalName,
|
|
||||||
transited[4] TransitedEncoding,
|
|
||||||
authtime[5] KerberosTime,
|
|
||||||
starttime[6] KerberosTime OPTIONAL,
|
|
||||||
endtime[7] KerberosTime,
|
|
||||||
renew-till[8] KerberosTime OPTIONAL,
|
|
||||||
caddr[9] HostAddresses OPTIONAL,
|
|
||||||
authorization-data[10] AuthorizationData OPTIONAL
|
|
||||||
}
|
|
||||||
|
|
||||||
Checksum ::= SEQUENCE {
|
|
||||||
cksumtype[0] INTEGER,
|
|
||||||
checksum[1] OCTET STRING
|
|
||||||
}
|
|
||||||
|
|
||||||
Authenticator ::= [APPLICATION 2] SEQUENCE {
|
|
||||||
authenticator-vno[0] INTEGER,
|
|
||||||
crealm[1] Realm,
|
|
||||||
cname[2] PrincipalName,
|
|
||||||
cksum[3] Checksum OPTIONAL,
|
|
||||||
cusec[4] INTEGER,
|
|
||||||
ctime[5] KerberosTime,
|
|
||||||
subkey[6] EncryptionKey OPTIONAL,
|
|
||||||
seq-number[7] INTEGER OPTIONAL,
|
|
||||||
authorization-data[8] AuthorizationData OPTIONAL
|
|
||||||
}
|
|
||||||
|
|
||||||
PA-DATA ::= SEQUENCE {
|
|
||||||
padata-type[1] INTEGER,
|
|
||||||
padata-value[2] OCTET STRING
|
|
||||||
-- ,
|
|
||||||
-- might be encoded AP-REQ
|
|
||||||
}
|
|
||||||
|
|
||||||
KDC-REQ-BODY ::= SEQUENCE {
|
|
||||||
kdc-options[0] KDCOptions,
|
|
||||||
cname[1] PrincipalName OPTIONAL,
|
|
||||||
-- Used only in AS-REQ
|
|
||||||
realm[2] Realm, -- Server's realm
|
|
||||||
-- Also client's in AS-REQ
|
|
||||||
sname[3] PrincipalName OPTIONAL,
|
|
||||||
from[4] KerberosTime OPTIONAL,
|
|
||||||
till[5] KerberosTime,
|
|
||||||
rtime[6] KerberosTime OPTIONAL,
|
|
||||||
nonce[7] INTEGER,
|
|
||||||
etype[8] SEQUENCE OF INTEGER, -- EncryptionType,
|
|
||||||
-- in preference order
|
|
||||||
addresses[9] HostAddresses OPTIONAL,
|
|
||||||
enc-authorization-data[10] EncryptedData OPTIONAL,
|
|
||||||
-- Encrypted AuthorizationData encoding
|
|
||||||
additional-tickets[11] SEQUENCE OF Ticket OPTIONAL
|
|
||||||
}
|
|
||||||
|
|
||||||
KDC-REQ ::= SEQUENCE {
|
|
||||||
pvno[1] INTEGER,
|
|
||||||
msg-type[2] INTEGER,
|
|
||||||
padata[3] SEQUENCE OF PA-DATA OPTIONAL,
|
|
||||||
req-body[4] KDC-REQ-BODY
|
|
||||||
}
|
|
||||||
|
|
||||||
AS-REQ ::= [APPLICATION 10] KDC-REQ
|
|
||||||
TGS-REQ ::= [APPLICATION 12] KDC-REQ
|
|
||||||
|
|
||||||
-- padata-type ::= PA-ENC-TIMESTAMP
|
|
||||||
-- padata-value ::= EncryptedData - PA-ENC-TS-ENC
|
|
||||||
|
|
||||||
PA-ENC-TS-ENC ::= SEQUENCE {
|
|
||||||
patimestamp[0] KerberosTime, -- client's time
|
|
||||||
pausec[1] INTEGER OPTIONAL
|
|
||||||
}
|
|
||||||
|
|
||||||
KDC-REP ::= SEQUENCE {
|
|
||||||
pvno[0] INTEGER,
|
|
||||||
msg-type[1] INTEGER,
|
|
||||||
padata[2] SEQUENCE OF PA-DATA OPTIONAL,
|
|
||||||
crealm[3] Realm,
|
|
||||||
cname[4] PrincipalName,
|
|
||||||
ticket[5] Ticket,
|
|
||||||
enc-part[6] EncryptedData
|
|
||||||
}
|
|
||||||
|
|
||||||
AS-REP ::= [APPLICATION 11] KDC-REP
|
|
||||||
TGS-REP ::= [APPLICATION 13] KDC-REP
|
|
||||||
|
|
||||||
EncKDCRepPart ::= SEQUENCE {
|
|
||||||
key[0] EncryptionKey,
|
|
||||||
last-req[1] LastReq,
|
|
||||||
nonce[2] INTEGER,
|
|
||||||
key-expiration[3] KerberosTime OPTIONAL,
|
|
||||||
flags[4] TicketFlags,
|
|
||||||
authtime[5] KerberosTime,
|
|
||||||
starttime[6] KerberosTime OPTIONAL,
|
|
||||||
endtime[7] KerberosTime,
|
|
||||||
renew-till[8] KerberosTime OPTIONAL,
|
|
||||||
srealm[9] Realm,
|
|
||||||
sname[10] PrincipalName,
|
|
||||||
caddr[11] HostAddresses OPTIONAL
|
|
||||||
}
|
|
||||||
|
|
||||||
EncASRepPart ::= [APPLICATION 25] EncKDCRepPart
|
|
||||||
EncTGSRepPart ::= [APPLICATION 26] EncKDCRepPart
|
|
||||||
|
|
||||||
AP-REQ ::= [APPLICATION 14] SEQUENCE {
|
|
||||||
pvno[0] INTEGER,
|
|
||||||
msg-type[1] INTEGER,
|
|
||||||
ap-options[2] APOptions,
|
|
||||||
ticket[3] Ticket,
|
|
||||||
authenticator[4] EncryptedData
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
AP-REP ::= [APPLICATION 15] SEQUENCE {
|
|
||||||
pvno[0] INTEGER,
|
|
||||||
msg-type[1] INTEGER,
|
|
||||||
enc-part[2] EncryptedData
|
|
||||||
}
|
|
||||||
|
|
||||||
EncAPRepPart ::= [APPLICATION 27] SEQUENCE {
|
|
||||||
ctime[0] KerberosTime,
|
|
||||||
cusec[1] INTEGER,
|
|
||||||
subkey[2] EncryptionKey OPTIONAL,
|
|
||||||
seq-number[3] INTEGER OPTIONAL
|
|
||||||
}
|
|
||||||
|
|
||||||
KRB-SAFE-BODY ::= SEQUENCE {
|
|
||||||
user-data[0] OCTET STRING,
|
|
||||||
timestamp[1] KerberosTime OPTIONAL,
|
|
||||||
usec[2] INTEGER OPTIONAL,
|
|
||||||
seq-number[3] INTEGER OPTIONAL,
|
|
||||||
s-address[4] HostAddress,
|
|
||||||
r-address[5] HostAddress OPTIONAL
|
|
||||||
}
|
|
||||||
|
|
||||||
KRB-SAFE ::= [APPLICATION 20] SEQUENCE {
|
|
||||||
pvno[0] INTEGER,
|
|
||||||
msg-type[1] INTEGER,
|
|
||||||
safe-body[2] KRB-SAFE-BODY,
|
|
||||||
cksum[3] Checksum
|
|
||||||
}
|
|
||||||
|
|
||||||
KRB-PRIV ::= [APPLICATION 21] SEQUENCE {
|
|
||||||
pvno[0] INTEGER,
|
|
||||||
msg-type[1] INTEGER,
|
|
||||||
enc-part[3] EncryptedData
|
|
||||||
}
|
|
||||||
EncKrbPrivPart ::= [APPLICATION 28] SEQUENCE {
|
|
||||||
user-data[0] OCTET STRING,
|
|
||||||
timestamp[1] KerberosTime OPTIONAL,
|
|
||||||
usec[2] INTEGER OPTIONAL,
|
|
||||||
seq-number[3] INTEGER OPTIONAL,
|
|
||||||
s-address[4] HostAddress, -- sender's addr
|
|
||||||
r-address[5] HostAddress OPTIONAL
|
|
||||||
-- recip's addr
|
|
||||||
}
|
|
||||||
|
|
||||||
KRB-CRED ::= [APPLICATION 22] SEQUENCE {
|
|
||||||
pvno[0] INTEGER,
|
|
||||||
msg-type[1] INTEGER, -- KRB_CRED
|
|
||||||
tickets[2] SEQUENCE OF Ticket,
|
|
||||||
enc-part[3] EncryptedData
|
|
||||||
}
|
|
||||||
|
|
||||||
KrbCredInfo ::= SEQUENCE {
|
|
||||||
key[0] EncryptionKey,
|
|
||||||
prealm[1] Realm OPTIONAL,
|
|
||||||
pname[2] PrincipalName OPTIONAL,
|
|
||||||
flags[3] TicketFlags OPTIONAL,
|
|
||||||
authtime[4] KerberosTime OPTIONAL,
|
|
||||||
starttime[5] KerberosTime OPTIONAL,
|
|
||||||
endtime[6] KerberosTime OPTIONAL,
|
|
||||||
renew-till[7] KerberosTime OPTIONAL,
|
|
||||||
srealm[8] Realm OPTIONAL,
|
|
||||||
sname[9] PrincipalName OPTIONAL,
|
|
||||||
caddr[10] HostAddresses OPTIONAL
|
|
||||||
}
|
|
||||||
|
|
||||||
EncKrbCredPart ::= [APPLICATION 29] SEQUENCE {
|
|
||||||
ticket-info[0] SEQUENCE OF KrbCredInfo,
|
|
||||||
nonce[1] INTEGER OPTIONAL,
|
|
||||||
timestamp[2] KerberosTime OPTIONAL,
|
|
||||||
usec[3] INTEGER OPTIONAL,
|
|
||||||
s-address[4] HostAddress OPTIONAL,
|
|
||||||
r-address[5] HostAddress OPTIONAL
|
|
||||||
}
|
|
||||||
|
|
||||||
KRB-ERROR ::= [APPLICATION 30] SEQUENCE {
|
|
||||||
pvno[0] INTEGER,
|
|
||||||
msg-type[1] INTEGER,
|
|
||||||
ctime[2] KerberosTime OPTIONAL,
|
|
||||||
cusec[3] INTEGER OPTIONAL,
|
|
||||||
stime[4] KerberosTime,
|
|
||||||
susec[5] INTEGER,
|
|
||||||
error-code[6] INTEGER,
|
|
||||||
crealm[7] Realm OPTIONAL,
|
|
||||||
cname[8] PrincipalName OPTIONAL,
|
|
||||||
realm[9] Realm, -- Correct realm
|
|
||||||
sname[10] PrincipalName, -- Correct name
|
|
||||||
e-text[11] GeneralString OPTIONAL,
|
|
||||||
e-data[12] OCTET STRING OPTIONAL
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
pvno INTEGER ::= 5 -- current Kerberos protocol version number
|
|
||||||
|
|
||||||
-- message types
|
|
||||||
|
|
||||||
krb-as-req INTEGER ::= 10 -- Request for initial authentication
|
|
||||||
krb-as-rep INTEGER ::= 11 -- Response to KRB_AS_REQ request
|
|
||||||
krb-tgs-req INTEGER ::= 12 -- Request for authentication based on TGT
|
|
||||||
krb-tgs-rep INTEGER ::= 13 -- Response to KRB_TGS_REQ request
|
|
||||||
krb-ap-req INTEGER ::= 14 -- application request to server
|
|
||||||
krb-ap-rep INTEGER ::= 15 -- Response to KRB_AP_REQ_MUTUAL
|
|
||||||
krb-safe INTEGER ::= 20 -- Safe (checksummed) application message
|
|
||||||
krb-priv INTEGER ::= 21 -- Private (encrypted) application message
|
|
||||||
krb-cred INTEGER ::= 22 -- Private (encrypted) message to forward credentials
|
|
||||||
krb-error INTEGER ::= 30 -- Error response
|
|
||||||
|
|
||||||
|
|
||||||
END
|
|
@@ -1,3 +0,0 @@
|
|||||||
/* $Id$ */
|
|
||||||
|
|
||||||
void error_message (char *, ...);
|
|
63
asn1/lex.l
63
asn1/lex.l
@@ -1,63 +0,0 @@
|
|||||||
/* $Id$ */
|
|
||||||
|
|
||||||
%{
|
|
||||||
#ifdef HAVE_CONFIG_H
|
|
||||||
#include <config.h>
|
|
||||||
#endif
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdarg.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include "symbol.h"
|
|
||||||
#include "parse.h"
|
|
||||||
|
|
||||||
char *strdup(char *);
|
|
||||||
|
|
||||||
void error_message(char *, ...);
|
|
||||||
|
|
||||||
static unsigned lineno = 1;
|
|
||||||
static char filename[256];
|
|
||||||
%}
|
|
||||||
|
|
||||||
|
|
||||||
%%
|
|
||||||
INTEGER { return INTEGER; }
|
|
||||||
SEQUENCE { return SEQUENCE; }
|
|
||||||
OF { return OF; }
|
|
||||||
OCTET { return OCTET; }
|
|
||||||
STRING { return STRING; }
|
|
||||||
GeneralizedTime { return GeneralizedTime; }
|
|
||||||
GeneralString { return GeneralString; }
|
|
||||||
BIT { return BIT; }
|
|
||||||
APPLICATION { return APPLICATION; }
|
|
||||||
OPTIONAL { return OPTIONAL; }
|
|
||||||
BEGIN { return TBEGIN; }
|
|
||||||
END { return END; }
|
|
||||||
DEFINITIONS { return DEFINITIONS; }
|
|
||||||
,|\{|\}|\(|\)|\[|\]|\| { return *yytext; }
|
|
||||||
::= { return EEQUAL; }
|
|
||||||
--[^\n]*\n { ; }
|
|
||||||
-?[0-9]+ { yylval.constant = atoi(yytext); return CONSTANT; }
|
|
||||||
[A-Za-z][-A-Za-z0-9_]* { yylval.name = strdup (yytext); return IDENTIFIER; }
|
|
||||||
[ \t] ;
|
|
||||||
\n { lineno++; }
|
|
||||||
. { error_message("Ignoring char(%c)\n", *yytext); }
|
|
||||||
%%
|
|
||||||
|
|
||||||
#ifndef yywrap /* XXX */
|
|
||||||
int
|
|
||||||
yywrap ()
|
|
||||||
{
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
void
|
|
||||||
error_message (char *format, ...)
|
|
||||||
{
|
|
||||||
va_list args;
|
|
||||||
|
|
||||||
va_start (args, format);
|
|
||||||
fprintf (stderr, ":%d: ", lineno);
|
|
||||||
vfprintf (stderr, format, args);
|
|
||||||
va_end (args);
|
|
||||||
}
|
|
31
asn1/main.c
31
asn1/main.c
@@ -1,31 +0,0 @@
|
|||||||
/* $Id$ */
|
|
||||||
|
|
||||||
#ifdef HAVE_CONFIG_H
|
|
||||||
#include <config.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
#include "symbol.h"
|
|
||||||
|
|
||||||
extern FILE *yyin;
|
|
||||||
|
|
||||||
int
|
|
||||||
main(int argc, char **argv)
|
|
||||||
{
|
|
||||||
int ret;
|
|
||||||
char *name;
|
|
||||||
|
|
||||||
if (argc == 1) {
|
|
||||||
name = "stdin";
|
|
||||||
yyin = stdin;
|
|
||||||
} else {
|
|
||||||
name = argv[1];
|
|
||||||
yyin = fopen (name, "r");
|
|
||||||
}
|
|
||||||
|
|
||||||
init_generate (name);
|
|
||||||
initsym ();
|
|
||||||
ret = yyparse ();
|
|
||||||
close_generate ();
|
|
||||||
return ret;
|
|
||||||
}
|
|
185
asn1/parse.y
185
asn1/parse.y
@@ -1,185 +0,0 @@
|
|||||||
/* $Id$ */
|
|
||||||
|
|
||||||
%{
|
|
||||||
#ifdef HAVE_CONFIG_H
|
|
||||||
#include <config.h>
|
|
||||||
#endif
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include "symbol.h"
|
|
||||||
#include "lex.h"
|
|
||||||
#include "gen.h"
|
|
||||||
|
|
||||||
static Type *new_type (Typetype t);
|
|
||||||
void yyerror (char *);
|
|
||||||
|
|
||||||
char *strdup(char *);
|
|
||||||
|
|
||||||
static void append (Member *l, Member *r);
|
|
||||||
|
|
||||||
%}
|
|
||||||
|
|
||||||
%union {
|
|
||||||
int constant;
|
|
||||||
char *name;
|
|
||||||
Type *type;
|
|
||||||
Member *member;
|
|
||||||
}
|
|
||||||
|
|
||||||
%token INTEGER SEQUENCE OF OCTET STRING GeneralizedTime GeneralString
|
|
||||||
%token BIT APPLICATION OPTIONAL EEQUAL TBEGIN END DEFINITIONS
|
|
||||||
%token <name> IDENTIFIER
|
|
||||||
%token <constant> CONSTANT
|
|
||||||
%token IDENTIFIER CONSTANT
|
|
||||||
|
|
||||||
%type <constant> constant optional2
|
|
||||||
%type <type> type
|
|
||||||
%type <member> memberdecls memberdecl bitdecls bitdecl
|
|
||||||
|
|
||||||
%start envelope
|
|
||||||
|
|
||||||
%%
|
|
||||||
|
|
||||||
envelope : IDENTIFIER DEFINITIONS EEQUAL TBEGIN specification END {}
|
|
||||||
;
|
|
||||||
|
|
||||||
specification :
|
|
||||||
| specification declaration
|
|
||||||
;
|
|
||||||
|
|
||||||
declaration : type_decl
|
|
||||||
| constant_decl
|
|
||||||
;
|
|
||||||
|
|
||||||
type_decl : IDENTIFIER EEQUAL type
|
|
||||||
{
|
|
||||||
Symbol *s = addsym ($1);
|
|
||||||
s->stype = Stype;
|
|
||||||
s->type = $3;
|
|
||||||
generate_type (s);
|
|
||||||
}
|
|
||||||
;
|
|
||||||
|
|
||||||
constant_decl : IDENTIFIER type EEQUAL constant
|
|
||||||
{
|
|
||||||
Symbol *s = addsym ($1);
|
|
||||||
s->stype = SConstant;
|
|
||||||
s->constant = $4;
|
|
||||||
generate_constant (s);
|
|
||||||
}
|
|
||||||
;
|
|
||||||
|
|
||||||
type : INTEGER { $$ = new_type(TInteger); }
|
|
||||||
| OCTET STRING { $$ = new_type(TOctetString); }
|
|
||||||
| GeneralString { $$ = new_type(TGeneralString); }
|
|
||||||
| GeneralizedTime { $$ = new_type(TGeneralizedTime); }
|
|
||||||
| SEQUENCE OF type
|
|
||||||
{
|
|
||||||
$$ = new_type(TSequenceOf);
|
|
||||||
$$->subtype = $3;
|
|
||||||
}
|
|
||||||
| SEQUENCE '{' memberdecls '}'
|
|
||||||
{
|
|
||||||
$$ = new_type(TSequence);
|
|
||||||
$$->members = $3;
|
|
||||||
}
|
|
||||||
| BIT STRING '{' bitdecls '}'
|
|
||||||
{
|
|
||||||
$$ = new_type(TBitString);
|
|
||||||
$$->members = $4;
|
|
||||||
}
|
|
||||||
| IDENTIFIER
|
|
||||||
{
|
|
||||||
Symbol *s = addsym($1);
|
|
||||||
$$ = new_type(TType);
|
|
||||||
if(s->stype != Stype)
|
|
||||||
error_message ("%s is not a type\n", $1);
|
|
||||||
else
|
|
||||||
$$->symbol = s;
|
|
||||||
}
|
|
||||||
| '[' APPLICATION constant ']' type
|
|
||||||
{
|
|
||||||
$$ = new_type(TApplication);
|
|
||||||
$$->subtype = $5;
|
|
||||||
$$->application = $3;
|
|
||||||
}
|
|
||||||
;
|
|
||||||
|
|
||||||
memberdecls : { $$ = NULL; }
|
|
||||||
| memberdecl { $$ = $1; }
|
|
||||||
| memberdecls ',' memberdecl { $$ = $1; append($$, $3); }
|
|
||||||
;
|
|
||||||
|
|
||||||
memberdecl : IDENTIFIER '[' constant ']' type optional2
|
|
||||||
{
|
|
||||||
$$ = malloc(sizeof(*$$));
|
|
||||||
$$->name = $1;
|
|
||||||
$$->gen_name = strdup($1);
|
|
||||||
output_name ($$->gen_name);
|
|
||||||
$$->val = $3;
|
|
||||||
$$->optional = $6;
|
|
||||||
$$->type = $5;
|
|
||||||
$$->next = $$->prev = $$;
|
|
||||||
}
|
|
||||||
;
|
|
||||||
|
|
||||||
optional2 : { $$ = 0; }
|
|
||||||
| OPTIONAL { $$ = 1; }
|
|
||||||
;
|
|
||||||
|
|
||||||
bitdecls : { $$ = NULL; }
|
|
||||||
| bitdecl { $$ = $1; }
|
|
||||||
| bitdecls ',' bitdecl { $$ = $1; append($$, $3); }
|
|
||||||
;
|
|
||||||
|
|
||||||
bitdecl : IDENTIFIER '(' constant ')'
|
|
||||||
{
|
|
||||||
$$ = malloc(sizeof(*$$));
|
|
||||||
$$->name = $1;
|
|
||||||
$$->gen_name = strdup($1);
|
|
||||||
output_name ($$->gen_name);
|
|
||||||
$$->val = $3;
|
|
||||||
$$->optional = 0;
|
|
||||||
$$->type = NULL;
|
|
||||||
$$->prev = $$->next = $$;
|
|
||||||
}
|
|
||||||
;
|
|
||||||
|
|
||||||
constant : CONSTANT { $$ = $1; }
|
|
||||||
| IDENTIFIER {
|
|
||||||
Symbol *s = addsym($1);
|
|
||||||
if(s->stype != SConstant)
|
|
||||||
error_message ("%s is not a constant\n",
|
|
||||||
s->name);
|
|
||||||
else
|
|
||||||
$$ = s->constant;
|
|
||||||
}
|
|
||||||
;
|
|
||||||
%%
|
|
||||||
|
|
||||||
void
|
|
||||||
yyerror (char *s)
|
|
||||||
{
|
|
||||||
error_message ("%s\n", s);
|
|
||||||
}
|
|
||||||
|
|
||||||
static Type *
|
|
||||||
new_type (Typetype tt)
|
|
||||||
{
|
|
||||||
Type *t = malloc(sizeof(*t));
|
|
||||||
t->type = tt;
|
|
||||||
t->application = 0;
|
|
||||||
t->members = NULL;
|
|
||||||
t->subtype = NULL;
|
|
||||||
t->symbol = NULL;
|
|
||||||
return t;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
append (Member *l, Member *r)
|
|
||||||
{
|
|
||||||
l->prev->next = r;
|
|
||||||
r->prev = l->prev;
|
|
||||||
l->prev = r;
|
|
||||||
r->next = l;
|
|
||||||
}
|
|
@@ -1,64 +0,0 @@
|
|||||||
/* $Id$ */
|
|
||||||
|
|
||||||
#ifdef HAVE_CONFIG_H
|
|
||||||
#include <config.h>
|
|
||||||
#endif
|
|
||||||
#include <string.h>
|
|
||||||
#include "symbol.h"
|
|
||||||
#include "hash.h"
|
|
||||||
|
|
||||||
static Hashtab *htab;
|
|
||||||
|
|
||||||
static int
|
|
||||||
cmp (void *a, void *b)
|
|
||||||
{
|
|
||||||
Symbol *s1 = (Symbol *)a;
|
|
||||||
Symbol *s2 = (Symbol *)b;
|
|
||||||
|
|
||||||
return strcmp (s1->name, s2->name);
|
|
||||||
}
|
|
||||||
|
|
||||||
static unsigned
|
|
||||||
hash (void *a)
|
|
||||||
{
|
|
||||||
Symbol *s = (Symbol *)a;
|
|
||||||
|
|
||||||
return hashjpw (s->name);
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
initsym ()
|
|
||||||
{
|
|
||||||
htab = hashtabnew (101, cmp, hash);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void
|
|
||||||
output_name (char *s)
|
|
||||||
{
|
|
||||||
char *p;
|
|
||||||
|
|
||||||
for (p = s; *p; ++p)
|
|
||||||
if (*p == '-')
|
|
||||||
*p = '_';
|
|
||||||
}
|
|
||||||
|
|
||||||
Symbol*
|
|
||||||
addsym (char *name)
|
|
||||||
{
|
|
||||||
Symbol key, *s;
|
|
||||||
|
|
||||||
key.name = name;
|
|
||||||
s = (Symbol *)hashtabsearch (htab, (void *)&key);
|
|
||||||
if (s == NULL) {
|
|
||||||
char *p;
|
|
||||||
|
|
||||||
s = (Symbol *)malloc (sizeof (*s));
|
|
||||||
s->name = name;
|
|
||||||
s->gen_name = strdup(name);
|
|
||||||
output_name (s->gen_name);
|
|
||||||
s->stype = SUndefined;
|
|
||||||
hashtabadd (htab, s);
|
|
||||||
}
|
|
||||||
return s;
|
|
||||||
}
|
|
@@ -1,49 +0,0 @@
|
|||||||
/* $Id$ */
|
|
||||||
|
|
||||||
#ifndef _SYMBOL_H
|
|
||||||
#define _SYMBOL_H
|
|
||||||
|
|
||||||
enum typetype { TInteger, TOctetString, TBitString, TSequence, TSequenceOf,
|
|
||||||
TGeneralizedTime, TGeneralString, TApplication, TType };
|
|
||||||
|
|
||||||
typedef enum typetype Typetype;
|
|
||||||
|
|
||||||
struct type;
|
|
||||||
|
|
||||||
struct member {
|
|
||||||
char *name;
|
|
||||||
char *gen_name;
|
|
||||||
int val;
|
|
||||||
int optional;
|
|
||||||
struct type *type;
|
|
||||||
struct member *next, *prev;
|
|
||||||
};
|
|
||||||
|
|
||||||
typedef struct member Member;
|
|
||||||
|
|
||||||
struct symbol;
|
|
||||||
|
|
||||||
struct type {
|
|
||||||
Typetype type;
|
|
||||||
int application;
|
|
||||||
Member *members;
|
|
||||||
struct type *subtype;
|
|
||||||
struct symbol *symbol;
|
|
||||||
};
|
|
||||||
|
|
||||||
typedef struct type Type;
|
|
||||||
|
|
||||||
struct symbol {
|
|
||||||
char *name;
|
|
||||||
char *gen_name;
|
|
||||||
enum { SUndefined, SConstant, Stype } stype;
|
|
||||||
int constant;
|
|
||||||
Type *type;
|
|
||||||
};
|
|
||||||
|
|
||||||
typedef struct symbol Symbol;
|
|
||||||
|
|
||||||
void initsym ();
|
|
||||||
Symbol *addsym (char *);
|
|
||||||
void output_name (char *);
|
|
||||||
#endif
|
|
Reference in New Issue
Block a user