forked from ldr123/update_project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatch.c
More file actions
217 lines (200 loc) · 8.2 KB
/
patch.c
File metadata and controls
217 lines (200 loc) · 8.2 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
//
// patch.cpp
// HPatch
//
/*
This is the HPatch copyright.
Copyright (c) 2012-2013 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.
*/
#include "patch.h"
#include "string.h" //memcpy memset
#include "assert.h" //assert
#include "patch_base.h"
static hpatch_BOOL _bytesRle_load(TByte* out_data,TByte* out_dataEnd,const TByte* rle_code,const TByte* rle_code_end);
static void addData(TByte* dst,const TByte* src,TUInt32 length);
hpatch_BOOL patch(TByte* newData,TByte* newData_end,
const TByte* oldData,const TByte* oldData_end,
const TByte* serializedDiff,const TByte* serializedDiff_end){
TUInt32 ctrlCount,lengthSize,inc_newPosSize,inc_oldPosSize,newDataDiffSize;
const TByte *code_length,*code_length_end, *code_inc_newPos,*code_inc_newPos_end,
*code_inc_oldPos,*code_inc_oldPos_end, *code_newDataDiff,*code_newDataDiff_end;
TUInt32 i,oldPosBack,newPos_end,inc_newPos,newPos,addLength,copyLength,oldPos,inc_oldPos,newDataSize;
int inc_oldPos_sign;
ctrlCount=unpack32Bit(&serializedDiff, serializedDiff_end);
lengthSize=unpack32Bit(&serializedDiff, serializedDiff_end);
inc_newPosSize=unpack32Bit(&serializedDiff, serializedDiff_end);
inc_oldPosSize=unpack32Bit(&serializedDiff, serializedDiff_end);
newDataDiffSize=unpack32Bit(&serializedDiff, serializedDiff_end);
#ifdef PATCH_RUN_MEM_SAFE_CHECK
if (lengthSize>(TUInt32)(serializedDiff_end-serializedDiff)) return hpatch_FALSE;
#endif
code_length=serializedDiff; serializedDiff+=lengthSize;
code_length_end=serializedDiff;
#ifdef PATCH_RUN_MEM_SAFE_CHECK
if (inc_newPosSize>(TUInt32)(serializedDiff_end-serializedDiff)) return hpatch_FALSE;
#endif
code_inc_newPos=serializedDiff; serializedDiff+=inc_newPosSize;
code_inc_newPos_end=serializedDiff;
#ifdef PATCH_RUN_MEM_SAFE_CHECK
if (inc_oldPosSize>(TUInt32)(serializedDiff_end-serializedDiff)) return hpatch_FALSE;
#endif
code_inc_oldPos=serializedDiff; serializedDiff+=inc_oldPosSize;
code_inc_oldPos_end=serializedDiff;
#ifdef PATCH_RUN_MEM_SAFE_CHECK
if (newDataDiffSize>(TUInt32)(serializedDiff_end-serializedDiff)) return hpatch_FALSE;
#endif
code_newDataDiff=serializedDiff; serializedDiff+=newDataDiffSize;
code_newDataDiff_end=serializedDiff;
//rle data begin==serializedDiff;
if (!_bytesRle_load(newData, newData_end, serializedDiff, serializedDiff_end))
return hpatch_FALSE;
oldPosBack=0;
newPos_end=0;
for (i=0; i<ctrlCount; ++i){
inc_newPos=unpack32Bit(&code_inc_newPos, code_inc_newPos_end);
newPos=newPos_end+inc_newPos;
addLength=unpack32Bit(&code_length, code_length_end);
inc_oldPos_sign=(*code_inc_oldPos)>>(8-1);
inc_oldPos=unpack32BitWithTag(&code_inc_oldPos, code_inc_oldPos_end, 1);
if (inc_oldPos_sign==0)
oldPos=oldPosBack+inc_oldPos;
else
oldPos=oldPosBack-inc_oldPos;
#ifdef PATCH_RUN_MEM_SAFE_CHECK
if ((oldPos>(TUInt32)(oldData_end-oldData))||(addLength>(TUInt32)(oldData_end-oldData-oldPos))) return hpatch_FALSE;
if ((newPos>(TUInt32)(newData_end-newData))||(addLength>(TUInt32)(newData_end-newData-newPos))) return hpatch_FALSE;
#endif
if (newPos>newPos_end){
copyLength=newPos-newPos_end;
#ifdef PATCH_RUN_MEM_SAFE_CHECK
if (copyLength>(TUInt32)(code_newDataDiff_end-code_newDataDiff)) return hpatch_FALSE;
#endif
memcpy(newData+newPos_end,code_newDataDiff,copyLength);
code_newDataDiff+=copyLength;
}
addData(newData+newPos,oldData+oldPos,addLength);
oldPosBack=oldPos;
newPos_end=newPos+addLength;
}
newDataSize=(TUInt32)(newData_end-newData);
if (newPos_end<newDataSize){
copyLength=newDataSize-newPos_end;
#ifdef PATCH_RUN_MEM_SAFE_CHECK
if (copyLength>(TUInt32)(code_newDataDiff_end-code_newDataDiff)) return hpatch_FALSE;
#endif
memcpy(newData+newPos_end,code_newDataDiff,copyLength);
code_newDataDiff+=copyLength;
newPos_end=newDataSize;
}
return (code_length==code_length_end)&&(code_inc_newPos==code_inc_newPos_end)
&&(code_inc_oldPos==code_inc_oldPos_end)&&(code_newDataDiff==code_newDataDiff_end);
}
//变长32bit正整数编码方案(x bit额外类型标志位,x<=3),从高位开始输出1-5byte:
// x0* 7-x bit
// x1* 0* 7+7-x bit
// x1* 1* 0* 7+7+7-x bit
// x1* 1* 1* 0* 7+7+7+7-x bit
// x1* 1* 1* 1* 0* 7+7+7+7+7-x bit
TUInt32 unpack32BitWithTag(const TByte** src_code,const TByte* src_code_end,const int kTagBit){//读出整数并前进指针.
const TByte* pcode;
TUInt32 value;
TByte code;
pcode=*src_code;
#ifdef PATCH_RUN_MEM_SAFE_CHECK
if (src_code_end-pcode<=0) return 0;
#endif
code=*pcode; ++pcode;
value=code&((1<<(7-kTagBit))-1);
if ((code&(1<<(7-kTagBit)))!=0){
do {
#ifdef PATCH_RUN_MEM_SAFE_CHECK
assert((value>>(sizeof(value)*8-7))==0);
if (src_code_end==pcode) break;
#endif
code=*pcode; ++pcode;
value=(value<<7) | (code&((1<<7)-1));
} while ((code&(1<<7))!=0);
}
(*src_code)=pcode;
return value;
}
static void addData(TByte* dst,const TByte* src,TUInt32 length){
TUInt32 length_fast,i;
length_fast=length&(~7);
for (i=0;i<length_fast;i+=8){
dst[i ]+=src[i ];
dst[i+1]+=src[i+1];
dst[i+2]+=src[i+2];
dst[i+3]+=src[i+3];
dst[i+4]+=src[i+4];
dst[i+5]+=src[i+5];
dst[i+6]+=src[i+6];
dst[i+7]+=src[i+7];
}
for (;i<length;++i)
dst[i]+=src[i];
}
static hpatch_BOOL _bytesRle_load(TByte* out_data,TByte* out_dataEnd,const TByte* rle_code,const TByte* rle_code_end){
TUInt32 ctrlSize,length;
const TByte* ctrlBuf,*ctrlBuf_end;
enum TByteRleType type;
ctrlSize= unpack32Bit(&rle_code,rle_code_end);
#ifdef PATCH_RUN_MEM_SAFE_CHECK
if (ctrlSize>(TUInt32)(rle_code_end-rle_code)) return hpatch_FALSE;
#endif
ctrlBuf=rle_code;
rle_code+=ctrlSize;
ctrlBuf_end=rle_code;
while (ctrlBuf_end-ctrlBuf>0){
type=(enum TByteRleType)((*ctrlBuf)>>(8-kByteRleType_bit));
length= 1 + unpack32BitWithTag(&ctrlBuf,ctrlBuf_end,kByteRleType_bit);
#ifdef PATCH_RUN_MEM_SAFE_CHECK
if (length>(TUInt32)(out_dataEnd-out_data)) return hpatch_FALSE;
#endif
switch (type){
case kByteRleType_rle0:{
memset(out_data,0,length);
out_data+=length;
}break;
case kByteRleType_rle255:{
memset(out_data,255,length);
out_data+=length;
}break;
case kByteRleType_rle:{
#ifdef PATCH_RUN_MEM_SAFE_CHECK
if (rle_code_end-rle_code<1) return hpatch_FALSE;
#endif
memset(out_data,*rle_code,length);
++rle_code;
out_data+=length;
}break;
case kByteRleType_unrle:{
#ifdef PATCH_RUN_MEM_SAFE_CHECK
if (length>(TUInt32)(rle_code_end-rle_code)) return hpatch_FALSE;
#endif
memcpy(out_data,rle_code,length);
rle_code+=length;
out_data+=length;
}break;
}
}
return (ctrlBuf==ctrlBuf_end)&&(rle_code==rle_code_end)&&(out_data==out_dataEnd);
}