vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1 | /*- |
| 2 | * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved. |
| 3 | * Redistribution and modifications are permitted subject to BSD license. |
| 4 | */ |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 5 | #include <asn_internal.h> |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 6 | #include <constr_TYPE.h> |
| 7 | #include <errno.h> |
| 8 | |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 9 | /* |
| 10 | * Version of the ASN.1 infrastructure shipped with compiler. |
| 11 | */ |
| 12 | int get_asn1c_environment_version() { return ASN1C_ENVIRONMENT_VERSION; } |
| 13 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 14 | static asn_app_consume_bytes_f _print2fp; |
| 15 | |
| 16 | /* |
| 17 | * Return the outmost tag of the type. |
| 18 | */ |
| 19 | ber_tlv_tag_t |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 20 | asn_TYPE_outmost_tag(asn_TYPE_descriptor_t *type_descriptor, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 21 | const void *struct_ptr, int tag_mode, ber_tlv_tag_t tag) { |
| 22 | |
| 23 | if(tag_mode) |
| 24 | return tag; |
| 25 | |
| 26 | if(type_descriptor->tags_count) |
| 27 | return type_descriptor->tags[0]; |
| 28 | |
| 29 | return type_descriptor->outmost_tag(type_descriptor, struct_ptr, 0, 0); |
| 30 | } |
| 31 | |
| 32 | /* |
| 33 | * Print the target language's structure in human readable form. |
| 34 | */ |
| 35 | int |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 36 | asn_fprint(FILE *stream, asn_TYPE_descriptor_t *td, const void *struct_ptr) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 37 | if(!stream) stream = stdout; |
| 38 | if(!td || !struct_ptr) { |
| 39 | errno = EINVAL; |
| 40 | return -1; |
| 41 | } |
| 42 | |
| 43 | /* Invoke type-specific printer */ |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 44 | if(td->print_struct(td, struct_ptr, 1, _print2fp, stream)) |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 45 | return -1; |
| 46 | |
| 47 | /* Terminate the output */ |
| 48 | if(_print2fp("\n", 1, stream)) |
| 49 | return -1; |
| 50 | |
| 51 | return fflush(stream); |
| 52 | } |
| 53 | |
| 54 | /* Dump the data into the specified stdio stream */ |
| 55 | static int |
| 56 | _print2fp(const void *buffer, size_t size, void *app_key) { |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 57 | FILE *stream = (FILE *)app_key; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 58 | |
| 59 | if(fwrite(buffer, 1, size, stream) != size) |
| 60 | return -1; |
| 61 | |
| 62 | return 0; |
| 63 | } |
| 64 | |
vlm | f5202a5 | 2004-08-11 08:32:04 +0000 | [diff] [blame] | 65 | |
| 66 | /* |
| 67 | * Some compilers do not support variable args macros. |
| 68 | * This function is a replacement of ASN_DEBUG() macro. |
| 69 | */ |
vlm | cd8575c | 2004-08-11 11:51:15 +0000 | [diff] [blame] | 70 | void ASN_DEBUG_f(const char *fmt, ...); |
vlm | f5202a5 | 2004-08-11 08:32:04 +0000 | [diff] [blame] | 71 | void ASN_DEBUG_f(const char *fmt, ...) { |
| 72 | va_list ap; |
| 73 | va_start(ap, fmt); |
| 74 | vfprintf(stderr, fmt, ap); |
| 75 | fprintf(stderr, "\n"); |
| 76 | va_end(ap); |
| 77 | } |