Coverage Report

Created: 2025-06-13 07:22

/src/libevtx/libevtx/libevtx_byte_stream.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Byte stream functions
3
 *
4
 * Copyright (C) 2011-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 <types.h>
24
25
#include "libevtx_byte_stream.h"
26
#include "libevtx_libcerror.h"
27
#include "libevtx_types.h"
28
29
/* Checks if a byte stream is filled with 0-byte values
30
 * Returns 1 if true, 0 if not or -1 on error
31
 */
32
int libevtx_byte_stream_check_for_zero_byte_fill(
33
     const uint8_t *byte_stream,
34
     size_t byte_stream_size,
35
     libcerror_error_t **error )
36
212k
{
37
212k
  libevtx_aligned_t *aligned_byte_stream_index = NULL;
38
212k
  uint8_t *byte_stream_index                   = NULL;
39
212k
  static char *function                        = "libevtx_byte_stream_check_for_zero_byte_fill";
40
41
212k
  if( byte_stream == NULL )
42
0
  {
43
0
    libcerror_error_set(
44
0
     error,
45
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
46
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
47
0
     "%s: invalid byte stream.",
48
0
     function );
49
50
0
    return( -1 );
51
0
  }
52
212k
  if( byte_stream_size > (size_t) SSIZE_MAX )
53
0
  {
54
0
    libcerror_error_set(
55
0
     error,
56
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
57
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
58
0
     "%s: invalid byte stream size value exceeds maximum.",
59
0
     function );
60
61
0
    return( -1 );
62
0
  }
63
212k
  byte_stream_index = (uint8_t *) byte_stream;
64
65
  /* Only optimize for byte stream larger than the alignment
66
   */
67
212k
  if( byte_stream_size > ( 2 * sizeof( libevtx_aligned_t ) ) )
68
212k
  {
69
    /* Align the byte stream index
70
     */
71
212k
    while( ( (intptr_t) byte_stream_index % sizeof( libevtx_aligned_t ) ) != 0 )
72
80.0k
    {
73
80.0k
      if( *byte_stream_index != 0 )
74
79.7k
      {
75
79.7k
        return( 0 );
76
79.7k
      }
77
322
      byte_stream_index += 1;
78
322
      byte_stream_size  -= 1;
79
322
    }
80
132k
    aligned_byte_stream_index = (libevtx_aligned_t *) byte_stream_index;
81
82
1.87M
    while( byte_stream_size > sizeof( libevtx_aligned_t ) )
83
1.87M
    {
84
1.87M
      if( *aligned_byte_stream_index != 0 )
85
132k
      {
86
132k
        return( 0 );
87
132k
      }
88
1.74M
      aligned_byte_stream_index += 1;
89
1.74M
      byte_stream_size          -= sizeof( libevtx_aligned_t );
90
1.74M
    }
91
334
    byte_stream_index = (uint8_t *) aligned_byte_stream_index;
92
334
  }
93
2.45k
  while( byte_stream_size != 0 )
94
2.20k
  {
95
2.20k
    if( *byte_stream_index != 0 )
96
84
    {
97
84
      return( 0 );
98
84
    }
99
2.12k
    byte_stream_index += 1;
100
2.12k
    byte_stream_size  -= 1;
101
2.12k
  }
102
250
  return( 1 );
103
334
}
104