1 
2 //          Copyright Luna & Cospec 2019.
3 // Distributed under the Boost Software License, Version 1.0.
4 //    (See accompanying file LICENSE_1_0.txt or copy at
5 //          https://www.boost.org/LICENSE_1_0.txt)
6 
7 module wsf.ast.common;
8 import std.bitmanip;
9 
10 /**
11     The magic bytes for the WSF format
12 */
13 const(ubyte[]) WSF_MAGIC_BYTES = cast(ubyte[])"WSF_DATA";
14 
15 /**
16     Encodes the value to little endian
17 */
18 ubyte[] encode(T)(T value) {
19     ubyte[] ovalue = new ubyte[T.sizeof];
20     ovalue[] = nativeToLittleEndian!T(value)[0..ovalue.length];
21     return ovalue;
22 }
23 
24 /**
25     Decodes the value from little-endian
26 */
27 T decode(T)(ubyte[] value) {
28     return littleEndianToNative!(T, T.sizeof)(cast(ubyte[T.sizeof])value[0..T.sizeof]);
29 }
30 
31 /**
32     Tags for deserializer to know type of sequence
33 */
34 enum WSFTag : ubyte {
35     /**
36         Equivalent to null
37     */
38     Nothing             = 0x00,
39 
40     /**
41         A byte
42     */
43     Int8                = 0x01,
44 
45     /**
46         A short
47     */
48     Int16               = 0x02,
49 
50     /**
51         A short
52     */
53     Int32               = 0x03,
54 
55     /**
56         A short
57     */
58     Int64               = 0x04,
59 
60     /**
61         A floating point number
62     */
63     Floating            = 0x05,
64 
65     /**
66         A boolean
67     */
68     Bool                = 0x06,
69 
70     /**
71         A string
72     */
73     String              = 0x07,
74 
75     /**
76         An array of values
77     */
78     Array               = 0x20,
79 
80     /**
81         An entry (key-value pair)
82     */
83     Entry               = 0x21,
84 
85     /**
86         The start of a compound
87     */
88     CompoundStart       = 0xC8,
89 
90     /**
91         The end of a compound
92     */
93     CompoundEnd         = 0xDC,
94 
95     /**
96         A deserialized compound
97     */
98     Compound            = 0xFF
99 }