-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathBinaryEncodingDecoding.ahk
118 lines (103 loc) · 2.96 KB
/
BinaryEncodingDecoding.ahk
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
/*
BinaryEncodingDecoding.ahk
Routines to encode and decode binary data to and from Ascii/Ansi data (source code).
// by Philippe Lhoste <PhiLho(a)GMX.net> http://Phi.Lho.free.fr
// File/Project history:
1.02.000 -- 2007/01/03 (PL) -- Code rewritten for speed, using new AHK expression features and regexes.
1.01.000 -- 2006/07/04 (PL) -- Finally, I put Pebwa in its own file, it has less generic use.
1.00.000 -- 2006/03/29 (PL) -- Creation, taking Bin2Hex and Hex2Bin
(and Bin2Pebwa & Pebwa2Bin) from DllCallStruct.
Bin2Hex & Hex2Bin derivated from Laszlo's code.
*/
/* Copyright notice: See the PhiLhoSoftLicence.txt file for details.
This file is distributed under the zlib/libpng license.
Copyright (c) 2006-2007 Philippe Lhoste / PhiLhoSoft
*/
FormatHexNumber(_value, _digitNb)
{
local hex, intFormat
; Save original integer format
intFormat := A_FormatInteger
; For converting bytes to hex
SetFormat Integer, Hex
hex := _value + (1 << 4 * _digitNb)
StringRight hex, hex, _digitNb
; I prefer my hex numbers to be in upper case
StringUpper hex, hex
; Restore original integer format
SetFormat Integer, %intFormat%
Return hex
}
/*
// Convert raw bytes stored in a variable to a string of hexa digit pairs.
// Convert either _byteNb bytes or, if null, the whole content of the variable.
//
// Return the number of converted bytes, or -1 if error (memory allocation)
*/
Bin2Hex(ByRef @hexString, ByRef @bin, _byteNb=0)
{
local dataSize, dataAddress, granted, f
; Get size of data
dataSize := _byteNb < 1 ? VarSetCapacity(@bin) : _byteNb
dataAddress := &@bin
; Make enough room (faster)
granted := VarSetCapacity(@hexString, dataSize * 5)
if (granted < dataSize * 5)
{
; Cannot allocate enough memory
ErrorLevel = Mem=%granted%
Return -1
}
f := A_FormatInteger
SetFormat Integer, H
Loop %dataSize%
{
@hexString .= *(dataAddress++) + 256
}
StringReplace @hexString, @hexString, 0x1, , All
StringUpper @hexString, @hexString
SetFormat Integer, %f%
Return dataSize
}
/*
// Convert a string of hexa digit pairs to raw bytes stored in a variable.
// Convert either _byteNb bytes or, if null, the whole content of the variable.
//
// Return the number of converted bytes, or -1 if error (memory allocation)
*/
Hex2Bin(ByRef @bin, ByRef @hex, _byteNb=0)
{
local l, data, granted, dataAddress
If (_byteNb < 1 or _byteNb > dataSize)
{
; Get size of data
l := StrLen(@hex)
_byteNb := l // 2
if (l = 0 or _byteNb * 2 != l)
{
; Invalid string, empty or odd number of digits
ErrorLevel = Param
Return -1
}
}
; Make enough room
granted := VarSetCapacity(@bin, _byteNb, 0)
if (granted < _byteNb)
{
; Cannot allocate enough memory
ErrorLevel = Mem=%granted%
Return -1
}
data := RegExReplace(@hex, "..", "0x$0!")
StringLeft data, data, _byteNb * 5
dataAddress := &@bin
Loop Parse, data, !
{
; Store integer in memory
DllCall("RtlFillMemory"
, "UInt", dataAddress++
, "UInt", 1
, "UChar", A_LoopField)
}
Return _byteNb
}