Skip to content

Commit

Permalink
Merge branch 'master' into fix_s1ap_code_generation_2
Browse files Browse the repository at this point in the history
  • Loading branch information
brchiu authored Jan 15, 2018
2 parents c307df9 + 4cc779f commit f3d0aa4
Show file tree
Hide file tree
Showing 18 changed files with 385 additions and 134 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ doc/docsrc/*.xdv
/libasn1fix/check_*

# /libasn1parser/
/libasn1parser/check_parser
/libasn1parser/check_*

#code coverage
*.gcno
Expand Down
3 changes: 3 additions & 0 deletions asn1c/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ asn1c_LDADD = \
$(top_builddir)/libasn1fix/libasn1fix.la \
$(top_builddir)/libasn1compiler/libasn1compiler.la

unber_LDADD = \
$(top_builddir)/libasn1common/libasn1common.la

bin_PROGRAMS = asn1c unber enber

noinst_HEADERS = sys-common.h
Expand Down
55 changes: 44 additions & 11 deletions asn1c/asn1c.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,49 @@ main(int ac, char **av) {
int print_arg__fix_n_print = 0; /* Fix and print */
int warnings_as_errors = 0; /* Treat warnings as errors */
char *skeletons_dir = NULL; /* Directory with supplementary stuff */
char destdir[PATH_MAX]; /* Destination directory for generated files */
char *destdir = NULL; /* Destination for generated files */
char **debug_type_names = 0; /* Debug stuff */
size_t debug_type_names_count = 0;
asn1p_t *asn = 0; /* An ASN.1 parsed tree */
int ret; /* Return value from misc functions */
int ch; /* Command line character */
int i; /* Index in some loops */
int exit_code = 0; /* Exit code */

destdir[0] = '\0';

/*
* Process command-line options.
*/
while((ch = getopt(ac, av, "EFf:g:hn:LPp:RS:D:vW:X")) != -1) switch(ch) {
while((ch = getopt(ac, av, "D:d:EFf:g:hn:LPp:RS:vW:X")) != -1) switch(ch) {
case 'D':
if(optarg && *optarg) {
size_t optarg_len = strlen(optarg);
free(destdir);
destdir = calloc(1, optarg_len + 2); /* + "/\0" */
assert(destdir);
strcpy(destdir, optarg);
if(destdir[optarg_len - 1] != '/') {
destdir[optarg_len] = '/';
}
} else {
free(destdir);
destdir = NULL;
}
break;
case 'd':
if(strncmp(optarg, "ebug-type-naming=", 17) == 0) {
char **p = realloc(debug_type_names,
(debug_type_names_count + 2) * sizeof(*p));
assert(p);
debug_type_names = p;
debug_type_names[debug_type_names_count++] =
strdup(optarg + 17);
debug_type_names[debug_type_names_count] = NULL;
break;
} else if(strcmp(optarg, "ebug-output-origin-lines") == 0) {
asn1_compiler_flags |= A1C_DEBUG_OUTPUT_ORIGIN_LINES;
break;
}
usage(av[0]);
case 'E':
print_arg__print_out = 1;
break;
Expand Down Expand Up @@ -186,11 +216,6 @@ main(int ac, char **av) {
case 'S':
skeletons_dir = optarg;
break;
case 'D':
strncat(destdir, optarg, PATH_MAX - 2); /* leave room for possible trailing '/' */
if(destdir[strlen(destdir)-1] != '/')
strcat(destdir, "/");
break;
case 'v':
fprintf(stderr, "ASN.1 Compiler, v" VERSION "\n" COPYRIGHT);
exit(0);
Expand Down Expand Up @@ -378,12 +403,20 @@ main(int ac, char **av) {
return 0;
}

/*
* -debug-type-naming=Type
*/
if(debug_type_names) {
asn1c_debug_type_naming(asn, asn1_compiler_flags, debug_type_names);
return 0;
}

/*
* Compile the ASN.1 tree into a set of source files
* of another language.
*/
if(asn1_compile(asn, skeletons_dir, destdir, asn1_compiler_flags, ac + optind,
optind, av - optind)) {
if(asn1_compile(asn, skeletons_dir, destdir ? destdir : "",
asn1_compiler_flags, ac + optind, optind, av - optind)) {
exit_code = EX_SOFTWARE;
}

Expand Down
2 changes: 1 addition & 1 deletion libasn1compiler/asn1c_C.c
Original file line number Diff line number Diff line change
Expand Up @@ -2064,7 +2064,7 @@ emit_single_member_PER_constraint(arg_t *arg, asn1cnst_range_t *range, int alpha
OUT("{ APC_CONSTRAINED%s,%s% d, % d, ",
range->extensible
? " | APC_EXTENSIBLE" : "",
range->extensible ? " " : "\t", rbits, ebits);
range->extensible ? " " : "\t", (int)rbits, (int)ebits);

if(alphabetsize) {
asn1c_integer_t lv = range->left.value;
Expand Down
27 changes: 17 additions & 10 deletions libasn1compiler/asn1c_compat.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ int mkstemp(char *template) {
#endif

FILE *
asn1c_open_file(const char* destdir, const char *name, const char *ext, char **opt_tmpname) {
char fname[PATH_MAX];
asn1c_open_file(const char *destdir, const char *name, const char *ext,
char **opt_tmpname) {
char fname[PATH_MAX];
int created = 1;
#ifndef _WIN32
struct stat sb;
Expand All @@ -49,10 +50,8 @@ asn1c_open_file(const char* destdir, const char *name, const char *ext, char **o
/*
* Compute filenames.
*/
ret = snprintf(fname, sizeof(fname), "%s%s%s%s",
opt_tmpname ? "" : destdir,
name, ext,
opt_tmpname ? ".XXXXXX" : "");
ret = snprintf(fname, sizeof(fname), "%s%s%s%s", destdir ? destdir : "",
name, ext, opt_tmpname ? ".XXXXXX" : "");
assert(ret > 0 && ret < (ssize_t)sizeof(fname));

if(opt_tmpname) {
Expand All @@ -61,8 +60,10 @@ asn1c_open_file(const char* destdir, const char *name, const char *ext, char **o
*/
fd = mkstemp(fname);
#ifndef _WIN32
/* fchmod() does not respect umask */
(void)fchmod(fd, REASONABLE_FILE_MODE);
if(fd != -1) {
/* fchmod() does not respect umask */
(void)fchmod(fd, REASONABLE_FILE_MODE);
}
#endif
} else {
/*
Expand All @@ -75,8 +76,14 @@ asn1c_open_file(const char* destdir, const char *name, const char *ext, char **o
}
}
if(fd == -1) {
perror(fname);
return NULL;
struct stat st;
if(destdir && stat(destdir, &st) == -1) {
fprintf(stderr, "%s: No such directory\n", destdir);
return NULL;
} else {
perror(fname);
return NULL;
}
}

#ifndef _WIN32
Expand Down
4 changes: 2 additions & 2 deletions libasn1compiler/asn1c_ioc.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,10 @@ emit_ioc_value(arg_t *arg, struct asn1p_ioc_cell_s *cell) {
asn1c_integer_t v = expr_value->value->value.v_integer;
if(v >= 0) {
if(v <= 127) {
OUT("\"\\x%02x\", 1", v);
OUT("\"\\x%02x\", 1", (int)v);
break;
} else if(v <= 32767) {
OUT("\"\\x%02x\\x%02x\", 2", (v >> 8), (v & 0xff));
OUT("\"\\x%02x\\x%02x\", 2", (int)(v >> 8), (int)(v & 0xff));
break;
}
}
Expand Down
22 changes: 19 additions & 3 deletions libasn1compiler/asn1c_out.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
* into appropriate output stream.
*/
int
asn1c_compiled_output(arg_t *arg, const char *fmt, ...) {
asn1c_compiled_output(arg_t *arg, const char *source, int lineno, const char *func, const char *fmt,
...) {
struct compiler_stream_destination_s *dst;
const char *p;
int lf_found;
Expand Down Expand Up @@ -45,13 +46,19 @@ asn1c_compiled_output(arg_t *arg, const char *fmt, ...) {
}
dst->indented = 1;
while(i--) {
ret = asn1c_compiled_output(arg, "\t");
ret = asn1c_compiled_output(arg, source, lineno, func, "\t");
if(ret == -1) return -1;
}
}
if(lf_found)
dst->indented = 0;

size_t debug_reserve_size = 0;
if(lf_found && (arg->flags & A1C_DEBUG_OUTPUT_ORIGIN_LINES)) {
debug_reserve_size =
sizeof("\t// :100000 ()") + strlen(source) + strlen(func);
}

/*
* Allocate buffer.
*/
Expand All @@ -62,7 +69,7 @@ asn1c_compiled_output(arg_t *arg, const char *fmt, ...) {
do {
void *tmp;
m->len <<= 2;
tmp = realloc(m->buf, m->len);
tmp = realloc(m->buf, m->len + debug_reserve_size);
if(tmp) {
m->buf = (char *)tmp;
} else {
Expand All @@ -77,6 +84,15 @@ asn1c_compiled_output(arg_t *arg, const char *fmt, ...) {

m->len = ret;

/* Print out the origin of the lines */
if(lf_found && (arg->flags & A1C_DEBUG_OUTPUT_ORIGIN_LINES)) {
assert(m->buf[m->len - 1] == '\n');
ret = snprintf(m->buf + m->len - 1, debug_reserve_size,
"\t// %s:%03d %s()\n", source, lineno, func);
assert(ret > 0 && (size_t)ret < debug_reserve_size);
m->len = m->len - 1 + ret;
}

if(arg->target->target == OT_INCLUDES
|| arg->target->target == OT_FWD_DECLS
|| arg->target->target == OT_POST_INCLUDE) {
Expand Down
7 changes: 5 additions & 2 deletions libasn1compiler/asn1c_out.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ typedef struct compiler_streams {
static char *_compiler_stream2str[] __attribute__ ((unused))
= { "IGNORE", "INCLUDES", "DEPS", "FWD-DECLS", "FWD-DEFS", "TYPE-DECLS", "FUNC-DECLS", "POST-INCLUDE", "IOC-TABLES", "CTABLES", "CODE", "CTDEFS", "STAT-DEFS" };

int asn1c_compiled_output(arg_t *arg, const char *fmt, ...);
int asn1c_compiled_output(arg_t *arg, const char *file, int lineno,
const char *func, const char *fmt, ...)
__attribute__((format(printf, 5, 6)));


/*****************************************************************
Expand Down Expand Up @@ -78,7 +80,8 @@ int asn1c_compiled_output(arg_t *arg, const char *fmt, ...);
} while(0)

/* Output a piece of text into a default stream */
#define OUT(fmt, args...) asn1c_compiled_output(arg, fmt, ##args)
#define OUT(fmt, args...) \
asn1c_compiled_output(arg, __FILE__, __LINE__, __func__, fmt, ##args)
#define OUT_NOINDENT(fmt, args...) do { \
int _saved_indent = INDENT_LEVEL; \
INDENT_LEVEL = 0; \
Expand Down
Loading

0 comments on commit f3d0aa4

Please sign in to comment.