Coverage Report

Created: 2024-02-25 07:20

/src/libvslvm/libvslvm/libvslvm_checksum.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Checksum functions
3
 *
4
 * Copyright (C) 2014-2023, 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 "libvslvm_libcerror.h"
26
27
/* CRC-32 functions
28
 * Based on RFC 1952
29
 */
30
31
/* Table of CRC-32 values of 8-bit values
32
 */
33
uint32_t libvslvm_checksum_crc32_table[ 256 ];
34
35
/* Value to indicate the CRC-32 table been computed
36
 */
37
int libvslvm_checksum_crc32_table_computed = 0;
38
39
/* Initializes the internal CRC-32 table
40
 * The table speeds up the CRC-32 calculation
41
 */
42
void libvslvm_checksum_initialize_crc32_table(
43
      void )
44
2
{
45
2
  uint32_t checksum    = 0;
46
2
  uint32_t table_index = 0;
47
2
  uint8_t bit_iterator = 0;
48
49
2
  for( table_index = 0;
50
514
       table_index < 256;
51
512
       table_index++ )
52
512
  {
53
512
    checksum = (uint32_t) table_index;
54
55
512
    for( bit_iterator = 0;
56
4.60k
         bit_iterator < 8;
57
4.09k
         bit_iterator++ )
58
4.09k
    {
59
4.09k
      if( checksum & 1 )
60
2.04k
      {
61
2.04k
        checksum = (uint32_t) 0xedb88320UL ^ ( checksum >> 1 );
62
2.04k
      }
63
2.04k
      else
64
2.04k
      {
65
2.04k
        checksum = checksum >> 1;
66
2.04k
      }
67
4.09k
    }
68
512
    libvslvm_checksum_crc32_table[ table_index ] = checksum;
69
512
  }
70
2
  libvslvm_checksum_crc32_table_computed = 1;
71
2
}
72
73
/* Calculates the CRC-32 checksum of a buffer
74
 * Returns 1 if successful or -1 on error
75
 */
76
int libvslvm_checksum_calculate_crc32(
77
     uint32_t *checksum,
78
     const uint8_t *buffer,
79
     size_t size,
80
     uint32_t initial_value,
81
     libcerror_error_t **error )
82
0
{
83
0
  static char *function = "libvslvm_checksum_calculate_crc32";
84
0
  size_t buffer_offset  = 0;
85
0
  uint32_t table_index  = 0;
86
87
0
  if( checksum == NULL )
88
0
  {
89
0
    libcerror_error_set(
90
0
     error,
91
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
92
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
93
0
     "%s: invalid checksum.",
94
0
     function );
95
96
0
    return( -1 );
97
0
  }
98
0
  if( buffer == NULL )
99
0
  {
100
0
    libcerror_error_set(
101
0
     error,
102
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
103
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
104
0
     "%s: invalid buffer.",
105
0
     function );
106
107
0
    return( -1 );
108
0
  }
109
0
  if( size > (size_t) SSIZE_MAX )
110
0
  {
111
0
    libcerror_error_set(
112
0
     error,
113
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
114
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
115
0
     "%s: invalid size value exceeds maximum.",
116
0
     function );
117
118
0
    return( -1 );
119
0
  }
120
0
  *checksum = initial_value ^ (uint32_t) 0xffffffffUL;
121
122
0
        if( libvslvm_checksum_crc32_table_computed == 0 )
123
0
  {
124
0
    libvslvm_checksum_initialize_crc32_table();
125
0
  }
126
0
        for( buffer_offset = 0;
127
0
       buffer_offset < size;
128
0
       buffer_offset++ )
129
0
  {
130
0
    table_index = ( *checksum ^ buffer[ buffer_offset ] ) & 0x000000ffUL;
131
132
0
    *checksum = libvslvm_checksum_crc32_table[ table_index ] ^ ( *checksum >> 8 );
133
0
        }
134
0
        *checksum ^= 0xffffffffUL;
135
136
0
  return( 1 );
137
0
}
138
139
/* Calculates the weak CRC-32 checksum of a buffer
140
 * Returns 1 if successful or -1 on error
141
 */
142
int libvslvm_checksum_calculate_weak_crc32(
143
     uint32_t *checksum,
144
     const uint8_t *buffer,
145
     size_t size,
146
     uint32_t initial_value,
147
     libcerror_error_t **error )
148
18.2k
{
149
18.2k
  static char *function = "libvslvm_checksum_calculate_weak_crc32";
150
18.2k
  size_t buffer_offset  = 0;
151
18.2k
  uint32_t table_index  = 0;
152
153
18.2k
  if( checksum == NULL )
154
0
  {
155
0
    libcerror_error_set(
156
0
     error,
157
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
158
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
159
0
     "%s: invalid checksum.",
160
0
     function );
161
162
0
    return( -1 );
163
0
  }
164
18.2k
  if( buffer == NULL )
165
0
  {
166
0
    libcerror_error_set(
167
0
     error,
168
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
169
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
170
0
     "%s: invalid buffer.",
171
0
     function );
172
173
0
    return( -1 );
174
0
  }
175
18.2k
  if( size > (size_t) SSIZE_MAX )
176
0
  {
177
0
    libcerror_error_set(
178
0
     error,
179
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
180
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
181
0
     "%s: invalid size value exceeds maximum.",
182
0
     function );
183
184
0
    return( -1 );
185
0
  }
186
18.2k
  *checksum = initial_value;
187
188
18.2k
        if( libvslvm_checksum_crc32_table_computed == 0 )
189
2
  {
190
2
    libvslvm_checksum_initialize_crc32_table();
191
2
  }
192
18.2k
        for( buffer_offset = 0;
193
278M
       buffer_offset < size;
194
278M
       buffer_offset++ )
195
278M
  {
196
278M
    table_index = ( *checksum ^ buffer[ buffer_offset ] ) & 0x000000ffUL;
197
198
278M
    *checksum = libvslvm_checksum_crc32_table[ table_index ] ^ ( *checksum >> 8 );
199
278M
        }
200
18.2k
        return( 1 );
201
18.2k
}
202