1 |
<!DOCTYPE xsl:stylesheet> |
2 |
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> |
3 |
|
4 |
<xsl:output method="text" media-type="text/plain" encoding="utf-8"/> |
5 |
|
6 |
<xsl:template match="/"><![CDATA[ |
7 |
-- This is an automatically generated file. |
8 |
-- This is an automatically generated file. |
9 |
-- This is an automatically generated file. |
10 |
-- This is an automatically generated file. |
11 |
-- This is an automatically generated file. |
12 |
|
13 |
-- See doc/protocol.xml and doc/doc2haskell.xsl |
14 |
|
15 |
-- primitive enc/decoders |
16 |
|
17 |
import Data.Word |
18 |
import Control.Monad.State |
19 |
|
20 |
hasData :: State [Word8] Bool |
21 |
hasData = do |
22 |
xs <- get |
23 |
return $ not $ null xs |
24 |
|
25 |
getU8 :: State [Word8] Int |
26 |
getU8 = do |
27 |
(x:xs) <- get |
28 |
put xs |
29 |
return x |
30 |
|
31 |
getU16 :: State [Word8] Int |
32 |
getU16 = do |
33 |
a <- getU8 |
34 |
b <- getU8 |
35 |
return $ a + b * 256 |
36 |
|
37 |
decU64 |
38 |
decI8 |
39 |
decI16 |
40 |
decI32 |
41 |
decDATA |
42 |
|
43 |
sub dec_STRING { |
44 |
$data =~ s/^((?:..)*?)(?:\x00\x00|\Z)//s; |
45 |
# use Encode... |
46 |
join "", map chr, unpack "v*", $1; |
47 |
} |
48 |
|
49 |
sub dec_CONSTANT { |
50 |
$_[0]; |
51 |
} |
52 |
|
53 |
sub dec_password { |
54 |
dec_U64; |
55 |
} |
56 |
|
57 |
sub dec_HEX { # for debugging |
58 |
"HEX: " . unpack "H*", $data;#d# |
59 |
} |
60 |
|
61 |
############################################################################# |
62 |
|
63 |
sub enc_U8 { |
64 |
$data .= pack "C", $_[0]; |
65 |
} |
66 |
|
67 |
sub enc_U16 { |
68 |
$data .= pack "v", $_[0]; |
69 |
} |
70 |
|
71 |
sub enc_U32 { |
72 |
$data .= pack "V", $_[0]; |
73 |
} |
74 |
|
75 |
sub enc_U64 { |
76 |
enc_U32 $_[0] & 0xffffffff; |
77 |
enc_U32 +($_[0] >> 32) & 0xffffffff; |
78 |
} |
79 |
|
80 |
sub enc_I8 { |
81 |
$data .= pack "c", $_[0]; |
82 |
} |
83 |
|
84 |
sub enc_I16 { |
85 |
enc_U16 unpack "S", pack "s", $_[0]; |
86 |
} |
87 |
|
88 |
sub enc_I32 { |
89 |
enc_U32 unpack "I", pack "i", $_[0]; |
90 |
} |
91 |
|
92 |
sub enc_DATA { |
93 |
# a dream! |
94 |
$data .= $_[0]; |
95 |
} |
96 |
|
97 |
sub enc_STRING { |
98 |
# should use encode for speed and clarity ;) |
99 |
$data .= pack "v*", map ord, split //, $_[0]; |
100 |
} |
101 |
|
102 |
sub enc_CONSTANT { |
103 |
# nop |
104 |
} |
105 |
|
106 |
sub enc_password { |
107 |
require Math::BigInt; # I insist on 32-bit-perl.. should use C |
108 |
# $hash must be 64 bit |
109 |
my $hash = new Math::BigInt; |
110 |
$hash = $hash * 1055 + ord for split //, $_[0]; |
111 |
enc_U64 $hash; |
112 |
} |
113 |
|
114 |
sub enc_HEX { |
115 |
die "enc_HEX not defined for good"; |
116 |
} |
117 |
|
118 |
]]> |
119 |
|
120 |
############################################################################# |
121 |
# types |
122 |
<xsl:apply-templates select="descendant::type"/> |
123 |
|
124 |
############################################################################# |
125 |
# structures |
126 |
<xsl:apply-templates select="descendant::struct"/> |
127 |
|
128 |
############################################################################# |
129 |
# "less" primitive types<![CDATA[ |
130 |
|
131 |
decTREE |
132 |
encTREE |
133 |
|
134 |
]]> |
135 |
|
136 |
############################################################################# |
137 |
# messages |
138 |
<xsl:apply-templates select="descendant::message"/> |
139 |
} |
140 |
|
141 |
1; |
142 |
</xsl:template> |
143 |
|
144 |
<xsl:template match="type[@type = 'S']"> |
145 |
dec<xsl:value-of select="@name"/> :: State [Word8] [Char] |
146 |
dec<xsl:value-of select="@name"/> = do |
147 |
-- decode UCS-2 string of length <xsl:value-of select="@length"/> |
148 |
-- the first 0 terminates the string, but not the field |
149 |
|
150 |
enc<xsl:value-of select="@name"/> = |
151 |
-- likewise, encode as UCS-2, fill space with 0 |
152 |
</xsl:template> |
153 |
|
154 |
<xsl:template match="type[@type = 'A']"> |
155 |
dec<xsl:value-of select="@name"/> :: State [Word8] [Char] |
156 |
dec<xsl:value-of select="@name"/> = do |
157 |
-- decode ASCII string of length <xsl:value-of select="@length"/> |
158 |
-- the first 0 terminates the string, but not the field |
159 |
|
160 |
enc<xsl:value-of select="@name"/> = |
161 |
-- likewise, encode as ASCII, fill space with 0 |
162 |
</xsl:template> |
163 |
|
164 |
<xsl:template match="type[@multiplier]"> |
165 |
dec<xsl:value-of select="@name"/> = do |
166 |
n <- dec<xsl:value-of select="@type"/> |
167 |
return n * (1 / <xsl:value-of select="@multiplier"/>) |
168 |
|
169 |
enc<xsl:value-of select="@name"/> = |
170 |
<xsl:value-of select="@multiplier"/> * enc<xsl:value-of select="@type"/> |
171 |
</xsl:template> |
172 |
|
173 |
<xsl:template match="member[@array = 'yes']" mode="dec"> |
174 |
$r->{<xsl:value-of select="@name"/>} = (my $array = []); |
175 |
while (length $data) { |
176 |
push @$array, dec_<xsl:value-of select="@type"/> |
177 |
<xsl:text> </xsl:text> |
178 |
<xsl:value-of select="@default"/>; |
179 |
} |
180 |
</xsl:template> |
181 |
|
182 |
<xsl:template match="member" mode="dec"> |
183 |
<xsl:if test="@guard-cond"> |
184 |
if ($r->{<xsl:value-of select="@guard-member"/>} <xsl:value-of select="@guard-cond"/>)</xsl:if> |
185 |
<xsl:text>;</xsl:text> |
186 |
dec<xsl:value-of select="@type"/> -- default <xsl:value-of select="@default"/> -- |
187 |
</xsl:template> |
188 |
|
189 |
<xsl:template match="member" mode="enc"> |
190 |
enc<xsl:value-of select="@type"/> defined $_[0]{<xsl:value-of select="@name"/>} ? $_[0]{<xsl:value-of select="@name"/> |
191 |
<xsl:text>} : (</xsl:text> |
192 |
<xsl:value-of select="@default"/> |
193 |
<xsl:text>);</xsl:text> |
194 |
</xsl:template> |
195 |
|
196 |
<xsl:template match="struct"> |
197 |
sub dec_<xsl:value-of select="@name"/> { |
198 |
my $r = {}; |
199 |
<xsl:apply-templates select="member" mode="dec"/> |
200 |
<xsl:if test="@class"> |
201 |
bless $r, <xsl:value-of select="@class"/>::; |
202 |
</xsl:if> |
203 |
$r; |
204 |
} |
205 |
|
206 |
sub enc_<xsl:value-of select="@name"/> { |
207 |
<xsl:apply-templates select="member" mode="enc"/> |
208 |
} |
209 |
</xsl:template> |
210 |
|
211 |
<xsl:template match="message"> |
212 |
# <xsl:value-of select="@name"/> |
213 |
$dec_<xsl:value-of select="@src"/>{0x<xsl:value-of select="@type"/>} = sub { |
214 |
$data = $_[0]; |
215 |
my $r; |
216 |
$r->{type} = "<xsl:value-of select="@name"/>"; |
217 |
<xsl:apply-templates select="member" mode="dec"/> |
218 |
$r; |
219 |
}; |
220 |
$enc_<xsl:value-of select="@src"/>{<xsl:value-of select="@name"/>} = sub { |
221 |
$data = ""; |
222 |
enc_U16 0x<xsl:value-of select="@type"/>; |
223 |
<xsl:apply-templates select="member" mode="enc"/> |
224 |
$data; |
225 |
}; |
226 |
</xsl:template> |
227 |
|
228 |
<xsl:template match="member"> |
229 |
[<xsl:value-of select="@name"/> |
230 |
<xsl:text>=> "</xsl:text> |
231 |
<xsl:value-of select="@type"/> |
232 |
<xsl:text>", "</xsl:text> |
233 |
<xsl:value-of select="@default"/> |
234 |
<xsl:text>", "</xsl:text> |
235 |
<xsl:value-of select="@guard"/> |
236 |
<xsl:text>;"],</xsl:text> |
237 |
</xsl:template> |
238 |
|
239 |
<xsl:template match="text()"> |
240 |
</xsl:template> |
241 |
|
242 |
</xsl:stylesheet> |
243 |
|