Plan 9 from Bell Labs’s /usr/web/sources/contrib/gabidiaz/root/sys/src/cmd/snmpfs/ber.h

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


#include <mp.h>

/* ANSI offsetof, backwards. */
#define	OFFSETOF(a, b)	offsetof(b, a)


/*  general ASN1 declarations and parsing */
typedef struct Elem Elem;
typedef struct Tag Tag;
typedef struct Value Value;
typedef struct Bytes Bytes;
typedef struct Ints Ints;
typedef struct Bits Bits;
typedef struct Elist Elist;

/* tag classes */
#define Universal 0
#define Context 0x80

/* universal tags */
#define BOOLEAN 1
#define INTEGER 2
#define BIT_STRING 3
#define OCTET_STRING 4
#define NULLTAG 5
#define OBJECT_ID 6
#define ObjectDescriptor 7
#define EXTERNAL 8
#define REAL 9
#define ENUMERATED 10
#define EMBEDDED_PDV 11
#define SEQUENCE 16		/* also SEQUENCE OF */
#define SETOF 17				/* also SETOF OF */
#define NumericString 18
#define PrintableString 19
#define TeletexString 20
#define VideotexString 21
#define IA5String 22
#define UTCTime 23
#define GeneralizedTime 24
#define GraphicString 25
#define VisibleString 26
#define GeneralString 27
#define UniversalString 28
#define BMPString 30



struct Bytes {
	int	len;
	uchar	data[1];
};

struct Ints {
	int	len;
	int	data[1];
};

struct Bits {
	int	len;		/* number of bytes */
	int	unusedbits;	/* unused bits in last byte */
	uchar	data[1];	/* most-significant bit first */
};

struct Tag {
	int	class;
	int	num;
};

enum { VBool, VInt, VOctets, VBigInt, VReal, VOther,
	VBitString, VNull, VEOC, VObjId, VString, VSeq, VSet };

struct Value {
	int	tag;		/* VBool, etc. */
	union {
		int	boolval;
		int	intval;
		Bytes*	octetsval;
		Bytes*	bigintval;
		Bytes*	realval;	/* undecoded; hardly ever used */
		Bytes*	otherval;
		Bits*	bitstringval;
		Ints*	objidval;
		char*	stringval;
		Elist*	seqval;
		Elist*	setval;
	} u;  /* (Don't use anonymous unions, for ease of porting) */
};

struct Elem {
	Tag	tag;
	Value	val;
};

struct Elist {
	Elist*	tl;
	Elem	hd;
};

/* decoding errors */
enum { ASN_OK, ASN_ESHORT, ASN_ETOOBIG, ASN_EVALLEN,
		ASN_ECONSTR, ASN_EPRIM, ASN_EINVAL, ASN_EUNIMPL };


/* here are the functions to consider making extern someday */
extern Bytes*	newbytes(int len);
extern Bytes*	makebytes(uchar* buf, int len);
extern void	freebytes(Bytes* b);
extern Bytes*	catbytes(Bytes* b1, Bytes* b2);
extern Ints*	newints(int len);
extern Ints*	makeints(int* buf, int len);
extern void	freeints(Ints* b);
extern Bits*	newbits(int len);
extern Bits*	makebits(uchar* buf, int len, int unusedbits);
extern void	freebits(Bits* b);
extern Elist*	mkel(Elem e, Elist* tail);
extern void	freeelist(Elist* el);
extern int	elistlen(Elist* el);
extern int	is_seq(Elem* pe, Elist** pseq);
extern int	is_set(Elem* pe, Elist** pset);
extern int	is_int(Elem* pe, int* pint);
extern int	is_bigint(Elem* pe, Bytes** pbigint);
extern int	is_bitstring(Elem* pe, Bits** pbits);
extern int	is_octetstring(Elem* pe, Bytes** poctets);
extern int	is_oid(Elem* pe, Ints** poid);
extern int	is_string(Elem* pe, char** pstring);
extern int	is_time(Elem* pe, char** ptime);
extern int	decode(uchar* a, int alen, Elem* pelem);
extern int	decode_seq(uchar* a, int alen, Elist** pelist);
extern int	decode_value(uchar* a, int alen, int kind, int isconstr, Value* pval);
extern int	encode(Elem e, Bytes** pbytes);
extern int	oid_lookup(Ints* o, Ints** tab);
extern void	freevalfields(Value* v);
extern mpint	*asn1mpint(Elem *e);



#define TAG_MASK 0x1F
#define CONSTR_MASK 0x20
#define CLASS_MASK 0xC0
#define MAXOBJIDLEN 20

extern int ber_decode(uchar** pp, uchar* pend, Elem* pelem);
extern int tag_decode(uchar** pp, uchar* pend, Tag* ptag, int* pisconstr);
extern int length_decode(uchar** pp, uchar* pend, int* plength);
extern int value_decode(uchar** pp, uchar* pend, int length, int kind, int isconstr, Value* pval);
extern int int_decode(uchar** pp, uchar* pend, int count, int unsgned, int* pint);
extern int uint7_decode(uchar** pp, uchar* pend, int* pint);
extern int octet_decode(uchar** pp, uchar* pend, int length, int isconstr, Bytes** pbytes);
extern int seq_decode(uchar** pp, uchar* pend, int length, int isconstr, Elist** pelist);
extern int enc(uchar** pp, Elem e, int lenonly);
extern int val_enc(uchar** pp, Elem e, int *pconstr, int lenonly);
extern void uint7_enc(uchar** pp, int num, int lenonly);
extern void int_enc(uchar** pp, int num, int unsgned, int lenonly);

extern Elem mkint(int j);
extern Elem mkbigint(mpint *p);
extern Elem mkstring(char *s);
extern Elem mkoctet(uchar *buf, int buflen);
extern Elem mkbits(uchar *buf, int buflen);
extern Elem mkutc(long t);
extern Elem mkoid(Ints *oid);
extern Elem mkseq(Elist *el);
extern Elem mkset(Elist *el);
extern Elem mkDN(char *dn);
extern Elem Null(void);

extern void * emalloc(int n);
extern char * estrdup(char *s);
extern void edump(Elem e);

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.