⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 asn1parser.c

📁 ASN.1解析解码工具,可以解析各种ASN.1格式的文件,并对相应的BER文件解码
💻 C
字号:
#include <stdio.h>#include <string.h>#include <stdlib.h>#include <sys/types.h>#include <sys/stat.h>#include <assert.h>#include <errno.h>#include "asn1parser/asn1parser.h"#include "asn1parser/asn1p_list.h"/*int asn1p_parse(void **param);*/void *asn1p__scan_bytes(const char *, int len);void *asn1p__delete_buffer(void *);void *asn1p_restart(FILE *);int asn1p__flex_debug;FILE *asn1p_in,*asn1p_out;char *asn1p_text;int asn1p_leng,asn1p_lineno = 1,asn1p__flex_debug = 0;int asn1p_debug;static int _asn1p_set_flags(enum asn1p_flags flags);static int _asn1p_fix_modules(asn1p_t *a, const char *fname);/* * Parse the given buffer. */asn1p_t *asn1p_parse_buffer(const char *buffer, int size /* = -1 */, enum asn1p_flags flags) {	asn1p_t *a = 0;	void *ap;	void *ybuf;	int ret;	if(_asn1p_set_flags(flags)) {		/* EINVAL */		return 0;	}	if(size < 0)		size = (int)strlen(buffer);	ybuf = asn1p__scan_bytes(buffer, size);	if(!ybuf) {		assert(ybuf);		return 0;	}	asn1p_lineno = 1;	ap = (void *)&a;	ret = asn1p_parse(ap);	asn1p__delete_buffer(ybuf);	if(ret == 0) {		assert(a);		if(_asn1p_fix_modules(a, "-"))			return NULL;	/* FIXME: destroy (a) */	} else if(a) {		asn1p_delete(a);		a = NULL;	}	return a;}/* * Parse the file identified by its name. */asn1p_t *asn1p_parse_file(const char *filename, enum asn1p_flags flags) {#ifndef	WIN32	struct stat sb;#endif	asn1p_t *a = 0;	void *ap;	FILE *fp;	int ret;	if(_asn1p_set_flags(flags)) {		/* EINVAL */		return 0;	}	fp = fopen(filename, "r");	if(fp == NULL) {		perror(filename);		return NULL;	}#ifndef	WIN32	if(fstat(fileno(fp), &sb)	|| !S_ISREG(sb.st_mode)) {		fclose(fp);		fprintf(stderr, "%s file not recognized: Bad file format\n",			filename);		errno = EINVAL;		return NULL;	}#endif	/* WIN32 */	asn1p_lineno = 1;	asn1p_restart(fp);	ap = (void *)&a;	ret = asn1p_parse(ap);	fclose(fp);	if(ret == 0) {		assert(a);		if(_asn1p_fix_modules(a, filename))			return NULL;	/* FIXME: destroy (a) */	} else if(a) {		asn1p_delete(a);		a = NULL;	}	return a;}extern int asn1p_lexer_types_year;extern int asn1p_lexer_constructs_year;extern int asn1p__flex_debug;static int_asn1p_set_flags(enum asn1p_flags flags) {	asn1p_lexer_types_year = 0;	asn1p_lexer_constructs_year = 0;	asn1p__flex_debug = 0;	/*	 * Enable debugging in lexer.	 */	if(flags & A1P_LEXER_DEBUG) {		flags &= ~A1P_LEXER_DEBUG;		asn1p__flex_debug = 1;	}	/*	 * Check that we haven't missed an unknown flag.	 */	if(flags) {		errno = EINVAL;		return -1;	}	return 0;}static int_asn1p_fix_modules(asn1p_t *a, const char *fname) {	asn1p_module_t *mod;	TQ_FOR(mod, &(a->modules), mod_next) {		mod->source_file_name = strdup(fname);		if(mod->source_file_name == NULL)			return -1;		mod->asn1p = a;	}	return 0;}intasn1p_atoi(const char *ptr, asn1c_integer_t *value) {	errno = 0;	/* Clear the error code */	if(sizeof(*value) <= sizeof(int)) {		*value = strtol(ptr, 0, 10);	} else {#ifdef	HAVE_STRTOIMAX		*value = strtoimax(ptr, 0, 10);#elif	HAVE_STRTOLL		*value = strtoll(ptr, 0, 10);#else		*value = strtol(ptr, 0, 10);#endif	}	if(errno != 0)	{		printf("%s\n",strerror(errno));	}	return errno == 0 ? 0 : -1;}void* asn1p_list_remove(tq_void_head_t* head,size_t offset){	void *__fel = TQ_FIRST(head);	tq_void_entry_t *next = (tq_void_entry_t *)(((char*)__fel)+offset);	if(__fel == 0							|| (TQ_FIRST((head)) = next->tq_next)			== 0) {							(head)->tq_tail = &TQ_FIRST((head));		} else {							next->tq_next = 0;		}							return __fel;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -