1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
|
/*
* Checksum functions
*
* Copyright (C) 2009-2016, Joachim Metz <joachim.metz@gmail.com>
*
* Refer to AUTHORS for acknowledgements.
*
* This software is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this software. If not, see <http://www.gnu.org/licenses/>.
*/
#include <common.h>
#include <byte_stream.h>
#include <types.h>
#include "libregf_checksum.h"
#include "libregf_libcerror.h"
/* The largest primary (or scalar) available
* supported by a single load and store instruction
*/
typedef unsigned long int libregf_aligned_t;
/* Calculates the little-endian XOR-32 of a buffer
* It uses the initial value to calculate a new XOR-32
* Returns 1 if successful or -1 on error
*/
int libregf_checksum_calculate_little_endian_xor32(
uint32_t *checksum_value,
const uint8_t *buffer,
size_t size,
uint32_t initial_value,
libcerror_error_t **error )
{
libregf_aligned_t *aligned_buffer_iterator = NULL;
uint8_t *buffer_iterator = NULL;
static char *function = "libregf_checksum_calculate_little_endian_xor32";
libregf_aligned_t value_aligned = 0;
uint32_t value_32bit = 0;
uint8_t alignment_count = 0;
uint8_t alignment_size = 0;
uint8_t byte_count = 0;
uint8_t byte_order = 0;
uint8_t byte_size = 0;
if( checksum_value == NULL )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
"%s: invalid checksum value.",
function );
return( -1 );
}
if( buffer == NULL )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
"%s: invalid buffer.",
function );
return( -1 );
}
if( size > (size_t) SSIZE_MAX )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
"%s: invalid size value exceeds maximum.",
function );
return( -1 );
}
*checksum_value = initial_value;
buffer_iterator = (uint8_t *) buffer;
/* Only optimize when there is the alignment is a multitude of 32-bit
* and for buffers larger than the alignment
*/
if( ( ( sizeof( libregf_aligned_t ) % 4 ) == 0 )
&& ( size > ( 2 * sizeof( libregf_aligned_t ) ) ) )
{
/* Align the buffer iterator
*/
alignment_size = (uint8_t) ( (intptr_t) buffer_iterator % sizeof( libregf_aligned_t ) );
byte_size = alignment_size;
while( byte_size != 0 )
{
value_32bit = 0;
byte_count = 1;
if( byte_size >= 4 )
{
value_32bit |= buffer_iterator[ 3 ];
value_32bit <<= 8;
byte_count++;
}
if( byte_size >= 3 )
{
value_32bit |= buffer_iterator[ 2 ];
value_32bit <<= 8;
byte_count++;
}
if( byte_size >= 2 )
{
value_32bit |= buffer_iterator[ 1 ];
value_32bit <<= 8;
byte_count++;
}
value_32bit |= buffer_iterator[ 0 ];
buffer_iterator += byte_count;
byte_size -= byte_count;
*checksum_value ^= value_32bit;
}
aligned_buffer_iterator = (libregf_aligned_t *) buffer_iterator;
size -= alignment_size;
if( *buffer_iterator != (uint8_t) ( *aligned_buffer_iterator & 0xff ) )
{
byte_order = _BYTE_STREAM_ENDIAN_BIG;
}
else
{
byte_order = _BYTE_STREAM_ENDIAN_LITTLE;
}
/* Determine the aligned XOR value
*/
while( size > sizeof( libregf_aligned_t ) )
{
value_aligned ^= *aligned_buffer_iterator;
aligned_buffer_iterator++;
size -= sizeof( libregf_aligned_t );
}
/* Align the aligned XOR value with the 32-bit XOR value
*/
if( alignment_size > 0 )
{
if( alignment_size >= sizeof( libregf_aligned_t ) )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_RUNTIME,
LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
"%s: invalid alignment size value out of bounds.",
function );
return( -1 );
}
byte_count = ( alignment_size % 4 ) * 8;
alignment_count = ( sizeof( libregf_aligned_t ) - alignment_size ) * 8;
if( byte_order == _BYTE_STREAM_ENDIAN_BIG )
{
/* Shift twice to set unused bytes to 0
*/
value_32bit = (uint32_t) ( ( value_aligned >> alignment_count ) << byte_count );
/* Strip-off the used part of the aligned value
*/
value_aligned <<= byte_count;
}
else if( byte_order == _BYTE_STREAM_ENDIAN_LITTLE )
{
value_32bit = (uint32_t) ( value_aligned << byte_count );
/* Strip-off the used part of the aligned value
*/
value_aligned >>= alignment_count;
}
*checksum_value ^= value_32bit;
}
/* Update the 32-bit XOR value with the aligned XOR value
*/
byte_size = (uint8_t) sizeof( libregf_aligned_t );
while( byte_size != 0 )
{
byte_count = ( ( byte_size / 4 ) - 1 ) * 32;
if( byte_order == _BYTE_STREAM_ENDIAN_BIG )
{
value_32bit = (uint32_t) ( value_aligned >> byte_count );
/* Change big-endian into little-endian
*/
value_32bit = ( ( value_32bit & 0x00ff ) << 24 )
| ( ( value_32bit & 0xff00 ) << 8 )
| ( ( value_32bit >> 8 ) & 0xff00 )
| ( ( value_32bit >> 24 ) & 0x00ff );
value_aligned <<= byte_count;
}
else if( byte_order == _BYTE_STREAM_ENDIAN_LITTLE )
{
value_32bit = (uint32_t) value_aligned;
value_aligned >>= byte_count;
}
byte_size -= 4;
*checksum_value ^= value_32bit;
}
/* Re-align the buffer iterator
*/
buffer_iterator = (uint8_t *) aligned_buffer_iterator;
byte_size = 4 - ( alignment_size % 4 );
if( byte_size != 4 )
{
value_32bit = buffer_iterator[ 0 ];
value_32bit <<= 8;
if( byte_size >= 2 )
{
value_32bit |= buffer_iterator[ 1 ];
}
value_32bit <<= 8;
if( byte_size >= 3 )
{
value_32bit |= buffer_iterator[ 2 ];
}
value_32bit <<= 8;
buffer_iterator += byte_size;
size -= byte_size;
*checksum_value ^= value_32bit;
}
}
while( size > 0 )
{
value_32bit = 0;
byte_count = 1;
if( size >= 4 )
{
value_32bit |= buffer_iterator[ 3 ];
value_32bit <<= 8;
byte_count++;
}
if( size >= 3 )
{
value_32bit |= buffer_iterator[ 2 ];
value_32bit <<= 8;
byte_count++;
}
if( size >= 2 )
{
value_32bit |= buffer_iterator[ 1 ];
value_32bit <<= 8;
byte_count++;
}
value_32bit |= buffer_iterator[ 0 ];
buffer_iterator += byte_count;
size -= byte_count;
*checksum_value ^= value_32bit;
}
return( 1 );
}
|