1 /*------------------------------------------------------------------------------
2 *
3 * Copyright (c) 2011-2021, EURid vzw. All rights reserved.
4 * The YADIFA TM software product is provided under the BSD 3-clause license:
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * * Neither the name of EURid nor the names of its contributors may be
16 * used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 *
31 *------------------------------------------------------------------------------
32 *
33 */
34
35 /** @ingroup dnscore
36 *
37 */
38 /*----------------------------------------------------------------------------*/
39 #ifndef _OUTPUT_STREAM_H
40 #define _OUTPUT_STREAM_H
41
42 #include <dnscore/sys_types.h>
43 #include <dnscore/dnsname.h>
44
45 #ifdef __cplusplus
46 extern "C" {
47 #endif
48
49 typedef struct output_stream output_stream;
50
51
52 typedef ya_result output_stream_write_method(output_stream* stream, const u8* buffer, u32 len);
53 typedef ya_result output_stream_flush_method(output_stream* stream);
54
55 typedef void output_stream_close_method(output_stream* stream);
56
57 typedef ya_result output_stream_skip_method(output_stream* stream, u32 byte_count);
58
59 typedef struct output_stream_vtbl output_stream_vtbl;
60
61 struct output_stream_vtbl
62 {
63 output_stream_write_method* write;
64 output_stream_flush_method* flush;
65 output_stream_close_method* close;
66
67 const char* __class__; /* MUST BE A UNIQUE POINTER, ie: One defined in the class's .c file
68 The name should be unique in order to avoid compiler tricks
69 */
70
71 /* Add your inheritable methods here */
72 };
73
74 struct output_stream
75 {
76 void* data;
77 const output_stream_vtbl* vtbl;
78 };
79
80 #define output_stream_class(os__) ((os__)->vtbl)
81 #define output_stream_class_name(os__) ((os__)->vtbl->__class__)
82 #define output_stream_write(os__,buffer__,len__) (os__)->vtbl->write((os__),(const u8*)(buffer__),(len__))
83 #define output_stream_flush(os__) (os__)->vtbl->flush(os__)
84 #define output_stream_close(os__) (os__)->vtbl->close(os__)
85 #define output_stream_skip(os__,len__) (os__)->vtbl->skip((os__),(len__))
86 #define output_stream_valid(os__) ((os__)->vtbl != NULL)
87
88 ya_result output_stream_write_nu32(output_stream* os, u32 value);
89 ya_result output_stream_write_nu16(output_stream* os, u16 value);
90
91 /*
92 * ya_result output_stream_write_u8(output_stream* os, u8 value);
93 */
94
95 static inline ya_result
output_stream_write_u8(output_stream * os,u8 value)96 output_stream_write_u8(output_stream* os, u8 value)
97 {
98 return output_stream_write(os, &value, 1);
99 }
100
output_stream_write_u16(output_stream * os,u16 value)101 static inline ya_result output_stream_write_u16(output_stream* os, u16 value)
102 {
103 return output_stream_write(os, (u8*) & value, 2);
104 }
105
output_stream_write_u32(output_stream * os,u32 value)106 static inline ya_result output_stream_write_u32(output_stream* os, u32 value)
107 {
108 return output_stream_write(os, (u8*) & value, 4);
109 }
110 /*
111 * PACKED unsigned 32 bits
112 *
113 * The integer is divided into 7 bits packets (lsb -> msb)
114 * The 8th bit is set until the end is reached
115 *
116 * [ 0.. 127] => [ 0x00 .. 0x7f]
117 * [128..16384] => [0x80 0x01 .. 0xff 0x7f]
118 *
119 */
120
121 ya_result output_stream_write_pu32(output_stream* os, u32 value);
122
123 ya_result output_stream_write_pu64(output_stream* os, u64 value);
124
125 // wire
126 ya_result output_stream_write_dnsname(output_stream* os, const u8* name);
127
128 // ascii
129 ya_result output_stream_write_dnsname_text(output_stream* os, const u8 *name);
130
131 ya_result output_stream_write_dnslabel_text_escaped(output_stream* os, const u8 *label);
132 ya_result output_stream_write_dnsname_text_escaped(output_stream* os, const u8 *name);
133
134 ya_result output_stream_write_dnslabel_vector(output_stream* os, dnslabel_vector_reference labels, s32 top);
135
136 ya_result output_stream_write_dnslabel_stack(output_stream* os, dnslabel_stack_reference labels, s32 top);
137
138 ya_result output_stream_decode_base64(output_stream* os, const char * string, u32 length);
139 ya_result output_stream_decode_base32(output_stream* os, const char * string, u32 length);
140 ya_result output_stream_decode_base32hex(output_stream* os, const char * string, u32 length);
141 ya_result output_stream_decode_base16(output_stream* os, const char * string, u32 length);
142
143 /**
144 * Note: the typebitmap.h file declares a type_bit_maps_output_stream_write function
145 */
146
147 output_stream *output_stream_alloc();
148
149 /**
150 * This tools allows a safer misuse (and detection) of closed streams
151 * It sets the stream to a sink that warns abouts its usage and for which every call that can fail fails.
152 */
153
154 void output_stream_set_void(output_stream *stream);
155
156 /**
157 * Used to temporarily initialise a stream with a sink that can be closed safely.
158 * Typically used as pre-init so the stream can be closed even if the function
159 * setup failed before reaching stream initialisation.
160 *
161 * @param os
162 */
163
164 void output_stream_set_sink(output_stream *os);
165
166 ya_result output_stream_write_fully(output_stream *stream, const void *buffer_start, u32 len_start);
167
168 #ifdef __cplusplus
169 }
170 #endif
171
172 #endif /* _OUTPUT_STREAM_H */
173
174