-
Notifications
You must be signed in to change notification settings - Fork 7
/
merger.pb
322 lines (290 loc) · 10.6 KB
/
merger.pb
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
EnableExplicit
IncludeFile "/libs/Curve64_pub.pb"
Structure settingsStructure
rangeB$
rangeE$
address$
isFind.b
itemsnumber.i
dp.i
mapFilename$
totalscanned.i
EndStructure
#ADDRESSSIZE=34
#HEADERSIZE=64+#ADDRESSSIZE+1
#DATASIZE=65
#colorBlue=1
#colorGreen=2
#colorCyan=3
#colorRed=4
#colorMagenta=5
#colorBrown=6
#colorDefault=7
#colorDarkgrey=8
#colorYellow=14
#colorWhite=15
#colorbrightmagenta=13
#colorBrightGreen = 10
Enumeration
#File
#File2
EndEnumeration
#APPVERSION="1.0"
Define NewMap settings.settingsStructure()
Define MutexConsole = CreateMutex()
Procedure SPrint(text$, cl)
Shared MutexConsole
LockMutex(MutexConsole)
ConsoleColor(cl,0)
Debug FormatDate("%hh:%ii:%ss ", Date())+" "+text$
PrintN(FormatDate("%hh:%ii:%ss ", Date())+" "+text$)
ConsoleColor(#colorDefault,0)
UnlockMutex(MutexConsole)
EndProcedure
Procedure.s m_cutHex(a$)
a$=Trim(UCase(a$))
If Left(a$,2)="0X"
a$=Mid(a$,3,Len(a$)-2)
EndIf
If Len(a$)=1
a$="0"+a$
EndIf
ProcedureReturn LCase(a$)
EndProcedure
Procedure.i getprogparam()
Protected parametrscount, err, i
Shared settings()
parametrscount=CountProgramParameters()
If parametrscount=3
i=0
While i<parametrscount
settings(Str(i))\mapFilename$ = ProgramParameter(i)
i+1
Wend
Else
Sprint("Invalid numbers of params", #colorRed)
err=1
EndIf
ProcedureReturn err
EndProcedure
Procedure.s m_gethex32(*bin, szbytes)
Protected *sertemp=AllocateMemory(szbytes*2, #PB_Memory_NoClear)
Protected res$
;************************************************************************
;Convert bytes in LITTLE indian format to HEX string in BIG indian format
;************************************************************************
Curve::m_serializeX64(*bin,0,*sertemp,szbytes/4)
res$=PeekS(*sertemp,szbytes*2, #PB_Ascii)
FreeMemory(*sertemp)
ProcedureReturn res$
EndProcedure
Procedure m_sethex32(*bin, *hash, szbytes)
Protected a$=PeekS(*hash), i
;************************************************************************
;Convert HEX string in BIG indian format to bytes in LITTLE indian format
;************************************************************************
a$ = m_cutHex(a$)
a$=RSet(a$,szbytes*2,"0")
Curve::m_deserializeX64(*bin,0,@a$,szbytes/4)
EndProcedure
Procedure readheader(*set.settingsStructure)
Protected err=0, i, *MemoryBuffer=AllocateMemory(#HEADERSIZE), procname$ = "[RDH] "
If FileSize(*set\mapFilename$)=-1
Sprint(procname$+ "Can`t found "+*set\mapFilename$+" map file", #colorRed)
err=1
EndIf
If Not err
If ReadFile(#File, *set\mapFilename$,#PB_File_SharedRead)
;read header
ReadData(#File, *MemoryBuffer, #HEADERSIZE)
*set\rangeB$ = m_gethex32(*MemoryBuffer, 32)
*set\rangeE$ = m_gethex32(*MemoryBuffer+32,32)
*set\address$ = PeekS(*MemoryBuffer+64,#ADDRESSSIZE)
*set\isFind = PeekB(*MemoryBuffer+64+#ADDRESSSIZE)
*set\itemsnumber = (Lof(#File) -#HEADERSIZE)/#DATASIZE
i=*set\itemsnumber
While i>1
i>>1
*set\dp+1
Wend
sprint(procname$+"File : "+*set\mapFilename$,#colorWhite)
sprint(procname$+"Range begin : "+*set\rangeB$,#colorDarkgrey)
sprint(procname$+"Range end : "+*set\rangeE$,#colorDarkgrey)
sprint(procname$+"Address : "+*set\address$,#colorDarkgrey)
sprint(procname$+"Number of subranges : "+*set\itemsnumber,#colorDarkgrey)
sprint(procname$+"DP : 2^"+Str(*set\dp),#colorDarkgrey)
If *set\isFind
sprint(procname$+"IsFind : True",#colorGreen)
Else
sprint(procname$+"IsFind : False",#colorDarkgrey)
EndIf
If *set\itemsnumber=0
Sprint(procname$+ "Invalid number of subranges", #colorRed)
err=1
EndIf
If Not err
i=0
While Not Eof(#File)
ReadData(#File, *MemoryBuffer, #DATASIZE)
If PeekB(*MemoryBuffer+64)
*set\totalscanned+1
EndIf
i+1
Wend
If i<>*set\itemsnumber
Sprint(procname$+ "Invalid number of subranges> need["+Str(*set\itemsnumber)+"] got ["+Str(i)+"]", #colorRed)
EndIf
sprint(procname$+"Total scanned : "+Str(*set\totalscanned),#colorDarkgrey)
EndIf
CloseFile(#File)
Else
Sprint(procname$+ "Can`t read "+*set\mapFilename$+" map file", #colorRed)
err=1
EndIf
EndIf
FreeMemory(*MemoryBuffer)
ProcedureReturn err
EndProcedure
Procedure merge(*init.settingsStructure,*merg.settingsStructure, savefilename$)
Protected err, procname$ = "[MERGER] ", filepos1, filepos2, newscannedcounter, groupsize, i, step1, step2
Protected *MemoryBuffer=AllocateMemory(#HEADERSIZE), *TempBuffer=AllocateMemory(#HEADERSIZE)
groupsize = *init\itemsnumber / *merg\itemsnumber
Sprint(procname$+ "groupsize = "+Str(groupsize), #colorDefault)
If CopyFile(*init\mapFilename$, savefilename$)
If Not OpenFile(#File, savefilename$)
Sprint(procname$+ "Can`t found "+savefilename$+" map file", #colorRed)
err=1
EndIf
If Not err
If Not ReadFile(#File2, *merg\mapFilename$,#PB_File_SharedRead)
Sprint(procname$+ "Can`t found "+*merg\mapFilename$+" map file", #colorRed)
err=1
EndIf
If Not err
Sprint(procname$+ "Merge "+*merg\mapFilename$+" into "+*init\mapFilename$+" and save as "+savefilename$, #colorWhite)
FileSeek(#File2, filepos2 * #DATASIZE+ #HEADERSIZE,#PB_Absolute)
While Not Eof(#File2) And err=0
ReadData(#File2, *MemoryBuffer, #DATASIZE)
Debug "rb>"+m_gethex32(*MemoryBuffer, 32)
Debug "re>"+m_gethex32(*MemoryBuffer+32,32)
Debug "iS>"+PeekC(*MemoryBuffer+64)
If PeekC(*MemoryBuffer+64)
;range scanned
filepos1 = filepos2 * groupsize
FileSeek(#File, filepos1 * #DATASIZE+ #HEADERSIZE ,#PB_Absolute)
i = 0
While i<groupsize
ReadData(#File, *TempBuffer, #DATASIZE)
Debug "loc>"+Loc(#File)
Debug "Srb>"+m_gethex32(*TempBuffer, 32)
Debug "Sre>"+m_gethex32(*TempBuffer+32,32)
Debug "SiS>"+PeekC(*TempBuffer+64)
;check range
step1 = Curve::m_check_less_more_equilX64(*TempBuffer,*MemoryBuffer)
step2 = Curve::m_check_less_more_equilX64(*TempBuffer+32,*MemoryBuffer+32)
If (step1=2 Or step1=0) And (step2=1 Or step2=0)
; rb2>=rb and re2<=re
If PeekC(*TempBuffer+64)=0
;subrange is not scanned yet
;move write pointer
FileSeek(#File, -1 ,#PB_Relative)
PokeB(*TempBuffer+64,1)
WriteData(#File, *TempBuffer+64, 1)
FileSeek(#File, (filepos1 + i+1) * #DATASIZE+ #HEADERSIZE -1 ,#PB_Absolute)
ReadData(#File, *TempBuffer+64, 1)
Debug "loc>"+Loc(#File)
If PeekC(*TempBuffer+64)=1
Debug "OK"
newscannedcounter+1
Else
Debug "SiS>"+PeekC(*TempBuffer+64)
Sprint(procname$+ "Can`t save value to map file", #colorRed)
err=1
Break
EndIf
EndIf
Else
Sprint(procname$+ "Hmm. subrange mismatch", #colorRed)
err=1
Break
EndIf
i+1
Wend
EndIf
filepos2+1
Wend
CloseFile(#File)
CloseFile(#File2)
EndIf
Else
Sprint(procname$+ "Can`t read "+savefilename$+" map file", #colorRed)
err=1
EndIf
If err
DeleteFile(savefilename$,#PB_FileSystem_Force)
err=1
EndIf
Else
Sprint(procname$+ "Can`t copy map file", #colorRed)
err=1
EndIf
If Not err
Sprint(procname$+ "Merged "+Str(newscannedcounter)+" new subranges", #colorCyan)
EndIf
FreeMemory(*MemoryBuffer)
FreeMemory(*TempBuffer)
ProcedureReturn err
EndProcedure
OpenConsole()
sprint("app version: "+#APPVERSION,#colorDarkgrey)
Define err
settings("0")\mapFilename$="mmm.bin"
settings("1")\mapFilename$="mmmmerge.bin"
settings("2")\mapFilename$="mmmsave.bin"
If Not getprogparam()
If CompareMemoryString(@settings("2")\mapFilename$, @settings("0")\mapFilename$ ,#PB_String_CaseSensitive)<>#PB_String_Equal And CompareMemoryString(@settings("2")\mapFilename$, @settings("1")\mapFilename$,#PB_String_CaseSensitive)<>#PB_String_Equal
If CompareMemoryString(@settings("0")\mapFilename$, @settings("1")\mapFilename$ ,#PB_String_CaseSensitive)<>#PB_String_Equal
If Not readheader(@settings("0"))
If Not readheader(@settings("1"))
If CompareMemoryString(@settings("0")\rangeB$, @settings("1")\rangeB$ ,#PB_String_CaseSensitive)=#PB_String_Equal
If CompareMemoryString(@settings("0")\rangeE$, @settings("1")\rangeE$ ,#PB_String_CaseSensitive)=#PB_String_Equal
If CompareMemoryString(@settings("0")\address$, @settings("1")\address$ ,#PB_String_CaseSensitive)=#PB_String_Equal
If settings("0")\isFind=0 And settings("1")\isFind=0
If settings("0")\itemsnumber>= settings("1")\itemsnumber
err = merge(@settings("0"),settings("1"), settings("2")\mapFilename$)
Else
err = merge(@settings("1"),settings("0"), settings("2")\mapFilename$)
EndIf
If Not err
readheader(@settings("2"))
EndIf
Else
Sprint("You already solve key", #colorRed)
EndIf
Else
Sprint("Address mismatch", #colorRed)
EndIf
Else
Sprint("Range end mismatch", #colorRed)
EndIf
Else
Sprint("Range begin mismatch", #colorRed)
EndIf
EndIf
EndIf
Else
Sprint("Source map can not be equil to merged map", #colorRed)
EndIf
Else
Sprint("Saved map can not be equil to source or merged map", #colorRed)
EndIf
EndIf
End
; IDE Options = PureBasic 5.31 (Windows - x64)
; ExecutableFormat = Console
; CursorPosition = 37
; FirstLine = 9
; Folding = --
; EnableXP
; Executable = ..\release\v1_0\merger.exe
; CommandLine = mmm.bin mmmmerge.bin mmmsave.bin