/********************************************************************/
/*Title:	asn1.h													*/
/*Descr:	ASN.1 Encoding/Decoding Module(Head file)				*/
/*Author:	Liang Hanxi												*/
/*Create:	2002-1-15												*/
/*Version:	R3V03_01												*/
/*Modify: 	2002-10-22												*/
/********************************************************************/
#ifndef _ASN_1_HEAD
#define _ASN_1_HEAD

#include <sys/types.h>
#include <netinet/in.h>

typedef struct ASN_TLV
{
	u_char			tagtype;
	u_short			tagcode;
	int				length;
	u_char			*pvalue;
	struct ASN_TLV	*pnext;
	struct ASN_TLV	*psub;
}ASN_TLV;

typedef struct _ASN_BUFFER
{
	u_char		*msgbuf;			//store pointer of buffer
	int			maxlen;
	int			msglen;				//totallength of message
	int			pos;

	ASN_TLV		heap[256];
	char		errmsg[1024];
	short		tlvcount;
}ASN_BUF;

//interface======>
void AsnEncode(u_char *msg_buf,u_int maxlen,ASN_BUF *asnbuf);
int AsnDecode(u_char *msg_buf,int msglen,int maxtlv,int *errpos,ASN_BUF *asnbuf);
int GetTLV(const char *tag_seq,int maxlen,u_char *pvalue,u_char flag,ASN_BUF *asnbuf);
int GetAnyTLV(const char *tag_seq,int maxlen,u_char *pvalue,u_char *pflag,u_short *ptagcode,ASN_BUF *asnbuf);
int AddTLV(const char *tag_seq,u_int length,u_char *pvalue,u_char tlv_type,ASN_BUF *asnbuf);
int GetInteger(const char *tag_seq,int *rtn,u_char flag,ASN_BUF *asnbuf);
int AddInteger(const char *tag_seq,int value,u_char tlv_type,ASN_BUF *asnbuf);

void asn_encode(u_char *asn_buf,ASN_BUF *asnbuf);
void asn_encode_v3(u_char *msg_buf,u_int maxlen,ASN_BUF *asnbuf);
int add_tlv(const char *tag_seq,u_int length,u_char *pvalue,u_char tlv_type,ASN_BUF *asnbuf);
int asn_decode(u_char *asn_buf,int ntlv,ASN_BUF *asnbuf);
int asn_decode_v2(u_char *msg_buf,int ntlv,int *plen,ASN_BUF *asnbuf);
int asn_decode_v3(u_char *msg_buf,int msglen,int *errpos,ASN_BUF *asnbuf);
int get_tlv(const char *tag_seq,u_char *pvalue,ASN_BUF *asnbuf);
int get_tlv_v2(const char *tag_seq,u_char *pvalue,u_char *flag,ASN_BUF *asnbuf);
int parse_tlv(ASN_TLV **pnode,ASN_BUF *asnbuf);
//int parse_tlv2(u_char *asn_buf,int rest,u_short (*tags)[2],int order,ASN_BUF *asnbuf);
int findtlv(u_char *pasn,u_int tagcode,u_char **pnext,int rest);
int findtlv2(u_char *pasn,int index,u_char **pnext,int rest);
int gettagcode(u_char *asnbuf,u_int *tagcode,u_char *type);
int getlength(u_char *asnbuf,int *len);
int addtagcode(u_char *asn_buf,u_int tagcode,u_char type);
int addlength(u_char *asn_buf,u_int len);
int parse_tags(const char *tag_seq,u_short (*tags)[2]);
int modifylength(u_char *ptr[],int level,int addval);
void showbuf(u_char *buf,int len);

int add_bool(const char *tag_seq,u_char value,u_char tlv_type,ASN_BUF *asnbuf);
u_char get_bool(const char *tag_seq,ASN_BUF *asnbuf);
int add_int(const char *tag_seq,int value,u_char tlv_type,ASN_BUF *asnbuf);
int get_int(const char *tag_seq,ASN_BUF *asnbuf);
int get_int_v2(const char *tag_seq,int *rtn,ASN_BUF *asnbuf);
int add_oid(const char *tag_seq,const char *oidstr,u_char tlv_type,ASN_BUF *asnbuf);
int get_oid(const char *tag_seq,char *oid,ASN_BUF *asnbuf);
int add_null(const char *tag_seq,u_char tlv_type,ASN_BUF *asnbuf);
int get_null(const char *tag_seq,ASN_BUF *asnbuf);
int DecodeInteger(u_char *buf, u_char len);
#endif
