/src/libfsntfs/libcnotify/libcnotify_print.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /*  | 
2  |  |  * Notification print functions  | 
3  |  |  *  | 
4  |  |  * Copyright (C) 2008-2024, Joachim Metz <joachim.metz@gmail.com>  | 
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  | 0  |   va_start( argument_list, name )  | 
47  |  | #define VAEND( argument_list ) \  | 
48  | 0  |   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  | 0  | { | 
68  | 0  |   va_list argument_list;  | 
69  |  | 
  | 
70  | 0  |   int print_count = 0;  | 
71  |  | 
  | 
72  | 0  |   if( libcnotify_stream == NULL )  | 
73  | 0  |   { | 
74  | 0  |     return( 0 );  | 
75  | 0  |   }  | 
76  | 0  |   VASTART(  | 
77  | 0  |    argument_list,  | 
78  | 0  |    char *,  | 
79  | 0  |    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  | 0  |   print_count = file_stream_vfprintf(  | 
93  | 0  |            libcnotify_stream,  | 
94  | 0  |            format,  | 
95  | 0  |            argument_list );  | 
96  | 0  | #endif  | 
97  |  | 
  | 
98  | 0  |   VAEND(  | 
99  | 0  |    argument_list );  | 
100  |  | 
  | 
101  | 0  |   if( print_count <= -1 )  | 
102  | 0  |   { | 
103  | 0  |     return( -1 );  | 
104  | 0  |   }  | 
105  | 0  |   return( print_count );  | 
106  | 0  | }  | 
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  | 0  | { | 
118  | 0  |   int print_count = 0;  | 
119  |  | 
  | 
120  | 0  |   if( ( data >= 0x20 )  | 
121  | 0  |    && ( data <= 0x7e ) )  | 
122  | 0  |   { | 
123  | 0  |     print_count = libcnotify_printf(  | 
124  | 0  |                    "%c",  | 
125  | 0  |                    (char) data );  | 
126  | 0  |   }  | 
127  | 0  |   else  | 
128  | 0  |   { | 
129  | 0  |     print_count = libcnotify_printf(  | 
130  | 0  |                    "." );  | 
131  | 0  |   }  | 
132  | 0  |   return( print_count );  | 
133  | 0  | }  | 
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  | 0  | { | 
143  | 0  |   int print_count       = 0;  | 
144  | 0  |   int total_print_count = 0;  | 
145  |  | 
  | 
146  | 0  |   if( data == NULL )  | 
147  | 0  |   { | 
148  | 0  |     return( -1 );  | 
149  | 0  |   }  | 
150  | 0  |   while( data_offset < data_size )  | 
151  | 0  |   { | 
152  | 0  |     print_count = libcnotify_print_data_as_character(  | 
153  | 0  |                    data[ data_offset++ ] );  | 
154  |  | 
  | 
155  | 0  |     if( print_count <= -1 )  | 
156  | 0  |     { | 
157  | 0  |       return( -1 );  | 
158  | 0  |     }  | 
159  | 0  |     total_print_count += print_count;  | 
160  |  | 
  | 
161  | 0  |     if( ( data_offset % 16 == 0 )  | 
162  | 0  |      || ( data_offset == data_size ) )  | 
163  | 0  |     { | 
164  | 0  |       break;  | 
165  | 0  |     }  | 
166  | 0  |     if( data_offset % 8 == 0 )  | 
167  | 0  |     { | 
168  | 0  |       print_count = libcnotify_printf(  | 
169  | 0  |                      " " );  | 
170  |  | 
  | 
171  | 0  |       if( print_count <= -1 )  | 
172  | 0  |       { | 
173  | 0  |         return( -1 );  | 
174  | 0  |       }  | 
175  | 0  |       total_print_count += print_count;  | 
176  | 0  |     }  | 
177  | 0  |   }  | 
178  | 0  |   return( total_print_count );  | 
179  | 0  | }  | 
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  | 0  | { | 
189  | 0  |   int print_count       = 0;  | 
190  | 0  |   int total_print_count = 0;  | 
191  |  | 
  | 
192  | 0  |   if( data == NULL )  | 
193  | 0  |   { | 
194  | 0  |     return( -1 );  | 
195  | 0  |   }  | 
196  | 0  |   while( data_offset < data_size )  | 
197  | 0  |   { | 
198  | 0  |     print_count = libcnotify_printf(  | 
199  | 0  |                    "%.2" PRIx8 " ",  | 
200  | 0  |                    data[ data_offset++ ] );  | 
201  |  | 
  | 
202  | 0  |     if( print_count <= -1 )  | 
203  | 0  |     { | 
204  | 0  |       return( -1 );  | 
205  | 0  |     }  | 
206  | 0  |     total_print_count += print_count;  | 
207  |  | 
  | 
208  | 0  |     if( data_offset % 16 == 0 )  | 
209  | 0  |     { | 
210  | 0  |       break;  | 
211  | 0  |     }  | 
212  | 0  |     else if( data_offset % 8 == 0 )  | 
213  | 0  |     { | 
214  | 0  |       print_count = libcnotify_printf(  | 
215  | 0  |                      " " );  | 
216  |  | 
  | 
217  | 0  |       if( print_count <= -1 )  | 
218  | 0  |       { | 
219  | 0  |         return( -1 );  | 
220  | 0  |       }  | 
221  | 0  |       total_print_count += print_count;  | 
222  | 0  |     }  | 
223  | 0  |   }  | 
224  | 0  |   while( data_offset % 16 != 0 )  | 
225  | 0  |   { | 
226  | 0  |     data_offset++;  | 
227  |  | 
  | 
228  | 0  |     print_count = libcnotify_printf(  | 
229  | 0  |                    "   " );  | 
230  |  | 
  | 
231  | 0  |     if( print_count <= -1 )  | 
232  | 0  |     { | 
233  | 0  |       return( -1 );  | 
234  | 0  |     }  | 
235  | 0  |     total_print_count += print_count;  | 
236  |  | 
  | 
237  | 0  |     if( ( data_offset % 8 == 0 )  | 
238  | 0  |      && ( data_offset % 16 != 0 ) )  | 
239  | 0  |     { | 
240  | 0  |       print_count = libcnotify_printf(  | 
241  | 0  |                      " " );  | 
242  |  | 
  | 
243  | 0  |       if( print_count <= -1 )  | 
244  | 0  |       { | 
245  | 0  |         return( -1 );  | 
246  | 0  |       }  | 
247  | 0  |       total_print_count += print_count;  | 
248  | 0  |     }  | 
249  | 0  |   }  | 
250  | 0  |   return( total_print_count );  | 
251  | 0  | }  | 
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  | 0  | { | 
261  | 0  |   size_t data_offset    = 0;  | 
262  | 0  |   int in_group          = 0;  | 
263  | 0  |   int print_count       = 0;  | 
264  | 0  |   int total_print_count = 0;  | 
265  |  | 
  | 
266  | 0  |   if( libcnotify_stream == NULL )  | 
267  | 0  |   { | 
268  | 0  |     return( 0 );  | 
269  | 0  |   }  | 
270  | 0  |   if( data_size > 0 )  | 
271  | 0  |   { | 
272  | 0  |     if( data == NULL )  | 
273  | 0  |     { | 
274  | 0  |       return( -1 );  | 
275  | 0  |     }  | 
276  | 0  |   }  | 
277  | 0  |   if( data_size > (size_t) SSIZE_MAX )  | 
278  | 0  |   { | 
279  | 0  |     return( -1 );  | 
280  | 0  |   }  | 
281  | 0  |   while( data_offset < data_size )  | 
282  | 0  |   { | 
283  | 0  |     if( ( ( print_data_flags & LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA ) != 0 )  | 
284  | 0  |      && ( data_size >= 32 )  | 
285  | 0  |      && ( data_offset >= 16 )  | 
286  | 0  |      && ( data_offset <= ( data_size - 32 ) ) )  | 
287  | 0  |     { | 
288  | 0  |       if( ( memory_compare(  | 
289  | 0  |              &( data[ data_offset - 16 ] ),  | 
290  | 0  |              &( data[ data_offset ] ),  | 
291  | 0  |              16 ) == 0 )  | 
292  | 0  |        && ( memory_compare(  | 
293  | 0  |              &( data[ data_offset + 16 ] ),  | 
294  | 0  |              &( data[ data_offset ] ),  | 
295  | 0  |              16 ) == 0 ) )  | 
296  | 0  |       { | 
297  | 0  |         if( in_group == 0 )  | 
298  | 0  |         { | 
299  | 0  |           print_count = libcnotify_printf(  | 
300  | 0  |                    "...\n" );  | 
301  |  | 
  | 
302  | 0  |           if( print_count <= -1 )  | 
303  | 0  |           { | 
304  | 0  |             return( -1 );  | 
305  | 0  |           }  | 
306  | 0  |           total_print_count += print_count;  | 
307  |  | 
  | 
308  | 0  |           in_group = 1;  | 
309  | 0  |         }  | 
310  | 0  |         data_offset += 16;  | 
311  |  | 
  | 
312  | 0  |         continue;  | 
313  | 0  |       }  | 
314  | 0  |       in_group = 0;  | 
315  | 0  |     }  | 
316  | 0  |     if( data_offset % 16 == 0 )  | 
317  | 0  |     { | 
318  | 0  |       print_count = libcnotify_printf(  | 
319  | 0  |                "%.8" PRIzx ": ",  | 
320  | 0  |                data_offset );  | 
321  |  | 
  | 
322  | 0  |       if( print_count <= -1 )  | 
323  | 0  |       { | 
324  | 0  |         return( -1 );  | 
325  | 0  |       }  | 
326  | 0  |       total_print_count += print_count;  | 
327  | 0  |     }  | 
328  | 0  |     print_count = libcnotify_print_data_as_hexadecimal(  | 
329  | 0  |                    data,  | 
330  | 0  |                    data_size,  | 
331  | 0  |                    data_offset );  | 
332  |  | 
  | 
333  | 0  |     if( print_count <= -1 )  | 
334  | 0  |     { | 
335  | 0  |       return( -1 );  | 
336  | 0  |     }  | 
337  | 0  |     total_print_count += print_count;  | 
338  |  | 
  | 
339  | 0  |     print_count = libcnotify_printf(  | 
340  | 0  |              "  " );  | 
341  |  | 
  | 
342  | 0  |     if( print_count <= -1 )  | 
343  | 0  |     { | 
344  | 0  |       return( -1 );  | 
345  | 0  |     }  | 
346  | 0  |     total_print_count += print_count;  | 
347  |  | 
  | 
348  | 0  |     print_count = libcnotify_print_data_as_characters(  | 
349  | 0  |                    data,  | 
350  | 0  |                    data_size,  | 
351  | 0  |                    data_offset );  | 
352  |  | 
  | 
353  | 0  |     if( print_count <= -1 )  | 
354  | 0  |     { | 
355  | 0  |       return( -1 );  | 
356  | 0  |     }  | 
357  | 0  |     total_print_count += print_count;  | 
358  |  | 
  | 
359  | 0  |     print_count = libcnotify_printf(  | 
360  | 0  |                    "\n" );  | 
361  |  | 
  | 
362  | 0  |     if( print_count <= -1 )  | 
363  | 0  |     { | 
364  | 0  |       return( -1 );  | 
365  | 0  |     }  | 
366  | 0  |     total_print_count += print_count;  | 
367  |  | 
  | 
368  | 0  |     data_offset += 16;  | 
369  | 0  |   }  | 
370  | 0  |   print_count = libcnotify_printf(  | 
371  | 0  |            "\n" );  | 
372  |  | 
  | 
373  | 0  |   if( print_count <= -1 )  | 
374  | 0  |   { | 
375  | 0  |     return( -1 );  | 
376  | 0  |   }  | 
377  | 0  |   total_print_count += print_count;  | 
378  |  | 
  | 
379  | 0  |   return( total_print_count );  | 
380  | 0  | }  | 
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  | 0  | { | 
388  | 0  |   int print_count = 0;  | 
389  |  | 
  | 
390  | 0  |   if( libcnotify_stream == NULL )  | 
391  | 0  |   { | 
392  | 0  |     return( 0 );  | 
393  | 0  |   }  | 
394  | 0  |   print_count = libcerror_error_backtrace_fprint(  | 
395  | 0  |                  error,  | 
396  | 0  |                  libcnotify_stream );  | 
397  |  | 
  | 
398  | 0  |   return( print_count );  | 
399  | 0  | }  | 
400  |  |  |