]> git.cworth.org Git - vogl/blob - src/voglcore/lzma_Bra.cpp
Initial vogl checkin
[vogl] / src / voglcore / lzma_Bra.cpp
1 /**************************************************************************
2  *
3  * Copyright 2013-2014 RAD Game Tools and Valve Software
4  * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC
5  * All Rights Reserved.
6  *
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:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
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
23  * THE SOFTWARE.
24  *
25  **************************************************************************/
26
27 /* Bra.c -- Converters for RISC code
28 2008-10-04 : Igor Pavlov : Public domain */
29 #include "vogl_core.h"
30 #include "lzma_Bra.h"
31
32 namespace vogl
33 {
34
35     SizeT ARM_Convert(Byte *data, SizeT size, UInt32 ip, int encoding)
36     {
37         SizeT i;
38         if (size < 4)
39             return 0;
40         size -= 4;
41         ip += 8;
42         for (i = 0; i <= size; i += 4)
43         {
44             if (data[i + 3] == 0xEB)
45             {
46                 UInt32 dest;
47                 UInt32 src = ((UInt32)data[i + 2] << 16) | ((UInt32)data[i + 1] << 8) | (data[i + 0]);
48                 src <<= 2;
49                 if (encoding)
50                     dest = ip + (UInt32)i + src;
51                 else
52                     dest = src - (ip + (UInt32)i);
53                 dest >>= 2;
54                 data[i + 2] = (Byte)(dest >> 16);
55                 data[i + 1] = (Byte)(dest >> 8);
56                 data[i + 0] = (Byte)dest;
57             }
58         }
59         return i;
60     }
61
62     SizeT ARMT_Convert(Byte *data, SizeT size, UInt32 ip, int encoding)
63     {
64         SizeT i;
65         if (size < 4)
66             return 0;
67         size -= 4;
68         ip += 4;
69         for (i = 0; i <= size; i += 2)
70         {
71             if ((data[i + 1] & 0xF8) == 0xF0 &&
72                 (data[i + 3] & 0xF8) == 0xF8)
73             {
74                 UInt32 dest;
75                 UInt32 src =
76                     (((UInt32)data[i + 1] & 0x7) << 19) |
77                     ((UInt32)data[i + 0] << 11) |
78                     (((UInt32)data[i + 3] & 0x7) << 8) |
79                     (data[i + 2]);
80
81                 src <<= 1;
82                 if (encoding)
83                     dest = ip + (UInt32)i + src;
84                 else
85                     dest = src - (ip + (UInt32)i);
86                 dest >>= 1;
87
88                 data[i + 1] = (Byte)(0xF0 | ((dest >> 19) & 0x7));
89                 data[i + 0] = (Byte)(dest >> 11);
90                 data[i + 3] = (Byte)(0xF8 | ((dest >> 8) & 0x7));
91                 data[i + 2] = (Byte)dest;
92                 i += 2;
93             }
94         }
95         return i;
96     }
97
98     SizeT PPC_Convert(Byte *data, SizeT size, UInt32 ip, int encoding)
99     {
100         SizeT i;
101         if (size < 4)
102             return 0;
103         size -= 4;
104         for (i = 0; i <= size; i += 4)
105         {
106             if ((data[i] >> 2) == 0x12 && (data[i + 3] & 3) == 1)
107             {
108                 UInt32 src = ((UInt32)(data[i + 0] & 3) << 24) |
109                              ((UInt32)data[i + 1] << 16) |
110                              ((UInt32)data[i + 2] << 8) |
111                              ((UInt32)data[i + 3] & (~3));
112
113                 UInt32 dest;
114                 if (encoding)
115                     dest = ip + (UInt32)i + src;
116                 else
117                     dest = src - (ip + (UInt32)i);
118                 data[i + 0] = (Byte)(0x48 | ((dest >> 24) & 0x3));
119                 data[i + 1] = (Byte)(dest >> 16);
120                 data[i + 2] = (Byte)(dest >> 8);
121                 data[i + 3] &= 0x3;
122                 data[i + 3] |= dest;
123             }
124         }
125         return i;
126     }
127
128     SizeT SPARC_Convert(Byte *data, SizeT size, UInt32 ip, int encoding)
129     {
130         UInt32 i;
131         if (size < 4)
132             return 0;
133         size -= 4;
134         for (i = 0; i <= size; i += 4)
135         {
136             if (((data[i] == 0x40) && ((data[i + 1] & 0xC0) == 0x00)) ||
137                 ((data[i] == 0x7F) && ((data[i + 1] & 0xC0) == 0xC0)))
138             {
139                 UInt32 src =
140                     ((UInt32)data[i + 0] << 24) |
141                     ((UInt32)data[i + 1] << 16) |
142                     ((UInt32)data[i + 2] << 8) |
143                     ((UInt32)data[i + 3]);
144                 UInt32 dest;
145
146                 src <<= 2;
147                 if (encoding)
148                     dest = ip + i + src;
149                 else
150                     dest = src - (ip + i);
151                 dest >>= 2;
152
153                 dest = (((0 - ((dest >> 22) & 1)) << 22) & 0x3FFFFFFF) | (dest & 0x3FFFFF) | 0x40000000;
154
155                 data[i + 0] = (Byte)(dest >> 24);
156                 data[i + 1] = (Byte)(dest >> 16);
157                 data[i + 2] = (Byte)(dest >> 8);
158                 data[i + 3] = (Byte)dest;
159             }
160         }
161         return i;
162     }
163 }