1 /**************************************************************************
3 * Copyright 2013-2014 RAD Game Tools and Valve Software
4 * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 **************************************************************************/
27 /* Bcj2.c -- Converter for x86 code (BCJ2)
28 2008-10-04 : Igor Pavlov : Public domain */
29 #include "vogl_core.h"
30 #include "lzma_Bcj2.h"
41 #define IsJcc(b0, b1) ((b0) == 0x0F && ((b1) & 0xF0) == 0x80)
42 #define IsJ(b0, b1) ((b1 & 0xFE) == 0xE8 || IsJcc(b0, b1))
44 #define kNumTopBits 24
45 #define kTopValue ((UInt32)1 << kNumTopBits)
47 #define kNumBitModelTotalBits 11
48 #define kBitModelTotal (1 << kNumBitModelTotalBits)
49 #define kNumMoveBits 5
51 #define RC_READ_BYTE (*buffer++)
54 if (buffer == bufferLim) \
55 return SZ_ERROR_DATA; \
62 for (i = 0; i < 5; i++) \
65 code = (code << 8) | RC_READ_BYTE; \
70 if (range < kTopValue) \
74 code = (code << 8) | RC_READ_BYTE; \
79 bound = (range >> kNumBitModelTotalBits) * ttt; \
83 *(p) = (CProb)(ttt + ((kBitModelTotal - ttt) >> kNumMoveBits)); \
88 *(p) = (CProb)(ttt - (ttt >> kNumMoveBits)); \
92 const Byte *buf0, SizeT size0,
93 const Byte *buf1, SizeT size1,
94 const Byte *buf2, SizeT size2,
95 const Byte *buf3, SizeT size3,
96 Byte *outBuf, SizeT outSize)
99 SizeT inPos = 0, outPos = 0;
101 const Byte *buffer, *bufferLim;
106 for (i = 0; i < sizeof(p) / sizeof(p[0]); i++)
107 p[i] = kBitModelTotal >> 1;
110 bufferLim = buffer + size3;
123 SizeT limit = size0 - inPos;
124 if (outSize - outPos < limit)
125 limit = outSize - outPos;
128 Byte b = buf0[inPos];
129 outBuf[outPos++] = b;
130 if (IsJ(prevByte, b))
137 if (limit == 0 || outPos == outSize)
163 return SZ_ERROR_DATA;
171 return SZ_ERROR_DATA;
175 dest = (((UInt32)v[0] << 24) | ((UInt32)v[1] << 16) |
176 ((UInt32)v[2] << 8) | ((UInt32)v[3])) -
177 ((UInt32)outPos + 4);
178 outBuf[outPos++] = (Byte)dest;
179 if (outPos == outSize)
181 outBuf[outPos++] = (Byte)(dest >> 8);
182 if (outPos == outSize)
184 outBuf[outPos++] = (Byte)(dest >> 16);
185 if (outPos == outSize)
187 outBuf[outPos++] = prevByte = (Byte)(dest >> 24);
190 return (outPos == outSize) ? SZ_OK : SZ_ERROR_DATA;