summaryrefslogtreecommitdiff
path: root/BaseTools/Source/C/LzmaCompress
diff options
context:
space:
mode:
Diffstat (limited to 'BaseTools/Source/C/LzmaCompress')
-rw-r--r--BaseTools/Source/C/LzmaCompress/GNUmakefile5
-rw-r--r--BaseTools/Source/C/LzmaCompress/LzmaCompress.c240
-rw-r--r--BaseTools/Source/C/LzmaCompress/LzmaF86Compress.bat31
-rw-r--r--BaseTools/Source/C/LzmaCompress/Makefile14
-rw-r--r--BaseTools/Source/C/LzmaCompress/Sdk/C/Bra.h60
-rw-r--r--BaseTools/Source/C/LzmaCompress/Sdk/C/Bra86.c85
6 files changed, 333 insertions, 102 deletions
diff --git a/BaseTools/Source/C/LzmaCompress/GNUmakefile b/BaseTools/Source/C/LzmaCompress/GNUmakefile
index 99e9cbbeb0..07f0a90b60 100644
--- a/BaseTools/Source/C/LzmaCompress/GNUmakefile
+++ b/BaseTools/Source/C/LzmaCompress/GNUmakefile
@@ -1,7 +1,7 @@
## @file
# Windows makefile for 'LzmaCompress' module build.
#
-# Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.<BR>
+# Copyright (c) 2009 - 2012, Intel Corporation. All rights reserved.<BR>
# This program and the accompanying materials
# are licensed and made available under the terms and conditions of the BSD License
# which accompanies this distribution. The full text of the license may be found at
@@ -24,7 +24,8 @@ OBJECTS = \
$(SDK_C)/LzmaDec.o \
$(SDK_C)/LzmaEnc.o \
$(SDK_C)/7zFile.o \
- $(SDK_C)/7zStream.o
+ $(SDK_C)/7zStream.o \
+ $(SDK_C)/Bra86.o
include $(MAKEROOT)/Makefiles/app.makefile
diff --git a/BaseTools/Source/C/LzmaCompress/LzmaCompress.c b/BaseTools/Source/C/LzmaCompress/LzmaCompress.c
index 7d254ed49a..b569b4e4d9 100644
--- a/BaseTools/Source/C/LzmaCompress/LzmaCompress.c
+++ b/BaseTools/Source/C/LzmaCompress/LzmaCompress.c
@@ -5,7 +5,7 @@
LzmaUtil.c -- Test application for LZMA compression
2008-11-23 : Igor Pavlov : Public domain
- Copyright (c) 2006 - 2009, Intel Corporation. All rights reserved.<BR>
+ Copyright (c) 2006 - 2012, Intel Corporation. All rights reserved.<BR>
This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
@@ -27,8 +27,17 @@
#include "Sdk/C/7zVersion.h"
#include "Sdk/C/LzmaDec.h"
#include "Sdk/C/LzmaEnc.h"
+#include "Sdk/C/Bra.h"
#include "CommonLib.h"
+#define LZMA_HEADER_SIZE (LZMA_PROPS_SIZE + 8)
+
+typedef enum {
+ NoConverter,
+ X86Converter,
+ MaxConverter
+} CONVERTER_TYPE;
+
const char *kCantReadMessage = "Can not read input file";
const char *kCantWriteMessage = "Can not write output file";
const char *kCantAllocateMessage = "Can not allocate memory";
@@ -39,12 +48,13 @@ static void SzFree(void *p, void *address) { p = p; MyFree(address); }
static ISzAlloc g_Alloc = { SzAlloc, SzFree };
static Bool mQuietMode = False;
+static CONVERTER_TYPE mConType = NoConverter;
#define UTILITY_NAME "LzmaCompress"
#define UTILITY_MAJOR_VERSION 0
-#define UTILITY_MINOR_VERSION 1
+#define UTILITY_MINOR_VERSION 2
#define INTEL_COPYRIGHT \
- "Copyright (c) 2009, Intel Corporation. All rights reserved."
+ "Copyright (c) 2009-2012, Intel Corporation. All rights reserved."
void PrintHelp(char *buffer)
{
strcat(buffer,
@@ -54,6 +64,7 @@ void PrintHelp(char *buffer)
" -e: encode file\n"
" -d: decode file\n"
" -o FileName, --output FileName: specify the output filename\n"
+ " --f86: enable converter for x86 code\n"
" -v, --verbose: increase output messages\n"
" -q, --quiet: reduce output messages\n"
" --debug [0-9]: set debug level\n"
@@ -86,121 +97,151 @@ void PrintVersion(char *buffer)
sprintf (buffer, "%s Version %d.%d %s ", UTILITY_NAME, UTILITY_MAJOR_VERSION, UTILITY_MINOR_VERSION, __BUILD_VERSION);
}
-#define IN_BUF_SIZE (1 << 16)
-#define OUT_BUF_SIZE (1 << 16)
-
-static SRes Decode2(CLzmaDec *state, ISeqOutStream *outStream, ISeqInStream *inStream,
- UInt64 unpackSize)
+static SRes Encode(ISeqOutStream *outStream, ISeqInStream *inStream, UInt64 fileSize)
{
- int thereIsSize = (unpackSize != (UInt64)(Int64)-1);
- Byte inBuf[IN_BUF_SIZE];
- Byte outBuf[OUT_BUF_SIZE];
- size_t inPos = 0, inSize = 0, outPos = 0;
- LzmaDec_Init(state);
- for (;;)
- {
- if (inPos == inSize)
- {
- inSize = IN_BUF_SIZE;
- RINOK(inStream->Read(inStream, inBuf, &inSize));
- inPos = 0;
- }
- {
- SRes res;
- SizeT inProcessed = inSize - inPos;
- SizeT outProcessed = OUT_BUF_SIZE - outPos;
- ELzmaFinishMode finishMode = LZMA_FINISH_ANY;
- ELzmaStatus status;
- if (thereIsSize && outProcessed > unpackSize)
- {
- outProcessed = (SizeT)unpackSize;
- finishMode = LZMA_FINISH_END;
- }
-
- res = LzmaDec_DecodeToBuf(state, outBuf + outPos, &outProcessed,
- inBuf + inPos, &inProcessed, finishMode, &status);
- inPos += inProcessed;
- outPos += outProcessed;
- unpackSize -= outProcessed;
-
- if (outStream)
- if (outStream->Write(outStream, outBuf, outPos) != outPos)
- return SZ_ERROR_WRITE;
+ SRes res;
+ size_t inSize = (size_t)fileSize;
+ Byte *inBuffer = 0;
+ Byte *outBuffer = 0;
+ Byte *filteredStream = 0;
+ size_t outSize;
+ CLzmaEncProps props;
- outPos = 0;
+ LzmaEncProps_Init(&props);
+ LzmaEncProps_Normalize(&props);
+
+ if (inSize != 0) {
+ inBuffer = (Byte *)MyAlloc(inSize);
+ if (inBuffer == 0)
+ return SZ_ERROR_MEM;
+ } else {
+ return SZ_ERROR_INPUT_EOF;
+ }
+
+ if (SeqInStream_Read(inStream, inBuffer, inSize) != SZ_OK) {
+ res = SZ_ERROR_READ;
+ goto Done;
+ }
- if (res != SZ_OK || (thereIsSize && unpackSize == 0))
- return res;
+ // we allocate 105% of original size + 64KB for output buffer
+ outSize = (size_t)fileSize / 20 * 21 + (1 << 16);
+ outBuffer = (Byte *)MyAlloc(outSize);
+ if (outBuffer == 0) {
+ res = SZ_ERROR_MEM;
+ goto Done;
+ }
+
+ {
+ int i;
+ for (i = 0; i < 8; i++)
+ outBuffer[i + LZMA_PROPS_SIZE] = (Byte)(fileSize >> (8 * i));
+ }
- if (inProcessed == 0 && outProcessed == 0)
+ if (mConType != NoConverter)
+ {
+ filteredStream = (Byte *)MyAlloc(inSize);
+ if (filteredStream == 0) {
+ res = SZ_ERROR_MEM;
+ goto Done;
+ }
+ memcpy(filteredStream, inBuffer, inSize);
+
+ if (mConType == X86Converter) {
{
- if (thereIsSize || status != LZMA_STATUS_FINISHED_WITH_MARK)
- return SZ_ERROR_DATA;
- return res;
+ UInt32 x86State;
+ x86_Convert_Init(x86State);
+ x86_Convert(filteredStream, (SizeT) inSize, 0, &x86State, 1);
}
}
}
-}
-
-static SRes Decode(ISeqOutStream *outStream, ISeqInStream *inStream)
-{
- UInt64 unpackSize;
- int i;
- SRes res = 0;
- CLzmaDec state;
-
- /* header: 5 bytes of LZMA properties and 8 bytes of uncompressed size */
- unsigned char header[LZMA_PROPS_SIZE + 8];
-
- /* Read and parse header */
+ {
+ size_t outSizeProcessed = outSize - LZMA_HEADER_SIZE;
+ size_t outPropsSize = LZMA_PROPS_SIZE;
+
+ res = LzmaEncode(outBuffer + LZMA_HEADER_SIZE, &outSizeProcessed,
+ mConType != NoConverter ? filteredStream : inBuffer, inSize,
+ &props, outBuffer, &outPropsSize, 0,
+ NULL, &g_Alloc, &g_Alloc);
+
+ if (res != SZ_OK)
+ goto Done;
+
+ outSize = LZMA_HEADER_SIZE + outSizeProcessed;
+ }
- RINOK(SeqInStream_Read(inStream, header, sizeof(header)));
+ if (outStream->Write(outStream, outBuffer, outSize) != outSize)
+ res = SZ_ERROR_WRITE;
- unpackSize = 0;
- for (i = 0; i < 8; i++)
- unpackSize += (UInt64)header[LZMA_PROPS_SIZE + i] << (i * 8);
+Done:
+ MyFree(outBuffer);
+ MyFree(inBuffer);
+ MyFree(filteredStream);
- LzmaDec_Construct(&state);
- RINOK(LzmaDec_Allocate(&state, header, LZMA_PROPS_SIZE, &g_Alloc));
- res = Decode2(&state, outStream, inStream, unpackSize);
- LzmaDec_Free(&state, &g_Alloc);
return res;
}
-static SRes Encode(ISeqOutStream *outStream, ISeqInStream *inStream, UInt64 fileSize, char *rs)
+static SRes Decode(ISeqOutStream *outStream, ISeqInStream *inStream, UInt64 fileSize)
{
- CLzmaEncHandle enc;
SRes res;
- CLzmaEncProps props;
+ size_t inSize = (size_t)fileSize;
+ Byte *inBuffer = 0;
+ Byte *outBuffer = 0;
+ size_t outSize = 0;
+ size_t inSizePure;
+ ELzmaStatus status;
+ UInt64 outSize64 = 0;
- rs = rs;
+ int i;
+
+ if (inSize < LZMA_HEADER_SIZE)
+ return SZ_ERROR_INPUT_EOF;
- enc = LzmaEnc_Create(&g_Alloc);
- if (enc == 0)
+ inBuffer = (Byte *)MyAlloc(inSize);
+ if (inBuffer == 0)
return SZ_ERROR_MEM;
+
+ if (SeqInStream_Read(inStream, inBuffer, inSize) != SZ_OK) {
+ res = SZ_ERROR_READ;
+ goto Done;
+ }
- LzmaEncProps_Init(&props);
- res = LzmaEnc_SetProps(enc, &props);
+ for (i = 0; i < 8; i++)
+ outSize64 += ((UInt64)inBuffer[LZMA_PROPS_SIZE + i]) << (i * 8);
+
+ outSize = (size_t)outSize64;
+ if (outSize != 0) {
+ outBuffer = (Byte *)MyAlloc(outSize);
+ if (outBuffer == 0) {
+ res = SZ_ERROR_MEM;
+ goto Done;
+ }
+ } else {
+ res = SZ_OK;
+ goto Done;
+ }
- if (res == SZ_OK)
- {
- Byte header[LZMA_PROPS_SIZE + 8];
- size_t headerSize = LZMA_PROPS_SIZE;
- int i;
+ inSizePure = inSize - LZMA_HEADER_SIZE;
+ res = LzmaDecode(outBuffer, &outSize, inBuffer + LZMA_HEADER_SIZE, &inSizePure,
+ inBuffer, LZMA_PROPS_SIZE, LZMA_FINISH_END, &status, &g_Alloc);
- res = LzmaEnc_WriteProperties(enc, header, &headerSize);
- for (i = 0; i < 8; i++)
- header[headerSize++] = (Byte)(fileSize >> (8 * i));
- if (outStream->Write(outStream, header, headerSize) != headerSize)
- res = SZ_ERROR_WRITE;
- else
- {
- if (res == SZ_OK)
- res = LzmaEnc_Encode(enc, outStream, inStream, NULL, &g_Alloc, &g_Alloc);
- }
+ if (res != SZ_OK)
+ goto Done;
+
+ if (mConType == X86Converter)
+ {
+ UInt32 x86State;
+ x86_Convert_Init(x86State);
+ x86_Convert(outBuffer, (SizeT) outSize, 0, &x86State, 0);
}
- LzmaEnc_Destroy(enc, &g_Alloc, &g_Alloc);
+
+ if (outStream->Write(outStream, outBuffer, outSize) != outSize)
+ res = SZ_ERROR_WRITE;
+
+Done:
+ MyFree(outBuffer);
+ MyFree(inBuffer);
+
return res;
}
@@ -214,6 +255,7 @@ int main2(int numArgs, const char *args[], char *rs)
const char *inputFile = NULL;
const char *outputFile = "file.tmp";
int param;
+ UInt64 fileSize;
FileSeqInStream_CreateVTable(&inStream);
File_Construct(&inStream.file);
@@ -231,6 +273,8 @@ int main2(int numArgs, const char *args[], char *rs)
if (strcmp(args[param], "-e") == 0 || strcmp(args[param], "-d") == 0) {
encodeMode = (args[param][1] == 'e');
modeWasSet = True;
+ } else if (strcmp(args[param], "--f86") == 0) {
+ mConType = X86Converter;
} else if (strcmp(args[param], "-o") == 0 ||
strcmp(args[param], "--output") == 0) {
if (numArgs < (param + 2)) {
@@ -292,21 +336,21 @@ int main2(int numArgs, const char *args[], char *rs)
if (OutFile_Open(&outStream.file, outputFile) != 0)
return PrintError(rs, "Can not open output file");
+ File_GetLength(&inStream.file, &fileSize);
+
if (encodeMode)
{
- UInt64 fileSize;
- File_GetLength(&inStream.file, &fileSize);
if (!mQuietMode) {
printf("Encoding\n");
}
- res = Encode(&outStream.s, &inStream.s, fileSize, rs);
+ res = Encode(&outStream.s, &inStream.s, fileSize);
}
else
{
if (!mQuietMode) {
printf("Decoding\n");
}
- res = Decode(&outStream.s, &inStream.s);
+ res = Decode(&outStream.s, &inStream.s, fileSize);
}
File_Close(&outStream.file);
diff --git a/BaseTools/Source/C/LzmaCompress/LzmaF86Compress.bat b/BaseTools/Source/C/LzmaCompress/LzmaF86Compress.bat
new file mode 100644
index 0000000000..77749dfd16
--- /dev/null
+++ b/BaseTools/Source/C/LzmaCompress/LzmaF86Compress.bat
@@ -0,0 +1,31 @@
+@REM
+@REM This script will exec LzmaCompress tool with --f86 option that enables converter for x86 code.
+@REM
+@REM Copyright (c) 2012, Intel Corporation. All rights reserved.<BR>
+@REM This program and the accompanying materials
+@REM are licensed and made available under the terms and conditions of the BSD License
+@REM which accompanies this distribution. The full text of the license may be found at
+@REM http://opensource.org/licenses/bsd-license.php
+@REM
+@REM THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+@REM WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+@REM
+
+@echo off
+@setlocal
+
+:Begin
+if "%1"=="" goto End
+if "%1"=="-e" (
+ set FLAG=--f86
+)
+if "%1"=="-d" (
+ set FLAG=--f86
+)
+set ARGS=%ARGS% %1
+shift
+goto Begin
+
+:End
+LzmaCompress %ARGS% %FLAG%
+@echo on
diff --git a/BaseTools/Source/C/LzmaCompress/Makefile b/BaseTools/Source/C/LzmaCompress/Makefile
index 1c1ba2bc38..872e899512 100644
--- a/BaseTools/Source/C/LzmaCompress/Makefile
+++ b/BaseTools/Source/C/LzmaCompress/Makefile
@@ -1,7 +1,7 @@
## @file
# Windows makefile for 'LzmaCompress' module build.
#
-# Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.<BR>
+# Copyright (c) 2009 - 2012, Intel Corporation. All rights reserved.<BR>
# This program and the accompanying materials
# are licensed and made available under the terms and conditions of the BSD License
# which accompanies this distribution. The full text of the license may be found at
@@ -25,7 +25,17 @@ OBJECTS = \
$(SDK_C)\LzmaDec.obj \
$(SDK_C)\LzmaEnc.obj \
$(SDK_C)\7zFile.obj \
- $(SDK_C)\7zStream.obj
+ $(SDK_C)\7zStream.obj \
+ $(SDK_C)\Bra86.obj
!INCLUDE ..\Makefiles\ms.app
+all: $(BIN_PATH)\LzmaF86Compress.bat
+
+$(BIN_PATH)\LzmaF86Compress.bat: LzmaF86Compress.bat
+ copy LzmaF86Compress.bat $(BIN_PATH)\LzmaF86Compress.bat /Y
+
+cleanall: localCleanall
+
+localCleanall:
+ del /f /q $(BIN_PATH)\LzmaF86Compress.bat > nul
diff --git a/BaseTools/Source/C/LzmaCompress/Sdk/C/Bra.h b/BaseTools/Source/C/LzmaCompress/Sdk/C/Bra.h
new file mode 100644
index 0000000000..b9018eb991
--- /dev/null
+++ b/BaseTools/Source/C/LzmaCompress/Sdk/C/Bra.h
@@ -0,0 +1,60 @@
+/* Bra.h -- Branch converters for executables
+2008-10-04 : Igor Pavlov : Public domain */
+
+#ifndef __BRA_H
+#define __BRA_H
+
+#include "Types.h"
+
+/*
+These functions convert relative addresses to absolute addresses
+in CALL instructions to increase the compression ratio.
+
+ In:
+ data - data buffer
+ size - size of data
+ ip - current virtual Instruction Pinter (IP) value
+ state - state variable for x86 converter
+ encoding - 0 (for decoding), 1 (for encoding)
+
+ Out:
+ state - state variable for x86 converter
+
+ Returns:
+ The number of processed bytes. If you call these functions with multiple calls,
+ you must start next call with first byte after block of processed bytes.
+
+ Type Endian Alignment LookAhead
+
+ x86 little 1 4
+ ARMT little 2 2
+ ARM little 4 0
+ PPC big 4 0
+ SPARC big 4 0
+ IA64 little 16 0
+
+ size must be >= Alignment + LookAhead, if it's not last block.
+ If (size < Alignment + LookAhead), converter returns 0.
+
+ Example:
+
+ UInt32 ip = 0;
+ for ()
+ {
+ ; size must be >= Alignment + LookAhead, if it's not last block
+ SizeT processed = Convert(data, size, ip, 1);
+ data += processed;
+ size -= processed;
+ ip += processed;
+ }
+*/
+
+#define x86_Convert_Init(state) { state = 0; }
+SizeT x86_Convert(Byte *data, SizeT size, UInt32 ip, UInt32 *state, int encoding);
+SizeT ARM_Convert(Byte *data, SizeT size, UInt32 ip, int encoding);
+SizeT ARMT_Convert(Byte *data, SizeT size, UInt32 ip, int encoding);
+SizeT PPC_Convert(Byte *data, SizeT size, UInt32 ip, int encoding);
+SizeT SPARC_Convert(Byte *data, SizeT size, UInt32 ip, int encoding);
+SizeT IA64_Convert(Byte *data, SizeT size, UInt32 ip, int encoding);
+
+#endif
diff --git a/BaseTools/Source/C/LzmaCompress/Sdk/C/Bra86.c b/BaseTools/Source/C/LzmaCompress/Sdk/C/Bra86.c
new file mode 100644
index 0000000000..93566cb212
--- /dev/null
+++ b/BaseTools/Source/C/LzmaCompress/Sdk/C/Bra86.c
@@ -0,0 +1,85 @@
+/* Bra86.c -- Converter for x86 code (BCJ)
+2008-10-04 : Igor Pavlov : Public domain */
+
+#include "Bra.h"
+
+#define Test86MSByte(b) ((b) == 0 || (b) == 0xFF)
+
+const Byte kMaskToAllowedStatus[8] = {1, 1, 1, 0, 1, 0, 0, 0};
+const Byte kMaskToBitNumber[8] = {0, 1, 2, 2, 3, 3, 3, 3};
+
+SizeT x86_Convert(Byte *data, SizeT size, UInt32 ip, UInt32 *state, int encoding)
+{
+ SizeT bufferPos = 0, prevPosT;
+ UInt32 prevMask = *state & 0x7;
+ if (size < 5)
+ return 0;
+ ip += 5;
+ prevPosT = (SizeT)0 - 1;
+
+ for (;;)
+ {
+ Byte *p = data + bufferPos;
+ Byte *limit = data + size - 4;
+ for (; p < limit; p++)
+ if ((*p & 0xFE) == 0xE8)
+ break;
+ bufferPos = (SizeT)(p - data);
+ if (p >= limit)
+ break;
+ prevPosT = bufferPos - prevPosT;
+ if (prevPosT > 3)
+ prevMask = 0;
+ else
+ {
+ prevMask = (prevMask << ((int)prevPosT - 1)) & 0x7;
+ if (prevMask != 0)
+ {
+ Byte b = p[4 - kMaskToBitNumber[prevMask]];
+ if (!kMaskToAllowedStatus[prevMask] || Test86MSByte(b))
+ {
+ prevPosT = bufferPos;
+ prevMask = ((prevMask << 1) & 0x7) | 1;
+ bufferPos++;
+ continue;
+ }
+ }
+ }
+ prevPosT = bufferPos;
+
+ if (Test86MSByte(p[4]))
+ {
+ UInt32 src = ((UInt32)p[4] << 24) | ((UInt32)p[3] << 16) | ((UInt32)p[2] << 8) | ((UInt32)p[1]);
+ UInt32 dest;
+ for (;;)
+ {
+ Byte b;
+ int index;
+ if (encoding)
+ dest = (ip + (UInt32)bufferPos) + src;
+ else
+ dest = src - (ip + (UInt32)bufferPos);
+ if (prevMask == 0)
+ break;
+ index = kMaskToBitNumber[prevMask] * 8;
+ b = (Byte)(dest >> (24 - index));
+ if (!Test86MSByte(b))
+ break;
+ src = dest ^ ((1 << (32 - index)) - 1);
+ }
+ p[4] = (Byte)(~(((dest >> 24) & 1) - 1));
+ p[3] = (Byte)(dest >> 16);
+ p[2] = (Byte)(dest >> 8);
+ p[1] = (Byte)dest;
+ bufferPos += 5;
+ }
+ else
+ {
+ prevMask = ((prevMask << 1) & 0x7) | 1;
+ bufferPos++;
+ }
+ }
+ prevPosT = bufferPos - prevPosT;
+ *state = ((prevPosT > 3) ? 0 : ((prevMask << ((int)prevPosT - 1)) & 0x7));
+ return bufferPos;
+}