libuna_base32_stream.c 80.3 KB
Newer Older
1 2 3
/*
 * Base32 stream functions
 *
4
 * Copyright (C) 2008-2018, Joachim Metz <joachim.metz@gmail.com>
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
 *
 * 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 "libuna_base32_stream.h"
#include "libuna_definitions.h"
#include "libuna_libcerror.h"
#include "libuna_types.h"

31 32 33
static uint8_t libuna_base32_quintet_to_character_table[ 32 ] = {
	'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
	'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '2', '3', '4', '5', '6', '7' };
34

35 36 37
static uint8_t libuna_base32hex_quintet_to_character_table[ 32 ] = {
	'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
	'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V' };
38 39 40 41 42 43 44 45 46 47

/* Copies a base32 character to a base32 quintet
 * Returns 1 if successful or -1 on error
 */
int libuna_base32_character_copy_to_quintet(
     uint8_t base32_character,
     uint8_t *base32_quintet,
     uint32_t base32_variant,
     libcerror_error_t **error )
{
48 49
	static char *function       = "libuna_base32_character_copy_to_quintet";
	uint8_t safe_base32_quintet = 0;
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70

	if( base32_quintet == NULL )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
		 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
		 "%s: invalid base32 quintet.",
		 function );

		return( -1 );
	}
	switch( base32_variant & 0x000f0000UL )
	{
		case LIBUNA_BASE32_VARIANT_ALPHABET_NORMAL:
			/* A-Z is not a continous range on an EBCDIC based system
			 * it consists of the ranges: A-I, J-R, S-Z
			 */
			if( ( base32_character >= (uint8_t) 'A' )
			 && ( base32_character <= (uint8_t) 'I' ) )
			{
71
				safe_base32_quintet = base32_character - (uint8_t) 'A';
72 73 74 75
			}
			else if( ( base32_character >= (uint8_t) 'J' )
			      && ( base32_character <= (uint8_t) 'R' ) )
			{
76
				safe_base32_quintet = base32_character - (uint8_t) 'J' + 9;
77 78 79 80
			}
			else if( ( base32_character >= (uint8_t) 'S' )
			      && ( base32_character <= (uint8_t) 'Z' ) )
			{
81
				safe_base32_quintet = base32_character - (uint8_t) 'S' + 18;
82 83 84 85
			}
			else if( ( base32_character >= (uint8_t) '2' )
			      && ( base32_character <= (uint8_t) '7' ) )
			{
86
				safe_base32_quintet = base32_character - (uint8_t) '2' + 26;
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
			}
			else
			{
				libcerror_error_set(
				 error,
				 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
				 LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
				 "%s: invalid base32 character: 0x%02" PRIx8 ".",
				 function,
				 base32_character );

				return( -1 );
			}
			break;

		case LIBUNA_BASE32_VARIANT_ALPHABET_HEX:
			if( ( base32_character >= (uint8_t) '0' )
			 && ( base32_character <= (uint8_t) '9' ) )
			{
106
				safe_base32_quintet = base32_character - (uint8_t) '0';
107 108 109 110 111 112 113
			}
			/* A-V is not a continous range on an EBCDIC based system
			 * it consists of the ranges: A-I, J-R, S-V
			 */
			else if( ( base32_character >= (uint8_t) 'A' )
			      && ( base32_character <= (uint8_t) 'I' ) )
			{
114
				safe_base32_quintet = base32_character - (uint8_t) 'A' + 10;
115 116 117 118
			}
			else if( ( base32_character >= (uint8_t) 'J' )
			      && ( base32_character <= (uint8_t) 'R' ) )
			{
119
				safe_base32_quintet = base32_character - (uint8_t) 'J' + 19;
120 121 122 123
			}
			else if( ( base32_character >= (uint8_t) 'S' )
			      && ( base32_character <= (uint8_t) 'V' ) )
			{
124
				safe_base32_quintet = base32_character - (uint8_t) 'S' + 27;
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
			}
			else
			{
				libcerror_error_set(
				 error,
				 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
				 LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
				 "%s: invalid base32 character: 0x%02" PRIx8 ".",
				 function,
				 base32_character );

				return( -1 );
			}
			break;

		default:
			libcerror_error_set(
			 error,
			 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
			 LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
			 "%s: unsupported base32 variant.",
			 function );

			return( -1 );
	}
150 151
	*base32_quintet = safe_base32_quintet;

152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
	return( 1 );
}

/* Copies a base32 quintuplet from a base32 stream
 *
 * The padding size will still be set to indicate the number of
 * quintets in the quintuplet
 *
 * Returns 1 if successful or -1 on error
 */
int libuna_base32_quintuplet_copy_from_base32_stream(
     uint64_t *base32_quintuplet,
     const uint8_t *base32_stream,
     size_t base32_stream_size,
     size_t *base32_stream_index,
     uint8_t *padding_size,
     uint32_t base32_variant,
     libcerror_error_t **error )
{
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
	static char *function           = "libuna_base32_quintuplet_copy_from_base32_stream";
	size_t base32_character_size    = 0;
	size_t safe_base32_stream_index = 0;
	uint64_t safe_base32_quintuplet = 0;
	uint32_t base32_character1      = 0;
	uint32_t base32_character2      = 0;
	uint8_t quintet1                = 0;
	uint8_t quintet2                = 0;
	uint8_t quintet3                = 0;
	uint8_t quintet4                = 0;
	uint8_t quintet5                = 0;
	uint8_t quintet6                = 0;
	uint8_t quintet7                = 0;
	uint8_t quintet8                = 0;
	uint8_t safe_padding_size       = 0;
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 294 295

	if( base32_quintuplet == NULL )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
		 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
		 "%s: invalid base32 quintuplet.",
		 function );

		return( -1 );
	}
	if( base32_stream == NULL )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
		 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
		 "%s: invalid base32 stream.",
		 function );

		return( -1 );
	}
	if( base32_stream_size > (size_t) SSIZE_MAX )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
		 LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
		 "%s: invalid base32 stream size value exceeds maximum.",
		 function );

		return( -1 );
	}
	if( base32_stream_index == NULL )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
		 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
		 "%s: invalid base32 stream index.",
		 function );

		return( -1 );
	}
	if( *base32_stream_index >= base32_stream_size )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
		 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
		 "%s: base32 stream string too small.",
		 function );

		return( -1 );
	}
	if( padding_size == NULL )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
		 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
		 "%s: invalid padding size.",
		 function );

		return( -1 );
	}
	switch( base32_variant & 0x0f000000UL )
	{
		case LIBUNA_BASE32_VARIANT_PADDING_NONE:
		case LIBUNA_BASE32_VARIANT_PADDING_OPTIONAL:
		case LIBUNA_BASE32_VARIANT_PADDING_REQUIRED:
			break;

		default:
			libcerror_error_set(
			 error,
			 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
			 LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
			 "%s: unsupported base32 variant.",
			 function );

			return( -1 );
	}
	switch( base32_variant & 0xf0000000UL )
	{
		case LIBUNA_BASE32_VARIANT_ENCODING_BYTE_STREAM:
			base32_character_size = 1;
			break;

		case LIBUNA_BASE32_VARIANT_ENCODING_UTF16_BIG_ENDIAN:
		case LIBUNA_BASE32_VARIANT_ENCODING_UTF16_LITTLE_ENDIAN:
			base32_character_size = 2;
			break;

		case LIBUNA_BASE32_VARIANT_ENCODING_UTF32_BIG_ENDIAN:
		case LIBUNA_BASE32_VARIANT_ENCODING_UTF32_LITTLE_ENDIAN:
			base32_character_size = 4;
			break;

		default:
			libcerror_error_set(
			 error,
			 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
			 LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
			 "%s: unsupported base32 variant.",
			 function );

			return( -1 );
	}
296
	safe_base32_stream_index = *base32_stream_index;
297

298
	if( base32_character_size > ( base32_stream_size - safe_base32_stream_index ) )
299 300 301 302 303 304 305 306 307 308 309 310 311
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
		 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
		 "%s: missing 1st base32 character.",
		 function );

		return( -1 );
	}
	switch( base32_variant & 0xf0000000UL )
	{
		case LIBUNA_BASE32_VARIANT_ENCODING_BYTE_STREAM:
312
			base32_character1 = base32_stream[ safe_base32_stream_index ];
313 314 315 316
			break;

		case LIBUNA_BASE32_VARIANT_ENCODING_UTF16_BIG_ENDIAN:
			byte_stream_copy_to_uint16_big_endian(
317
			 &( base32_stream[ safe_base32_stream_index ] ),
318 319 320 321 322
			 base32_character1 );
			break;

		case LIBUNA_BASE32_VARIANT_ENCODING_UTF16_LITTLE_ENDIAN:
			byte_stream_copy_to_uint16_little_endian(
323
			 &( base32_stream[ safe_base32_stream_index ] ),
324 325 326 327 328
			 base32_character1 );
			break;

		case LIBUNA_BASE32_VARIANT_ENCODING_UTF32_BIG_ENDIAN:
			byte_stream_copy_to_uint32_big_endian(
329
			 &( base32_stream[ safe_base32_stream_index ] ),
330 331 332 333 334
			 base32_character1 );
			break;

		case LIBUNA_BASE32_VARIANT_ENCODING_UTF32_LITTLE_ENDIAN:
			byte_stream_copy_to_uint32_little_endian(
335
			 &( base32_stream[ safe_base32_stream_index ] ),
336 337 338
			 base32_character1 );
			break;
	}
339
	safe_base32_stream_index += base32_character_size;
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366

	if( ( base32_character1 & 0xffffff00UL ) != 0 )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
		 LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
		 "%s: invalid 1st base32 character.",
		 function );

		return( -1 );
	}
	if( libuna_base32_character_copy_to_quintet(
	     (uint8_t) base32_character1,
	     &quintet1,
	     base32_variant,
	     error ) != 1 )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_CONVERSION,
		 LIBCERROR_CONVERSION_ERROR_INPUT_FAILED,
		 "%s: unable to copy base32 character to 1st quintet.",
		 function );

		return( -1 );
	}
367
	if( base32_character_size > ( base32_stream_size - safe_base32_stream_index ) )
368 369 370 371 372 373 374 375 376 377 378 379 380
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
		 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
		 "%s: missing 2nd base32 character.",
		 function );

		return( -1 );
	}
	switch( base32_variant & 0xf0000000UL )
	{
		case LIBUNA_BASE32_VARIANT_ENCODING_BYTE_STREAM:
381
			base32_character1 = base32_stream[ safe_base32_stream_index ];
382 383 384 385
			break;

		case LIBUNA_BASE32_VARIANT_ENCODING_UTF16_BIG_ENDIAN:
			byte_stream_copy_to_uint16_big_endian(
386
			 &( base32_stream[ safe_base32_stream_index ] ),
387 388 389 390 391
			 base32_character1 );
			break;

		case LIBUNA_BASE32_VARIANT_ENCODING_UTF16_LITTLE_ENDIAN:
			byte_stream_copy_to_uint16_little_endian(
392
			 &( base32_stream[ safe_base32_stream_index ] ),
393 394 395 396 397
			 base32_character1 );
			break;

		case LIBUNA_BASE32_VARIANT_ENCODING_UTF32_BIG_ENDIAN:
			byte_stream_copy_to_uint32_big_endian(
398
			 &( base32_stream[ safe_base32_stream_index ] ),
399 400 401 402 403
			 base32_character1 );
			break;

		case LIBUNA_BASE32_VARIANT_ENCODING_UTF32_LITTLE_ENDIAN:
			byte_stream_copy_to_uint32_little_endian(
404
			 &( base32_stream[ safe_base32_stream_index ] ),
405 406 407
			 base32_character1 );
			break;
	}
408
	safe_base32_stream_index += base32_character_size;
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435

	if( ( base32_character1 & 0xffffff00UL ) != 0 )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
		 LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
		 "%s: invalid 2nd base32 character.",
		 function );

		return( -1 );
	}
	if( libuna_base32_character_copy_to_quintet(
	     (uint8_t) base32_character1,
	     &quintet2,
	     base32_variant,
	     error ) != 1 )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_CONVERSION,
		 LIBCERROR_CONVERSION_ERROR_INPUT_FAILED,
		 "%s: unable to copy base32 character to 2nd quintet.",
		 function );

		return( -1 );
	}
436
	safe_padding_size = 6;
437

438
	if( ( 2 * base32_character_size ) <= ( base32_stream_size - safe_base32_stream_index ) )
439 440 441 442
	{
		switch( base32_variant & 0xf0000000UL )
		{
			case LIBUNA_BASE32_VARIANT_ENCODING_BYTE_STREAM:
443 444
				base32_character1 = base32_stream[ safe_base32_stream_index ];
				base32_character2 = base32_stream[ safe_base32_stream_index + 1 ];
445 446 447 448
				break;

			case LIBUNA_BASE32_VARIANT_ENCODING_UTF16_BIG_ENDIAN:
				byte_stream_copy_to_uint16_big_endian(
449
				 &( base32_stream[ safe_base32_stream_index ] ),
450 451 452
				 base32_character1 );

				byte_stream_copy_to_uint16_big_endian(
453
				 &( base32_stream[ safe_base32_stream_index + 2 ] ),
454 455 456 457 458
				 base32_character2 );
				break;

			case LIBUNA_BASE32_VARIANT_ENCODING_UTF16_LITTLE_ENDIAN:
				byte_stream_copy_to_uint16_little_endian(
459
				 &( base32_stream[ safe_base32_stream_index ] ),
460 461 462
				 base32_character1 );

				byte_stream_copy_to_uint16_little_endian(
463
				 &( base32_stream[ safe_base32_stream_index + 2 ] ),
464 465 466 467 468
				 base32_character2 );
				break;

			case LIBUNA_BASE32_VARIANT_ENCODING_UTF32_BIG_ENDIAN:
				byte_stream_copy_to_uint32_big_endian(
469
				 &( base32_stream[ safe_base32_stream_index ] ),
470 471 472
				 base32_character1 );

				byte_stream_copy_to_uint32_big_endian(
473
				 &( base32_stream[ safe_base32_stream_index + 4 ] ),
474 475 476 477 478
				 base32_character2 );
				break;

			case LIBUNA_BASE32_VARIANT_ENCODING_UTF32_LITTLE_ENDIAN:
				byte_stream_copy_to_uint32_little_endian(
479
				 &( base32_stream[ safe_base32_stream_index ] ),
480 481 482
				 base32_character1 );

				byte_stream_copy_to_uint32_little_endian(
483
				 &( base32_stream[ safe_base32_stream_index + 4 ] ),
484 485 486
				 base32_character2 );
				break;
		}
487
		safe_base32_stream_index += 2 * base32_character_size;
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510

		if( ( base32_character1 & 0xffffff00UL ) != 0 )
		{
			libcerror_error_set(
			 error,
			 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
			 LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
			 "%s: invalid 3rd base32 character.",
			 function );

			return( -1 );
		}
		if( ( base32_character2 & 0xffffff00UL ) != 0 )
		{
			libcerror_error_set(
			 error,
			 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
			 LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
			 "%s: invalid 4th base32 character.",
			 function );

			return( -1 );
		}
511 512
		if( ( base32_character1 == (uint32_t) '=' )
		 || ( base32_character2 == (uint32_t) '=' ) )
513
		{
514 515 516 517 518 519 520 521 522 523 524 525
			if( ( base32_variant & 0x0f000000UL ) == LIBUNA_BASE32_VARIANT_PADDING_NONE )
			{
				libcerror_error_set(
				 error,
				 LIBCERROR_ERROR_DOMAIN_RUNTIME,
				 LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
				 "%s: padding where not supposed to - invalid 3rd or 4th base32 character.",
				 function );

				return( -1 );
			}
			if( base32_character1 != (uint32_t) '=' )
526 527 528 529 530 531 532 533 534 535
			{
				libcerror_error_set(
				 error,
				 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
				 LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
				 "%s: invalid 3rd base32 character.",
				 function );

				return( -1 );
			}
536
			if( base32_character2 != (uint32_t) '=' )
537 538 539 540 541 542 543 544 545 546 547
			{
				libcerror_error_set(
				 error,
				 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
				 LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
				 "%s: invalid 4th base32 character.",
				 function );

				return( -1 );
			}
		}
548
		else
549
		{
550 551 552 553 554
			if( libuna_base32_character_copy_to_quintet(
			     (uint8_t) base32_character1,
			     &quintet3,
			     base32_variant,
			     error ) != 1 )
555 556 557 558 559 560 561 562 563 564
			{
				libcerror_error_set(
				 error,
				 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
				 LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
				 "%s: invalid 3rd base32 quintet.",
				 function );

				return( -1 );
			}
565 566 567 568 569
			if( libuna_base32_character_copy_to_quintet(
			     (uint8_t) base32_character2,
			     &quintet4,
			     base32_variant,
			     error ) != 1 )
570 571 572 573 574 575 576 577 578 579
			{
				libcerror_error_set(
				 error,
				 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
				 LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
				 "%s: invalid 4th base32 quintet.",
				 function );

				return( -1 );
			}
580
			safe_padding_size -= 2;
581 582
		}
	}
583
	else if( ( base32_variant & 0x0f000000UL ) != LIBUNA_BASE32_VARIANT_PADDING_NONE )
584
	{
585 586 587 588 589 590
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
		 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
		 "%s: missing 3rd or 4th base32 character.",
		 function );
591

592
		return( -1 );
593
	}
594
	if( base32_character_size <= ( base32_stream_size - safe_base32_stream_index ) )
595 596 597 598
	{
		switch( base32_variant & 0xf0000000UL )
		{
			case LIBUNA_BASE32_VARIANT_ENCODING_BYTE_STREAM:
599
				base32_character1 = base32_stream[ safe_base32_stream_index ];
600 601 602 603
				break;

			case LIBUNA_BASE32_VARIANT_ENCODING_UTF16_BIG_ENDIAN:
				byte_stream_copy_to_uint16_big_endian(
604
				 &( base32_stream[ safe_base32_stream_index ] ),
605 606 607 608 609
				 base32_character1 );
				break;

			case LIBUNA_BASE32_VARIANT_ENCODING_UTF16_LITTLE_ENDIAN:
				byte_stream_copy_to_uint16_little_endian(
610
				 &( base32_stream[ safe_base32_stream_index ] ),
611 612 613 614 615
				 base32_character1 );
				break;

			case LIBUNA_BASE32_VARIANT_ENCODING_UTF32_BIG_ENDIAN:
				byte_stream_copy_to_uint32_big_endian(
616
				 &( base32_stream[ safe_base32_stream_index ] ),
617 618 619 620 621
				 base32_character1 );
				break;

			case LIBUNA_BASE32_VARIANT_ENCODING_UTF32_LITTLE_ENDIAN:
				byte_stream_copy_to_uint32_little_endian(
622
				 &( base32_stream[ safe_base32_stream_index ] ),
623 624 625
				 base32_character1 );
				break;
		}
626
		safe_base32_stream_index += base32_character_size;
627 628 629 630 631 632 633 634 635 636 637 638

		if( ( base32_character1 & 0xffffff00UL ) != 0 )
		{
			libcerror_error_set(
			 error,
			 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
			 LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
			 "%s: invalid 5th base32 character.",
			 function );

			return( -1 );
		}
639
		if( base32_character1 == (uint32_t) '=' )
640
		{
641
			if( ( base32_variant & 0x0f000000UL ) == LIBUNA_BASE32_VARIANT_PADDING_NONE )
642 643 644
			{
				libcerror_error_set(
				 error,
645 646 647
				 LIBCERROR_ERROR_DOMAIN_RUNTIME,
				 LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
				 "%s: padding where not supposed to - invalid 5th base32 character.",
648 649 650 651 652
				 function );

				return( -1 );
			}
		}
653
		else
654
		{
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
			if( safe_padding_size > 4 )
			{
				libcerror_error_set(
				 error,
				 LIBCERROR_ERROR_DOMAIN_RUNTIME,
				 LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
				 "%s: non-padding where not supposed to - invalid 5th base32 character.",
				 function );

				return( -1 );
			}
			if( libuna_base32_character_copy_to_quintet(
			     (uint8_t) base32_character1,
			     &quintet5,
			     base32_variant,
			     error ) != 1 )
671 672 673 674 675 676 677 678 679 680
			{
				libcerror_error_set(
				 error,
				 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
				 LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
				 "%s: invalid 5th base32 quintet.",
				 function );

				return( -1 );
			}
681
			safe_padding_size -= 1;
682 683
		}
	}
684
	else if( ( base32_variant & 0x0f000000UL ) != LIBUNA_BASE32_VARIANT_PADDING_NONE )
685 686 687 688 689 690 691 692 693 694
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
		 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
		 "%s: missing 5th base32 character.",
		 function );

		return( -1 );
	}
695
	if( ( 2 * base32_character_size ) <= ( base32_stream_size - safe_base32_stream_index ) )
696 697 698 699
	{
		switch( base32_variant & 0xf0000000UL )
		{
			case LIBUNA_BASE32_VARIANT_ENCODING_BYTE_STREAM:
700 701
				base32_character1 = base32_stream[ safe_base32_stream_index ];
				base32_character2 = base32_stream[ safe_base32_stream_index + 1 ];
702 703 704 705
				break;

			case LIBUNA_BASE32_VARIANT_ENCODING_UTF16_BIG_ENDIAN:
				byte_stream_copy_to_uint16_big_endian(
706
				 &( base32_stream[ safe_base32_stream_index ] ),
707 708 709
				 base32_character1 );

				byte_stream_copy_to_uint16_big_endian(
710
				 &( base32_stream[ safe_base32_stream_index + 2 ] ),
711 712 713 714 715
				 base32_character2 );
				break;

			case LIBUNA_BASE32_VARIANT_ENCODING_UTF16_LITTLE_ENDIAN:
				byte_stream_copy_to_uint16_little_endian(
716
				 &( base32_stream[ safe_base32_stream_index ] ),
717 718 719
				 base32_character1 );

				byte_stream_copy_to_uint16_little_endian(
720
				 &( base32_stream[ safe_base32_stream_index + 2 ] ),
721 722 723 724 725
				 base32_character2 );
				break;

			case LIBUNA_BASE32_VARIANT_ENCODING_UTF32_BIG_ENDIAN:
				byte_stream_copy_to_uint32_big_endian(
726
				 &( base32_stream[ safe_base32_stream_index ] ),
727 728 729
				 base32_character1 );

				byte_stream_copy_to_uint32_big_endian(
730
				 &( base32_stream[ safe_base32_stream_index + 4 ] ),
731 732 733 734 735
				 base32_character2 );
				break;

			case LIBUNA_BASE32_VARIANT_ENCODING_UTF32_LITTLE_ENDIAN:
				byte_stream_copy_to_uint32_little_endian(
736
				 &( base32_stream[ safe_base32_stream_index ] ),
737 738 739
				 base32_character1 );

				byte_stream_copy_to_uint32_little_endian(
740
				 &( base32_stream[ safe_base32_stream_index + 4 ] ),
741 742 743
				 base32_character2 );
				break;
		}
744
		safe_base32_stream_index += 2 * base32_character_size;
745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767

		if( ( base32_character1 & 0xffffff00UL ) != 0 )
		{
			libcerror_error_set(
			 error,
			 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
			 LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
			 "%s: invalid 6th base32 character.",
			 function );

			return( -1 );
		}
		if( ( base32_character2 & 0xffffff00UL ) != 0 )
		{
			libcerror_error_set(
			 error,
			 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
			 LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
			 "%s: invalid 7th base32 character.",
			 function );

			return( -1 );
		}
768 769
		if( ( base32_character1 == (uint32_t) '=' )
		 || ( base32_character2 == (uint32_t) '=' ) )
770
		{
771 772 773 774 775 776 777 778 779 780 781 782
			if( ( base32_variant & 0x0f000000UL ) == LIBUNA_BASE32_VARIANT_PADDING_NONE )
			{
				libcerror_error_set(
				 error,
				 LIBCERROR_ERROR_DOMAIN_RUNTIME,
				 LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
				 "%s: padding where not supposed to - invalid 6th or 7th base32 character.",
				 function );

				return( -1 );
			}
			if( base32_character1 != (uint32_t) '=' )
783 784 785 786 787 788 789 790 791 792
			{
				libcerror_error_set(
				 error,
				 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
				 LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
				 "%s: invalid 6th base32 quintet.",
				 function );

				return( -1 );
			}
793
			if( base32_character2 != (uint32_t) '=' )
794 795 796 797 798 799 800 801 802 803 804
			{
				libcerror_error_set(
				 error,
				 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
				 LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
				 "%s: invalid 7th base32 quintet.",
				 function );

				return( -1 );
			}
		}
805
		else
806
		{
807
			if( safe_padding_size > 3 )
808 809 810
			{
				libcerror_error_set(
				 error,
811 812 813
				 LIBCERROR_ERROR_DOMAIN_RUNTIME,
				 LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
				 "%s: non-padding where not supposed to - invalid 6th or 7th base32 character.",
814 815 816 817
				 function );

				return( -1 );
			}
818 819 820 821 822
			if( libuna_base32_character_copy_to_quintet(
			     (uint8_t) base32_character1,
			     &quintet6,
			     base32_variant,
			     error ) != 1 )
823 824 825 826 827 828 829 830 831 832
			{
				libcerror_error_set(
				 error,
				 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
				 LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
				 "%s: invalid 6th base32 quintet.",
				 function );

				return( -1 );
			}
833 834 835 836 837
			if( libuna_base32_character_copy_to_quintet(
			     (uint8_t) base32_character2,
			     &quintet7,
			     base32_variant,
			     error ) != 1 )
838 839 840 841 842 843 844 845 846 847
			{
				libcerror_error_set(
				 error,
				 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
				 LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
				 "%s: invalid 7th base32 quintet.",
				 function );

				return( -1 );
			}
848
			safe_padding_size -= 2;
849 850
		}
	}
851
	else if( ( base32_variant & 0x0f000000UL ) != LIBUNA_BASE32_VARIANT_PADDING_NONE )
852
	{
853 854 855 856 857 858
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
		 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
		 "%s: missing 6th or 7th base32 character.",
		 function );
859

860
		return( -1 );
861
	}
862
	if( base32_character_size <= ( base32_stream_size - safe_base32_stream_index ) )
863 864 865 866
	{
		switch( base32_variant & 0xf0000000UL )
		{
			case LIBUNA_BASE32_VARIANT_ENCODING_BYTE_STREAM:
867
				base32_character1 = base32_stream[ safe_base32_stream_index ];
868 869 870 871
				break;

			case LIBUNA_BASE32_VARIANT_ENCODING_UTF16_BIG_ENDIAN:
				byte_stream_copy_to_uint16_big_endian(
872
				 &( base32_stream[ safe_base32_stream_index ] ),
873 874 875 876 877
				 base32_character1 );
				break;

			case LIBUNA_BASE32_VARIANT_ENCODING_UTF16_LITTLE_ENDIAN:
				byte_stream_copy_to_uint16_little_endian(
878
				 &( base32_stream[ safe_base32_stream_index ] ),
879 880 881 882 883
				 base32_character1 );
				break;

			case LIBUNA_BASE32_VARIANT_ENCODING_UTF32_BIG_ENDIAN:
				byte_stream_copy_to_uint32_big_endian(
884
				 &( base32_stream[ safe_base32_stream_index ] ),
885 886 887 888 889
				 base32_character1 );
				break;

			case LIBUNA_BASE32_VARIANT_ENCODING_UTF32_LITTLE_ENDIAN:
				byte_stream_copy_to_uint32_little_endian(
890
				 &( base32_stream[ safe_base32_stream_index ] ),
891 892 893
				 base32_character1 );
				break;
		}
894
		safe_base32_stream_index += base32_character_size;
895 896 897 898 899 900 901 902 903 904 905 906

		if( ( base32_character1 & 0xffffff00UL ) != 0 )
		{
			libcerror_error_set(
			 error,
			 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
			 LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
			 "%s: invalid 8th base32 character.",
			 function );

			return( -1 );
		}
907
		if( base32_character1 == (uint32_t) '=' )
908
		{
909
			if( ( base32_variant & 0x0f000000UL ) == LIBUNA_BASE32_VARIANT_PADDING_NONE )
910 911 912
			{
				libcerror_error_set(
				 error,
913 914 915
				 LIBCERROR_ERROR_DOMAIN_RUNTIME,
				 LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
				 "%s: padding where not supposed to - invalid 8th base32 character.",
916 917 918 919 920
				 function );

				return( -1 );
			}
		}
921
		else
922
		{
923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938
			if( safe_padding_size > 1 )
			{
				libcerror_error_set(
				 error,
				 LIBCERROR_ERROR_DOMAIN_RUNTIME,
				 LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
				 "%s: non-padding where not supposed to - invalid 8th base32 character.",
				 function );

				return( -1 );
			}
			if( libuna_base32_character_copy_to_quintet(
			     (uint8_t) base32_character1,
			     &quintet8,
			     base32_variant,
			     error ) != 1 )
939 940 941 942 943 944 945 946 947 948
			{
				libcerror_error_set(
				 error,
				 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
				 LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
				 "%s: invalid 8th base32 quintet.",
				 function );

				return( -1 );
			}
949
			safe_padding_size -= 1;
950 951
		}
	}
952
	else if( ( base32_variant & 0x0f000000UL ) != LIBUNA_BASE32_VARIANT_PADDING_NONE )
953 954 955 956 957 958 959 960 961 962
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
		 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
		 "%s: missing 8th base32 character.",
		 function );

		return( -1 );
	}
963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981
	safe_base32_quintuplet   = quintet1;
	safe_base32_quintuplet <<= 5;
	safe_base32_quintuplet  |= quintet2;
	safe_base32_quintuplet <<= 5;
	safe_base32_quintuplet  |= quintet3;
	safe_base32_quintuplet <<= 5;
	safe_base32_quintuplet  |= quintet4;
	safe_base32_quintuplet <<= 5;
	safe_base32_quintuplet  |= quintet5;
	safe_base32_quintuplet <<= 5;
	safe_base32_quintuplet  |= quintet6;
	safe_base32_quintuplet <<= 5;
	safe_base32_quintuplet  |= quintet7;
	safe_base32_quintuplet <<= 5;
	safe_base32_quintuplet  |= quintet8;

	*base32_stream_index = safe_base32_stream_index;
	*base32_quintuplet   = safe_base32_quintuplet;
	*padding_size        = safe_padding_size;
982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999

	return( 1 );
}

/* Copies a base32 quintuplet to a base32 stream
 * Returns 1 if successful or -1 on error
 */
int libuna_base32_quintuplet_copy_to_base32_stream(
     uint64_t base32_quintuplet,
     uint8_t *base32_stream,
     size_t base32_stream_size,
     size_t *base32_stream_index,
     uint8_t padding_size,
     uint32_t base32_variant,
     libcerror_error_t **error )
{
	uint8_t *quintet_to_character_table = NULL;
	static char *function               = "libuna_base32_quintuplet_copy_to_base32_stream";
1000 1001 1002 1003
	size_t base32_character_size        = 0;
	size_t safe_base32_stream_index     = 0;
	uint32_t base32_character1          = 0;
	uint32_t base32_character2          = 0;
1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091
	uint8_t quintet1                    = 0;
	uint8_t quintet2                    = 0;
	uint8_t quintet3                    = 0;
	uint8_t quintet4                    = 0;
	uint8_t quintet5                    = 0;
	uint8_t quintet6                    = 0;
	uint8_t quintet7                    = 0;
	uint8_t quintet8                    = 0;

	if( base32_stream == NULL )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
		 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
		 "%s: invalid base32 stream.",
		 function );

		return( -1 );
	}
	if( base32_stream_size > (size_t) SSIZE_MAX )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
		 LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
		 "%s: invalid base32 stream size value exceeds maximum.",
		 function );

		return( -1 );
	}
	if( base32_stream_index == NULL )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
		 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
		 "%s: invalid base32 stream index.",
		 function );

		return( -1 );
	}
	if( *base32_stream_index >= base32_stream_size )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
		 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
		 "%s: base32 stream string too small.",
		 function );

		return( -1 );
	}
	if( padding_size > 6 )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
		 LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
		 "%s: invalid padding size value out of bounds.",
		 function );

		return( -1 );
	}
	switch( base32_variant & 0x000f0000UL )
	{
		case LIBUNA_BASE32_VARIANT_ALPHABET_NORMAL:
			quintet_to_character_table = libuna_base32_quintet_to_character_table;
			break;

		case LIBUNA_BASE32_VARIANT_ALPHABET_HEX:
			quintet_to_character_table = libuna_base32hex_quintet_to_character_table;
			break;

		default:
			libcerror_error_set(
			 error,
			 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
			 LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
			 "%s: unsupported base32 variant.",
			 function );

			return( -1 );
	}
	switch( base32_variant & 0x0f000000UL )
	{
		case LIBUNA_BASE32_VARIANT_PADDING_NONE:
		case LIBUNA_BASE32_VARIANT_PADDING_OPTIONAL:
1092
		case LIBUNA_BASE32_VARIANT_PADDING_REQUIRED:
1093 1094
			break;

1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118
		default:
			libcerror_error_set(
			 error,
			 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
			 LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
			 "%s: unsupported base32 variant.",
			 function );

			return( -1 );
	}
	switch( base32_variant & 0xf0000000UL )
	{
		case LIBUNA_BASE32_VARIANT_ENCODING_BYTE_STREAM:
			base32_character_size = 1;
			break;

		case LIBUNA_BASE32_VARIANT_ENCODING_UTF16_BIG_ENDIAN:
		case LIBUNA_BASE32_VARIANT_ENCODING_UTF16_LITTLE_ENDIAN:
			base32_character_size = 2;
			break;

		case LIBUNA_BASE32_VARIANT_ENCODING_UTF32_BIG_ENDIAN:
		case LIBUNA_BASE32_VARIANT_ENCODING_UTF32_LITTLE_ENDIAN:
			base32_character_size = 4;
1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130
			break;

		default:
			libcerror_error_set(
			 error,
			 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
			 LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
			 "%s: unsupported base32 variant.",
			 function );

			return( -1 );
	}
1131
	safe_base32_stream_index = *base32_stream_index;
1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152

	/* Separate the 3 bytes value into 8 x 5 bit values
	 */
	quintet8          = (uint8_t) ( base32_quintuplet & 0x1f );
	base32_quintuplet >>= 5;
	quintet7          = (uint8_t) ( base32_quintuplet & 0x1f );
	base32_quintuplet >>= 5;
	quintet6          = (uint8_t) ( base32_quintuplet & 0x1f );
	base32_quintuplet >>= 5;
	quintet5          = (uint8_t) ( base32_quintuplet & 0x1f );
	base32_quintuplet >>= 5;
	quintet4          = (uint8_t) ( base32_quintuplet & 0x1f );
	base32_quintuplet >>= 5;
	quintet3          = (uint8_t) ( base32_quintuplet & 0x1f );
	base32_quintuplet >>= 5;
	quintet2          = (uint8_t) ( base32_quintuplet & 0x1f );
	base32_quintuplet >>= 5;
	quintet1          = (uint8_t) ( base32_quintuplet & 0x1f );

	/* Spread the encoding over 2 characters if 1 byte is available
	 */
1153
	if( ( 2 * base32_character_size ) > ( base32_stream_size - safe_base32_stream_index ) )
1154 1155 1156 1157 1158
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
		 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
1159
		 "%s: base32 stream is too small - insufficient space for 1st and 2nd base32 characters.",
1160 1161 1162 1163
		 function );

		return( -1 );
	}
1164 1165
	base32_character1 = (uint32_t) quintet_to_character_table[ quintet1 ];
	base32_character2 = (uint32_t) quintet_to_character_table[ quintet2 ];
1166

1167
	switch( base32_variant & 0xf0000000UL )
1168
	{
1169 1170 1171 1172
		case LIBUNA_BASE32_VARIANT_ENCODING_BYTE_STREAM:
			base32_stream[ safe_base32_stream_index     ] = (uint8_t) base32_character1;
			base32_stream[ safe_base32_stream_index + 1 ] = (uint8_t) base32_character2;
			break;
1173

1174 1175 1176 1177
		case LIBUNA_BASE32_VARIANT_ENCODING_UTF16_BIG_ENDIAN:
			byte_stream_copy_from_uint16_big_endian(
			 &( base32_stream[ safe_base32_stream_index ] ),
			 base32_character1 );
1178

1179 1180 1181 1182
			byte_stream_copy_from_uint16_big_endian(
			 &( base32_stream[ safe_base32_stream_index + 2 ] ),
			 base32_character2 );
			break;
1183

1184 1185 1186 1187
		case LIBUNA_BASE32_VARIANT_ENCODING_UTF16_LITTLE_ENDIAN:
			byte_stream_copy_from_uint16_little_endian(
			 &( base32_stream[ safe_base32_stream_index ] ),
			 base32_character1 );
1188

1189 1190 1191 1192
			byte_stream_copy_from_uint16_little_endian(
			 &( base32_stream[ safe_base32_stream_index + 2 ] ),
			 base32_character2 );
			break;
1193

1194 1195 1196 1197
		case LIBUNA_BASE32_VARIANT_ENCODING_UTF32_BIG_ENDIAN:
			byte_stream_copy_from_uint32_big_endian(
			 &( base32_stream[ safe_base32_stream_index ] ),
			 base32_character1 );
1198

1199 1200 1201 1202
			byte_stream_copy_from_uint32_big_endian(
			 &( base32_stream[ safe_base32_stream_index + 4 ] ),
			 base32_character2 );
			break;
1203

1204 1205 1206 1207
		case LIBUNA_BASE32_VARIANT_ENCODING_UTF32_LITTLE_ENDIAN:
			byte_stream_copy_from_uint32_little_endian(
			 &( base32_stream[ safe_base32_stream_index ] ),
			 base32_character1 );
1208

1209 1210 1211 1212
			byte_stream_copy_from_uint32_little_endian(
			 &( base32_stream[ safe_base32_stream_index + 4 ] ),
			 base32_character2 );
			break;
1213
	}
1214
	safe_base32_stream_index += 2 * base32_character_size;
1215

1216
	/* Spread the encoding over 4 characters if 2 bytes are available
1217 1218
	 * Otherwise pad the remaining bytes if required
	 */
1219 1220
	if( ( padding_size < 6 )
	 || ( ( base32_variant & 0x0f000000UL ) != LIBUNA_BASE32_VARIANT_PADDING_NONE ) )
1221
	{
1222 1223 1224 1225 1226 1227 1228 1229
		if( ( 2 * base32_character_size ) > ( base32_stream_size - safe_base32_stream_index ) )
		{
			libcerror_error_set(
			 error,
			 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
			 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
			 "%s: base32 stream is too small - insufficient space for 3rd and 4th base32 characters.",
			 function );
1230

1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248
			return( -1 );
		}
		if( padding_size < 6 )
		{
			base32_character1 = (uint32_t) quintet_to_character_table[ quintet3 ];
			base32_character2 = (uint32_t) quintet_to_character_table[ quintet4 ];
		}
		else
		{
			base32_character1 = (uint32_t) '=';
			base32_character2 = (uint32_t) '=';
		}
		switch( base32_variant & 0xf0000000UL )
		{
			case LIBUNA_BASE32_VARIANT_ENCODING_BYTE_STREAM:
				base32_stream[ safe_base32_stream_index     ] = (uint8_t) base32_character1;
				base32_stream[ safe_base32_stream_index + 1 ] = (uint8_t) base32_character2;
				break;
1249

1250 1251 1252 1253
			case LIBUNA_BASE32_VARIANT_ENCODING_UTF16_BIG_ENDIAN:
				byte_stream_copy_from_uint16_big_endian(
				 &( base32_stream[ safe_base32_stream_index ] ),
				 base32_character1 );
1254

1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485
				byte_stream_copy_from_uint16_big_endian(
				 &( base32_stream[ safe_base32_stream_index + 2 ] ),
				 base32_character2 );
				break;

			case LIBUNA_BASE32_VARIANT_ENCODING_UTF16_LITTLE_ENDIAN:
				byte_stream_copy_from_uint16_little_endian(
				 &( base32_stream[ safe_base32_stream_index ] ),
				 base32_character1 );

				byte_stream_copy_from_uint16_little_endian(
				 &( base32_stream[ safe_base32_stream_index + 2 ] ),
				 base32_character2 );
				break;

			case LIBUNA_BASE32_VARIANT_ENCODING_UTF32_BIG_ENDIAN:
				byte_stream_copy_from_uint32_big_endian(
				 &( base32_stream[ safe_base32_stream_index ] ),
				 base32_character1 );

				byte_stream_copy_from_uint32_big_endian(
				 &( base32_stream[ safe_base32_stream_index + 4 ] ),
				 base32_character2 );
				break;

			case LIBUNA_BASE32_VARIANT_ENCODING_UTF32_LITTLE_ENDIAN:
				byte_stream_copy_from_uint32_little_endian(
				 &( base32_stream[ safe_base32_stream_index ] ),
				 base32_character1 );

				byte_stream_copy_from_uint32_little_endian(
				 &( base32_stream[ safe_base32_stream_index + 4 ] ),
				 base32_character2 );
				break;
		}
		safe_base32_stream_index += 2 * base32_character_size;
	}
	/* Spread the encoding over 5 characters if 3 bytes are available
	 * Otherwise pad the remaining bytes if required
	 */
	if( ( padding_size < 4 )
	 || ( ( base32_variant & 0x0f000000UL ) != LIBUNA_BASE32_VARIANT_PADDING_NONE ) )
	{
		if( base32_character_size > ( base32_stream_size - safe_base32_stream_index ) )
		{
			libcerror_error_set(
			 error,
			 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
			 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
			 "%s: base32 stream is too small - insufficient space for 5th base32 character.",
			 function );

			return( -1 );
		}
		if( padding_size < 4 )
		{
			base32_character1 = (uint32_t) quintet_to_character_table[ quintet5 ];
		}
		else
		{
			base32_character1 = (uint32_t) '=';
		}
		switch( base32_variant & 0xf0000000UL )
		{
			case LIBUNA_BASE32_VARIANT_ENCODING_BYTE_STREAM:
				base32_stream[ safe_base32_stream_index ] = (uint8_t) base32_character1;
				break;

			case LIBUNA_BASE32_VARIANT_ENCODING_UTF16_BIG_ENDIAN:
				byte_stream_copy_from_uint16_big_endian(
				 &( base32_stream[ safe_base32_stream_index ] ),
				 base32_character1 );
				break;

			case LIBUNA_BASE32_VARIANT_ENCODING_UTF16_LITTLE_ENDIAN:
				byte_stream_copy_from_uint16_little_endian(
				 &( base32_stream[ safe_base32_stream_index ] ),
				 base32_character1 );
				break;

			case LIBUNA_BASE32_VARIANT_ENCODING_UTF32_BIG_ENDIAN:
				byte_stream_copy_from_uint32_big_endian(
				 &( base32_stream[ safe_base32_stream_index ] ),
				 base32_character1 );
				break;

			case LIBUNA_BASE32_VARIANT_ENCODING_UTF32_LITTLE_ENDIAN:
				byte_stream_copy_from_uint32_little_endian(
				 &( base32_stream[ safe_base32_stream_index ] ),
				 base32_character1 );
				break;
		}
		safe_base32_stream_index += base32_character_size;
	}
	/* Spread the encoding over 7 characters if 4 bytes are available
	 * Otherwise pad the remaining bytes if required
	 */
	if( ( padding_size < 3 )
	 || ( ( base32_variant & 0x0f000000UL ) != LIBUNA_BASE32_VARIANT_PADDING_NONE ) )
	{
		if( ( 2 * base32_character_size ) > ( base32_stream_size - safe_base32_stream_index ) )
		{
			libcerror_error_set(
			 error,
			 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
			 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
			 "%s: base32 stream is too small - insufficient space for 6th and 7th base32 characters.",
			 function );

			return( -1 );
		}
		if( padding_size < 3 )
		{
			base32_character1 = (uint32_t) quintet_to_character_table[ quintet6 ];
			base32_character2 = (uint32_t) quintet_to_character_table[ quintet7 ];
		}
		else
		{
			base32_character1 = (uint32_t) '=';
			base32_character2 = (uint32_t) '=';
		}
		switch( base32_variant & 0xf0000000UL )
		{
			case LIBUNA_BASE32_VARIANT_ENCODING_BYTE_STREAM:
				base32_stream[ safe_base32_stream_index     ] = (uint8_t) base32_character1;
				base32_stream[ safe_base32_stream_index + 1 ] = (uint8_t) base32_character2;
				break;

			case LIBUNA_BASE32_VARIANT_ENCODING_UTF16_BIG_ENDIAN:
				byte_stream_copy_from_uint16_big_endian(
				 &( base32_stream[ safe_base32_stream_index ] ),
				 base32_character1 );

				byte_stream_copy_from_uint16_big_endian(
				 &( base32_stream[ safe_base32_stream_index + 2 ] ),
				 base32_character2 );
				break;

			case LIBUNA_BASE32_VARIANT_ENCODING_UTF16_LITTLE_ENDIAN:
				byte_stream_copy_from_uint16_little_endian(
				 &( base32_stream[ safe_base32_stream_index ] ),
				 base32_character1 );

				byte_stream_copy_from_uint16_little_endian(
				 &( base32_stream[ safe_base32_stream_index + 2 ] ),
				 base32_character2 );
				break;

			case LIBUNA_BASE32_VARIANT_ENCODING_UTF32_BIG_ENDIAN:
				byte_stream_copy_from_uint32_big_endian(
				 &( base32_stream[ safe_base32_stream_index ] ),
				 base32_character1 );

				byte_stream_copy_from_uint32_big_endian(
				 &( base32_stream[ safe_base32_stream_index + 4 ] ),
				 base32_character2 );
				break;

			case LIBUNA_BASE32_VARIANT_ENCODING_UTF32_LITTLE_ENDIAN:
				byte_stream_copy_from_uint32_little_endian(
				 &( base32_stream[ safe_base32_stream_index ] ),
				 base32_character1 );

				byte_stream_copy_from_uint32_little_endian(
				 &( base32_stream[ safe_base32_stream_index + 4 ] ),
				 base32_character2 );
				break;
		}
		safe_base32_stream_index += 2 * base32_character_size;
	}
	/* Spread the encoding over 8 characters if 5 bytes are available
	 * Otherwise pad the remaining bytes if required
	 */
	if( ( padding_size < 1 )
	 || ( ( base32_variant & 0x0f000000UL ) != LIBUNA_BASE32_VARIANT_PADDING_NONE ) )
	{
		if( base32_character_size > ( base32_stream_size - safe_base32_stream_index ) )
		{
			libcerror_error_set(
			 error,
			 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
			 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
			 "%s: base32 stream is too small - insufficient space for 8th base32 character.",
			 function );

			return( -1 );
		}
		if( padding_size < 1 )
		{
			base32_character1 = (uint32_t) quintet_to_character_table[ quintet8 ];
		}
		else
		{
			base32_character1 = (uint32_t) '=';
		}
		switch( base32_variant & 0xf0000000UL )
		{
			case LIBUNA_BASE32_VARIANT_ENCODING_BYTE_STREAM:
				base32_stream[ safe_base32_stream_index ] = (uint8_t) base32_character1;
				break;

			case LIBUNA_BASE32_VARIANT_ENCODING_UTF16_BIG_ENDIAN:
				byte_stream_copy_from_uint16_big_endian(
				 &( base32_stream[ safe_base32_stream_index ] ),
				 base32_character1 );
				break;

			case LIBUNA_BASE32_VARIANT_ENCODING_UTF16_LITTLE_ENDIAN:
				byte_stream_copy_from_uint16_little_endian(
				 &( base32_stream[ safe_base32_stream_index ] ),
				 base32_character1 );
				break;

			case LIBUNA_BASE32_VARIANT_ENCODING_UTF32_BIG_ENDIAN:
				byte_stream_copy_from_uint32_big_endian(
				 &( base32_stream[ safe_base32_stream_index ] ),
				 base32_character1 );
				break;

			case LIBUNA_BASE32_VARIANT_ENCODING_UTF32_LITTLE_ENDIAN:
				byte_stream_copy_from_uint32_little_endian(
				 &( base32_stream[ safe_base32_stream_index ] ),
				 base32_character1 );
				break;
		}
		safe_base32_stream_index += base32_character_size;
	}
	*base32_stream_index = safe_base32_stream_index;

	return( 1 );
}
1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497

/* Copies a base32 quintuplet from a byte stream
 * Returns 1 if successful or -1 on error
 */
int libuna_base32_quintuplet_copy_from_byte_stream(
     uint64_t *base32_quintuplet,
     const uint8_t *byte_stream,
     size_t byte_stream_size,
     size_t *byte_stream_index,
     uint8_t *padding_size,
     libcerror_error_t **error )
{
1498 1499 1500 1501
	static char *function           = "libuna_base32_quintuplet_copy_from_byte_stream";
	size_t safe_byte_stream_index   = 0;
	uint64_t safe_base32_quintuplet = 0;
	uint8_t safe_padding_size       = 0;
1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557

	if( base32_quintuplet == NULL )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
		 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
		 "%s: invalid base32 quintuplet.",
		 function );

		return( -1 );
	}
	if( byte_stream == NULL )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
		 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
		 "%s: invalid byte stream.",
		 function );

		return( -1 );
	}
	if( byte_stream_size > (size_t) SSIZE_MAX )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
		 LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
		 "%s: invalid byte stream size value exceeds maximum.",
		 function );

		return( -1 );
	}
	if( byte_stream_index == NULL )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
		 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
		 "%s: invalid byte stream index.",
		 function );

		return( -1 );
	}
	if( *byte_stream_index >= byte_stream_size )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
		 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
		 "%s: byte stream string too small.",
		 function );

		return( -1 );
	}
1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568
	if( padding_size == NULL )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
		 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
		 "%s: invalid padding size.",
		 function );

		return( -1 );
	}
1569 1570
	/* Determine the value of 5 bytes (40 bits)
	 */
1571 1572 1573 1574
	safe_byte_stream_index = *byte_stream_index;

	safe_base32_quintuplet = byte_stream[ safe_byte_stream_index++ ];
	safe_padding_size      = 6;
1575

1576 1577 1578
	safe_base32_quintuplet <<= 8;

	if( safe_byte_stream_index < byte_stream_size )
1579
	{
1580 1581
		safe_base32_quintuplet |= byte_stream[ safe_byte_stream_index++ ];
		safe_padding_size      -= 2;
1582
	}
1583 1584 1585
	safe_base32_quintuplet <<= 8;

	if( safe_byte_stream_index < byte_stream_size )
1586
	{
1587 1588
		safe_base32_quintuplet |= byte_stream[ safe_byte_stream_index++ ];
		safe_padding_size      -= 1;
1589
	}
1590 1591 1592
	safe_base32_quintuplet <<= 8;

	if( safe_byte_stream_index < byte_stream_size )
1593
	{
1594 1595
		safe_base32_quintuplet |= byte_stream[ safe_byte_stream_index++ ];
		safe_padding_size      -= 2;
1596
	}
1597 1598 1599
	safe_base32_quintuplet <<= 8;

	if( safe_byte_stream_index < byte_stream_size )
1600
	{
1601 1602
		safe_base32_quintuplet |= byte_stream[ safe_byte_stream_index++ ];
		safe_padding_size      -= 1;
1603
	}
1604 1605 1606 1607
	*base32_quintuplet = safe_base32_quintuplet;
	*byte_stream_index = safe_byte_stream_index;
	*padding_size      = safe_padding_size;

1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621
	return( 1 );
}

/* Copies a base32 quintuplet to a byte stream
 * Returns 1 if successful or -1 on error
 */
int libuna_base32_quintuplet_copy_to_byte_stream(
     uint64_t base32_quintuplet,
     uint8_t *byte_stream,
     size_t byte_stream_size,
     size_t *byte_stream_index,
     uint8_t padding_size,
     libcerror_error_t **error )
{
1622 1623
	static char *function         = "libuna_base32_quintuplet_copy_to_byte_stream";
	size_t safe_byte_stream_index = 0;
1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679

	if( byte_stream == NULL )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
		 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
		 "%s: invalid byte stream.",
		 function );

		return( -1 );
	}
	if( byte_stream_size > (size_t) SSIZE_MAX )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
		 LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
		 "%s: invalid byte stream size value exceeds maximum.",
		 function );

		return( -1 );
	}
	if( byte_stream_index == NULL )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
		 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
		 "%s: invalid byte stream index.",
		 function );

		return( -1 );
	}
	if( *byte_stream_index >= byte_stream_size )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
		 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
		 "%s: byte stream string too small.",
		 function );

		return( -1 );
	}
	if( padding_size > 6 )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
		 LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
		 "%s: invalid padding size value out of bounds.",
		 function );

		return( -1 );
	}
1680
	safe_byte_stream_index = *byte_stream_index;
1681

1682
	byte_stream[ safe_byte_stream_index++ ] = (uint8_t) ( ( base32_quintuplet >> 32 ) & 0xff );
1683 1684 1685

	if( padding_size <= 4 )
	{
1686
		if( safe_byte_stream_index >= byte_stream_size )
1687 1688 1689 1690 1691 1692 1693 1694 1695 1696
		{
			libcerror_error_set(
			 error,
			 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
			 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
			 "%s: byte stream string too small.",
			 function );

			return( -1 );
		}
1697
		byte_stream[ safe_byte_stream_index++ ] = (uint8_t) ( ( base32_quintuplet >> 24 ) & 0xff );
1698 1699 1700
	}
	if( padding_size <= 3 )
	{
1701
		if( safe_byte_stream_index >= byte_stream_size )
1702 1703 1704 1705 1706 1707 1708 1709 1710 1711
		{
			libcerror_error_set(
			 error,
			 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
			 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
			 "%s: byte stream string too small.",
			 function );

			return( -1 );
		}
1712
		byte_stream[ safe_byte_stream_index++ ] = (uint8_t) ( ( base32_quintuplet >> 16 ) & 0xff );
1713 1714 1715
	}
	if( padding_size <= 2 )
	{
1716
		if( safe_byte_stream_index >= byte_stream_size )
1717 1718 1719 1720 1721 1722 1723 1724 1725 1726
		{
			libcerror_error_set(
			 error,
			 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
			 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
			 "%s: byte stream string too small.",
			 function );

			return( -1 );
		}
1727
		byte_stream[ safe_byte_stream_index++ ] = (uint8_t) ( ( base32_quintuplet >> 8 ) & 0xff );
1728 1729 1730
	}
	if( padding_size == 0 )
	{
1731
		if( safe_byte_stream_index >= byte_stream_size )
1732 1733 1734 1735 1736 1737 1738 1739 1740 1741
		{
			libcerror_error_set(
			 error,
			 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
			 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
			 "%s: byte stream is too small.",
			 function );

			return( -1 );
		}
1742
		byte_stream[ safe_byte_stream_index++ ] = (uint8_t) ( base32_quintuplet & 0xff );
1743
	}
1744 1745
	*byte_stream_index = safe_byte_stream_index;

1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763
	return( 1 );
}

/* Determines the size of a byte stream from a base32 stream
 *
 * LIBUNA_BASE32_FLAG_STRIP_WHITESPACE removes leading space and tab characters,
 * and trailing space, tab and end of line characters
 *
 * Returns 1 if successful or -1 on error
 */
int libuna_base32_stream_size_to_byte_stream(
     const uint8_t *base32_stream,
     size_t base32_stream_size,
     size_t *byte_stream_size,
     uint32_t base32_variant,
     uint8_t flags,
     libcerror_error_t **error )
{
1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774
	static char *function        = "libuna_base32_stream_size_to_byte_stream";
	size_t base32_character_size = 0;
	size_t base32_stream_index   = 0;
	size_t number_of_characters  = 0;
	size_t safe_byte_stream_size = 0;
	size_t whitespace_size       = 0;
	uint32_t base32_character1   = 0;
	uint32_t base32_character2   = 0;
	uint8_t character_limit      = 0;
	uint8_t padding_size         = 0;
	uint8_t strip_mode           = LIBUNA_STRIP_MODE_LEADING_WHITESPACE;
1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786

	if( base32_stream == NULL )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
		 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
		 "%s: invalid base32 stream.",
		 function );

		return( -1 );
	}
1787
	if( base32_stream_size > (size_t) SSIZE_MAX )
1788 1789 1790 1791
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1792 1793
		 LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
		 "%s: invalid base32 stream size value exceeds maximum.",
1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831
		 function );

		return( -1 );
	}
	if( byte_stream_size == NULL )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
		 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
		 "%s: invalid byte stream size.",
		 function );

		return( -1 );
	}
	switch( base32_variant & 0x000000ffUL )
	{
		case LIBUNA_BASE32_VARIANT_CHARACTER_LIMIT_NONE:
			character_limit = 0;
			break;

		case LIBUNA_BASE32_VARIANT_CHARACTER_LIMIT_64:
			character_limit = 64;
			break;

		default:
			libcerror_error_set(
			 error,
			 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
			 LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
			 "%s: unsupported base32 variant.",
			 function );

			return( -1 );
	}
	switch( base32_variant & 0x000f0000UL )
	{
		case LIBUNA_BASE32_VARIANT_ALPHABET_NORMAL:
1832
		case LIBUNA_BASE32_VARIANT_ALPHABET_HEX:
1833 1834
			break;

1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858
		default:
			libcerror_error_set(
			 error,
			 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
			 LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
			 "%s: unsupported base32 variant.",
			 function );

			return( -1 );
	}
	switch( base32_variant & 0xf0000000UL )
	{
		case LIBUNA_BASE32_VARIANT_ENCODING_BYTE_STREAM:
			base32_character_size = 1;
			break;

		case LIBUNA_BASE32_VARIANT_ENCODING_UTF16_BIG_ENDIAN:
		case LIBUNA_BASE32_VARIANT_ENCODING_UTF16_LITTLE_ENDIAN:
			base32_character_size = 2;
			break;

		case LIBUNA_BASE32_VARIANT_ENCODING_UTF32_BIG_ENDIAN:
		case LIBUNA_BASE32_VARIANT_ENCODING_UTF32_LITTLE_ENDIAN:
			base32_character_size = 4;
1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881
			break;

		default:
			libcerror_error_set(
			 error,
			 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
			 LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
			 "%s: unsupported base32 variant.",
			 function );

			return( -1 );
	}
	if( ( flags & ~( LIBUNA_BASE32_FLAG_STRIP_WHITESPACE ) ) != 0 )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
		 LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
		 "%s: unsupported flags.",
		 function );

		return( -1 );
	}
1882 1883 1884
	/* Ignore trailing whitespace
	 */
	if( base32_stream_size > base32_character_size )
1885
	{
1886 1887 1888 1889
		base32_stream_index = base32_stream_size - base32_character_size;
		whitespace_size     = 0;

		while( base32_stream_index > base32_character_size )
1890
		{
1891
			switch( base32_variant & 0xf0000000UL )
1892
			{
1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938
				case LIBUNA_BASE32_VARIANT_ENCODING_BYTE_STREAM:
					base32_character1 = base32_stream[ base32_stream_index ];
					break;

				case LIBUNA_BASE32_VARIANT_ENCODING_UTF16_BIG_ENDIAN:
					byte_stream_copy_to_uint16_big_endian(
					 &( base32_stream[ base32_stream_index ] ),
					 base32_character1 );
					break;

				case LIBUNA_BASE32_VARIANT_ENCODING_UTF16_LITTLE_ENDIAN:
					byte_stream_copy_to_uint16_little_endian(
					 &( base32_stream[ base32_stream_index ] ),
					 base32_character1 );
					break;

				case LIBUNA_BASE32_VARIANT_ENCODING_UTF32_BIG_ENDIAN:
					byte_stream_copy_to_uint32_big_endian(
					 &( base32_stream[ base32_stream_index ] ),
					 base32_character1 );
					break;

				case LIBUNA_BASE32_VARIANT_ENCODING_UTF32_LITTLE_ENDIAN:
					byte_stream_copy_to_uint32_little_endian(
					 &( base32_stream[ base32_stream_index ] ),
					 base32_character1 );
					break;
			}
			base32_stream_index -= base32_character_size;

			if( ( base32_character1 == (uint32_t) '\n' )
			 || ( base32_character1 == (uint32_t) '\r' ) )
			{
				whitespace_size += base32_character_size;

				continue;
			}
			else if( ( flags & LIBUNA_BASE32_FLAG_STRIP_WHITESPACE ) == 0 )
			{
				break;
			}
			if( ( base32_character1 == (uint32_t) ' ' )
			 || ( base32_character1 == (uint32_t) '\t' )
			 || ( base32_character1 == (uint32_t) '\v' ) )
			{
				whitespace_size += base32_character_size;
1939 1940 1941 1942 1943 1944
			}
			else
			{
				break;
			}
		}
1945
		base32_stream_size -= whitespace_size;
1946
	}
1947 1948 1949
	/* Determine and ignore the padding
	 */
	if( base32_stream_size > base32_character_size )
1950
	{
1951
		base32_stream_index = base32_stream_size - base32_character_size;
1952

1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004
		while( ( base32_stream_index > base32_character_size )
		    && ( padding_size <= 6 ) )
		{
			switch( base32_variant & 0xf0000000UL )
			{
				case LIBUNA_BASE32_VARIANT_ENCODING_BYTE_STREAM:
					base32_character1 = base32_stream[ base32_stream_index ];
					break;

				case LIBUNA_BASE32_VARIANT_ENCODING_UTF16_BIG_ENDIAN:
					byte_stream_copy_to_uint16_big_endian(
					 &( base32_stream[ base32_stream_index ] ),
					 base32_character1 );
					break;

				case LIBUNA_BASE32_VARIANT_ENCODING_UTF16_LITTLE_ENDIAN:
					byte_stream_copy_to_uint16_little_endian(
					 &( base32_stream[ base32_stream_index ] ),
					 base32_character1 );
					break;

				case LIBUNA_BASE32_VARIANT_ENCODING_UTF32_BIG_ENDIAN:
					byte_stream_copy_to_uint32_big_endian(
					 &( base32_stream[ base32_stream_index ] ),
					 base32_character1 );
					break;

				case LIBUNA_BASE32_VARIANT_ENCODING_UTF32_LITTLE_ENDIAN:
					byte_stream_copy_to_uint32_little_endian(
					 &( base32_stream[ base32_stream_index ] ),
					 base32_character1 );
					break;
			}
			base32_stream_index -= base32_character_size;

			if( base32_character1 == (uint32_t) '=' )
			{
				padding_size += 1;
			}
		}
		if( padding_size > 6 )
		{
			libcerror_error_set(
			 error,
			 LIBCERROR_ERROR_DOMAIN_CONVERSION,
			 LIBCERROR_CONVERSION_ERROR_INPUT_FAILED,
			 "%s: invalid amount of padding - found more than 6 padding characters.",
			 function );

			return( -1 );
		}
		base32_stream_size -= padding_size * base32_character_size;
2005 2006 2007 2008
	}
	base32_stream_index = 0;
	whitespace_size     = 0;

2009
	while( ( base32_stream_index + base32_character_size ) < base32_stream_size )
2010
	{
2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042
		switch( base32_variant & 0xf0000000UL )
		{
			case LIBUNA_BASE32_VARIANT_ENCODING_BYTE_STREAM:
				base32_character1 = base32_stream[ base32_stream_index ];
				break;

			case LIBUNA_BASE32_VARIANT_ENCODING_UTF16_BIG_ENDIAN:
				byte_stream_copy_to_uint16_big_endian(
				 &( base32_stream[ base32_stream_index ] ),
				 base32_character1 );
				break;

			case LIBUNA_BASE32_VARIANT_ENCODING_UTF16_LITTLE_ENDIAN:
				byte_stream_copy_to_uint16_little_endian(
				 &( base32_stream[ base32_stream_index ] ),
				 base32_character1 );
				break;

			case LIBUNA_BASE32_VARIANT_ENCODING_UTF32_BIG_ENDIAN:
				byte_stream_copy_to_uint32_big_endian(
				 &( base32_stream[ base32_stream_index ] ),
				 base32_character1 );
				break;

			case LIBUNA_BASE32_VARIANT_ENCODING_UTF32_LITTLE_ENDIAN:
				byte_stream_copy_to_uint32_little_endian(
				 &( base32_stream[ base32_stream_index ] ),
				 base32_character1 );
				break;
		}
		if( ( base32_character1 == (uint32_t) '\n' )
		 || ( base32_character1 == (uint32_t) '\r' ) )
2043 2044 2045 2046 2047 2048 2049 2050
		{
			if( ( strip_mode != LIBUNA_STRIP_MODE_NON_WHITESPACE )
			 && ( strip_mode != LIBUNA_STRIP_MODE_TRAILING_WHITESPACE ) )
			{
				strip_mode = LIBUNA_STRIP_MODE_INVALID_CHARACTER;
			}
			else
			{
2051 2052 2053
				/* Handle multi-character end-of-line
				 */
				if( ( base32_stream_index + base32_character_size ) < base32_stream_size )
2054
				{
2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086
					switch( base32_variant & 0xf0000000UL )
					{
						case LIBUNA_BASE32_VARIANT_ENCODING_BYTE_STREAM:
							base32_character2 = base32_stream[ base32_stream_index + 1 ];
							break;

						case LIBUNA_BASE32_VARIANT_ENCODING_UTF16_BIG_ENDIAN:
							byte_stream_copy_to_uint16_big_endian(
							 &( base32_stream[ base32_stream_index + 2 ] ),
							 base32_character2 );
							break;

						case LIBUNA_BASE32_VARIANT_ENCODING_UTF16_LITTLE_ENDIAN:
							byte_stream_copy_to_uint16_little_endian(
							 &( base32_stream[ base32_stream_index + 2 ] ),
							 base32_character2 );
							break;

						case LIBUNA_BASE32_VARIANT_ENCODING_UTF32_BIG_ENDIAN:
							byte_stream_copy_to_uint32_big_endian(
							 &( base32_stream[ base32_stream_index + 4 ] ),
							 base32_character2 );
							break;

						case LIBUNA_BASE32_VARIANT_ENCODING_UTF32_LITTLE_ENDIAN:
							byte_stream_copy_to_uint32_little_endian(
							 &( base32_stream[ base32_stream_index + 4 ] ),
							 base32_character2 );
							break;
					}
					if( ( base32_character2 == (uint32_t) '\n' )
					 || ( base32_character2 == (uint32_t) '\r' ) )
2087
					{
2088
						base32_stream_index += base32_character_size;
2089

2090
						whitespace_size += base32_character_size;
2091 2092 2093 2094
					}
				}
				strip_mode = LIBUNA_STRIP_MODE_LEADING_WHITESPACE;
			}
2095 2096
			if( ( number_of_characters != 0 )
			 && ( character_limit != 0 ) )
2097 2098 2099 2100 2101 2102 2103
			{
				if( number_of_characters != (size_t) character_limit )
				{
					libcerror_error_set(
					 error,
					 LIBCERROR_ERROR_DOMAIN_CONVERSION,
					 LIBCERROR_CONVERSION_ERROR_INPUT_FAILED,
2104 2105 2106 2107
					 "%s: number of characters in line: %" PRIzd " does not match character limit: %" PRIu8 ".",
					 function,
					 number_of_characters,
					 character_limit );
2108 2109 2110 2111 2112

					return( -1 );
				}
				number_of_characters = 0;
			}
2113
			whitespace_size += base32_character_size;
2114
		}
2115 2116 2117
		else if( ( base32_character1 == (uint32_t) ' ' )
		      || ( base32_character1 == (uint32_t) '\t' )
		      || ( base32_character1 == (uint32_t) '\v' ) )
2118
		{
2119 2120 2121 2122 2123
			if( ( flags & LIBUNA_BASE32_FLAG_STRIP_WHITESPACE ) == 0 )
			{
				strip_mode = LIBUNA_STRIP_MODE_INVALID_CHARACTER;
			}
			else