-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils.bmx
153 lines (116 loc) · 3.86 KB
/
Utils.bmx
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
Framework BRL.StandardIO
Import BRL.System
Import BRL.Retro
Import "Parameters.bmx"
Import "microseconds.c"
Import "http_time.c"
Extern
Function microseconds:ULong()
Function http_time:Int(memory:Byte Ptr, epoch_time:ULong)
Function strlen:Size_T(memory:Byte Ptr)
End Extern
Function LoggedPrint(ToPrint:String)
Local ConnectionID:ULong = get_thread_parameters().ConnectionID
Print "[" + CurrentDate() + " " + CurrentTime() + "][ConnectionID: " + ConnectionID + "] " + ToPrint
End Function
Function IsInArray:Int(Target:String, Array:String[])
For Local i:String = EachIn Array
If Target = i Then Return 1
Next
Return 0
End Function
Function PrintArray(Array:String[])
For Local i=0 To (Array.length - 1)
Print "["+i+"] " + Array[i]
Next
End Function
' Converts "ff" or "FF" to 255
' Or "A5" to 165
Function HexToDec8:Byte(Inp:String)
Local LUT:Byte[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, 0, $A, $B, $C, $D, $E, $F, ..
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ..
$A, $B, $C, $D, $E, $F]
Return (LUT[Inp[0] - 48] Shl 4) | LUT[Inp[1] - 48]
End Function
' Alternative version under the same name that takes two "chars" instead of a string
Function HexToDec8:Byte(First:Int, Second:Int)
Local LUT:Byte[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, 0, $A, $B, $C, $D, $E, $F, ..
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ..
$A, $B, $C, $D, $E, $F]
Return (LUT[First - 48] Shl 4) | LUT[Second - 48]
End Function
Function URLDecode:String(Inp:String)
If Not Instr(Inp, "%") Then Return Inp ' No action required if no %'s found
Local DecodedLine:String
Local StartPosition:Int = 0
Local InpLen:Size_T = Len(Inp)
Local iInp:Int = 0
Local iOut:Int = 0
Local RawDecodedLine:Byte Ptr = MemAlloc(InpLen)
While iInp < InpLen
' 37 is "%"
If Inp[iInp] = 37
If iInp + 2 >= InpLen Then Exit
RawDecodedLine[iOut] = HexToDec8(Inp[iInp + 1], Inp[iInp + 2])
iInp :+ 3
iOut :+ 1
Else
RawDecodedLine[iOut] = Inp[iInp]
iInp :+ 1
iOut :+ 1
End If
Wend
' Make sure to insert the null-terminator at the end
RawDecodedLine[iOut] = 0
DecodedLine = String.FromUTF8String(RawDecodedLine)
MemFree(RawDecodedLine)
Return DecodedLine
End Function
' This function will generate a completely random string
' It will generate only standard ASCII letters, no special symbols
Function GenerateRandomString:String(length:Int)
Local Memory:Byte Ptr = MemAlloc(Size_T(length))
Local Result:String
For Local i=0 To (length - 1)
Memory[i] = Rnd(94) + 33
Next
Result = String.FromBytes(Memory, length)
MemFree(Memory)
Return Result
End Function
Function GetKiloseconds:String(ms:Long)
Local Unit:String
Local time:String
If ms < 1000
time = ms
Unit = "ms"
ElseIf ms < 1000 * 1000
time = ms / 10
Unit = "seconds"
ElseIf ms < 1000 * 1000 * 1000
time = ms / 1000 / 10
Unit = "kiloseconds"
ElseIf ms < 1000 * 1000 * 1000 * 1000
time = ms / 1000 / 1000 / 10
Unit = "megaseconds"
Else
time = ms / 1000 / 1000 / 1000 / 10
Unit = "gigaseconds"
End If
' Insert a point before the two rightmost digits
time = Left(time, Len(time) - 2) + "." + Right(time, 2)
Return time + " " + Unit
End Function
' This function converts time produced by functions like FileTime() into an HTTP-compliant human readable string
' Keep in mind that it also converts to GMT/UTC from local time
Function GetHTTPTime:String(EpochTime:Int)
Local RawString:Byte Ptr = MemAlloc(128)
Local Result:String
If http_time(RawString, ULong(EpochTime)) > 0
Result = String.FromCString(RawString)
Else
Result = "Time conversion error"
End If
MemFree(RawString)
Return Result
End Function