aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLev Walkin <vlm@lionet.info>2016-03-14 02:23:48 -0700
committerLev Walkin <vlm@lionet.info>2016-03-14 02:23:48 -0700
commitf2b2f3766068fb87e5ad2c2827e730229fa7bd7f (patch)
treeffb0e2767df1cd172407ec4e7cc0ba780fbd1793
parent4cc4599a7cda13e39891eae9d92dec48093cd81b (diff)
error values check from printf, fprintf, fwrite
-rw-r--r--libasn1compiler/asn1c_save.c115
-rw-r--r--libasn1print/asn1print.c350
2 files changed, 250 insertions, 215 deletions
diff --git a/libasn1compiler/asn1c_save.c b/libasn1compiler/asn1c_save.c
index fa207d35..f8348cc6 100644
--- a/libasn1compiler/asn1c_save.c
+++ b/libasn1compiler/asn1c_save.c
@@ -10,10 +10,27 @@
#define symlink(a,b) (errno=ENOSYS, -1)
#endif
+/* Pedantically check fprintf's return value. */
+static int safe_fprintf(FILE *fp, const char *fmt, ...) {
+ va_list ap;
+ va_start(ap, fmt);
+ int ret = vfprintf(fp, fmt, ap);
+ va_end(ap);
+ assert(ret >= 0);
+ return ret;
+}
+
+/* Pedantically check fwrite's return value. */
+static size_t safe_fwrite(const void *ptr, size_t size, size_t nitems, FILE *stream) {
+ size_t ret = fwrite(ptr, 1, size * nitems, stream);
+ assert(ret == size * nitems);
+ return ret;
+}
+
#define HINCLUDE(s) \
((arg->flags & A1C_INCLUDES_QUOTED) \
- ? fprintf(fp_h, "#include \"%s\"\n", s) \
- : fprintf(fp_h, "#include <%s>\n", s)) \
+ ? safe_fprintf(fp_h, "#include \"%s\"\n", s) \
+ : safe_fprintf(fp_h, "#include <%s>\n", s)) \
static int asn1c_dump_streams(arg_t *arg, asn1c_fdeps_t *, int, char **);
static int asn1c_print_streams(arg_t *arg);
@@ -66,27 +83,27 @@ asn1c_save_compiled_output(arg_t *arg, const char *datadir,
return -1;
}
- fprintf(mkf, "ASN_MODULE_SOURCES=");
+ safe_fprintf(mkf, "ASN_MODULE_SOURCES=");
TQ_FOR(mod, &(arg->asn->modules), mod_next) {
TQ_FOR(arg->expr, &(mod->members), next) {
if(asn1_lang_map[arg->expr->meta_type]
[arg->expr->expr_type].type_cb) {
- fprintf(mkf, "\t\\\n\t%s.c",
+ safe_fprintf(mkf, "\t\\\n\t%s.c",
arg->expr->Identifier);
}
}
}
- fprintf(mkf, "\n\nASN_MODULE_HEADERS=");
+ safe_fprintf(mkf, "\n\nASN_MODULE_HEADERS=");
TQ_FOR(mod, &(arg->asn->modules), mod_next) {
TQ_FOR(arg->expr, &(mod->members), next) {
if(asn1_lang_map[arg->expr->meta_type]
[arg->expr->expr_type].type_cb) {
- fprintf(mkf, "\t\\\n\t%s.h",
+ safe_fprintf(mkf, "\t\\\n\t%s.h",
arg->expr->Identifier);
}
}
}
- fprintf(mkf, "\n\n");
+ safe_fprintf(mkf, "\n\n");
/*
* Move necessary skeleton files and add them to Makefile.am.sample.
@@ -112,7 +129,7 @@ asn1c_save_compiled_output(arg_t *arg, const char *datadir,
strcpy(dir_end, fname);
if(asn1c_copy_over(arg, buf) == -1) {
- fprintf(mkf, ">>>ABORTED<<<");
+ safe_fprintf(mkf, ">>>ABORTED<<<");
fclose(mkf);
return -1;
}
@@ -129,18 +146,18 @@ asn1c_save_compiled_output(arg_t *arg, const char *datadir,
what_kind = "HEADERS";
else
what_kind = "SOURCES";
- fprintf(mkf, "ASN_%s_%s+=%s\n",
+ safe_fprintf(mkf, "ASN_%s_%s+=%s\n",
what_class, what_kind, fname);
}
}
if(need_to_generate_pdu_collection(arg)) {
- fprintf(mkf, "ASN_CONVERTER_SOURCES+=pdu_collection.c\n");
+ safe_fprintf(mkf, "ASN_CONVERTER_SOURCES+=pdu_collection.c\n");
if(generate_pdu_collection_file(arg))
return -1;
}
- fprintf(mkf, "\n\n"
+ safe_fprintf(mkf, "\n\n"
"lib_LTLIBRARIES=libsomething.la\n"
"libsomething_la_SOURCES="
"$(ASN_MODULE_SOURCES) $(ASN_MODULE_HEADERS)\n"
@@ -170,11 +187,11 @@ asn1c_save_compiled_output(arg_t *arg, const char *datadir,
);
for(i = 0; i < argc; i++)
- fprintf(mkf, "%s%s", i ? " " : "", argv[i]);
- fprintf(mkf, "\n\n");
+ safe_fprintf(mkf, "%s%s", i ? " " : "", argv[i]);
+ safe_fprintf(mkf, "\n\n");
fclose(mkf);
- fprintf(stderr, "Generated Makefile.am.sample\n");
+ safe_fprintf(stderr, "Generated Makefile.am.sample\n");
return 0;
}
@@ -207,7 +224,7 @@ asn1c_print_streams(arg_t *arg) {
expr->Identifier);
TQ_FOR(ot, &(cs->destination[i].chunks), next) {
- fwrite(ot->buf, ot->len, 1, stdout);
+ safe_fwrite(ot->buf, ot->len, 1, stdout);
}
}
@@ -227,7 +244,7 @@ asn1c_save_streams(arg_t *arg, asn1c_fdeps_t *deps, int optc, char **argv) {
const char *h_retained = "";
if(cs == NULL) {
- fprintf(stderr, "Cannot compile %s at line %d\n",
+ safe_fprintf(stderr, "Cannot compile %s at line %d\n",
expr->Identifier, expr->_lineno);
return -1;
}
@@ -244,49 +261,49 @@ asn1c_save_streams(arg_t *arg, asn1c_fdeps_t *deps, int optc, char **argv) {
generate_preamble(arg, fp_h, optc, argv);
header_id = asn1c_make_identifier(0, expr, NULL);
- fprintf(fp_h,
+ safe_fprintf(fp_h,
"#ifndef\t_%s_H_\n"
"#define\t_%s_H_\n"
"\n", header_id, header_id);
- fprintf(fp_h, "\n");
+ safe_fprintf(fp_h, "\n");
HINCLUDE("asn_application.h");
#define SAVE_STREAM(fp, idx, msg, actdep) do { \
if(TQ_FIRST(&(cs->destination[idx].chunks)) && *msg) \
- fprintf(fp, "\n/* %s */\n", msg); \
+ safe_fprintf(fp, "\n/* %s */\n", msg); \
TQ_FOR(ot, &(cs->destination[idx].chunks), next) { \
if(actdep) asn1c_activate_dependency(deps, 0, ot->buf); \
- fwrite(ot->buf, ot->len, 1, fp); \
+ safe_fwrite(ot->buf, ot->len, 1, fp); \
} \
} while(0)
SAVE_STREAM(fp_h, OT_INCLUDES, "Including external dependencies", 1);
- fprintf(fp_h, "\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n");
+ safe_fprintf(fp_h, "\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n");
SAVE_STREAM(fp_h, OT_DEPS, "Dependencies", 0);
SAVE_STREAM(fp_h, OT_FWD_DECLS, "Forward declarations", 0);
SAVE_STREAM(fp_h, OT_TYPE_DECLS, expr->Identifier, 0);
SAVE_STREAM(fp_h, OT_FUNC_DECLS,"Implementation", 0);
- fprintf(fp_h, "\n#ifdef __cplusplus\n}\n#endif\n");
+ safe_fprintf(fp_h, "\n#ifdef __cplusplus\n}\n#endif\n");
if(!(arg->flags & A1C_NO_INCLUDE_DEPS))
SAVE_STREAM(fp_h, OT_POST_INCLUDE, "Referred external types", 1);
- fprintf(fp_h, "\n#endif\t/* _%s_H_ */\n", header_id);
+ safe_fprintf(fp_h, "\n#endif\t/* _%s_H_ */\n", header_id);
HINCLUDE("asn_internal.h");
- fprintf(fp_c, "#include \"%s.h\"\n\n", expr->Identifier);
+ safe_fprintf(fp_c, "#include \"%s.h\"\n\n", expr->Identifier);
if(arg->flags & A1C_NO_INCLUDE_DEPS)
SAVE_STREAM(fp_c, OT_POST_INCLUDE, "", 1);
TQ_FOR(ot, &(cs->destination[OT_CTABLES].chunks), next)
- fwrite(ot->buf, ot->len, 1, fp_c);
+ safe_fwrite(ot->buf, ot->len, 1, fp_c);
TQ_FOR(ot, &(cs->destination[OT_CODE].chunks), next)
- fwrite(ot->buf, ot->len, 1, fp_c);
+ safe_fwrite(ot->buf, ot->len, 1, fp_c);
TQ_FOR(ot, &(cs->destination[OT_CTDEFS].chunks), next)
- fwrite(ot->buf, ot->len, 1, fp_c);
+ safe_fwrite(ot->buf, ot->len, 1, fp_c);
TQ_FOR(ot, &(cs->destination[OT_STAT_DEFS].chunks), next)
- fwrite(ot->buf, ot->len, 1, fp_c);
+ safe_fwrite(ot->buf, ot->len, 1, fp_c);
assert(OT_MAX == 11); /* Protection from reckless changes */
@@ -326,16 +343,16 @@ asn1c_save_streams(arg_t *arg, asn1c_fdeps_t *deps, int optc, char **argv) {
free(tmpname_c);
free(tmpname_h);
- fprintf(stderr, "Compiled %s.c%s\n",
+ safe_fprintf(stderr, "Compiled %s.c%s\n",
expr->Identifier, c_retained);
- fprintf(stderr, "Compiled %s.h%s\n",
+ safe_fprintf(stderr, "Compiled %s.h%s\n",
expr->Identifier, h_retained);
return 0;
}
static int
generate_preamble(arg_t *arg, FILE *fp, int optc, char **argv) {
- fprintf(fp,
+ safe_fprintf(fp,
"/*\n"
" * Generated by asn1c-" VERSION " (http://lionet.info/asn1c)\n"
" * From ASN.1 module \"%s\"\n"
@@ -344,12 +361,12 @@ generate_preamble(arg_t *arg, FILE *fp, int optc, char **argv) {
arg->expr->module->source_file_name);
if(optc > 1) {
int i;
- fprintf(fp, " * \t`asn1c ");
+ safe_fprintf(fp, " * \t`asn1c ");
for(i = 1; i < optc; i++)
- fprintf(fp, "%s%s", i>1?" ":"", argv[i]);
- fprintf(fp, "`\n");
+ safe_fprintf(fp, "%s%s", i>1?" ":"", argv[i]);
+ safe_fprintf(fp, "`\n");
}
- fprintf(fp, " */\n\n");
+ safe_fprintf(fp, " */\n\n");
return 0;
}
@@ -410,7 +427,7 @@ real_copy(const char *src, const char *dst) {
while(!feof(fpsrc)) {
len = fread(buf, 1, sizeof(buf), fpsrc);
- if(fwrite(buf, 1, len, fpdst) != len) {
+ if(safe_fwrite(buf, 1, len, fpdst) != len) {
perror(tmpname);
errno = EIO;
retval = -1;
@@ -454,12 +471,12 @@ asn1c_copy_over(arg_t *arg, char *path) {
/*
* Nothing to do.
*/
- fprintf(stderr,
+ safe_fprintf(stderr,
"File %s is already here as %s\n",
path, fname);
return 1;
} else {
- fprintf(stderr,
+ safe_fprintf(stderr,
"Retaining local %s (%s suggested)\n",
fname, path);
return 1;
@@ -468,14 +485,14 @@ asn1c_copy_over(arg_t *arg, char *path) {
/* Ignore this */
return 0;
} else {
- fprintf(stderr, "%s %s -> %s failed: %s\n",
+ safe_fprintf(stderr, "%s %s -> %s failed: %s\n",
use_real_copy ? "Copy" : "Symlink",
path, fname, strerror(errno));
return -1;
}
}
- fprintf(stderr, "%s %s\t-> %s\n",
+ safe_fprintf(stderr, "%s %s\t-> %s\n",
use_real_copy ? "Copied" : "Symlinked", path, fname);
return 1;
@@ -493,45 +510,45 @@ generate_pdu_collection_file(arg_t *arg) {
return -1;
}
- fprintf(fp,
+ safe_fprintf(fp,
"/*\n"
" * Generated by asn1c-" VERSION " (http://lionet.info/asn1c)\n"
" */\n\n");
- fprintf(fp, "struct asn_TYPE_descriptor_s;\t"
+ safe_fprintf(fp, "struct asn_TYPE_descriptor_s;\t"
"/* Forward declaration */\n\n");
TQ_FOR(mod, &(arg->asn->modules), mod_next) {
TQ_FOR(arg->expr, &(mod->members), next) {
if(!include_type_to_pdu_collection(arg))
continue;
- fprintf(fp, "extern struct asn_TYPE_descriptor_s "
+ safe_fprintf(fp, "extern struct asn_TYPE_descriptor_s "
"asn_DEF_%s;\n",
asn1c_make_identifier(0, arg->expr, NULL));
}
}
- fprintf(fp, "\n\n");
- fprintf(fp, "struct asn_TYPE_descriptor_s *asn_pdu_collection[] = {\n");
+ safe_fprintf(fp, "\n\n");
+ safe_fprintf(fp, "struct asn_TYPE_descriptor_s *asn_pdu_collection[] = {\n");
TQ_FOR(mod, &(arg->asn->modules), mod_next) {
int mod_printed = 0;
TQ_FOR(arg->expr, &(mod->members), next) {
if(!include_type_to_pdu_collection(arg))
continue;
if(!mod_printed++)
- fprintf(fp, "\t/* From module %s in %s */\n",
+ safe_fprintf(fp, "\t/* From module %s in %s */\n",
arg->expr->module->ModuleName,
arg->expr->module->source_file_name);
- fprintf(fp, "\t&asn_DEF_%s,\t\n",
+ safe_fprintf(fp, "\t&asn_DEF_%s,\t\n",
asn1c_make_identifier(0, arg->expr, NULL));
}
}
- fprintf(fp, "\t0\n};\n\n");
+ safe_fprintf(fp, "\t0\n};\n\n");
pdu_collection_print_unused_types(arg);
fclose(fp);
- fprintf(stderr, "Generated pdu_collection.c\n");
+ safe_fprintf(stderr, "Generated pdu_collection.c\n");
return 0;
}
diff --git a/libasn1print/asn1print.c b/libasn1print/asn1print.c
index 3a9cb024..0cbf9646 100644
--- a/libasn1print/asn1print.c
+++ b/libasn1print/asn1print.c
@@ -1,4 +1,5 @@
#include <stdio.h>
+#include <stdarg.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
@@ -12,9 +13,9 @@
#define INDENT(fmt, args...) do { \
if(!(flags & APF_NOINDENT)) { \
int tmp_i = level; \
- while(tmp_i--) printf(" "); \
+ while(tmp_i--) safe_printf(" "); \
} \
- printf(fmt, ##args); \
+ safe_printf(fmt, ##args); \
} while(0)
static int asn1print_module(asn1p_t *asn, asn1p_module_t *mod, enum asn1print_flags flags);
@@ -28,6 +29,23 @@ static int asn1print_value(asn1p_value_t *val, enum asn1print_flags flags);
static int asn1print_expr(asn1p_t *asn, asn1p_module_t *mod, asn1p_expr_t *tc, enum asn1print_flags flags, int level);
static int asn1print_expr_dtd(asn1p_t *asn, asn1p_module_t *mod, asn1p_expr_t *tc, enum asn1print_flags flags, int level);
+/* Check printf's error code, to be pedantic. */
+static int safe_printf(const char *fmt, ...) {
+ va_list ap;
+ va_start(ap, fmt);
+ int ret = vprintf(fmt, ap);
+ assert(ret >= 0);
+ va_end(ap);
+ return ret;
+}
+
+/* Pedantically check fwrite's return value. */
+static size_t safe_fwrite(const void *ptr, size_t size, size_t nitems, FILE *stream) {
+ size_t ret = fwrite(ptr, 1, size * nitems, stream);
+ assert(ret == size * nitems);
+ return ret;
+}
+
/*
* Print the contents of the parsed ASN tree.
*/
@@ -42,19 +60,19 @@ asn1print(asn1p_t *asn, enum asn1print_flags flags) {
}
if(flags & APF_PRINT_XML_DTD)
- printf("<!-- XML DTD generated by asn1c-" VERSION " -->\n\n");
+ safe_printf("<!-- XML DTD generated by asn1c-" VERSION " -->\n\n");
TQ_FOR(mod, &(asn->modules), mod_next) {
if(mod->_tags & MT_STANDARD_MODULE)
return 0; /* Ignore modules imported from skeletons */
- if(modno++) printf("\n");
+ if(modno++) safe_printf("\n");
asn1print_module(asn, mod, flags);
}
if(flags & APF_PRINT_XML_DTD) {
/* Values for BOOLEAN */
- printf("<!ELEMENT true EMPTY>\n");
- printf("<!ELEMENT false EMPTY>\n");
+ safe_printf("<!ELEMENT true EMPTY>\n");
+ safe_printf("<!ELEMENT false EMPTY>\n");
}
return 0;
@@ -65,19 +83,19 @@ asn1print_module(asn1p_t *asn, asn1p_module_t *mod, enum asn1print_flags flags)
asn1p_expr_t *tc;
if(flags & APF_PRINT_XML_DTD)
- printf("<!-- ASN.1 module\n");
+ safe_printf("<!-- ASN.1 module\n");
- printf("%s ", mod->ModuleName);
+ safe_printf("%s ", mod->ModuleName);
if(mod->module_oid) {
asn1print_oid(strlen(mod->ModuleName), mod->module_oid, flags);
- printf("\n");
+ safe_printf("\n");
}
if(flags & APF_PRINT_XML_DTD) {
if(mod->source_file_name
&& strcmp(mod->source_file_name, "-"))
- printf("found in %s", mod->source_file_name);
- printf(" -->\n\n");
+ safe_printf("found in %s", mod->source_file_name);
+ safe_printf(" -->\n\n");
TQ_FOR(tc, &(mod->members), next) {
asn1print_expr_dtd(asn, mod, tc, flags, 0);
@@ -86,33 +104,33 @@ asn1print_module(asn1p_t *asn, asn1p_module_t *mod, enum asn1print_flags flags)
return 0;
}
- printf("DEFINITIONS");
+ safe_printf("DEFINITIONS");
if(mod->module_flags & MSF_TAG_INSTRUCTIONS)
- printf(" TAG INSTRUCTIONS");
+ safe_printf(" TAG INSTRUCTIONS");
if(mod->module_flags & MSF_XER_INSTRUCTIONS)
- printf(" XER INSTRUCTIONS");
+ safe_printf(" XER INSTRUCTIONS");
if(mod->module_flags & MSF_EXPLICIT_TAGS)
- printf(" EXPLICIT TAGS");
+ safe_printf(" EXPLICIT TAGS");
if(mod->module_flags & MSF_IMPLICIT_TAGS)
- printf(" IMPLICIT TAGS");
+ safe_printf(" IMPLICIT TAGS");
if(mod->module_flags & MSF_AUTOMATIC_TAGS)
- printf(" AUTOMATIC TAGS");
+ safe_printf(" AUTOMATIC TAGS");
if(mod->module_flags & MSF_EXTENSIBILITY_IMPLIED)
- printf(" EXTENSIBILITY IMPLIED");
+ safe_printf(" EXTENSIBILITY IMPLIED");
- printf(" ::=\n");
- printf("BEGIN\n\n");
+ safe_printf(" ::=\n");
+ safe_printf("BEGIN\n\n");
TQ_FOR(tc, &(mod->members), next) {
asn1print_expr(asn, mod, tc, flags, 0);
if(flags & APF_PRINT_CONSTRAINTS)
- printf("\n");
+ safe_printf("\n");
else
- printf("\n\n");
+ safe_printf("\n\n");
}
- printf("END\n");
+ safe_printf("END\n");
return 0;
}
@@ -124,28 +142,28 @@ asn1print_oid(int prior_len, asn1p_oid_t *oid, enum asn1print_flags flags) {
(void)flags; /* Unused argument */
- printf("{");
+ safe_printf("{");
for(ac = 0; ac < oid->arcs_count; ac++) {
const char *arcname = oid->arcs[ac].name;
if(accum + strlen(arcname ? arcname : "") > 72) {
- printf("\n\t");
+ safe_printf("\n\t");
accum = 8;
} else {
- accum += printf(" ");
+ accum += safe_printf(" ");
}
if(arcname) {
- accum += printf("%s", arcname);
+ accum += safe_printf("%s", arcname);
if(oid->arcs[ac].number >= 0) {
- accum += printf("(%" PRIdASN ")",
+ accum += safe_printf("(%" PRIdASN ")",
oid->arcs[ac].number);
}
} else {
- accum += printf("%" PRIdASN, oid->arcs[ac].number);
+ accum += safe_printf("%" PRIdASN, oid->arcs[ac].number);
}
}
- printf(" }");
+ safe_printf(" }");
return 0;
}
@@ -157,8 +175,8 @@ asn1print_ref(asn1p_ref_t *ref, enum asn1print_flags flags) {
(void)flags; /* Unused argument */
for(cc = 0; cc < ref->comp_count; cc++) {
- if(cc) printf(".");
- printf("%s", ref->components[cc].name);
+ if(cc) safe_printf(".");
+ safe_printf("%s", ref->components[cc].name);
}
return 0;
@@ -170,7 +188,7 @@ asn1print_tag(asn1p_expr_t *tc, enum asn1print_flags flags) {
(void)flags; /* Unused argument */
- printf("%s", asn1p_tag2string(tag, 0));
+ safe_printf("%s", asn1p_tag2string(tag, 0));
return 0;
}
@@ -185,10 +203,10 @@ asn1print_value(asn1p_value_t *val, enum asn1print_flags flags) {
case ATV_NOVALUE:
break;
case ATV_NULL:
- printf("NULL");
+ safe_printf("NULL");
return 0;
case ATV_REAL:
- printf("%f", val->value.v_double);
+ safe_printf("%f", val->value.v_double);
return 0;
case ATV_TYPE:
asn1print_expr(val->value.v_type->module->asn1p,
@@ -196,19 +214,19 @@ asn1print_value(asn1p_value_t *val, enum asn1print_flags flags) {
val->value.v_type, flags, 0);
return 0;
case ATV_INTEGER:
- printf("%" PRIdASN, val->value.v_integer);
+ safe_printf("%" PRIdASN, val->value.v_integer);
return 0;
- case ATV_MIN: printf("MIN"); return 0;
- case ATV_MAX: printf("MAX"); return 0;
- case ATV_FALSE: printf("FALSE"); return 0;
- case ATV_TRUE: printf("TRUE"); return 0;
+ case ATV_MIN: safe_printf("MIN"); return 0;
+ case ATV_MAX: safe_printf("MAX"); return 0;
+ case ATV_FALSE: safe_printf("FALSE"); return 0;
+ case ATV_TRUE: safe_printf("TRUE"); return 0;
case ATV_TUPLE:
- printf("{%d, %d}",
+ safe_printf("{%d, %d}",
(int)(val->value.v_integer >> 4),
(int)(val->value.v_integer & 0x0f));
return 0;
case ATV_QUADRUPLE:
- printf("{%d, %d, %d, %d}",
+ safe_printf("{%d, %d, %d, %d}",
(int)((val->value.v_integer >> 24) & 0xff),
(int)((val->value.v_integer >> 16) & 0xff),
(int)((val->value.v_integer >> 8) & 0xff),
@@ -244,21 +262,21 @@ asn1print_value(asn1p_value_t *val, enum asn1print_flags flags) {
bitvector = val->value.binary_vector.bits;
bits = val->value.binary_vector.size_in_bits;
- printf("'");
+ safe_printf("'");
if(bits%8) {
for(i = 0; i < bits; i++) {
uint8_t uc;
uc = bitvector[i>>3];
putchar(((uc >> (7-(i%8)))&1)?'1':'0');
}
- printf("'B");
+ safe_printf("'B");
} else {
char hextable[16] = "0123456789ABCDEF";
for(i = 0; i < (bits>>3); i++) {
putchar(hextable[bitvector[i] >> 4]);
putchar(hextable[bitvector[i] & 0x0f]);
}
- printf("'H");
+ safe_printf("'H");
}
return 0;
}
@@ -267,7 +285,7 @@ asn1print_value(asn1p_value_t *val, enum asn1print_flags flags) {
case ATV_VALUESET:
return asn1print_constraint(val->value.constraint, flags);
case ATV_CHOICE_IDENTIFIER:
- printf("%s: ", val->value.choice_identifier.identifier);
+ safe_printf("%s: ", val->value.choice_identifier.identifier);
return asn1print_value(val->value.choice_identifier.value, flags);
}
@@ -283,7 +301,7 @@ asn1print_constraint(asn1p_constraint_t *ct, enum asn1print_flags flags) {
if(ct == 0) return 0;
if(ct->type == ACT_CA_SET)
- printf("(");
+ safe_printf("(");
switch(ct->type) {
case ACT_EL_TYPE:
@@ -298,76 +316,76 @@ asn1print_constraint(asn1p_constraint_t *ct, enum asn1print_flags flags) {
case ACT_EL_ULRANGE:
asn1print_value(ct->range_start, flags);
switch(ct->type) {
- case ACT_EL_RANGE: printf(".."); break;
- case ACT_EL_LLRANGE: printf("<.."); break;
- case ACT_EL_RLRANGE: printf("..<"); break;
- case ACT_EL_ULRANGE: printf("<..<"); break;
- default: printf("?..?"); break;
+ case ACT_EL_RANGE: safe_printf(".."); break;
+ case ACT_EL_LLRANGE: safe_printf("<.."); break;
+ case ACT_EL_RLRANGE: safe_printf("..<"); break;
+ case ACT_EL_ULRANGE: safe_printf("<..<"); break;
+ default: safe_printf("?..?"); break;
}
asn1print_value(ct->range_stop, flags);
break;
case ACT_EL_EXT:
- printf("...");
+ safe_printf("...");
break;
case ACT_CT_SIZE:
case ACT_CT_FROM:
switch(ct->type) {
- case ACT_CT_SIZE: printf("SIZE("); break;
- case ACT_CT_FROM: printf("FROM("); break;
- default: printf("??? ("); break;
+ case ACT_CT_SIZE: safe_printf("SIZE("); break;
+ case ACT_CT_FROM: safe_printf("FROM("); break;
+ default: safe_printf("??? ("); break;
}
assert(ct->el_count != 0);
assert(ct->el_count == 1);
asn1print_constraint(ct->elements[0], flags);
- printf(")");
+ safe_printf(")");
break;
case ACT_CT_WCOMP:
assert(ct->el_count != 0);
assert(ct->el_count == 1);
- printf("WITH COMPONENT (");
+ safe_printf("WITH COMPONENT (");
asn1print_constraint(ct->elements[0], flags);
- printf(")");
+ safe_printf(")");
break;
case ACT_CT_WCOMPS: {
unsigned int i;
- printf("WITH COMPONENTS { ");
+ safe_printf("WITH COMPONENTS { ");
for(i = 0; i < ct->el_count; i++) {
asn1p_constraint_t *cel = ct->elements[i];
- if(i) printf(", ");
- fwrite(cel->value->value.string.buf,
+ if(i) safe_printf(", ");
+ safe_fwrite(cel->value->value.string.buf,
1, cel->value->value.string.size,
stdout);
if(cel->el_count) {
assert(cel->el_count == 1);
- printf(" ");
+ safe_printf(" ");
asn1print_constraint(cel->elements[0],
flags);
}
switch(cel->presence) {
case ACPRES_DEFAULT: break;
- case ACPRES_PRESENT: printf(" PRESENT"); break;
- case ACPRES_ABSENT: printf(" ABSENT"); break;
- case ACPRES_OPTIONAL: printf(" OPTIONAL");break;
+ case ACPRES_PRESENT: safe_printf(" PRESENT"); break;
+ case ACPRES_ABSENT: safe_printf(" ABSENT"); break;
+ case ACPRES_OPTIONAL: safe_printf(" OPTIONAL");break;
}
}
- printf(" }");
+ safe_printf(" }");
}
break;
case ACT_CT_CTDBY:
- printf("CONSTRAINED BY ");
+ safe_printf("CONSTRAINED BY ");
assert(ct->value->type == ATV_UNPARSED);
- fwrite(ct->value->value.string.buf,
+ safe_fwrite(ct->value->value.string.buf,
1, ct->value->value.string.size, stdout);
break;
case ACT_CT_CTNG:
- printf("CONTAINING ");
+ safe_printf("CONTAINING ");
asn1print_expr(ct->value->value.v_type->module->asn1p,
ct->value->value.v_type->module,
ct->value->value.v_type,
flags, 1);
break;
case ACT_CT_PATTERN:
- printf("PATTERN ");
+ safe_printf("PATTERN ");
asn1print_value(ct->value, flags);
break;
case ACT_CA_SET: symno++;
@@ -393,7 +411,7 @@ asn1print_constraint(asn1p_constraint_t *ct, enum asn1print_flags flags) {
break;
case ACT_CA_AEX:
assert(ct->el_count == 1);
- printf("ALL EXCEPT ");
+ safe_printf("ALL EXCEPT ");
asn1print_constraint(ct->elements[0], flags);
break;
case ACT_INVALID:
@@ -402,7 +420,7 @@ asn1print_constraint(asn1p_constraint_t *ct, enum asn1print_flags flags) {
}
if(ct->type == ACT_CA_SET)
- printf(")");
+ safe_printf(")");
return 0;
}
@@ -411,16 +429,16 @@ static int
asn1print_params(asn1p_paramlist_t *pl, enum asn1print_flags flags) {
if(pl) {
int i;
- printf("{");
+ safe_printf("{");
for(i = 0; i < pl->params_count; i++) {
- if(i) printf(", ");
+ if(i) safe_printf(", ");
if(pl->params[i].governor) {
asn1print_ref(pl->params[i].governor, flags);
- printf(":");
+ safe_printf(":");
}
- printf("%s", pl->params[i].argument);
+ safe_printf("%s", pl->params[i].argument);
}
- printf("}");
+ safe_printf("}");
}
return 0;
@@ -435,12 +453,12 @@ asn1print_with_syntax(asn1p_wsyntx_t *wx, enum asn1print_flags flags) {
case WC_LITERAL:
case WC_WHITESPACE:
case WC_FIELD:
- printf("%s", wc->content.token);
+ safe_printf("%s", wc->content.token);
break;
case WC_OPTIONALGROUP:
- printf("[");
+ safe_printf("[");
asn1print_with_syntax(wc->content.syntax,flags);
- printf("]");
+ safe_printf("]");
break;
}
}
@@ -452,13 +470,13 @@ asn1print_with_syntax(asn1p_wsyntx_t *wx, enum asn1print_flags flags) {
static int
asn1print_crange_value(asn1cnst_edge_t *edge, int as_char) {
switch(edge->type) {
- case ARE_MIN: printf("MIN"); break;
- case ARE_MAX: printf("MAX"); break;
+ case ARE_MIN: safe_printf("MIN"); break;
+ case ARE_MAX: safe_printf("MAX"); break;
case ARE_VALUE:
if(as_char) {
- printf("\"%c\"", (unsigned char)edge->value);
+ safe_printf("\"%c\"", (unsigned char)edge->value);
} else {
- printf("%" PRIdASN, edge->value);
+ safe_printf("%" PRIdASN, edge->value);
}
}
return 0;
@@ -481,9 +499,9 @@ asn1print_constraint_explain_type(asn1p_expr_type_e expr_type, asn1p_constraint_
}
switch(type) {
- case ACT_CT_FROM: printf("(FROM("); break;
- case ACT_CT_SIZE: printf("(SIZE("); break;
- default: printf("("); break;
+ case ACT_CT_FROM: safe_printf("(FROM("); break;
+ case ACT_CT_SIZE: safe_printf("(SIZE("); break;
+ default: safe_printf("("); break;
}
for(i = -1; i < range->el_count; i++) {
asn1cnst_range_t *r;
@@ -494,21 +512,21 @@ asn1print_constraint_explain_type(asn1p_expr_type_e expr_type, asn1p_constraint_
r = range->elements[i];
}
if(i > 0) {
- printf(" | ");
+ safe_printf(" | ");
}
asn1print_crange_value(&r->left, as_char);
if(r->left.type != r->right.type
|| r->left.value != r->right.value) {
- printf("..");
+ safe_printf("..");
asn1print_crange_value(&r->right, as_char);
}
}
if(range->extensible)
- printf(",...");
- printf(type==ACT_EL_RANGE?")":"))");
+ safe_printf(",...");
+ safe_printf(type==ACT_EL_RANGE?")":"))");
if(range->empty_constraint)
- printf(":Empty!");
+ safe_printf(":Empty!");
asn1constraint_range_free(range);
return 0;
@@ -519,9 +537,9 @@ asn1print_constraint_explain(asn1p_expr_type_e expr_type,
asn1p_constraint_t *ct, int s_PV) {
asn1print_constraint_explain_type(expr_type, ct, ACT_EL_RANGE, s_PV);
- printf(" ");
+ safe_printf(" ");
asn1print_constraint_explain_type(expr_type, ct, ACT_CT_SIZE, s_PV);
- printf(" ");
+ safe_printf(" ");
asn1print_constraint_explain_type(expr_type, ct, ACT_CT_FROM, s_PV);
return 0;
@@ -538,7 +556,7 @@ asn1print_expr(asn1p_t *asn, asn1p_module_t *mod, asn1p_expr_t *tc, enum asn1pri
if((tc->marker.flags & EM_INDIRECT)
&& (tc->marker.flags & EM_OMITABLE) != EM_OMITABLE) {
if((flags & APF_NOINDENT))
- printf(" --<ASN1C.RepresentAsPointer>-- ");
+ safe_printf(" --<ASN1C.RepresentAsPointer>-- ");
else
INDENT("--<ASN1C.RepresentAsPointer>--\n");
}
@@ -557,33 +575,33 @@ asn1print_expr(asn1p_t *asn, asn1p_module_t *mod, asn1p_expr_t *tc, enum asn1pri
&& tc->expr_type != A1TC_EXTENSIBLE) {
if(level) {
if(tc->Identifier && !(flags & APF_NOINDENT))
- printf("\t");
+ safe_printf("\t");
} else {
- printf(" ::=");
+ safe_printf(" ::=");
}
}
if(tc->tag.tag_class) {
- printf(" ");
+ safe_printf(" ");
asn1print_tag(tc, flags);
}
switch(tc->expr_type) {
case A1TC_EXTENSIBLE:
if(tc->value) {
- printf("!");
+ safe_printf("!");
asn1print_value(tc->value, flags);
}
break;
case A1TC_COMPONENTS_OF:
SEQ_OF = 1; /* Equivalent to SET OF for printint purposes */
- printf(" COMPONENTS OF");
+ safe_printf(" COMPONENTS OF");
break;
case A1TC_REFERENCE:
case A1TC_UNIVERVAL:
break;
case A1TC_CLASSDEF:
- printf(" CLASS");
+ safe_printf(" CLASS");
break;
case A1TC_CLASSFIELD_TFS ... A1TC_CLASSFIELD_OSFS:
/* Nothing to print here */
@@ -592,21 +610,21 @@ asn1print_expr(asn1p_t *asn, asn1p_module_t *mod, asn1p_expr_t *tc, enum asn1pri
case ASN_CONSTR_SEQUENCE_OF:
SEQ_OF = 1;
if(tc->expr_type == ASN_CONSTR_SET_OF)
- printf(" SET");
+ safe_printf(" SET");
else
- printf(" SEQUENCE");
+ safe_printf(" SEQUENCE");
if(tc->constraints) {
- printf(" ");
+ safe_printf(" ");
asn1print_constraint(tc->constraints, flags);
}
- printf(" OF");
+ safe_printf(" OF");
break;
case A1TC_VALUESET:
break;
default:
{
char *p = ASN_EXPR_TYPE2STR(tc->expr_type);
- printf(" %s", p?p:"<unknown type!>");
+ safe_printf(" %s", p?p:"<unknown type!>");
}
break;
}
@@ -615,12 +633,12 @@ asn1print_expr(asn1p_t *asn, asn1p_module_t *mod, asn1p_expr_t *tc, enum asn1pri
* Put the name of the referred type.
*/
if(tc->reference) {
- printf(" ");
+ safe_printf(" ");
asn1print_ref(tc->reference, flags);
}
if(tc->meta_type == AMT_VALUESET && level == 0)
- printf(" ::=");
+ safe_printf(" ::=");
/*
* Display the descendants (children) of the current type.
@@ -637,15 +655,15 @@ asn1print_expr(asn1p_t *asn, asn1p_module_t *mod, asn1p_expr_t *tc, enum asn1pri
if(put_braces) {
if(flags & APF_NOINDENT) {
- printf("{");
+ safe_printf("{");
if(!TQ_FIRST(&tc->members))
- printf("}");
+ safe_printf("}");
} else {
- printf(" {");
+ safe_printf(" {");
if(TQ_FIRST(&tc->members))
- printf("\n");
+ safe_printf("\n");
else
- printf(" }");
+ safe_printf(" }");
}
}
@@ -655,14 +673,14 @@ asn1print_expr(asn1p_t *asn, asn1p_module_t *mod, asn1p_expr_t *tc, enum asn1pri
*/
asn1print_expr(asn, mod, se, flags, level + 1);
if((se->marker.flags & EM_DEFAULT) == EM_DEFAULT) {
- printf(" DEFAULT ");
+ safe_printf(" DEFAULT ");
asn1print_value(se->marker.default_value, flags);
} else if((se->marker.flags & EM_OPTIONAL)
== EM_OPTIONAL) {
- printf(" OPTIONAL");
+ safe_printf(" OPTIONAL");
}
if(TQ_NEXT(se, next)) {
- printf(",");
+ safe_printf(",");
if(!(flags & APF_NOINDENT))
INDENT("\n");
}
@@ -670,51 +688,51 @@ asn1print_expr(asn1p_t *asn, asn1p_module_t *mod, asn1p_expr_t *tc, enum asn1pri
if(put_braces && TQ_FIRST(&tc->members)) {
if(!(flags & APF_NOINDENT))
- printf("\n");
+ safe_printf("\n");
INDENT("}");
}
}
if(tc->with_syntax) {
- printf(" WITH SYNTAX {");
+ safe_printf(" WITH SYNTAX {");
asn1print_with_syntax(tc->with_syntax, flags);
- printf("}\n");
+ safe_printf("}\n");
}
/* Right hand specialization */
if(tc->rhs_pspecs) {
asn1p_expr_t *se;
- printf("{");
+ safe_printf("{");
TQ_FOR(se, &(tc->rhs_pspecs->members), next) {
asn1print_expr(asn, mod, se, flags, level + 1);
- if(TQ_NEXT(se, next)) printf(", ");
+ if(TQ_NEXT(se, next)) safe_printf(", ");
}
- printf("}");
+ safe_printf("}");
}
if(!SEQ_OF && tc->constraints) {
- printf(" ");
+ safe_printf(" ");
if(tc->meta_type == AMT_VALUESET)
- printf("{");
+ safe_printf("{");
asn1print_constraint(tc->constraints, flags);
if(tc->meta_type == AMT_VALUESET)
- printf("}");
+ safe_printf("}");
}
if(tc->unique) {
- printf(" UNIQUE");
+ safe_printf(" UNIQUE");
}
if(tc->meta_type == AMT_VALUE
&& tc->expr_type != A1TC_EXTENSIBLE) {
if(tc->expr_type == A1TC_UNIVERVAL) {
if(tc->value) {
- printf("(");
+ safe_printf("(");
asn1print_value(tc->value, flags);
- printf(")");
+ safe_printf(")");
}
} else {
- if(level == 0) printf(" ::= ");
+ if(level == 0) safe_printf(" ::= ");
asn1print_value(tc->value, flags);
}
}
@@ -727,56 +745,56 @@ asn1print_expr(asn1p_t *asn, asn1p_module_t *mod, asn1p_expr_t *tc, enum asn1pri
asn1p_expr_t *top_parent;
if(tc->combined_constraints) {
- printf("\n-- Combined constraints: ");
+ safe_printf("\n-- Combined constraints: ");
asn1print_constraint(tc->combined_constraints, flags);
}
top_parent = asn1f_find_terminal_type_ex(asn, tc);
if(top_parent) {
- printf("\n-- Practical constraints (%s): ",
+ safe_printf("\n-- Practical constraints (%s): ",
top_parent->Identifier);
asn1print_constraint_explain(top_parent->expr_type,
tc->combined_constraints, 0);
- printf("\n-- PER-visible constraints (%s): ",
+ safe_printf("\n-- PER-visible constraints (%s): ",
top_parent->Identifier);
asn1print_constraint_explain(top_parent->expr_type,
tc->combined_constraints, 1);
}
- printf("\n");
+ safe_printf("\n");
}
if(flags & APF_PRINT_CLASS_MATRIX
&& tc->expr_type == A1TC_CLASSDEF) do {
int r, col, maxidlen;
if(tc->object_class_matrix.rows == 0) {
- printf("\n-- Class matrix is empty");
+ safe_printf("\n-- Class matrix is empty");
break;
}
- printf("\n-- Class matrix has %d entr%s:\n",
+ safe_printf("\n-- Class matrix has %d entr%s:\n",
tc->object_class_matrix.rows,
tc->object_class_matrix.rows==1 ? "y" : "ies");
maxidlen = tc->object_class_matrix.max_identifier_length;
for(r = -1; r < tc->object_class_matrix.rows; r++) {
struct asn1p_ioc_row_s *row;
row = tc->object_class_matrix.row[r<0?0:r];
- if(r < 0) printf("-- %s", r > 9 ? " " : "");
- else printf("-- [%*d]", r > 9 ? 2 : 1, r+1);
+ if(r < 0) safe_printf("-- %s", r > 9 ? " " : "");
+ else safe_printf("-- [%*d]", r > 9 ? 2 : 1, r+1);
for(col = 0; col < row->columns; col++) {
struct asn1p_ioc_cell_s *cell;
cell = &row->column[col];
if(r < 0) {
- printf("[%*s]", maxidlen,
+ safe_printf("[%*s]", maxidlen,
cell->field->Identifier);
continue;
}
if(!cell->value) {
- printf(" %*s ", maxidlen, "<no entry>");
+ safe_printf(" %*s ", maxidlen, "<no entry>");
continue;
}
- printf(" %*s ", maxidlen,
+ safe_printf(" %*s ", maxidlen,
cell->value->Identifier);
}
- printf("\n");
+ safe_printf("\n");
}
} while(0);
@@ -784,21 +802,21 @@ asn1print_expr(asn1p_t *asn, asn1p_module_t *mod, asn1p_expr_t *tc, enum asn1pri
&& tc->lhs_params) do {
int i;
if(tc->specializations.pspecs_count == 0) {
- printf("\n-- No specializations found\n");
+ safe_printf("\n-- No specializations found\n");
break;
}
- printf("\n-- Specializations list has %d entr%s:\n",
+ safe_printf("\n-- Specializations list has %d entr%s:\n",
tc->specializations.pspecs_count,
tc->specializations.pspecs_count == 1 ? "y" : "ies");
for(i = 0; i < tc->specializations.pspecs_count; i++) {
asn1p_expr_t *se;
struct asn1p_pspec_s *pspec;
pspec = &tc->specializations.pspec[i];
- printf("-- ");
+ safe_printf("-- ");
TQ_FOR(se, &(pspec->rhs_pspecs->members), next) {
asn1print_expr(asn, mod, se, flags, level+1);
}
- printf("\n");
+ safe_printf("\n");
}
} while(0);
@@ -831,7 +849,7 @@ asn1print_expr_dtd(asn1p_t *asn, asn1p_module_t *mod, asn1p_expr_t *expr, enum a
if(expr->expr_type == A1TC_REFERENCE) {
se = asn1f_find_terminal_type_ex(asn, expr);
if(!se) {
- printf(" (ANY)");
+ safe_printf(" (ANY)");
return 0;
}
expr = se;
@@ -851,7 +869,7 @@ asn1print_expr_dtd(asn1p_t *asn, asn1p_module_t *mod, asn1p_expr_t *expr, enum a
int extensible = 0;
if(expr->expr_type == ASN_BASIC_BIT_STRING)
dont_involve_children = 1;
- printf(" (");
+ safe_printf(" (");
TQ_FOR(se, &(expr->members), next) {
if(se->expr_type == A1TC_EXTENSIBLE) {
extensible = 1;
@@ -860,43 +878,43 @@ asn1print_expr_dtd(asn1p_t *asn, asn1p_module_t *mod, asn1p_expr_t *expr, enum a
&& se->expr_type == A1TC_REFERENCE) {
asn1print_ref(se->reference, flags);
} else if(se->Identifier) {
- printf("%s", se->Identifier);
+ safe_printf("%s", se->Identifier);
} else {
- printf("ANY");
+ safe_printf("ANY");
}
if(expr->expr_type != ASN_CONSTR_SET
&& expr->expr_type != ASN_CONSTR_CHOICE
&& expr->expr_type != ASN_BASIC_INTEGER
&& expr->expr_type != ASN_BASIC_ENUMERATED) {
if(expr_unordered)
- printf("*");
+ safe_printf("*");
else if(se->marker.flags)
- printf("?");
+ safe_printf("?");
else if(expr->expr_type == ASN_BASIC_BIT_STRING)
- printf("?");
+ safe_printf("?");
}
if(TQ_NEXT(se, next)
&& TQ_NEXT(se, next)->expr_type != A1TC_EXTENSIBLE) {
- printf(expr_unordered?"|":", ");
+ safe_printf(expr_unordered?"|":", ");
}
}
if(extensible) {
- printf(expr_unordered?"|":", ");
- printf("ANY");
+ safe_printf(expr_unordered?"|":", ");
+ safe_printf("ANY");
if(expr->expr_type != ASN_CONSTR_SET
&& expr->expr_type != ASN_CONSTR_CHOICE
&& expr->expr_type != ASN_BASIC_INTEGER
&& expr->expr_type != ASN_BASIC_ENUMERATED)
- printf("*");
+ safe_printf("*");
}
- printf(")");
+ safe_printf(")");
if(expr->expr_type == ASN_CONSTR_SET)
- printf("*");
+ safe_printf("*");
} else switch(expr->expr_type) {
case ASN_BASIC_BOOLEAN:
- printf(" (true|false)");
+ safe_printf(" (true|false)");
break;
case ASN_CONSTR_CHOICE:
case ASN_CONSTR_SET:
@@ -905,10 +923,10 @@ asn1print_expr_dtd(asn1p_t *asn, asn1p_module_t *mod, asn1p_expr_t *expr, enum a
case ASN_CONSTR_SEQUENCE_OF:
case ASN_BASIC_NULL:
case A1TC_UNIVERVAL:
- printf(" EMPTY");
+ safe_printf(" EMPTY");
break;
case ASN_TYPE_ANY:
- printf(" ANY");
+ safe_printf(" ANY");
break;
case ASN_BASIC_BIT_STRING:
case ASN_BASIC_OCTET_STRING:
@@ -919,12 +937,12 @@ asn1print_expr_dtd(asn1p_t *asn, asn1p_module_t *mod, asn1p_expr_t *expr, enum a
case ASN_BASIC_GeneralizedTime:
case ASN_STRING_NumericString:
case ASN_STRING_PrintableString:
- printf(" (#PCDATA)");
+ safe_printf(" (#PCDATA)");
break;
case ASN_STRING_VisibleString:
case ASN_STRING_ISO646String:
/* Entity references, but not XML elements may be present */
- printf(" (#PCDATA)");
+ safe_printf(" (#PCDATA)");
break;
case ASN_BASIC_REAL: /* e.g. <MINUS-INFINITY/> */
case ASN_BASIC_ENUMERATED: /* e.g. <enumIdentifier1/> */
@@ -933,9 +951,9 @@ asn1print_expr_dtd(asn1p_t *asn, asn1p_module_t *mod, asn1p_expr_t *expr, enum a
* XML elements are allowed.
* For example, a UTF8String may contain "<bel/>".
*/
- printf(" ANY");
+ safe_printf(" ANY");
}
- printf(">\n");
+ safe_printf(">\n");
/*
* Display the descendants (children) of the current type.