• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Bra.h -- Branch converters for executables
2 2023-04-02 : Igor Pavlov : Public domain */
3 
4 #ifndef ZIP7_INC_BRA_H
5 #define ZIP7_INC_BRA_H
6 
7 #include "7zTypes.h"
8 
9 EXTERN_C_BEGIN
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 
37 /*
38 These functions convert data that contain CPU instructions.
39 Each such function converts relative addresses to absolute addresses in some
40 branch instructions: CALL (in all converters) and JUMP (X86 converter only).
41 Such conversion allows to increase compression ratio, if we compress that data.
42 
43 There are 2 types of converters:
44   Byte * Conv_RISC (Byte *data, SizeT size, UInt32 pc);
45   Byte * ConvSt_X86(Byte *data, SizeT size, UInt32 pc, UInt32 *state);
46 Each Converter supports 2 versions: one for encoding
47 and one for decoding (_Enc/_Dec postfixes in function name).
48 
49 In params:
50   data  : data buffer
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.
62 
63 Each converter has some characteristics: Endian, Alignment, LookAhead.
64   Type   Endian  Alignment  LookAhead
65 
66   X86    little      1          4
67   ARMT   little      2          2
68   ARM    little      4          0
69   ARM64  little      4          0
70   PPC     big        4          0
71   SPARC   big        4          0
72   IA64   little     16          0
73 
74   (data) must be aligned for (Alignment).
75   processed size can be calculated as:
76     SizeT processed = Conv(data, size, pc) - data;
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.
81 
82 Example code for conversion in loop:
83   UInt32 pc = 0;
84   size = 0;
85   for (;;)
86   {
87     size += Load_more_input_data(data + size);
88     SizeT processed = Conv(data, size, pc) - data;
89     if (processed == 0 && no_more_input_data_after_size)
90       break; // we stop convert loop
91     data += processed;
92     size -= processed;
93     pc += processed;
94   }
95 */
96 
97 EXTERN_C_END
98 
99 #endif
100