-
Notifications
You must be signed in to change notification settings - Fork 0
/
check-md5.ps1
189 lines (167 loc) · 5.85 KB
/
check-md5.ps1
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
if($args.length -ne 3){
Write-Output $("Wrong parameters number!\nThese 3 parameters are needed:`n - First checksum file`n - Second checksum file`n - File where to write negative results of the comparison`n")
exit
}
$filepath1=$args[0]
$filepath2=$args[1]
$file1=Get-Content $args[0]
$file2=Get-Content $args[1]
$results=$args[2]
$matchFound=$True
$totalLines=$file1.length
$startIndex=0
$resumeData=@(0)*3
$scriptDataPath=".check_md5/.resume_data"
if(-not (Test-Path $results -PathType Leaf)){
New-Item $results -Force | Out-Null
}
function generateResumeData{
New-Item $scriptDataPath -Force | Out-Null
Write-Output "Calculating md5 of the files to compare..."
$integrity1=$(md5deep $filepath1).Substring(0, 32)
$integrity2=$(md5deep $filepath2).Substring(0, 32)
Add-Content -Path $scriptDataPath $integrity1 | Out-Null
Add-Content -Path $scriptDataPath $integrity2 | Out-Null
$resumeData[0]=$integrity1
$resumeData[1]=$integrity2
return
}
$global:counter = 0
# Merges two sorted halves of a subarray
# $theArray is an array of comparable objects
# $tempArray is an array to place the merged result
# $leftPos is the left-most index of the subarray
# $rightPos is the index of the start of the second half
# $rightEnd is the right-most index of the subarray
function merge($theArray, $tempArray, [int] $leftPos, [int] $rightPos, [int] $rightEnd)
{
$leftEnd = $rightPos - 1
$tmpPos = $leftPos
$numElements = $rightEnd - $leftPos + 1
# Main loop
while (($leftPos -le $leftEnd) -and ($rightPos -le $rightEnd))
{
$global:counter++
if ($theArray[$leftPos].CompareTo($theArray[$rightPos]) -le 0)
{
$tempArray[$tmpPos++] = $theArray[$leftPos++]
}
else
{
$tempArray[$tmpPos++] = $theArray[$rightPos++]
}
}
while ($leftPos -le $leftEnd)
{
$tempArray[$tmpPos++] = $theArray[$leftPos++]
}
while ($rightPos -le $rightEnd)
{
$tempArray[$tmpPos++] = $theArray[$rightPos++]
}
# Copy $tempArray back
for ($i = 0; $i -lt $numElements; $i++, $rightEnd--)
{
$theArray[$rightEnd] = $tempArray[$rightEnd]
}
}
# Makes recursive calls
# $theArray is an array of comparable objects
# $tempArray is an array to place the merged result
# $left is the left-most index of the subarray
# $right is the right-most index of the subarray
function mergesorter( $theArray, $tempArray, [int] $left, [int] $right )
{
if ($left -lt $right)
{
[int] $center = [Math]::Floor(($left + $right) / 2)
mergesorter $theArray $tempArray $left $center
mergesorter $theArray $tempArray ($center + 1) $right
merge $theArray $tempArray $left ($center + 1) $right
}
}
function Binary-Search {
Param (
[Parameter(Mandatory=$True)
]
$InputArray,
$SearchVal,
$Attribute)
$LowIndex = 0 #Low side of array segment
$Counter = 0
$TempVal = "" #Used to determine end of search where $Found = $False
$HighIndex = $InputArray.count #High Side of array segment
[int]$MidPoint = ($HighIndex-$LowIndex)/2 #Mid point of array segment
$found = $False
While($LowIndex -le $HighIndex){
$MidVal = $InputArray[$MidPoint]
If($TempVal -eq $MidVal){ #If identical, the search has completed and $Found = $False
$found = $False
Return
}
else{
$TempVal = $MidVal #Update the TempVal. Search continues.
}
If($SearchVal -lt $MidVal) {
$Counter++
$HighIndex = $MidPoint
[int]$MidPoint = (($HighIndex-$LowIndex)/ 2 +$LowIndex)
}
If($SearchVal -gt $MidVal) {
$Counter++
$LowIndex = $MidPoint
[int]$MidPoint = ($MidPoint+(($HighIndex - $MidPoint) / 2))
}
If($SearchVal -eq $MidVal) {
$found = $True
break
}
}
return $found
}
if(Test-Path $scriptDataPath -PathType Leaf){
Write-Output "Parsing resume data..."
$resumeData=Get-Content $scriptDataPath
if($resumeData.length -ne 3){
$resumeData=@(0)*3
Write-Output "Corrupted resume data file. Deleting..."
Remove-Item $scriptDataPath -Force
generateResumeData
}
else{
$oldIntegrity1=$resumeData[0]
$oldIntegrity2=$resumeData[1]
Write-Output "Calculating md5 of the files to compare..."
$integrity1=(md5deep $args[0]).Substring(0, 32)
$integrity2=(md5deep $args[1]).Substring(0, 32)
if($integrity1 -eq $oldIntegrity1){
Write-Output "Files to compare md5 matched! Starting from the last index before interruption."
$startIndex=$resumeData[2] -as [int]
}
else{
Write-Output "Files to compare md5 mismatch. Starting from the first index."
}
}
}
else{
Write-Output "No old execution data found."
generateResumeData
}
Write-Host "Sorting second file with MergeSort..."
$tempArray = New-Object Object[] $file2.Count
mergesorter $file2 $tempArray 0 ($file2.Count - 1)
Write-Host "MergeSort finished."
Write-Output "`t--- Start of comparison ---`n"
for($i=$startIndex; $i -lt $file1.length; $i++){
$found=Binary-Search $file2 $file1[$i]
if($found){
Write-Output $($filename1+" OK"+"`t`t["+$($i+1)+"/"+$totalLines+"]")
}
else{
Write-Output $($file1[$i]+" NO!"+"`t`t["+$($i+1)+"/"+$totalLines+"]")| Out-File -Path $results -Append
}
$resumeData[2]=$i
$resumeData | Out-File -Path $scriptDataPath
}
Write-Output "`n`t--- End of comparison ---"
Remove-Item $scriptDataPath -Force