Coverage Report

Created: 2026-07-10 07:32

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libsmraw/libcnotify/libcnotify_print.c
Line
Count
Source
1
/*
2
 * Notification print functions
3
 *
4
 * Copyright (C) 2008-2026, 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
#if defined( HAVE_STDARG_H ) || defined( WINAPI )
41
#define VARARGS( function, type, argument ) \
42
  function( type argument, ... )
43
#define VASTART( argument_list, type, name ) \
44
0
  va_start( argument_list, name )
45
#define VAEND( argument_list ) \
46
0
  va_end( argument_list )
47
48
#elif defined( HAVE_VARARGS_H )
49
#define VARARGS( function, type, argument ) \
50
  function( va_alist ) va_dcl
51
#define VASTART( argument_list, type, name ) \
52
  { type name; va_start( argument_list ); name = va_arg( argument_list, type )
53
#define VAEND( argument_list ) \
54
  va_end( argument_list ); }
55
56
#endif
57
58
/* Print a formatted string on the notify stream
59
 * Returns the number of printed characters if successful or -1 on error
60
 */
61
int VARARGS(
62
     libcnotify_printf,
63
     const char *,
64
     format )
65
0
{
66
0
  va_list argument_list;
67
68
0
  int print_count = 0;
69
70
0
  if( libcnotify_stream == NULL )
71
0
  {
72
0
    return( 0 );
73
0
  }
74
0
  VASTART(
75
0
   argument_list,
76
0
   char *,
77
0
   format );
78
79
0
  print_count = vfprintf(
80
0
           libcnotify_stream,
81
0
           format,
82
0
           argument_list );
83
84
0
  VAEND(
85
0
   argument_list );
86
87
0
  if( print_count <= -1 )
88
0
  {
89
0
    return( -1 );
90
0
  }
91
0
  return( print_count );
92
0
}
93
94
#undef VARARGS
95
#undef VASTART
96
#undef VAEND
97
98
/* Prints the data as a character on the notify stream
99
 * Returns the number of printed characters if successful or -1 on error
100
 */
101
int libcnotify_print_data_as_character(
102
     uint8_t data )
103
0
{
104
0
  int print_count = 0;
105
106
0
  if( ( data >= 0x20 )
107
0
   && ( data <= 0x7e ) )
108
0
  {
109
0
    print_count = libcnotify_printf(
110
0
                   "%c",
111
0
                   (char) data );
112
0
  }
113
0
  else
114
0
  {
115
0
    print_count = libcnotify_printf(
116
0
                   "." );
117
0
  }
118
0
  return( print_count );
119
0
}
120
121
/* Prints the first 16 bytes of data as a characters on the notify stream
122
 * Returns the number of printed characters if successful or -1 on error
123
 */
124
int libcnotify_print_data_as_characters(
125
     const uint8_t *data,
126
     size_t data_size,
127
     size_t data_offset )
128
0
{
129
0
  int print_count       = 0;
130
0
  int total_print_count = 0;
131
132
0
  if( data == NULL )
133
0
  {
134
0
    return( -1 );
135
0
  }
136
0
  while( data_offset < data_size )
137
0
  {
138
0
    print_count = libcnotify_print_data_as_character(
139
0
                   data[ data_offset++ ] );
140
141
0
    if( print_count <= -1 )
142
0
    {
143
0
      return( -1 );
144
0
    }
145
0
    total_print_count += print_count;
146
147
0
    if( ( data_offset % 16 == 0 )
148
0
     || ( data_offset == data_size ) )
149
0
    {
150
0
      break;
151
0
    }
152
0
    if( data_offset % 8 == 0 )
153
0
    {
154
0
      print_count = libcnotify_printf(
155
0
                     " " );
156
157
0
      if( print_count <= -1 )
158
0
      {
159
0
        return( -1 );
160
0
      }
161
0
      total_print_count += print_count;
162
0
    }
163
0
  }
164
0
  return( total_print_count );
165
0
}
166
167
/* Prints the first 16 bytes of data as a hexadecimal values on the notify stream
168
 * Returns the number of printed characters if successful or -1 on error
169
 */
170
int libcnotify_print_data_as_hexadecimal(
171
     const uint8_t *data,
172
     size_t data_size,
173
     size_t data_offset )
174
0
{
175
0
  int print_count       = 0;
176
0
  int total_print_count = 0;
177
178
0
  if( data == NULL )
179
0
  {
180
0
    return( -1 );
181
0
  }
182
0
  while( data_offset < data_size )
183
0
  {
184
0
    print_count = libcnotify_printf(
185
0
                   "%.2" PRIx8 " ",
186
0
                   data[ data_offset++ ] );
187
188
0
    if( print_count <= -1 )
189
0
    {
190
0
      return( -1 );
191
0
    }
192
0
    total_print_count += print_count;
193
194
0
    if( data_offset % 16 == 0 )
195
0
    {
196
0
      break;
197
0
    }
198
0
    else if( data_offset % 8 == 0 )
199
0
    {
200
0
      print_count = libcnotify_printf(
201
0
                     " " );
202
203
0
      if( print_count <= -1 )
204
0
      {
205
0
        return( -1 );
206
0
      }
207
0
      total_print_count += print_count;
208
0
    }
209
0
  }
210
0
  while( data_offset % 16 != 0 )
211
0
  {
212
0
    data_offset++;
213
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
223
0
    if( ( data_offset % 8 == 0 )
224
0
     && ( data_offset % 16 != 0 ) )
225
0
    {
226
0
      print_count = libcnotify_printf(
227
0
                     " " );
228
229
0
      if( print_count <= -1 )
230
0
      {
231
0
        return( -1 );
232
0
      }
233
0
      total_print_count += print_count;
234
0
    }
235
0
  }
236
0
  return( total_print_count );
237
0
}
238
239
/* Prints the data on the notify stream
240
 * Returns the number of printed characters if successful or -1 on error
241
 */
242
int libcnotify_print_data(
243
     const uint8_t *data,
244
     size_t data_size,
245
     uint8_t print_data_flags )
246
0
{
247
0
  size_t data_offset    = 0;
248
0
  int in_group          = 0;
249
0
  int print_count       = 0;
250
0
  int total_print_count = 0;
251
252
0
  if( libcnotify_stream == NULL )
253
0
  {
254
0
    return( 0 );
255
0
  }
256
0
  if( data_size > 0 )
257
0
  {
258
0
    if( data == NULL )
259
0
    {
260
0
      return( -1 );
261
0
    }
262
0
  }
263
0
  if( data_size > (size_t) SSIZE_MAX )
264
0
  {
265
0
    return( -1 );
266
0
  }
267
0
  while( data_offset < data_size )
268
0
  {
269
0
    if( ( ( print_data_flags & LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA ) != 0 )
270
0
     && ( data_size >= 32 )
271
0
     && ( data_offset >= 16 )
272
0
     && ( data_offset <= ( data_size - 32 ) ) )
273
0
    {
274
0
      if( ( memory_compare(
275
0
             &( data[ data_offset - 16 ] ),
276
0
             &( data[ data_offset ] ),
277
0
             16 ) == 0 )
278
0
       && ( memory_compare(
279
0
             &( data[ data_offset + 16 ] ),
280
0
             &( data[ data_offset ] ),
281
0
             16 ) == 0 ) )
282
0
      {
283
0
        if( in_group == 0 )
284
0
        {
285
0
          print_count = libcnotify_printf(
286
0
                   "...\n" );
287
288
0
          if( print_count <= -1 )
289
0
          {
290
0
            return( -1 );
291
0
          }
292
0
          total_print_count += print_count;
293
294
0
          in_group = 1;
295
0
        }
296
0
        data_offset += 16;
297
298
0
        continue;
299
0
      }
300
0
      in_group = 0;
301
0
    }
302
0
    if( data_offset % 16 == 0 )
303
0
    {
304
0
      print_count = libcnotify_printf(
305
0
               "%.8" PRIzx ": ",
306
0
               data_offset );
307
308
0
      if( print_count <= -1 )
309
0
      {
310
0
        return( -1 );
311
0
      }
312
0
      total_print_count += print_count;
313
0
    }
314
0
    print_count = libcnotify_print_data_as_hexadecimal(
315
0
                   data,
316
0
                   data_size,
317
0
                   data_offset );
318
319
0
    if( print_count <= -1 )
320
0
    {
321
0
      return( -1 );
322
0
    }
323
0
    total_print_count += print_count;
324
325
0
    print_count = libcnotify_printf(
326
0
             "  " );
327
328
0
    if( print_count <= -1 )
329
0
    {
330
0
      return( -1 );
331
0
    }
332
0
    total_print_count += print_count;
333
334
0
    print_count = libcnotify_print_data_as_characters(
335
0
                   data,
336
0
                   data_size,
337
0
                   data_offset );
338
339
0
    if( print_count <= -1 )
340
0
    {
341
0
      return( -1 );
342
0
    }
343
0
    total_print_count += print_count;
344
345
0
    print_count = libcnotify_printf(
346
0
                   "\n" );
347
348
0
    if( print_count <= -1 )
349
0
    {
350
0
      return( -1 );
351
0
    }
352
0
    total_print_count += print_count;
353
354
0
    data_offset += 16;
355
0
  }
356
0
  print_count = libcnotify_printf(
357
0
           "\n" );
358
359
0
  if( print_count <= -1 )
360
0
  {
361
0
    return( -1 );
362
0
  }
363
0
  total_print_count += print_count;
364
365
0
  return( total_print_count );
366
0
}
367
368
/* Prints the backtrace of the error on the notify stream
369
 * Returns the number of printed characters if successful or -1 on error
370
 */
371
int libcnotify_print_error_backtrace(
372
     libcerror_error_t *error )
373
0
{
374
0
  int print_count = 0;
375
376
0
  if( libcnotify_stream == NULL )
377
0
  {
378
0
    return( 0 );
379
0
  }
380
0
  print_count = libcerror_error_backtrace_fprint(
381
0
                 error,
382
0
                 libcnotify_stream );
383
384
0
  return( print_count );
385
0
}
386