-
Notifications
You must be signed in to change notification settings - Fork 0
/
maths.go
260 lines (192 loc) · 6.45 KB
/
maths.go
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
package main
import (
"fmt"
"math"
"math/rand"
"time"
)
// Defines sections of file to process
type section struct {
sectionType SECTION_TYPE
start int64
end int64
size int64
}
// Returns true if given value is in range of 1 to 100, false if outside of range
func IsPercentageValid(_percentage int) bool {
percentageValid := false
if _percentage > 0 && _percentage <= math.MaxInt32 {
if _percentage >= 1 && _percentage <= 100 {
percentageValid = true
} else {
percentageValid = false
}
} else {
fmt.Println(fmt.Sprintf(UI_ParameterInvalid, GetFunctionName()), "_percentage:", _percentage)
}
return percentageValid
}
// Gets the size of data (in bytes) to overwrite for the given file
func GetFilePercentage(_fileSize int64, _percentage int) int64 {
var filePercentageSize int64
filePercentageSize = -1
if _fileSize > 0 && _fileSize <= math.MaxInt64 && _percentage > 0 && _percentage <= math.MaxInt32 {
if _fileSize <= 100 {
filePercentageSize = _fileSize
} else {
filePercentageSize = _fileSize * int64(_percentage) / 100
}
} else {
fmt.Println(fmt.Sprintf(UI_ParameterInvalid, GetFunctionName()), "_fileSize:", _fileSize, "_percentage:", _percentage)
}
return filePercentageSize
}
// Gets a single file section to overwrite entire file
func GetSection(_fileSize int64) []section {
var sections []section
var currentSection section
if _fileSize >= 1 && _fileSize <= math.MaxInt64 {
currentSection.sectionType = OVERWRITE_SECTION
currentSection.start = 0
currentSection.end = _fileSize
currentSection.size = _fileSize
sections = append(sections, currentSection)
} else {
fmt.Println(fmt.Sprintf(UI_ParameterInvalid, GetFunctionName()), "_fileSize:", _fileSize)
}
return sections
}
// Gets the file locations for the sections to overwrite
func GetSections(_fileSize int64, _filePercentageToOverwriteSize int64) []section {
var sections []section
sections = nil
var sectionsFit = false
if _fileSize >= 1 && _fileSize <= math.MaxInt64 && _filePercentageToOverwriteSize >= 1 && _filePercentageToOverwriteSize <= math.MaxInt64 {
if _filePercentageToOverwriteSize > 100 {
var sectionSize int64
sectionSize = -1
numberOfSections := -1
sectionSize, numberOfSections = GetSectionSize(_filePercentageToOverwriteSize, SECTIONS)
if sectionSize != -1 && numberOfSections != -1 {
var offset int64
offset = 0
overwriteSection := false
for checkCounter := 0; checkCounter != 5 && sectionsFit == false; checkCounter++ {
for counter := 0; counter != numberOfSections; counter++ {
var currentSection section
if overwriteSection == true {
currentSection.sectionType = OVERWRITE_SECTION
currentSection.start = offset
currentSection.end = offset + sectionSize
currentSection.size = sectionSize
} else {
currentSection.sectionType = RANDOM_SPACE
currentSection.start = offset
currentSection.end = offset + GetRandomNumber(int64(numberOfSections)*10, int64(numberOfSections)*500)
}
offset = currentSection.end
// if sections are greater than file size - stop
if offset > _fileSize {
break
}
if overwriteSection == true {
overwriteSection = false
} else {
overwriteSection = true
}
sections = append(sections, currentSection)
} // end sections loop
sectionsFit = CheckSections(_fileSize, sections)
} // end check sections loop
}
}
if sectionsFit == false {
sections = nil
}
} else {
fmt.Println(fmt.Sprintf(UI_ParameterInvalid, GetFunctionName()), "_fileSize:", _fileSize, "_filePercentageToOverwriteSize:", _filePercentageToOverwriteSize)
}
return sections
}
// Check that file offsets are not larger than the file size
func CheckSections(_fileSize int64, _sections []section) bool {
pass := true
if _fileSize <= math.MaxInt64 && _sections != nil && len(_sections) > 0 {
if _fileSize < 1 {
pass = false
} else {
if _fileSize >= 1 && _fileSize <= math.MaxInt64 && len(_sections) > 0 {
for counter := 0; counter != len(_sections); counter++ {
if _sections[counter].start >= _fileSize || _sections[counter].end >= _fileSize {
pass = false
break
}
} // end loop
}
}
} else {
pass = false
fmt.Println(fmt.Sprintf(UI_ParameterInvalid, GetFunctionName()), "_fileSize:", _fileSize, "_sections:", _sections)
}
return pass
}
// Gets the size of a single section in bytes, and number of sections
func GetSectionSize(_filePercentageToOverwrite int64, _numberOfSections int64) (int64, int) {
var sectionSize int64
sectionSize = -1
var numberOfSections int
numberOfSections = -1
if _filePercentageToOverwrite >= 1 && _filePercentageToOverwrite <= math.MaxInt64 && _numberOfSections >= 1 && _numberOfSections <= math.MaxInt64 {
for currentSection := _numberOfSections; currentSection != 0; currentSection-- {
sectionSize = _filePercentageToOverwrite / _numberOfSections
if sectionSize <= 0 {
if currentSection == 1 && sectionSize == 0 {
sectionSize = 1
numberOfSections = int(currentSection)
}
} else {
numberOfSections = int(currentSection)
break
}
}
} else {
fmt.Println(fmt.Sprintf(UI_ParameterInvalid, GetFunctionName()), "_filePercentageToOverwrite:", _filePercentageToOverwrite, "_numberOfSections:", _numberOfSections)
}
return sectionSize, numberOfSections
}
// Gets a random number between given low and high values
func GetRandomNumber(_low int64, _high int64) int64 {
var randomNumber int64
randomNumber = -1
if _low >= 1 && _low <= math.MaxInt64 && _high >= 1 && _high <= math.MaxInt64 {
if _low < _high {
rand.Seed(time.Now().UnixNano())
randomNumber = rand.Int63n(_high-_low+1) + _low
}
} else {
fmt.Println(fmt.Sprintf(UI_ParameterInvalid, GetFunctionName()), "_low:", _low, "_high:", _high)
}
return randomNumber
}
// Gets a byte array of given size filled with random data
func GetRandomData(_size int64) []byte {
var randomData []byte
randomData = nil
if _size > 0 && _size <= math.MaxInt64 {
randomData = make([]byte, _size)
if int64(len(randomData)) != _size {
fmt.Println(UI_RandomDataError)
} else {
_, err := rand.Read(randomData)
if err != nil {
fmt.Println(UI_RandomDataError, _size, err.Error())
}
if int64(len(randomData)) != _size {
fmt.Println(UI_RandomDataError)
}
}
} else {
fmt.Println(fmt.Sprintf(UI_ParameterInvalid, GetFunctionName()), "_size:", _size)
}
return randomData
}