diff options
Diffstat (limited to 'C/Bra.h')
-rw-r--r-- | C/Bra.h | 115 |
1 files changed, 75 insertions, 40 deletions
@@ -1,64 +1,99 @@ | |||
1 | /* Bra.h -- Branch converters for executables | 1 | /* Bra.h -- Branch converters for executables |
2 | 2013-01-18 : Igor Pavlov : Public domain */ | 2 | 2023-04-02 : Igor Pavlov : Public domain */ |
3 | 3 | ||
4 | #ifndef __BRA_H | 4 | #ifndef ZIP7_INC_BRA_H |
5 | #define __BRA_H | 5 | #define ZIP7_INC_BRA_H |
6 | 6 | ||
7 | #include "7zTypes.h" | 7 | #include "7zTypes.h" |
8 | 8 | ||
9 | EXTERN_C_BEGIN | 9 | EXTERN_C_BEGIN |
10 | 10 | ||
11 | #define Z7_BRANCH_CONV_DEC(name) z7_BranchConv_ ## name ## _Dec | ||
12 | #define Z7_BRANCH_CONV_ENC(name) z7_BranchConv_ ## name ## _Enc | ||
13 | #define Z7_BRANCH_CONV_ST_DEC(name) z7_BranchConvSt_ ## name ## _Dec | ||
14 | #define Z7_BRANCH_CONV_ST_ENC(name) z7_BranchConvSt_ ## name ## _Enc | ||
15 | |||
16 | #define Z7_BRANCH_CONV_DECL(name) Byte * name(Byte *data, SizeT size, UInt32 pc) | ||
17 | #define Z7_BRANCH_CONV_ST_DECL(name) Byte * name(Byte *data, SizeT size, UInt32 pc, UInt32 *state) | ||
18 | |||
19 | typedef Z7_BRANCH_CONV_DECL( (*z7_Func_BranchConv)); | ||
20 | typedef Z7_BRANCH_CONV_ST_DECL((*z7_Func_BranchConvSt)); | ||
21 | |||
22 | #define Z7_BRANCH_CONV_ST_X86_STATE_INIT_VAL 0 | ||
23 | Z7_BRANCH_CONV_ST_DECL(Z7_BRANCH_CONV_ST_DEC(X86)); | ||
24 | Z7_BRANCH_CONV_ST_DECL(Z7_BRANCH_CONV_ST_ENC(X86)); | ||
25 | |||
26 | #define Z7_BRANCH_FUNCS_DECL(name) \ | ||
27 | Z7_BRANCH_CONV_DECL(Z7_BRANCH_CONV_DEC(name)); \ | ||
28 | Z7_BRANCH_CONV_DECL(Z7_BRANCH_CONV_ENC(name)); | ||
29 | |||
30 | Z7_BRANCH_FUNCS_DECL(ARM64) | ||
31 | Z7_BRANCH_FUNCS_DECL(ARM) | ||
32 | Z7_BRANCH_FUNCS_DECL(ARMT) | ||
33 | Z7_BRANCH_FUNCS_DECL(PPC) | ||
34 | Z7_BRANCH_FUNCS_DECL(SPARC) | ||
35 | Z7_BRANCH_FUNCS_DECL(IA64) | ||
36 | |||
11 | /* | 37 | /* |
12 | These functions convert relative addresses to absolute addresses | 38 | These functions convert data that contain CPU instructions. |
13 | in CALL instructions to increase the compression ratio. | 39 | Each such function converts relative addresses to absolute addresses in some |
14 | 40 | branch instructions: CALL (in all converters) and JUMP (X86 converter only). | |
15 | In: | 41 | Such conversion allows to increase compression ratio, if we compress that data. |
16 | data - data buffer | 42 | |
17 | size - size of data | 43 | There are 2 types of converters: |
18 | ip - current virtual Instruction Pinter (IP) value | 44 | Byte * Conv_RISC (Byte *data, SizeT size, UInt32 pc); |
19 | state - state variable for x86 converter | 45 | Byte * ConvSt_X86(Byte *data, SizeT size, UInt32 pc, UInt32 *state); |
20 | encoding - 0 (for decoding), 1 (for encoding) | 46 | Each Converter supports 2 versions: one for encoding |
21 | 47 | and one for decoding (_Enc/_Dec postfixes in function name). | |
22 | Out: | ||
23 | state - state variable for x86 converter | ||
24 | 48 | ||
25 | Returns: | 49 | In params: |
26 | The number of processed bytes. If you call these functions with multiple calls, | 50 | data : data buffer |
27 | you must start next call with first byte after block of processed bytes. | 51 | size : size of data |
52 | pc : current virtual Program Counter (Instruction Pinter) value | ||
53 | In/Out param: | ||
54 | state : pointer to state variable (for X86 converter only) | ||
55 | |||
56 | Return: | ||
57 | The pointer to position in (data) buffer after last byte that was processed. | ||
58 | If the caller calls converter again, it must call it starting with that position. | ||
59 | But the caller is allowed to move data in buffer. so pointer to | ||
60 | current processed position also will be changed for next call. | ||
61 | Also the caller must increase internal (pc) value for next call. | ||
28 | 62 | ||
63 | Each converter has some characteristics: Endian, Alignment, LookAhead. | ||
29 | Type Endian Alignment LookAhead | 64 | Type Endian Alignment LookAhead |
30 | 65 | ||
31 | x86 little 1 4 | 66 | X86 little 1 4 |
32 | ARMT little 2 2 | 67 | ARMT little 2 2 |
33 | ARM little 4 0 | 68 | ARM little 4 0 |
69 | ARM64 little 4 0 | ||
34 | PPC big 4 0 | 70 | PPC big 4 0 |
35 | SPARC big 4 0 | 71 | SPARC big 4 0 |
36 | IA64 little 16 0 | 72 | IA64 little 16 0 |
37 | 73 | ||
38 | size must be >= Alignment + LookAhead, if it's not last block. | 74 | (data) must be aligned for (Alignment). |
39 | If (size < Alignment + LookAhead), converter returns 0. | 75 | processed size can be calculated as: |
40 | 76 | SizeT processed = Conv(data, size, pc) - data; | |
41 | Example: | 77 | if (processed == 0) |
78 | it means that converter needs more data for processing. | ||
79 | If (size < Alignment + LookAhead) | ||
80 | then (processed == 0) is allowed. | ||
42 | 81 | ||
43 | UInt32 ip = 0; | 82 | Example code for conversion in loop: |
44 | for () | 83 | UInt32 pc = 0; |
45 | { | 84 | size = 0; |
46 | ; size must be >= Alignment + LookAhead, if it's not last block | 85 | for (;;) |
47 | SizeT processed = Convert(data, size, ip, 1); | 86 | { |
48 | data += processed; | 87 | size += Load_more_input_data(data + size); |
49 | size -= processed; | 88 | SizeT processed = Conv(data, size, pc) - data; |
50 | ip += processed; | 89 | if (processed == 0 && no_more_input_data_after_size) |
51 | } | 90 | break; // we stop convert loop |
91 | data += processed; | ||
92 | size -= processed; | ||
93 | pc += processed; | ||
94 | } | ||
52 | */ | 95 | */ |
53 | 96 | ||
54 | #define x86_Convert_Init(state) { state = 0; } | ||
55 | SizeT x86_Convert(Byte *data, SizeT size, UInt32 ip, UInt32 *state, int encoding); | ||
56 | SizeT ARM_Convert(Byte *data, SizeT size, UInt32 ip, int encoding); | ||
57 | SizeT ARMT_Convert(Byte *data, SizeT size, UInt32 ip, int encoding); | ||
58 | SizeT PPC_Convert(Byte *data, SizeT size, UInt32 ip, int encoding); | ||
59 | SizeT SPARC_Convert(Byte *data, SizeT size, UInt32 ip, int encoding); | ||
60 | SizeT IA64_Convert(Byte *data, SizeT size, UInt32 ip, int encoding); | ||
61 | |||
62 | EXTERN_C_END | 97 | EXTERN_C_END |
63 | 98 | ||
64 | #endif | 99 | #endif |