-
Notifications
You must be signed in to change notification settings - Fork 3
/
mqtt_len.rex
executable file
·72 lines (54 loc) · 1.31 KB
/
mqtt_len.rex
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
#!/usr/bin/rxsock
/*
This shows how to encode/decode the remaining length bytes
(C) 2011 Dougie Lawson, all rights reserved.
*/
len_1 = '7d'x
len_2 = 'de6d'x
len_3 = 'dead5d'x
len_4 = 'deaded4d'x
len_9 = 'de6d5555'x
len_a = '7d5050'x
say mqtt_length_decode(len_1)
say mqtt_length_decode(len_2)
say mqtt_length_decode(len_3)
say mqtt_length_decode(len_4)
say mqtt_length_decode(len_9)
say mqtt_length_decode(len_a)
len_5 = 125
len_6 = 14046
len_7 = 1529566
len_8 = 163272414
say c2x(mqtt_length_encode(len_5))
say c2x(mqtt_length_encode(len_6))
say c2x(mqtt_length_encode(len_7))
say c2x(mqtt_length_encode(len_8))
exit 0
mqtt_length_encode: procedure
x = arg(1)
remlen = ""
do while(x > 0)
digit = x // 128
x = x % 128
if x > 0 then digit = digit + 128
remlen = remlen || d2x(digit,2)
end
return x2c(remlen)
mqtt_length_decode: procedure
remlen = arg(1)
digits = c2x(remlen)
multiplier = 1
value = 0
done = 0
offset = 3
do i = 1 to length(digits) by 2
digit = x2d(substr(digits,i,2))
if digit > 128 then digit = digit - 128
else done = 1
value = value + (digit * multiplier)
if done = 1 then leave
multiplier = multiplier * 128
offset = offset + 1
end
ret = value offset
return ret