Debian Codegraph @

libcnotify/libcnotify_print.c

raw symbols SCIP
1 /*
2 * Notification print functions
3 *
4 * Copyright (C) 2008-2024, Joachim Metz <[email protected]>
5 *
6 * Refer to AUTHORS for acknowledgements.
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22 #include <common.h>
23 #include <file_stream.h>
24 #include <memory.h>
25 #include <types.h>
26
27 #if defined( HAVE_STDARG_H ) || defined( WINAPI )
28 #include <stdarg.h>
29 #elif defined( HAVE_VARARGS_H )
30 #include <varargs.h>
31 #else
32 #error Missing headers stdarg.h and varargs.h
33 #endif
34
35 #include "libcnotify_definitions.h"
36 #include "libcnotify_libcerror.h"
37 #include "libcnotify_print.h"
38 #include "libcnotify_stream.h"
39
40 extern FILE *libcnotify_stream;
41
42 #if defined( HAVE_STDARG_H ) || defined( WINAPI )
43 #define VARARGS( function, type, argument ) \
44 function( type argument, ... )
45 #define VASTART( argument_list, type, name ) \
46 va_start( argument_list, name )
47 #define VAEND( argument_list ) \
48 va_end( argument_list )
49
50 #elif defined( HAVE_VARARGS_H )
51 #define VARARGS( function, type, argument ) \
52 function( va_alist ) va_dcl
53 #define VASTART( argument_list, type, name ) \
54 { type name; va_start( argument_list ); name = va_arg( argument_list, type )
55 #define VAEND( argument_list ) \
56 va_end( argument_list ); }
57
58 #endif
59
60 /* Print a formatted string on the notify stream
61 * Returns the number of printed characters if successful or -1 on error
62 */
63 int VARARGS(
64 libcnotify_printf,
65 const char *,
66 format )
67 {
68 va_list argument_list;
69
70 int print_count = 0;
71
72 if( libcnotify_stream == NULL )
73 {
74 return( 0 );
75 }
76 VASTART(
77 argument_list,
78 char *,
79 format );
80
81 #if defined( HAVE_GLIB_H )
82 g_logv(
83 G_LOG_DOMAIN,
84 G_LOG_LEVEL_MESSAGE,
85 format,
86 argument_list );
87
88 #else
89 /* TODO handle narrow and wide streams
90 * for multi platform support
91 */
92 print_count = file_stream_vfprintf(
93 libcnotify_stream,
94 format,
95 argument_list );
96 #endif
97
98 VAEND(
99 argument_list );
100
101 if( print_count <= -1 )
102 {
103 return( -1 );
104 }
105 return( print_count );
106 }
107
108 #undef VARARGS
109 #undef VASTART
110 #undef VAEND
111
112 /* Prints the data as a character on the notify stream
113 * Returns the number of printed characters if successful or -1 on error
114 */
115 int libcnotify_print_data_as_character(
116 uint8_t data )
117 {
118 int print_count = 0;
119
120 if( ( data >= 0x20 )
121 && ( data <= 0x7e ) )
122 {
123 print_count = libcnotify_printf(
124 "%c",
125 (char) data );
126 }
127 else
128 {
129 print_count = libcnotify_printf(
130 "." );
131 }
132 return( print_count );
133 }
134
135 /* Prints the first 16 bytes of data as a characters on the notify stream
136 * Returns the number of printed characters if successful or -1 on error
137 */
138 int libcnotify_print_data_as_characters(
139 const uint8_t *data,
140 size_t data_size,
141 size_t data_offset )
142 {
143 int print_count = 0;
144 int total_print_count = 0;
145
146 if( data == NULL )
147 {
148 return( -1 );
149 }
150 while( data_offset < data_size )
151 {
152 print_count = libcnotify_print_data_as_character(
153 data[ data_offset++ ] );
154
155 if( print_count <= -1 )
156 {
157 return( -1 );
158 }
159 total_print_count += print_count;
160
161 if( ( data_offset % 16 == 0 )
162 || ( data_offset == data_size ) )
163 {
164 break;
165 }
166 if( data_offset % 8 == 0 )
167 {
168 print_count = libcnotify_printf(
169 " " );
170
171 if( print_count <= -1 )
172 {
173 return( -1 );
174 }
175 total_print_count += print_count;
176 }
177 }
178 return( total_print_count );
179 }
180
181 /* Prints the first 16 bytes of data as a hexadecimal values on the notify stream
182 * Returns the number of printed characters if successful or -1 on error
183 */
184 int libcnotify_print_data_as_hexadecimal(
185 const uint8_t *data,
186 size_t data_size,
187 size_t data_offset )
188 {
189 int print_count = 0;
190 int total_print_count = 0;
191
192 if( data == NULL )
193 {
194 return( -1 );
195 }
196 while( data_offset < data_size )
197 {
198 print_count = libcnotify_printf(
199 "%.2" PRIx8 " ",
200 data[ data_offset++ ] );
201
202 if( print_count <= -1 )
203 {
204 return( -1 );
205 }
206 total_print_count += print_count;
207
208 if( data_offset % 16 == 0 )
209 {
210 break;
211 }
212 else if( data_offset % 8 == 0 )
213 {
214 print_count = libcnotify_printf(
215 " " );
216
217 if( print_count <= -1 )
218 {
219 return( -1 );
220 }
221 total_print_count += print_count;
222 }
223 }
224 while( data_offset % 16 != 0 )
225 {
226 data_offset++;
227
228 print_count = libcnotify_printf(
229 " " );
230
231 if( print_count <= -1 )
232 {
233 return( -1 );
234 }
235 total_print_count += print_count;
236
237 if( ( data_offset % 8 == 0 )
238 && ( data_offset % 16 != 0 ) )
239 {
240 print_count = libcnotify_printf(
241 " " );
242
243 if( print_count <= -1 )
244 {
245 return( -1 );
246 }
247 total_print_count += print_count;
248 }
249 }
250 return( total_print_count );
251 }
252
253 /* Prints the data on the notify stream
254 * Returns the number of printed characters if successful or -1 on error
255 */
256 int libcnotify_print_data(
257 const uint8_t *data,
258 size_t data_size,
259 uint8_t print_data_flags )
260 {
261 size_t data_offset = 0;
262 int in_group = 0;
263 int print_count = 0;
264 int total_print_count = 0;
265
266 if( libcnotify_stream == NULL )
267 {
268 return( 0 );
269 }
270 if( data_size > 0 )
271 {
272 if( data == NULL )
273 {
274 return( -1 );
275 }
276 }
277 if( data_size > (size_t) SSIZE_MAX )
278 {
279 return( -1 );
280 }
281 while( data_offset < data_size )
282 {
283 if( ( ( print_data_flags & LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA ) != 0 )
284 && ( data_size >= 32 )
285 && ( data_offset >= 16 )
286 && ( data_offset <= ( data_size - 32 ) ) )
287 {
288 if( ( memory_compare(
289 &( data[ data_offset - 16 ] ),
290 &( data[ data_offset ] ),
291 16 ) == 0 )
292 && ( memory_compare(
293 &( data[ data_offset + 16 ] ),
294 &( data[ data_offset ] ),
295 16 ) == 0 ) )
296 {
297 if( in_group == 0 )
298 {
299 print_count = libcnotify_printf(
300 "...\n" );
301
302 if( print_count <= -1 )
303 {
304 return( -1 );
305 }
306 total_print_count += print_count;
307
308 in_group = 1;
309 }
310 data_offset += 16;
311
312 continue;
313 }
314 in_group = 0;
315 }
316 if( data_offset % 16 == 0 )
317 {
318 print_count = libcnotify_printf(
319 "%.8" PRIzx ": ",
320 data_offset );
321
322 if( print_count <= -1 )
323 {
324 return( -1 );
325 }
326 total_print_count += print_count;
327 }
328 print_count = libcnotify_print_data_as_hexadecimal(
329 data,
330 data_size,
331 data_offset );
332
333 if( print_count <= -1 )
334 {
335 return( -1 );
336 }
337 total_print_count += print_count;
338
339 print_count = libcnotify_printf(
340 " " );
341
342 if( print_count <= -1 )
343 {
344 return( -1 );
345 }
346 total_print_count += print_count;
347
348 print_count = libcnotify_print_data_as_characters(
349 data,
350 data_size,
351 data_offset );
352
353 if( print_count <= -1 )
354 {
355 return( -1 );
356 }
357 total_print_count += print_count;
358
359 print_count = libcnotify_printf(
360 "\n" );
361
362 if( print_count <= -1 )
363 {
364 return( -1 );
365 }
366 total_print_count += print_count;
367
368 data_offset += 16;
369 }
370 print_count = libcnotify_printf(
371 "\n" );
372
373 if( print_count <= -1 )
374 {
375 return( -1 );
376 }
377 total_print_count += print_count;
378
379 return( total_print_count );
380 }
381
382 /* Prints the backtrace of the error on the notify stream
383 * Returns the number of printed characters if successful or -1 on error
384 */
385 int libcnotify_print_error_backtrace(
386 libcerror_error_t *error )
387 {
388 int print_count = 0;
389
390 if( libcnotify_stream == NULL )
391 {
392 return( 0 );
393 }
394 print_count = libcerror_error_backtrace_fprint(
395 error,
396 libcnotify_stream );
397
398 return( print_count );
399 }
400