forked from sisong/HDiffPatch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_hextobytes.h
More file actions
112 lines (99 loc) · 4.52 KB
/
_hextobytes.h
File metadata and controls
112 lines (99 loc) · 4.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//_hextobytes.h
//
/*
This is the HDiffPatch copyright.
Copyright (c) 2018-2025 HouSisong All Rights Reserved.
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef HDiffPatch_hextobytes_h
#define HDiffPatch_hextobytes_h
#include "libHDiffPatch/HPatch/patch_types.h"
#ifdef __cplusplus
extern "C" {
#endif
static const char _halfbyte_to_hex1[16]={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
static const char _halfbyte_to_HEX1[16]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
hpatch_inline static
void byte_to_hex2(hpatch_byte b,char out_hex2[2]){
out_hex2[0]=_halfbyte_to_hex1[b>>4]; //h
out_hex2[1]=_halfbyte_to_hex1[b&0xF];//l
}
hpatch_inline static
void byte_to_HEX2(hpatch_byte b,char out_HEX2[2]){
out_HEX2[0]=_halfbyte_to_HEX1[b>>4]; //h
out_HEX2[1]=_halfbyte_to_HEX1[b&0xF];//l
}
//out_hexs must have enough space for 2*blen
hpatch_inline static
void bytes_to_hexs(const hpatch_byte* bytes,hpatch_size_t blen,char* out_hexs){
size_t i;
for (i=0; i<blen; ++i)
byte_to_hex2(bytes[i],&out_hexs[i*2]);
}
hpatch_inline static
void bytes_to_HEXs(const hpatch_byte* bytes,hpatch_size_t blen,char* out_HEXs){
size_t i;
for (i=0; i<blen; ++i)
byte_to_HEX2(bytes[i],&out_HEXs[i*2]);
}
static const unsigned char _hex1_to_halfbyte[256] = {
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07, 0x08,0x09,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, //0-9
0xFF,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,0xFF, 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, //A-F
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0xFF, 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, //a-f
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
};
#define __private_hex2_to_byte(hex_0,hex_1,rb) \
hpatch_byte h=_hex1_to_halfbyte[(unsigned char)(hex_0)]; \
hpatch_byte l=_hex1_to_halfbyte[(unsigned char)(hex_1)]; \
(rb)=(hpatch_byte)((h<<4)|l);
hpatch_inline static
hpatch_BOOL hex2_to_byte(const char hex2[2],hpatch_byte* out_b){
__private_hex2_to_byte(hex2[0],hex2[1],*out_b);
return ((h|l) <= 0xF);
}
//hexStrLen must be even, out_bytes must have enough space for hexStrLen/2 bytes
hpatch_inline static
hpatch_BOOL hexs_to_bytes(const char* hexs,hpatch_size_t hexStrLen,hpatch_byte* out_bytes){
hpatch_size_t i;
hpatch_size_t blen=hexStrLen>>1;
hpatch_byte check=0;
assert((hexStrLen&1)==0);
for (i=0; i<blen; ++i){
__private_hex2_to_byte(hexs[i*2],hexs[i*2+1],out_bytes[i]);
check|=h; check|=l;
}
return (check <= 0xF);
}
#ifdef __cplusplus
}
#endif
#endif //HDiffPatch_hextobytes_h