From: Piotr Dymacz Date: Wed, 31 Aug 2016 23:08:13 +0000 (+0200) Subject: Cleanups and minor fixes in LZMA related code X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=d9490f957cae9711fbef7741fabff7b5bf761b7f;p=oweals%2Fu-boot_mod.git Cleanups and minor fixes in LZMA related code --- diff --git a/u-boot/lib_bootstrap/LzmaDecode.c b/u-boot/lib_bootstrap/LzmaDecode.c index 28263e6..95400d3 100644 --- a/u-boot/lib_bootstrap/LzmaDecode.c +++ b/u-boot/lib_bootstrap/LzmaDecode.c @@ -37,27 +37,15 @@ #ifdef _LZMA_IN_CB - -#if 0 -#define RC_TEST { if (Buffer == BufferLim) \ - { SizeT size; int result = InCallback->Read(InCallback, &Buffer, &size); if (result != LZMA_RESULT_OK) { printf("ERROR, %s, %d\n", __FILE__, __LINE__); return result; } \ - BufferLim = Buffer + size; if (size == 0) { printf("ERROR, %s, %d\n", __FILE__, __LINE__); return LZMA_RESULT_DATA_ERROR; } }} -#else - #define RC_TEST { if (Buffer == BufferLim) \ { SizeT size; int result = InCallback->Read(InCallback, &Buffer, &size); if (result != LZMA_RESULT_OK) { return result; } \ BufferLim = Buffer + size; if (size == 0) { return LZMA_RESULT_DATA_ERROR; } }} -#endif #define RC_INIT Buffer = BufferLim = 0; RC_INIT2 #else -#if 0 -#define RC_TEST { if (Buffer == BufferLim) { printf("ERROR, %s, %d\n", __FILE__, __LINE__); return LZMA_RESULT_DATA_ERROR; } } -#else #define RC_TEST { if (Buffer == BufferLim) { return LZMA_RESULT_DATA_ERROR; } } -#endif #define RC_INIT(buffer, bufferSize) Buffer = buffer; BufferLim = buffer + bufferSize; RC_INIT2 @@ -136,17 +124,13 @@ int LzmaDecodeProperties(CLzmaProperties *propsRes, const unsigned char *propsDa unsigned char prop0; if (size < LZMA_PROPERTIES_SIZE) { -#ifdef DEBUG_ENABLE_BOOTSTRAP_PRINTF - printf("ERROR: %s, %d\n", __FILE__, __LINE__); -#endif + //printf("ERROR: %s, %d\n", __FILE__, __LINE__); return LZMA_RESULT_DATA_ERROR; } prop0 = propsData[0]; if (prop0 >= (9 * 5 * 5)) { -#ifdef DEBUG_ENABLE_BOOTSTRAP_PRINTF - printf("ERROR: %s, %d\n", __FILE__, __LINE__); -#endif + //printf("ERROR: %s, %d\n", __FILE__, __LINE__); return LZMA_RESULT_DATA_ERROR; } { @@ -396,10 +380,7 @@ int LzmaDecode(CLzmaDecoderState *vs, if (nowPos == 0) #endif { - -#ifdef DEBUG_ENABLE_BOOTSTRAP_PRINTF - printf("ERROR: %s, %d\n", __FILE__, __LINE__); -#endif + //printf("ERROR: %s, %d\n", __FILE__, __LINE__); return LZMA_RESULT_DATA_ERROR; } @@ -559,10 +540,7 @@ int LzmaDecode(CLzmaDecoderState *vs, if (rep0 > nowPos) #endif { - -#ifdef DEBUG_ENABLE_BOOTSTRAP_PRINTF - printf("ERROR: %s, %d\n", __FILE__, __LINE__); -#endif + //printf("ERROR: %s, %d\n", __FILE__, __LINE__); return LZMA_RESULT_DATA_ERROR; } diff --git a/u-boot/lib_bootstrap/LzmaWrapper.c b/u-boot/lib_bootstrap/LzmaWrapper.c index fd5d047..b0dcdd9 100644 --- a/u-boot/lib_bootstrap/LzmaWrapper.c +++ b/u-boot/lib_bootstrap/LzmaWrapper.c @@ -21,13 +21,6 @@ ** 2 Nov 2006 Lin Mars init version which derived from LzmaTest.c from ** LZMA v4.43 SDK *******************************************************************************/ -#define LZMA_NO_STDIO -#ifndef LZMA_NO_STDIO -#include -#include -#include -#endif - #include #include #include @@ -40,184 +33,129 @@ #include "LzmaDecode.h" #include "LzmaWrapper.h" -#ifdef DEBUG_ENABLE_BOOTSTRAP_PRINTF -static const char *kCantReadMessage = "Can not read from source buffer"; -static const char *kCantAllocateMessage = "Not enough buffer for decompression"; -#endif +static int MyReadFileAndCheck(unsigned char *src, + void *dest, + size_t size, + size_t *rpos) +{ + if (size == 0) + return 0; -static size_t rpos=0; + memcpy(dest, src + *rpos, size); + *rpos += size; -static int MyReadFileAndCheck(unsigned char *src, void *dest, size_t size){ - if (size == 0) - return 0; - memcpy(dest, src + rpos, size); - rpos += size; - return 1; + return 1; } -int lzma_inflate(unsigned char *source, int s_len, unsigned char *dest, int *d_len){ - /* We use two 32-bit integers to construct 64-bit integer for file size. - You can remove outSizeHigh, if you don't need >= 4GB supporting, - or you can use UInt64 outSize, if your compiler supports 64-bit integers*/ - UInt32 outSize = 0; - UInt32 outSizeHigh = 0; - SizeT outSizeFull; - unsigned char *outStream; - - int waitEOS = 1; - /* waitEOS = 1, if there is no uncompressed size in headers, - so decoder will wait EOS (End of Stream Marker) in compressed stream */ - - SizeT compressedSize; - unsigned char *inStream; - - CLzmaDecoderState state; /* it's about 24-80 bytes structure, if int is 32-bit */ - unsigned char properties[LZMA_PROPERTIES_SIZE]; - - int res; - - if (sizeof(UInt32) < 4) - { -#ifdef DEBUG_ENABLE_BOOTSTRAP_PRINTF - printf("LZMA decoder needs correct UInt32\n"); -#endif - return LZMA_RESULT_DATA_ERROR; - } - - { - long length=s_len; - if ((long)(SizeT)length != length) - { - -#ifdef DEBUG_ENABLE_BOOTSTRAP_PRINTF - printf("Too big compressed stream\n"); -#endif - return LZMA_RESULT_DATA_ERROR; - } - compressedSize = (SizeT)(length - (LZMA_PROPERTIES_SIZE + 8)); - } - - /* Read LZMA properties for compressed stream */ - - if (!MyReadFileAndCheck(source, properties, sizeof(properties))) - { - -#ifdef DEBUG_ENABLE_BOOTSTRAP_PRINTF - printf("%s\n", kCantReadMessage); -#endif - return LZMA_RESULT_DATA_ERROR; - } - - /* Read uncompressed size */ - { - int i; - for (i = 0; i < 8; i++) - { - unsigned char b; - if (!MyReadFileAndCheck(source, &b, 1)) - { - -#ifdef DEBUG_ENABLE_BOOTSTRAP_PRINTF - printf("%s\n", kCantReadMessage); -#endif - return LZMA_RESULT_DATA_ERROR; - } - if (b != 0xFF) - waitEOS = 0; - if (i < 4) - outSize += (UInt32)(b) << (i * 8); - else - outSizeHigh += (UInt32)(b) << ((i - 4) * 8); - } - - if (waitEOS) - { - -#ifdef DEBUG_ENABLE_BOOTSTRAP_PRINTF - printf("Stream with EOS marker is not supported"); -#endif - return LZMA_RESULT_DATA_ERROR; - } - outSizeFull = (SizeT)outSize; - if (sizeof(SizeT) >= 8) - outSizeFull |= (((SizeT)outSizeHigh << 16) << 16); - else if (outSizeHigh != 0 || (UInt32)(SizeT)outSize != outSize) - { - -#ifdef DEBUG_ENABLE_BOOTSTRAP_PRINTF - printf("Too big uncompressed stream"); -#endif - return LZMA_RESULT_DATA_ERROR; - } - } - - /* Decode LZMA properties and allocate memory */ - if (LzmaDecodeProperties(&state.Properties, properties, LZMA_PROPERTIES_SIZE) != LZMA_RESULT_OK) - { - -#ifdef DEBUG_ENABLE_BOOTSTRAP_PRINTF - printf("Incorrect stream properties"); -#endif - return LZMA_RESULT_DATA_ERROR; - } - state.Probs = (CProb *)malloc(LzmaGetNumProbs(&state.Properties) * sizeof(CProb)); - - if (outSizeFull == 0) - outStream = 0; - else - { - if (outSizeFull > (int)d_len) - outStream = 0; - else - outStream = dest; - } - - if (compressedSize == 0) - inStream = 0; - else - { - if ((compressedSize+rpos) > s_len ) - inStream = 0; - else - inStream = source + rpos; - } - - if (state.Probs == 0 - || (outStream == 0 && outSizeFull != 0) - || (inStream == 0 && compressedSize != 0) - ) - { - free(state.Probs); - -#ifdef DEBUG_ENABLE_BOOTSTRAP_PRINTF - printf("%s\n", kCantAllocateMessage); -#endif - return LZMA_RESULT_DATA_ERROR; - } - - /* Decompress */ - { - SizeT inProcessed; - SizeT outProcessed; - res = LzmaDecode(&state, - inStream, compressedSize, &inProcessed, - outStream, outSizeFull, &outProcessed); - if (res != 0) - { - -#ifdef DEBUG_ENABLE_BOOTSTRAP_PRINTF - printf("\nDecoding error = %d\n", res); -#endif - res = 1; - } - else - { - *d_len = outProcessed; - } - } - - free(state.Probs); - return res; +int lzma_inflate(unsigned char *source, + int s_len, + unsigned char *dest, + int *d_len) +{ + /* + * We use two 32-bit integers to construct 64-bit integer for file size. + * You can remove outSizeHigh, if you don't need >= 4GB supporting, + * or you can use UInt64 outSize, if your compiler supports 64-bit integers + */ + unsigned char *outStream; + UInt32 outSize = 0; + SizeT outSizeFull; + + size_t rpos = 0; + + /* + * waitEOS = 1, if there is no uncompressed size in headers, + * so decoder will wait EOS (End of Stream Marker) in compressed stream + */ + int waitEOS = 1; + + SizeT compressedSize; + unsigned char *inStream; + + /* It's about 24-80 bytes structure, if int is 32-bit */ + CLzmaDecoderState state; + unsigned char properties[LZMA_PROPERTIES_SIZE]; + + int res; + + compressedSize = (SizeT)(s_len - (LZMA_PROPERTIES_SIZE + 8)); + + /* Read LZMA properties for compressed stream */ + if (!MyReadFileAndCheck(source, properties, + sizeof(properties), &rpos)) { + return LZMA_RESULT_DATA_ERROR; + } + + /* Read uncompressed size */ + int i; + for (i = 0; i < 8; i++) { + unsigned char b; + + if (!MyReadFileAndCheck(source, &b, 1, &rpos)) + return LZMA_RESULT_DATA_ERROR; + + if (b != 0xFF) + waitEOS = 0; + + if (i < 4) + outSize += (UInt32)(b) << (i * 8); + } + + if (waitEOS) + return LZMA_RESULT_DATA_ERROR; + + outSizeFull = (SizeT)outSize; + + if ((UInt32)(SizeT)outSize != outSize) + return LZMA_RESULT_DATA_ERROR; + + /* Decode LZMA properties and allocate memory */ + if (LzmaDecodeProperties(&state.Properties, properties, + LZMA_PROPERTIES_SIZE) != LZMA_RESULT_OK) + return LZMA_RESULT_DATA_ERROR; + + state.Probs = (CProb *)malloc(LzmaGetNumProbs(&state.Properties) + * sizeof(CProb)); + + if (outSizeFull == 0) { + outStream = 0; + } else { + if (outSizeFull > (int)d_len) + outStream = 0; + else + outStream = dest; + } + + if (compressedSize == 0) { + inStream = 0; + } else { + if ((compressedSize + rpos) > s_len ) + inStream = 0; + else + inStream = source + rpos; + } + + if (state.Probs == 0 || + (outStream == 0 && outSizeFull != 0) || + (inStream == 0 && compressedSize != 0)) { + free(state.Probs); + return LZMA_RESULT_DATA_ERROR; + } + + /* Decompress */ + SizeT inProcessed; + SizeT outProcessed; + + res = LzmaDecode(&state, inStream, compressedSize, &inProcessed, + outStream, outSizeFull, &outProcessed); + if (res != 0) + res = 1; + else + *d_len = outProcessed; + + free(state.Probs); + + return res; } #endif /* CONFIG_LZMA */ diff --git a/u-boot/lib_generic/LzmaWrapper.c b/u-boot/lib_generic/LzmaWrapper.c index 076e8ab..b0dcdd9 100644 --- a/u-boot/lib_generic/LzmaWrapper.c +++ b/u-boot/lib_generic/LzmaWrapper.c @@ -1,33 +1,26 @@ /****************************************************************************** - ** - ** FILE NAME : LzmaWrapper.c - ** PROJECT : bootloader - ** MODULES : U-boot - ** - ** DATE : 2 Nov 2006 - ** AUTHOR : Lin Mars - ** DESCRIPTION : LZMA decoder support for U-boot 1.1.5 - ** COPYRIGHT : Copyright (c) 2006 - ** Infineon Technologies AG - ** Am Campeon 1-12, 85579 Neubiberg, Germany - ** - ** This program is free software; you can redistribute it and/or modify - ** it under the terms of the GNU General Public License as published by - ** the Free Software Foundation; either version 2 of the License, or - ** (at your option) any later version. - ** - ** HISTORY - ** $Date $Author $Comment - ** 2 Nov 2006 Lin Mars init version which derived from LzmaTest.c from - ** LZMA v4.43 SDK - *******************************************************************************/ -#define LZMA_NO_STDIO -#ifndef LZMA_NO_STDIO -#include -#include -#include -#endif - +** +** FILE NAME : LzmaWrapper.c +** PROJECT : bootloader +** MODULES : U-boot +** +** DATE : 2 Nov 2006 +** AUTHOR : Lin Mars +** DESCRIPTION : LZMA decoder support for U-boot 1.1.5 +** COPYRIGHT : Copyright (c) 2006 +** Infineon Technologies AG +** Am Campeon 1-12, 85579 Neubiberg, Germany +** +** This program is free software; you can redistribute it and/or modify +** it under the terms of the GNU General Public License as published by +** the Free Software Foundation; either version 2 of the License, or +** (at your option) any later version. +** +** HISTORY +** $Date $Author $Comment +** 2 Nov 2006 Lin Mars init version which derived from LzmaTest.c from +** LZMA v4.43 SDK +*******************************************************************************/ #include #include #include @@ -40,139 +33,128 @@ #include "LzmaDecode.h" #include "LzmaWrapper.h" -//static const char *kCantReadMessage = "can not read from source buffer"; -//static const char *kCantAllocateMessage = "not enough buffer for decompression"; +static int MyReadFileAndCheck(unsigned char *src, + void *dest, + size_t size, + size_t *rpos) +{ + if (size == 0) + return 0; -static size_t rpos = 0; + memcpy(dest, src + *rpos, size); + *rpos += size; -static int MyReadFileAndCheck(unsigned char *src, void *dest, size_t size) { - if (size == 0) return 0; - memcpy(dest, src + rpos, size); - rpos += size; return 1; } -int lzma_inflate(unsigned char *source, int s_len, unsigned char *dest, int *d_len) { - /* We use two 32-bit integers to construct 64-bit integer for file size. - You can remove outSizeHigh, if you don't need >= 4GB supporting, - or you can use UInt64 outSize, if your compiler supports 64-bit integers*/ +int lzma_inflate(unsigned char *source, + int s_len, + unsigned char *dest, + int *d_len) +{ + /* + * We use two 32-bit integers to construct 64-bit integer for file size. + * You can remove outSizeHigh, if you don't need >= 4GB supporting, + * or you can use UInt64 outSize, if your compiler supports 64-bit integers + */ + unsigned char *outStream; UInt32 outSize = 0; - UInt32 outSizeHigh = 0; SizeT outSizeFull; - unsigned char *outStream; + size_t rpos = 0; + + /* + * waitEOS = 1, if there is no uncompressed size in headers, + * so decoder will wait EOS (End of Stream Marker) in compressed stream + */ int waitEOS = 1; - /* waitEOS = 1, if there is no uncompressed size in headers, - so decoder will wait EOS (End of Stream Marker) in compressed stream */ SizeT compressedSize; unsigned char *inStream; - CLzmaDecoderState state; /* it's about 24-80 bytes structure, if int is 32-bit */ + /* It's about 24-80 bytes structure, if int is 32-bit */ + CLzmaDecoderState state; unsigned char properties[LZMA_PROPERTIES_SIZE]; int res; - if (sizeof(UInt32) < 4) { - //printf("## Error: LZMA decoder needs correct UInt32\n"); - return LZMA_RESULT_DATA_ERROR; - } - - long length = s_len; - - if ((long) (SizeT) length != length) { - //printf("## Error: too big compressed stream\n"); - return LZMA_RESULT_DATA_ERROR; - } - - compressedSize = (SizeT) (length - (LZMA_PROPERTIES_SIZE + 8)); + compressedSize = (SizeT)(s_len - (LZMA_PROPERTIES_SIZE + 8)); /* Read LZMA properties for compressed stream */ - - if (!MyReadFileAndCheck(source, properties, sizeof(properties))) { - //printf("## Error: %s\n", kCantReadMessage); + if (!MyReadFileAndCheck(source, properties, + sizeof(properties), &rpos)) { return LZMA_RESULT_DATA_ERROR; } /* Read uncompressed size */ - int i; - for (i = 0; i < 8; i++) { unsigned char b; - if (!MyReadFileAndCheck(source, &b, 1)) { - //printf("## Error: %s\n", kCantReadMessage); + + if (!MyReadFileAndCheck(source, &b, 1, &rpos)) return LZMA_RESULT_DATA_ERROR; - } - if (b != 0xFF) waitEOS = 0; + + if (b != 0xFF) + waitEOS = 0; + if (i < 4) - outSize += (UInt32) (b) << (i * 8); - else - outSizeHigh += (UInt32) (b) << ((i - 4) * 8); + outSize += (UInt32)(b) << (i * 8); } - if (waitEOS) { - //printf("## Error: stream with EOS marker is not supported\n"); + if (waitEOS) return LZMA_RESULT_DATA_ERROR; - } - outSizeFull = (SizeT) outSize; + outSizeFull = (SizeT)outSize; - if (sizeof(SizeT) >= 8){ - outSizeFull |= (((SizeT) outSizeHigh << 16) << 16); - } else if (outSizeHigh != 0 || (UInt32) (SizeT) outSize != outSize) { - //printf("## Error: too big uncompressed stream\n"); + if ((UInt32)(SizeT)outSize != outSize) return LZMA_RESULT_DATA_ERROR; - } - /* Decode LZMA properties and allocate memory */ - if (LzmaDecodeProperties(&state.Properties, properties, LZMA_PROPERTIES_SIZE) != LZMA_RESULT_OK) { - //printf("## Error: incorrect stream properties\n"); + if (LzmaDecodeProperties(&state.Properties, properties, + LZMA_PROPERTIES_SIZE) != LZMA_RESULT_OK) return LZMA_RESULT_DATA_ERROR; - } - state.Probs = (CProb *) malloc(LzmaGetNumProbs(&state.Properties) * sizeof(CProb)); + state.Probs = (CProb *)malloc(LzmaGetNumProbs(&state.Properties) + * sizeof(CProb)); - if (outSizeFull == 0){ + if (outSizeFull == 0) { outStream = 0; } else { - if (outSizeFull > *d_len){ + if (outSizeFull > (int)d_len) outStream = 0; - } else{ + else outStream = dest; - } } - if (compressedSize == 0){ + if (compressedSize == 0) { inStream = 0; } else { - if ((compressedSize + rpos) > s_len){ + if ((compressedSize + rpos) > s_len ) inStream = 0; - } else{ + else inStream = source + rpos; - } } - if (state.Probs == 0 || (outStream == 0 && outSizeFull != 0) || (inStream == 0 && compressedSize != 0)) { + if (state.Probs == 0 || + (outStream == 0 && outSizeFull != 0) || + (inStream == 0 && compressedSize != 0)) { free(state.Probs); - //printf("## Error: %s\n", kCantAllocateMessage); return LZMA_RESULT_DATA_ERROR; } /* Decompress */ SizeT inProcessed; SizeT outProcessed; - res = LzmaDecode(&state, inStream, compressedSize, &inProcessed, outStream, outSizeFull, &outProcessed); - if (res != 0) { - //printf("\n## Error: decoding error = %d\n", res); + res = LzmaDecode(&state, inStream, compressedSize, &inProcessed, + outStream, outSizeFull, &outProcessed); + if (res != 0) res = 1; - } else { + else *d_len = outProcessed; - } free(state.Probs); + return res; }