Repository navigation
Expand file tree
/
Copy pathcommon.go
More file actions
422 lines (389 loc) · 10.4 KB
/
Copy pathcommon.go
File metadata and controls
422 lines (389 loc) · 10.4 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
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
// ⚡️ Fiber is an Express inspired web framework written in Go with ☕️
// 🤖 Github Repository: https://github.com/gofiber/fiber
// 📌 API Documentation: https://docs.gofiber.io
package utils
import (
"crypto/rand"
"encoding/base64"
"fmt"
"math"
"math/bits"
"net"
"os"
"reflect"
"runtime"
"slices"
"strconv"
"github.com/google/uuid"
)
const (
defaultSecureTokenLength = 32
maxFastTokenEncodedLength = 43 // base64.RawURLEncoding.EncodedLen(32)
)
var whitespaceTable = [256]bool{
'\t': true,
'\n': true,
'\v': true,
'\f': true,
'\r': true,
' ': true,
}
// UUIDv4 returns a random (version 4) UUID from crypto/rand; it panics if the random source fails.
func UUIDv4() string {
token, err := uuid.NewRandom()
if err != nil {
panic(fmt.Errorf("utils: failed to generate secure UUID: %w", err))
}
return token.String()
}
// UUID is an alias for UUIDv4, kept for backward compatibility.
func UUID() string {
return UUIDv4()
}
// GenerateSecureToken returns a cryptographically secure random token of length bytes, encoded with
// base64.RawURLEncoding, so it is longer; length <= 0 means 32 bytes. The length is not capped, so
// bound one that comes from outside. If the random source fails, the program ends.
func GenerateSecureToken(length int) string {
if length <= 0 {
length = defaultSecureTokenLength
}
if length == defaultSecureTokenLength {
var randomBuf [defaultSecureTokenLength]byte
src := randomBuf[:]
rand.Read(src) //nolint:errcheck // never fails: crypto/rand ends the program instead (Go 1.24+)
var encoded [maxFastTokenEncodedLength]byte
encodedLen := base64.RawURLEncoding.EncodedLen(length)
base64.RawURLEncoding.Encode(encoded[:encodedLen], src)
return string(encoded[:encodedLen])
}
bytes := make([]byte, length)
rand.Read(bytes) //nolint:errcheck // never fails: crypto/rand ends the program instead (Go 1.24+)
return base64.RawURLEncoding.EncodeToString(bytes)
}
// SecureToken generates a secure token with 32 bytes of entropy; see GenerateSecureToken.
func SecureToken() string {
return GenerateSecureToken(defaultSecureTokenLength)
}
// FunctionName returns function name
func FunctionName(fn any) string {
if fn == nil {
return ""
}
v := reflect.ValueOf(fn)
if v.Kind() == reflect.Func {
if v.IsNil() {
return ""
}
return runtime.FuncForPC(v.Pointer()).Name() // "" for an address that belongs to no function
}
return v.Type().String()
}
// GetArgument reports whether arg is among the program arguments; os.Args[0] does not count.
func GetArgument(arg string) bool {
if len(os.Args) < 2 {
return false
}
return slices.Contains(os.Args[1:], arg)
}
// IncrementIPRange adds one to ip in place, wrapping the last address to all zeros. A 16-byte IPv4
// address from net.ParseIP carries out of 255.255.255.255 into the ::ffff: prefix; call To4 first.
func IncrementIPRange(ip net.IP) {
for j := len(ip) - 1; j >= 0; j-- {
ip[j]++
if ip[j] > 0 {
break
}
}
}
// ConvertToBytes returns the size in bytes of a human-readable string, ex. 42kb, 42M, 1.5e3K.
// The first letter after the number picks the unit (k, m, g, t, p in either case, and e before a
// b or i, as in EB, since a bare E is an exponent; any other letter means bytes); the rest is
// ignored. Units are powers of 1000 (42k = 42000), or of 1024 with an 'i' right after the letter,
// as in Ki and KiB but not in kilobytes (42Ki = 43008), so the KB that ByteSize writes for 1024
// reads back as 1000. The result is rounded down and capped at math.MaxInt; it is 0 for a
// missing, negative or malformed number.
func ConvertToBytes(humanReadableString string) int {
strLen := len(humanReadableString)
if strLen == 0 {
return 0
}
var sizeFast uint64
maxInt := uint64(math.MaxInt)
i := 0
for ; i < strLen; i++ {
c := humanReadableString[i]
if c < '0' || c > '9' {
break
}
d := uint64(c - '0')
if sizeFast > maxInt/10 || (sizeFast == maxInt/10 && d > maxInt%10) {
sizeFast = maxInt
} else if sizeFast < maxInt {
sizeFast = sizeFast*10 + d
}
}
if i > 0 {
if i == strLen {
return int(sizeFast)
}
if i+1 == strLen {
last := humanReadableString[i]
if last == 'b' || last == 'B' {
return int(sizeFast)
}
}
}
lastNumberPos := -1
unitPrefixPos := 0
for j := strLen - 1; j >= 0; j-- {
c := humanReadableString[j]
if c >= '0' && c <= '9' {
lastNumberPos = j
break
}
if (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') {
unitPrefixPos = j
}
}
if lastNumberPos < 0 {
return 0
}
numPart := humanReadableString[:lastNumberPos+1]
mult := uint64(1)
if unitPrefixPos > 0 {
binary := false
if next := unitPrefixPos + 1; next < strLen && (humanReadableString[next] == 'i' || humanReadableString[next] == 'I') {
binary = next+1 == strLen || !continuesUnitWord(humanReadableString[next+1])
}
var decMul, binMul uint64
switch humanReadableString[unitPrefixPos] {
case 'k', 'K':
decMul, binMul = 1e3, 1<<10
case 'm', 'M':
decMul, binMul = 1e6, 1<<20
case 'g', 'G':
decMul, binMul = 1e9, 1<<30
case 't', 'T':
decMul, binMul = 1e12, 1<<40
case 'p', 'P':
decMul, binMul = 1e15, 1<<50
case 'e', 'E':
decMul, binMul = 1, 1
if next := unitPrefixPos + 1; next < strLen && (humanReadableString[next]|0x20 == 'b' || humanReadableString[next]|0x20 == 'i') {
decMul, binMul = 1e18, 1<<60
}
default:
decMul, binMul = 1, 1
}
mult = decMul
if binary {
mult = binMul
}
}
if lastNumberPos == i-1 {
hi, lo := bits.Mul64(sizeFast, mult)
if hi != 0 || lo > maxInt {
return math.MaxInt
}
return int(lo)
}
// Integers, not floats: 1.001 * 1000 is 1000.9999999999999.
return scaleDecimal(numPart, mult)
}
// continuesUnitWord reports whether c makes the "i" of "Ki" part of a word like "kilobytes".
func continuesUnitWord(c byte) bool {
l := c | 0x20
return l >= 'a' && l <= 'z' && l != 'b'
}
// ParseByteSize is ConvertToBytes without guessing: a number, blanks and at most one unit (b; k to p,
// with i and b: KiB; e, with i or b: EB), else 0 and a *strconv.NumError, ErrSyntax or, for
// math.MaxInt or more, ErrRange. The only sign is '+'.
func ParseByteSize(s string) (int, error) {
if !validSize(s) {
return 0, &strconv.NumError{Func: "ParseByteSize", Num: s, Err: strconv.ErrSyntax}
}
n := ConvertToBytes(s)
if n == math.MaxInt {
return 0, &strconv.NumError{Func: "ParseByteSize", Num: s, Err: strconv.ErrRange}
}
return n, nil
}
func validSize(s string) bool {
n := len(s)
i := 0
if i < n && s[i] == '+' {
i++
}
j := skipDigits(s, i)
digits := j - i
i = j
if i < n && s[i] == '.' {
j = skipDigits(s, i+1)
digits += j - i - 1
i = j
}
if digits == 0 {
return false
}
// An e without digits after it starts the unit instead.
if i < n && s[i]|0x20 == 'e' {
j = i + 1
if j < n && (s[j] == '+' || s[j] == '-') {
j++
}
if k := skipDigits(s, j); k > j {
i = k
}
}
for i < n && (s[i] == ' ' || s[i] == '\t') {
i++
}
return i == n || validSizeUnit(s[i:])
}
func skipDigits(s string, i int) int {
for i < len(s) && s[i]-'0' <= 9 {
i++
}
return i
}
// validSizeUnit reports whether u, not empty, is a unit ParseByteSize accepts.
func validSizeUnit(u string) bool {
c, rest := u[0]|0x20, u[1:]
if c == 'b' {
return rest == ""
}
if c != 'k' && c != 'm' && c != 'g' && c != 't' && c != 'p' && c != 'e' {
return false
}
hasI := rest != "" && rest[0]|0x20 == 'i'
if hasI {
rest = rest[1:]
}
hasB := rest != "" && rest[0]|0x20 == 'b'
if hasB {
rest = rest[1:]
}
return rest == "" && (c != 'e' || hasI || hasB)
}
func scaleDecimal(s string, mult uint64) int {
var mantissa uint64
var exp int64
end := len(s)
places, dropped := -1, 0 // places: digits after the point, -1 before it
droppedFrac := 0
inexact := false
scan:
for i := 0; i < len(s); i++ {
c := s[i]
switch {
case c-'0' <= 9:
switch {
case mantissa < maxUint64Cutoff:
mantissa = mantissa*10 + uint64(c-'0')
if places >= 0 {
places++
}
case places < 0:
dropped++
inexact = inexact || c != '0'
default:
droppedFrac++
inexact = inexact || c != '0'
}
case c == '.' && places < 0:
places = 0
case c == 'e' || c == 'E':
var ok bool
if exp, ok = parseExp(s, i+1); !ok {
return 0
}
end = i
break scan
case c == '+' && i == 0:
default:
return 0
}
}
if mantissa == 0 {
return 0
}
// The mantissa is a prefix of the digits, so the number lies in [mantissa, mantissa+1) * 10^shift;
// if both ends scale alike, so does it.
shift := exp + int64(dropped) - int64(max(places, 0))
n := scaleMantissa(mantissa, shift, mult)
if inexact && n != scaleMantissa(mantissa+1, shift, mult) {
return scaleDigits(s, end, int64(droppedFrac)-shift+int64(dropped), mult)
}
return n
}
func scaleMantissa(mantissa uint64, shift int64, mult uint64) int {
if shift > 0 {
if shift >= int64(len(pow10)) {
return math.MaxInt
}
hi, lo := bits.Mul64(mantissa, pow10[shift])
if hi != 0 {
return math.MaxInt
}
mantissa = lo
}
hi, lo := bits.Mul64(mantissa, mult)
if shift >= 0 {
if hi != 0 || lo > uint64(math.MaxInt) {
return math.MaxInt
}
return int(lo)
}
var quotient uint64
if down := -shift; down < int64(len(pow10)) {
divisor := pow10[down]
switch {
case hi == 0:
quotient = lo / divisor
case hi >= divisor:
return math.MaxInt
default:
quotient, _ = bits.Div64(hi, lo, divisor)
}
} else if down < int64(2*len(pow10)-1) {
// The product is below 2^114, so dividing by 10^19 cannot overflow.
quotient, _ = bits.Div64(hi, lo, pow10[len(pow10)-1])
quotient /= pow10[down-int64(len(pow10)-1)]
} // else the product is below 10^down
if quotient > uint64(math.MaxInt) {
return math.MaxInt
}
return int(quotient)
}
// scaleDigits multiplies the digits of s[:end] by mult and keeps the product's digits from the
// down-th on: the integer part, as the point is down places from the end.
func scaleDigits(s string, end int, down int64, mult uint64) int {
if down <= 0 {
return math.MaxInt
}
var result, carry uint64
pos := int64(0)
for i := end - 1; i >= 0 || carry != 0; i-- {
var d uint64
if i >= 0 {
c := s[i] - '0'
if c > 9 {
continue
}
d = uint64(c)
}
t := d*mult + carry
carry = t / 10
if digit := t % 10; digit != 0 && pos >= down {
if pos-down >= int64(len(pow10)-1) { // 10^19 > math.MaxInt
return math.MaxInt
}
result += digit * pow10[pos-down] // below 2^64 even after the last sum
if result > uint64(math.MaxInt) {
return math.MaxInt
}
}
pos++
}
return int(result)
}