-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLibFZMP.lua
1313 lines (1233 loc) · 43.7 KB
/
LibFZMP.lua
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
local _Name = "FZMP"
local _Version = 1
--~---------------------------------------------------------------------------
--
-- Module Definition
-- =================
local _G = _G
local _M = _G[_Name]
if _M then
if _M.Version >= _Version then
return
end
if _M.Unload then
_M.Unload()
end
end
_M = { Version = _Version }
_G[_Name] = _M
--~---------------------------------------------------------------------------
--
-- External functions
-- ==================
local ipairs = ipairs
local pairs = pairs
local setmetatable = setmetatable
local tonumber = tonumber
local type = type
local unpack = unpack
local string = string
local string_byte = string.byte
local string_char = string.char
local string_find = string.find
local string_format = string.format
local string_gmatch = string.gmatch
local string_lower = string.lower
local string_match = string.match
local string_rep = string.rep
local string_sub = string.sub
local table_concat = table.concat
local BNGetFriendInfo = BNGetFriendInfo
local BNGetNumFriendGameAccounts = BNGetNumFriendGameAccounts
local BNGetGameAccountInfo = BNGetGameAccountInfo
local BNSendWhisper = BNSendWhisper
local ChatFrame_AddMessageEventFilter = ChatFrame_AddMessageEventFilter
local GetChannelName = GetChannelName
local GetTime = GetTime
local SendAddonMessage = SendAddonMessage
------------------------------------------------------------------------------
--
-- Percent Encoder / Decoder
-- =========================
--
---
---
-- Encodes a string using the percent-encoding that escapes all characters
-- except dash, underscore, dot, tilde, and all alphanumeric characters
-- ("Unreserved Characters"). The encoded string will contain only the above
-- characters and the percentage sign (the escape character).
--
-- @param str [string]
-- Data to be encoded.
--
-- @return [string]
-- Encoded result.
--
function _M.PercentEncode(str)
local chunks = {}
local index = 1
for chunk, char in string_gmatch(str, "([-_.~%w]*)(.?)") do
chunks[index] = chunk
index = index + 1
if #char == 1 then
chunks[index] = string_format("%%%02x", string_byte(char))
index = index + 1
end
end
return table_concat(chunks)
end
---
-- Decodes a percent-encoded string.
--
-- @param str [string]
-- Data to be decoded.
--
-- @return [string]
-- Decoded result.
--
function _M.PercentDecode(str)
local chunks = {}
local index = 1
for chunk, escape, hex in string_gmatch(str, "([^%%]*)(.?)(%x?%x?)") do
chunks[index] = chunk
if #hex == 2 then
chunks[index + 1] = string_char(tonumber(hex, 16))
index = index + 2
else
chunks[index + 1] = escape
chunks[index + 2] = hex
index = index + 3
end
end
return table_concat(chunks)
end
------------------------------------------------------------------------------
--
-- Base-85 Encoder / Decoder
-- =========================
--
-- To create a custom `encoding` table for use with `Base85Encode` and
-- `Base85Decode`, it must contain the following:
--
-- * `toBase85` [table]
-- Mapping from 0 through 84 to characters.
--
-- * `fromBase85` [table]
-- Mapping from characters to 0 through 84.
--
-- * `zeroChar` [character, optional]
-- Used to represents a group group zero-bytes.
--
-- * `invalidChars` [pattern, optional]
-- Characters to be rejected as errors. The base-85 characters are
-- already excluded so it's OK for this pattern to match them.
--
---
_M.BASE85 = {}
---
-- The standard ASCII85 encoding for encoding data in base-85.
--
_M.BASE85.ASCII = {
zeroChar = "z",
invalidChars = "[^%s]",
toBase85 = {
[0] = "!", '"', "#", "$", "%", "&", "'", "(", ")", "*", "+", ",",
"-", ".", "/", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
":", ";", "<", "=", ">", "?", "@", "A", "B", "C", "D", "E", "F",
"G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S",
"T", "U", "V", "W", "X", "Y", "Z", "[", "\\", "]", "^", "_", "`",
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",
"n", "o", "p", "q", "r", "s", "t", "u"
},
fromBase85 = {
["!"] = 0, ['"'] = 1, ["#"] = 2, ["$"] = 3, ["%"] = 4, ["&"] = 5,
["'"] = 6, ["("] = 7, [")"] = 8, ["*"] = 9, ["+"] = 10, [","] = 11,
["-"] = 12, ["."] = 13, ["/"] = 14, ["0"] = 15, ["1"] = 16,
["2"] = 17, ["3"] = 18, ["4"] = 19, ["5"] = 20, ["6"] = 21,
["7"] = 22, ["8"] = 23, ["9"] = 24, [":"] = 25, [";"] = 26,
["<"] = 27, ["="] = 28, [">"] = 29, ["?"] = 30, ["@"] = 31,
["A"] = 32, ["B"] = 33, ["C"] = 34, ["D"] = 35, ["E"] = 36,
["F"] = 37, ["G"] = 38, ["H"] = 39, ["I"] = 40, ["J"] = 41,
["K"] = 42, ["L"] = 43, ["M"] = 44, ["N"] = 45, ["O"] = 46,
["P"] = 47, ["Q"] = 48, ["R"] = 49, ["S"] = 50, ["T"] = 51,
["U"] = 52, ["V"] = 53, ["W"] = 54, ["X"] = 55, ["Y"] = 56,
["Z"] = 57, ["["] = 58, ["\\"] = 59, ["]"] = 60, ["^"] = 61,
["_"] = 62, ["`"] = 63, ["a"] = 64, ["b"] = 65, ["c"] = 66,
["d"] = 67, ["e"] = 68, ["f"] = 69, ["g"] = 70, ["h"] = 71,
["i"] = 72, ["j"] = 73, ["k"] = 74, ["l"] = 75, ["m"] = 76,
["n"] = 77, ["o"] = 78, ["p"] = 79, ["q"] = 80, ["r"] = 81,
["s"] = 82, ["t"] = 83, ["u"] = 84
}
}
---
-- Custom base-85 encoding for the internal messaging protocol.
--
_M.BASE85.FZM = {
zeroChar = ".",
invalidChars = "[^%s]",
toBase85 = {
[0] = "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b",
"c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o",
"p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B",
"C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O",
"P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "_", "~",
"-", "^", "=", "/", "+", "*", "%", "$", "#", "@", "&", "(", ")",
"{", "}", "`", "'", ",", ";", "?", "!"
},
fromBase85 = {
["0"] = 0, ["1"] = 1, ["2"] = 2, ["3"] = 3, ["4"] = 4, ["5"] = 5,
["6"] = 6, ["7"] = 7, ["8"] = 8, ["9"] = 9, ["a"] = 10, ["b"] = 11,
["c"] = 12, ["d"] = 13, ["e"] = 14, ["f"] = 15, ["g"] = 16,
["h"] = 17, ["i"] = 18, ["j"] = 19, ["k"] = 20, ["l"] = 21,
["m"] = 22, ["n"] = 23, ["o"] = 24, ["p"] = 25, ["q"] = 26,
["r"] = 27, ["s"] = 28, ["t"] = 29, ["u"] = 30, ["v"] = 31,
["w"] = 32, ["x"] = 33, ["y"] = 34, ["z"] = 35, ["A"] = 36,
["B"] = 37, ["C"] = 38, ["D"] = 39, ["E"] = 40, ["F"] = 41,
["G"] = 42, ["H"] = 43, ["I"] = 44, ["J"] = 45, ["K"] = 46,
["L"] = 47, ["M"] = 48, ["N"] = 49, ["O"] = 50, ["P"] = 51,
["Q"] = 52, ["R"] = 53, ["S"] = 54, ["T"] = 55, ["U"] = 56,
["V"] = 57, ["W"] = 58, ["X"] = 59, ["Y"] = 60, ["Z"] = 61,
["_"] = 62, ["~"] = 63, ["-"] = 64, ["^"] = 65, ["="] = 66,
["/"] = 67, ["+"] = 68, ["*"] = 69, ["%"] = 70, ["$"] = 71,
["#"] = 72, ["@"] = 73, ["&"] = 74, ["("] = 75, [")"] = 76,
["{"] = 77, ["}"] = 78, ["`"] = 79, ["'"] = 80, [","] = 81,
[";"] = 82, ["?"] = 83, ["!"] = 84
}
}
local BASE85_ASCII = _M.BASE85.ASCII
---
-- Encodes a string of bytes using a base-85 encoding.
--
-- @param str [string]
-- Data to be encoded.
--
-- @param encoding [table, optional]
-- Encoding table. Default: `BASE85_ASCII`.
--
-- @return [string]
-- Encoded result.
--
function _M.Base85Encode(str, encoding)
if not encoding then encoding = BASE85_ASCII end
local toBase85 = encoding.toBase85
local zeroChar = encoding.zeroChar or string_rep(toBase85[0], 5)
local chunks = {}
local chunksEnd = 1
for group in string_gmatch(str, "(....)") do
local b3, b2, b1, b0 = string_byte(group, 1, 4)
local n = 16777216 * b3 + 65536 * b2 + 256 * b1 + b0
if n == 0 then
chunks[chunksEnd] = zeroChar
chunksEnd = chunksEnd + 1
else
local r = n % 85
chunks[chunksEnd + 4] = toBase85[r]
n = (n - r) / 85
r = n % 85
chunks[chunksEnd + 3] = toBase85[r]
n = (n - r) / 85
r = n % 85
chunks[chunksEnd + 2] = toBase85[r]
n = (n - r) / 85
r = n % 85
chunks[chunksEnd + 1] = toBase85[r]
n = (n - r) / 85
chunks[chunksEnd] = toBase85[n]
chunksEnd = chunksEnd + 5
end
end
local padLen = #str % 4
if padLen > 0 then
local b3, b2, b1 = string_byte(str, -padLen, -1)
-- Pad with zeros
if not b2 then b2 = 0 end
if not b1 then b1 = 0 end
local n = 16777216 * b3 + 65536 * b2 + 256 * b1
n = (n - n % 85) / 85
local r = n % 85
if padLen > 2 then
chunks[chunksEnd + 3] = toBase85[r]
end
n = (n - r) / 85
r = n % 85
if padLen > 1 then
chunks[chunksEnd + 2] = toBase85[r]
end
n = (n - r) / 85
r = n % 85
chunks[chunksEnd + 1] = toBase85[r]
n = (n - r) / 85
chunks[chunksEnd] = toBase85[n]
end
return table_concat(chunks)
end
---
-- Decodes a string of bytes using a base-85 encoding.
--
-- @param str [string]
-- Data to be decoded.
--
-- @param encoding [table, optional]
-- Encoding table. Default: `BASE85_ASCII`.
--
-- @return [string or (`nil`, string)]
-- Decoded result. If an error occurs, `nil` is returned along with an error
-- message.
--
function _M.Base85Decode(str, encoding)
if not encoding then encoding = BASE85_ASCII end
local fromBase85 = encoding.fromBase85
local zeroChar = encoding.zeroChar
local invalidChars = encoding.invalidChars or ".^" -- Won't ever match
local chunks = {}
local chunksEnd = 1
local n = 0
local j = 4
local len = #str
for i = 1, len do
local char = string_sub(str, i, i)
local value = fromBase85[char]
if value then
n = n * 85 + value
if j > 0 then
j = j - 1
else
local r = n % 256
chunks[chunksEnd + 3] = string_char(r)
n = (n - r) / 256
r = n % 256
chunks[chunksEnd + 2] = string_char(r)
n = (n - r) / 256
r = n % 256
chunks[chunksEnd + 1] = string_char(r)
n = (n - r) / 256
if n > 255 then
-- `n` is too large to be converted into 4-bytes
return nil, string_format("group %q is invalid",
string_sub(str, i - 4, i))
end
chunks[chunksEnd] = string_char(n)
chunksEnd = chunksEnd + 4
j = 4
n = 0
end
elseif char == zeroChar then
if j < 4 then
return nil, string_format("%q found in middle of group", char)
end
chunks[chunksEnd] = "\000\000\000\000"
chunksEnd = chunksEnd + 1
elseif string_find(char, invalidChars) then
return nil, string_format("invalid character %q", char)
end
end
if j < 4 then
if j == 3 then
return nil, string_format("terminal group %q is incomplete",
string_sub(str, -1))
end
-- Pad with 84's
for i = 0, j do
n = n * 85 + 84
end
n = (n - n % 256) / 256
local r = n % 256
if j < 1 then
chunks[chunksEnd + 2] = string_char(r)
end
n = (n - r) / 256
r = n % 256
if j < 2 then
chunks[chunksEnd + 1] = string_char(r)
end
n = (n - r) / 256
if n > 255 then
-- `n` is too large to be converted into 4-bytes
return nil, string_format("group %q is invalid",
string_sub(str, len - 3 + j, len))
end
chunks[chunksEnd] = string_char(n)
end
return table_concat(chunks)
end
------------------------------------------------------------------------------
--
-- String Streams
-- ==============
---
---
-- This class provides the ability to read a string in sequence from beginning
-- to end.
_M.StringReader = {}
_M.StringReader.__index = _M.StringReader
---
-- Creates a `StringReader` instance from the input string.
--
-- @param str [string]
-- The data to be read.
--
-- @return [`StringReader`]
-- An instance of the class.
--
function _M.StringReader.New(str)
local self = {str = str, index = 1, len = #str}
setmetatable(self, _M.StringReader)
return self
end
---
-- Determines whether the stream has reached its end (i.e. nothing more can be
-- read).
--
-- @return [boolean]
-- `true` if the stream has reached its end.
--
function _M.StringReader:End()
return self.index > self.len
end
---
-- Reads up to `size` characters from the stream and advances the stream
-- position by the number of characters read.
--
-- @param size [unsigned, optional]
-- Maximum number of characters to read. If omitted, all remaining
-- characters will be read.
--
-- @return [string]
-- The data read from the stream.
--
function _M.StringReader:Read(size)
local index = self.index
if not size then
return string_sub(self.str, index)
end
local new_index = index + size
self.index = new_index
return string_sub(self.str, index, new_index - 1)
end
---
-- Reads a byte from the stream and advances the stream position by one.
--
-- @return [`0` to `255`]
-- The value of the byte.
--
function _M.StringReader:ReadByte()
local index = self.index
self.index = index + 1
return string_byte(string_sub(self.str, index, index))
end
---
-- Reads a nibble (half-byte) from the stream and advances the stream position
-- by one byte. If the stream remains unchanged and `ReadNibble` is called
-- again, the remaining nibble is read without advancing the stream further.
-- The more significant half-byte is read first, followed by the less
-- significant one.
--
-- @return [`0` to `127`]
-- The value of the half-byte.
--
function _M.StringReader:ReadNibble()
local nibbleIndex = self.nibbleIndex
local index = self.index
if nibbleIndex and nibbleIndex + 1 == index then
self.nibbleIndex = nil
return self.nibbleOther
else
local byte = string_byte(string_sub(self.str, index, index))
local nibbleOther = byte % 16
self.index = index + 1
self.nibbleOther = nibbleOther
self.nibbleIndex = index
return (byte - nibbleOther) / 16
end
end
---
-- Attempts to read the given Lua pattern from the current stream location and
-- advances the stream further if successful.
--
-- @param pattern [pattern]
-- The pattern to match.
--
-- @return [string or `nil`]
-- The matched result if successful. On failure, `nil` is returned.
--
function _M.StringReader:ReadPattern(pattern)
local index = self.index
local results = {string_find(self.str, pattern, index)}
if index == results[1] then
self.index = results[2] + 1
local captures = {}
for resultIndex, result in ipairs(results) do
captures[resultIndex - 2] = result
end
return unpack(captures)
end
end
---
-- This class provides the ability to write a string from beginning to end in
-- variable-sized blocks of data.
_M.StringWriter = {}
_M.StringWriter.__index = _M.StringWriter
---
-- Creates a `StringWriter` that allows stream-based writing.
--
-- @return [`StringWriter`]
-- An instance of the class.
--
function _M.StringWriter.New()
local chunks = {}
local self = {chunks = chunks, index = #chunks + 1}
setmetatable(self, _M.StringWriter)
return self
end
---
-- Writes a string to the stream.
--
-- @param str [string]
-- The data to be written.
--
function _M.StringWriter:Write(str)
local index = self.index
self.chunks[index] = str
self.index = index + 1
end
---
-- Writes a byte to the stream.
--
-- @param byte [`0` to `255`]
-- The data to be written.
--
function _M.StringWriter:WriteByte(byte)
local index = self.index
self.chunks[index] = byte
self.index = index + 1
end
---
-- Writes a nibble (half-byte) to the stream in the more significant half and
-- advances the stream by one byte. If another nibble is immediately written,
-- then it will fill the less significant half of the same byte without
-- advancing the stream further.
--
-- @param nibble [`0` to `127`]
-- The data to be written.
--
function _M.StringWriter:WriteNibble(nibble)
local nibbleIndex = self.nibbleIndex
local index = self.index
if nibbleIndex and nibbleIndex + 1 == index then
local chunks = self.chunks
chunks[nibbleIndex] = chunks[nibbleIndex] + nibble
self.nibbleIndex = nil
else
self.chunks[index] = nibble * 16
self.index = index + 1
self.nibbleIndex = index
end
end
---
-- Merges data from another writer into the current one.
--
-- @param writer [`StringWriter`]
-- The data to be merged in.
--
function _M.StringWriter:Merge(writer)
local chunks = self.chunks
local lastIndex = self.index - 1
for chunkIndex, chunk in ipairs(writer.chunks) do
chunks[lastIndex + chunkIndex] = chunk
end
self.index = lastIndex + writer.index
end
---
-- Constructs a string that contains all the written data
--
-- @return [string]
-- All of the written data.
--
function _M.StringWriter:ToString()
local chunks = self.chunks
for chunkIndex, chunk in ipairs(chunks) do
if type(chunk) == "number" then
chunks[chunkIndex] = string_char(chunk)
end
end
return table_concat(chunks)
end
------------------------------------------------------------------------------
--
-- Data Serialization
-- ==================
---
---
-- Contains various serialization formats.
--
-- `FZSF1`
-- FZ Serialization Format version 1.
--
-- `FZSF0`
-- FZ Serialization Format version 0 (deprecated).
--
-- `ACE3`
-- AceSerializer-3.0 Format (requires Ace3 to be installed).
--
_M.SERIALIZATION_FORMAT = {
FZSF0 = 0x40,
FZSF1 = 0x20,
ACE3 = 0x50,
}
local StringReader = _M.StringReader
local StringWriter = _M.StringWriter
local newWriter = StringWriter.New
local writerToString = StringWriter.ToString
local write = StringWriter.Write
local writeByte = StringWriter.WriteByte
local writeNibble = StringWriter.WriteNibble
local newReader = StringReader.New
local readerEnd = StringReader.End
local read = StringReader.Read
local readNibble = StringReader.ReadNibble
local readPattern = StringReader.ReadPattern
local FORMAT = _M.SERIALIZATION_FORMAT
local TYPE_NIL = 0x0
local TYPE_BOOLEAN_FALSE = 0x1
local TYPE_BOOLEAN_TRUE = 0x2
local TYPE_TABLE = 0x3
local TYPE_INT = 0x4
local TYPE_FLOAT = 0x5
local TYPE_STRING = 0x6
local TYPE_STRING_REF = 0x7
local TYPE_ARRAY = 0x8
-- Note: on some platforms NaN can cause crashes so support has been removed.
local INF = math.huge
local NAN = nil -- (-1)^.5
-- Find the maximum integer that can be represented fully
local INT_MAX
local EXP_MAX = 1024 -- Safety net to prevent infinite loops
do
local i = 1
for n = 1, EXP_MAX do
i = i * 2
if i + 1 == i then break end
end
INT_MAX = i / 2
end
-- Encodes an nonnegative integer of arbitrary size into a variable-length
-- string. This returns a string in which all characters have byte-value 127
-- or lower, except for the very last character whose value is 128 or higher.
--
-- The `flag` parameter can be used to attach an additional unsigned integer
-- of fixed size to the returned string (e.g. to store sign information). The
-- parameter `flagSize` is to be specified in bits (at most 7).
local function encodeUnsigned(unsigned, flag, flagSize)
if not flag then flag = 0 end
if not flagSize then flagSize = 0 end
local leftover = 128 / 2^flagSize
local bytes = {flag * leftover + unsigned % leftover}
local bytesEnd = 2
unsigned = (unsigned - unsigned % leftover) / leftover
while unsigned > 0 do
bytes[bytesEnd] = unsigned % 128
bytesEnd = bytesEnd + 1
unsigned = (unsigned - unsigned % 128) / 128
end
bytes[bytesEnd - 1] = bytes[bytesEnd - 1] + 128
return string_char(unpack(bytes))
end
-- Returns the original unsigned integer, the string captured, and the flag.
local function decodeUnsigned(stream, flagSize)
if not flagSize then flagSize = 0 end
local leftover = 128 / 2^flagSize
local str = readPattern(stream, "([%z\001-\127]*[\128-\255])")
if not str then return end
local bytes = {string_byte(str, 1, -1)}
local unsigned = 0
for i = #str, 2, -1 do
unsigned = unsigned * 128 + bytes[i]
end
unsigned = unsigned * leftover + bytes[1] % leftover
return unsigned - 128, str, (bytes[1] - bytes[1] % leftover) / leftover
end
local function serializeObject(object, state)
local size = ""
local stream = newWriter()
local typeID = TYPE_NIL
local objectType = type(object)
if objectType == "table" then
typeID = TYPE_TABLE
elseif objectType == "boolean" then
typeID = object and TYPE_BOOLEAN_TRUE or TYPE_BOOLEAN_FALSE
elseif objectType == "string" then
typeID = TYPE_STRING
elseif objectType == "number" then
local n = object
if n < 0 then
n = -n
end
if n % 1 == 0 and n < INT_MAX then
typeID = TYPE_INT
else
typeID = TYPE_FLOAT
end
end
if not state then
writeByte(stream, FORMAT.FZSF0 + typeID)
end
if typeID == TYPE_TABLE then
if state then
local cache = state.tableCache
local ref = cache[object]
if not ref then
local cacheCount = state.tableCacheCount
ref = encodeUnsigned(cacheCount)
cache[object] = ref
state.tableCacheCount = cacheCount + 1
state.tables[cacheCount] = object
end
write(stream, ref)
else
state = {
tables = {object},
tableCache = {[object] = encodeUnsigned(0)},
tableCacheCount = 1,
stringCache = {},
stringCacheCount = 0
}
local tables = state.tables
local tableIndex = 0
repeat
local tableTypes = newWriter()
local tableData = newWriter()
local arrayIndices = {}
local numItems = 0
-- First serialize the array items
for index, value in ipairs(object) do
local valueData, typeID, valueSize
= serializeObject(value, stream, state)
writeNibble(tableTypes, TYPE_ARRAY + typeID)
write(tableData, valueSize)
write(tableData, valueData)
arrayIndices[index] = true
numItems = index
end
-- Then serialize the non-array items
for key, value in pairs(object) do
if not arrayIndices[key] then
local keyData, keyType, keySize
= serializeObject(key, stream, state)
writeNibble(tableTypes, keyType)
write(tableData, keySize)
write(tableData, keyData)
local valueData, typeID, valueSize
= serializeObject(value, stream, state)
writeNibble(tableTypes, typeID)
write(tableData, valueSize)
write(tableData, valueData)
numItems = numItems + 1
end
end
write(stream, encodeUnsigned(numItems))
merge(stream, tableTypes)
merge(stream, tableData)
tableIndex = tableIndex + 1
object = tables[tableIndex]
until object == nil
end
elseif typeID == TYPE_INT then
-- Make it nonnegative and store the sign as an extra bit
if object < 0 then
write(stream, encodeUnsigned(-object, 1, 1))
else
write(stream, encodeUnsigned(object, 0, 1))
end
elseif typeID == TYPE_FLOAT then
local e, s, eSign, sSign, de
-- Store special numbers using an abnormal combo of sign & magnitude
if object == INF then
if object == -INF then
eSign, e, sSign, s = 1, 0, 0, 0 -- Not-a-number
else
eSign, e, sSign, s = 1, 0, 0, 1 -- Infinity
end
elseif object == -INF then
eSign, e, sSign, s = 1, 0, 1, 1 -- Minus-infinity
elseif object ~= object or object == NAN then
eSign, e, sSign, s = 1, 0, 0, 0 -- Not-a-number
else
sSign, eSign = 0, 0
s, e = math_frexp(object)
-- Make the significand an integer by altering the exponent
for i = 1, 1024 do
s = s * 2
e = e - 1
if s % 1 == 0 then
break
end
end
s = s - s % 1
-- Make them nonnegative and store the sign as an extra bit
if s < 0 then
sSign = 1
s = -s
end
if e < 0 then
eSign = 1
e = -e
end
end
write(stream, encodeUnsigned(e, eSign, 1)) -- Exponent
write(stream, encodeUnsigned(s, sSign, 1)) -- Significand
elseif typeID == TYPE_STRING then
local len = #object
size = encodeUnsigned(len)
if state then -- This is not the root element
local cache = state.stringCache
local ref = cache[object]
if ref then -- Reference is worth using
typeID = TYPE_STRING_REF
size = ""
object = ref
elseif ref == nil then -- Has never been cached
local cacheCount = state.stringCacheCount
local ref = encodeUnsigned(cacheCount)
if #ref <= #size + len then -- There is a size benefit
cache[object] = ref
state.stringCacheCount = cacheCount + 1
else -- No benefit; avoid using it
cache[object] = false
end
end
end
write(stream, object)
end
return writerToString(stream), typeID, size
end
local function deserializeObject(typeID, stream, state)
if typeID == TYPE_TABLE then
if state then
local ref = decodeUnsigned(stream)
local cache = state.tableCache
local t = cache[ref]
if not t then
t = {}
cache[ref] = t
end
return t
end
state = {
tableCache = {},
tableCacheCount = 0,
stringCache = {},
stringCacheCount = 0
}
local cache = state.tableCache
repeat
local numItems = decodeUnsigned(stream)
local keyTypes = {}
local valueTypes = {}
for i = 1, numItems do
local nibble = readNibble(stream)
if nibble >= TYPE_ARRAY then
keyTypes[i] = TYPE_ARRAY
valueTypes[i] = nibble % TYPE_ARRAY
else
keyTypes[i] = nibble
valueTypes[i] = readNibble(stream)
end
end
local count = state.tableCacheCount
local t = cache[count]
if not t then
t = {}
cache[count] = t
end
state.tableCacheCount = count + 1
for i = 1, numItems do
local keyType = keyTypes[i]
if keyType == TYPE_ARRAY then
t[i] = deserializeObject(valueTypes[i], stream, state)
else
local key = deserializeObject(keyType, stream, state)
if key then
t[key] = deserializeObject(valueTypes[i], stream, state)
end
end
end
until readerEnd(stream)
return cache[0]
elseif typeID == TYPE_INT then
local magnitude, sign = decodeUnsigned(stream, 1)
if sign == 1 then
return -magnitude
end
return magnitude
elseif typeID == TYPE_FLOAT then
local e, eSign = decodeUnsigned(stream, 1)
local s, sSign = decodeUnsigned(stream, 1)
if eSign == 1 then
if e == 0 then -- Special value
if s == 0 then
return NAN
elseif sSign == 0 then
return INF
end
return -INF
end
e = -e
end
if sSign == 1 then
s = -s
end
return math_ldexp(s, e)
elseif typeID == TYPE_STRING then
if state then
local cache = state.stringCache
local size, sizeStr = decodeUnsigned(stream)
local str = read(stream, size)
local cacheCount = state.stringCacheCount
local ref = encodeUnsigned(cacheCount)
-- Check if there's any size benefit before caching it
if #ref <= #sizeStr + #str then
cache[cacheCount] = str
state.stringCacheCount = cacheCount + 1
end
return str
end
return read(stream)
elseif typeID == TYPE_STRING_REF then
if state then
local cache = state.stringCache
local size = decodeUnsigned(stream)
if cache and size then
return cache[size]
end
end
elseif typeID == TYPE_BOOLEAN_FALSE then
if not (state or readerEnd(stream)) then
return nil, "trailing garbage found"
end
return false
elseif typeID == TYPE_BOOLEAN_TRUE then
if not (state or readerEnd(stream)) then
return nil, "trailing garbage found"
end
return true
elseif typeID == TYPE_NIL then -- Return nothing
else
return nil, "invalid root type identifier"
end
end
---
-- Serializes/deserializes an arbitrary Lua object.
--
-- FZSF1-specific: Any unserializable data will be ignored.
--
-- @param object [anything]
-- Object to be serialized.
--
-- @param format [member of `SERIALIZATION_FORMAT`, optional]
-- Serialization format.
--
-- @return [string or (`nil`, string)]
-- Serialized data. If an error occurs, `nil` is returned along with an
-- error message.
--
function _M.Serialize(object, format)
if not format then format = FORMAT.FZSF1 end
if format == FORMAT.FZSF1 then
return serializeObject(object)
elseif format == FORMAT.FZSF0 then
-- Known bug: NaN can get mixed up with +/-Infinity.
local chunks = {}
local chunksEnd = 1
local objects = {object} -- Stores all objects as an array
local objects_i = {[object] = 1} -- Stores indices of the objects
local objectsEnd = 2
repeat
local chunk
local object_type = type(object)
if object_type == "table" then
-- The table keys and values are stored as references only.
-- This saves space and also allows recursive tables to be
-- handled easily.
local items = {}
local itemsEnd = 1
for key, value in pairs(object) do
local key_i = objects_i[key]
if not key_i then
key_i = objectsEnd
objects_i[key] = key_i