Moving in e2sim originally from it/test/simulators
[sim/e2-interface.git] / e2sim / ASN1c / constr_TYPE.c
1 /*****************************************************************************
2 #                                                                            *
3 # Copyright 2019 AT&T Intellectual Property                                  *
4 #                                                                            *
5 # Licensed under the Apache License, Version 2.0 (the "License");            *
6 # you may not use this file except in compliance with the License.           *
7 # You may obtain a copy of the License at                                    *
8 #                                                                            *
9 #      http://www.apache.org/licenses/LICENSE-2.0                            *
10 #                                                                            *
11 # Unless required by applicable law or agreed to in writing, software        *
12 # distributed under the License is distributed on an "AS IS" BASIS,          *
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   *
14 # See the License for the specific language governing permissions and        *
15 # limitations under the License.                                             *
16 #                                                                            *
17 ******************************************************************************/
18
19 /*-
20  * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
21  * Redistribution and modifications are permitted subject to BSD license.
22  */
23 #include <asn_internal.h>
24 #include <constr_TYPE.h>
25 #include <errno.h>
26
27 /*
28  * Version of the ASN.1 infrastructure shipped with compiler.
29  */
30 int get_asn1c_environment_version() { return ASN1C_ENVIRONMENT_VERSION; }
31
32 static asn_app_consume_bytes_f _print2fp;
33
34 /*
35  * Return the outmost tag of the type.
36  */
37 ber_tlv_tag_t
38 asn_TYPE_outmost_tag(const asn_TYPE_descriptor_t *type_descriptor,
39                 const void *struct_ptr, int tag_mode, ber_tlv_tag_t tag) {
40
41         if(tag_mode)
42                 return tag;
43
44         if(type_descriptor->tags_count)
45                 return type_descriptor->tags[0];
46
47         return type_descriptor->op->outmost_tag(type_descriptor, struct_ptr, 0, 0);
48 }
49
50 /*
51  * Print the target language's structure in human readable form.
52  */
53 int
54 asn_fprint(FILE *stream, const asn_TYPE_descriptor_t *td,
55            const void *struct_ptr) {
56     if(!stream) stream = stdout;
57     if(!td || !struct_ptr) {
58         errno = EINVAL;
59         return -1;
60         }
61
62         /* Invoke type-specific printer */
63     if(td->op->print_struct(td, struct_ptr, 1, _print2fp, stream)) {
64         return -1;
65     }
66
67     /* Terminate the output */
68     if(_print2fp("\n", 1, stream)) {
69         return -1;
70     }
71
72     return fflush(stream);
73 }
74
75 /* Dump the data into the specified stdio stream */
76 static int
77 _print2fp(const void *buffer, size_t size, void *app_key) {
78         FILE *stream = (FILE *)app_key;
79
80         if(fwrite(buffer, 1, size, stream) != size)
81                 return -1;
82
83         return 0;
84 }
85
86
87 /*
88  * Some compilers do not support variable args macros.
89  * This function is a replacement of ASN_DEBUG() macro.
90  */
91 void ASN_DEBUG_f(const char *fmt, ...);
92 void ASN_DEBUG_f(const char *fmt, ...) {
93         va_list ap;
94         va_start(ap, fmt);
95         vfprintf(stderr, fmt, ap);
96         fprintf(stderr, "\n");
97         va_end(ap);
98 }