ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/kgsueme/doc/doc2haskell.xsl
Revision: 1.13
Committed: Tue Jul 29 03:29:51 2003 UTC (20 years, 10 months ago) by pcg
Content type: application/xml
Branch: MAIN
Changes since 1.12: +102 -74 lines
Log Message:
*** empty log message ***

File Contents

# Content
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 module KGSProtocoll where
18 import Numeric
19 import Data.Word
20 import Control.Monad.State
21
22 type U8 = Int
23 type U16 = Int
24 type U32 = Word64
25 type U64 = Word64
26 type I8 = Int
27 type I16 = Int
28 type I32 = Word64 -- elmex? signed 32 bit
29 type I64 = Word64 -- elmex? likewise
30
31 data DecState = DecState { data :: [Word8] }
32
33 -- type DecS a = State DecState a
34 type DecS a = StateT DecState IO a -- for dec_HEX
35
36 hasData :: DecS Bool
37 hasData = do
38 xs <- get
39 return $ not $ null xs
40
41 dec_U8 :: Word8
42 dec_U8 =
43 do (x:xs) <- get
44 put xs
45 return x
46
47 dec_U16 :: DecS Word16
48 dec_U16 =
49 do a <- dec_U8
50 b <- dec_U8
51 return $ (fromIntegral a :: Word16) + (fromIntegral b :: Word16) * 256
52
53
54 dec_U64 :: DecS Word64
55 dec_U64 =
56 do a1 <- dec_U16
57 a2 <- dec_U16
58 a3 <- dec_U16
59 a4 <- dec_U16
60 return $ (fI a4) `shiftL` 48
61 + (fI a3) `shiftL` 32
62 + (fI a2) `shiftL` 16
63 + (fI a1)
64 where fI a = fromIntegral a :: Word64
65
66
67 dec_I8 :: DecS Int8
68 dec_I8 =
69 do w8 <- dec_U8
70 return $ fromIntegral w8 :: Int8
71
72 dec_I16 :: DecS Int16
73 dec_I16 =
74 do w16 <- dec_U16
75 return $ fromIntegral w16 :: Int16
76
77 dec_I32 :: DecS Int32
78 dec_I32 =
79 do w16_1 <- dec_U16
80 w16_2 <- dec_U16
81 return $ (fI w16_2) `shiftL` 16
82 + (fI w16_1)
83 where fI a = fromIntegral a :: Int32
84
85 dec_DATA :: DecS [Word8]
86 dec_DATA =
87 do da <- get
88 put []
89 return da
90
91 dec_STRING :: DecS [Word16]
92 dec_STRING =
93 do c <- dec_U16
94 if c == 0 then do return []
95 else return (c:dec_STRING)
96
97 {- do da <- get
98 let (str,rest) = mkstr da
99 where
100 mkstr str [] =
101 (reverse str,[])
102 mkstr str (0:rest) =
103 (reverse str, rest)
104 mkstr str (c:rest) =
105 mkstr ((toEnum (fromIntegral c :: Int)):str) rest -}
106
107 {-
108 sub dec_STRING {
109 $data =~ s/^((?:..)*?)(?:\x00\x00|\Z)//s;
110 # use Encode...
111 join "", map chr, unpack "v*", $1; -- marc ???
112 }
113 -}
114
115 dec_CONSTANT :: DecS a
116 dec_CONSTANT x = x
117
118 type Password = Word64
119
120 dec_password :: DecS Password
121 dec_password = dec_U64
122
123 dec_HEX :: DecS ()
124 dec_HEX =
125 do da <- get
126 putStr "HEX: "
127 putStrLn $ (dec_data da) ""
128 where dec_data (x:xs) =
129 ((drop 2) . showHex x . (':':)) . (dec_data xs)
130 dec_data [] = (\_ -> "")
131
132 #############################################################################
133
134 enc_U8 :: Word8 -> DecS ()
135 enc_U8 a =
136 do x <- get
137 put (a:x)
138 return ()
139
140 enc_U16 :: Word16 -> DecS ()
141 enc_U16 a =
142 do x <- get
143 let b1 = fromIntegral (a `shiftR` 8) :: Word8
144 b2 = fromIntegral a :: Word8
145 put (b1:b2:x)
146
147 enc_U32 :: Word32 -> DecS ()
148 enc_U32 a =
149 let b1 = fromIntegral (a `shiftR` 16) :: Word16
150 b2 = fromIntegral a :: Word16
151 enc_U16 b2
152 enc_U16 b1
153
154 enc_U64 :: Word64 -> DecS ()
155 enc_U64 a =
156 let b1 = fromIntegral (a `shiftR` 48) :: Word16
157 b2 = fromIntegral (a `shiftR` 32) :: Word16
158 b3 = fromIntegral (a `shiftR` 16) :: Word16
159 b4 = fromIntegral a :: Word16
160 enc_U16 b4
161 enc_U16 b3
162 enc_U16 b2
163 enc_U16 b1
164
165 enc_I8 :: Int8 -> DecS ()
166 enc_I8 a =
167 enc_U8 (fromIntegral a :: Word8)
168
169 enc_I16 :: Int16 -> DecS ()
170 enc_I16 a =
171 let b1 = fromIntegral (a `shiftR` 8) :: Word8
172 b2 = fromIntegral a :: Word8
173 enc_U8 b2
174 enc_U8 b1
175
176 enc_I32 :: Int32 -> DecS ()
177 enc_I32 a =
178 let
179 b1 = fromIntegral (a `shiftR` 16) :: Word16
180 b2 = fromIntegral a :: Word16
181 enc_U16 b2
182 enc_U16 b1
183
184 enc_DATA :: [Word8] -> DecS ()
185 enc_DATA d =
186 do x <- get
187 put $ (reverse d) ++ x
188
189 enc_STRING :: [Word16] -> DecS ()
190 enc_STRING (s:ss) =
191 do enc_U16 s
192 enc_STRING ss
193
194 {- do let mstr = reverse s
195 putall (u:ls) =
196 do enc_U8 u
197 putall ls
198 putall [] = return ()
199 putall mstr
200 enc_U8 0 -}
201
202 enc_CONSTANT :: a -> DecS ()
203 enc_CONSTANT _ = return ()
204
205 enc_password :: Password -> DecS ()
206 enc_password (p:ps) =
207 (1055 * (enc_password ps)) + (fromEnum p)
208
209 {- marc???
210 sub enc_password {
211 require Math::BigInt; # I insist on 32-bit-perl.. should use C
212 # $hash must be 64 bit
213 my $hash = new Math::BigInt;
214 $hash = $hash * 1055 + ord for split //, $_[0];
215 enc_U64 $hash;
216 }
217 -}
218
219 ]]>
220
221 #############################################################################
222 # types
223 <xsl:apply-templates select="descendant::type"/>
224
225 #############################################################################
226 # structures
227 <xsl:apply-templates select="descendant::struct"/>
228
229 #############################################################################
230 # "less" primitive types<![CDATA[
231
232 dec_TREE
233 enc_TREE
234
235 ]]>
236
237 #############################################################################
238 # messages
239 data KGS_server_msg =
240 KGS_server_msg_null
241 <xsl:for-each select="descendant::message[@src='server']">
242 | KGS_server_<xsl:value-of select="@name"/> {
243 <xsl:apply-templates select="member" mode="decl"/>
244 <xsl:text> }</xsl:text>
245 </xsl:for-each>
246
247 data KGS_client_msg =
248 KGS_server_msg_null
249 <xsl:for-each select="descendant::message[@src='client']">
250 | KGS_client_<xsl:value-of select="@name"/> {
251 <xsl:apply-templates select="member" mode="decl"/>
252 <xsl:text> }</xsl:text>
253 </xsl:for-each>
254
255 <xsl:apply-templates select="descendant::message"/>
256 }
257
258 1;
259 </xsl:template>
260
261 <xsl:template match="type[@type = 'S']">
262 type <xsl:value-of select="@name"/> = String
263
264 -- decode UCS-2 string of length <xsl:value-of select="@length"/>
265 -- the first 0 terminates the string, but not the field
266 dec_<xsl:value-of select="@name"/> :: DecS String
267 dec_<xsl:value-of select="@name"/> =
268 do getS <xsl:value-of select="@length"/>
269 where
270 -- getS ls 0 = reverse ls
271 getS ls 0 = reverse (map (\c -> toEnum (fromIntegral c :: Int)) ls)
272 getS ls n = dec_U8 >>=\c getS (c:[]) (n-1)
273
274 -- likewise, encode as UCS-2, fill space with 0
275 enc_<xsl:value-of select="@name"/> :: String -> DecS ()
276 enc_<xsl:value-of select="@name"/> str =
277 do let rlen = (length str) - <xsl:value-of select="@length"/>
278 putS str
279 putN rlen
280 where
281 putN 0 = return ()
282 putN n = enc_U8 0 n-1
283 putS [] = return ()
284 putS (c:cs) = do enc_U8 c
285 putS cs
286 </xsl:template>
287
288 <xsl:template match="type[@type = 'A']">
289 type <xsl:value-of select="@name"/> = String
290
291 -- decode ASCII string of length <xsl:value-of select="@length"/>
292 -- the first 0 terminates the string, but not the field
293 dec_<xsl:value-of select="@name"/> :: DecS String
294 dec_<xsl:value-of select="@name"/> =
295 do getS <xsl:value-of select="@length"/>
296 where
297 getS ls 0 = reverse (map (\c -> toEnum (fromIntegral c :: Int)) ls)
298 -- getS (0:ls) n = getS (0:ls) (n-1)
299 getS ls n = dec_U8 >>=\c getS (c:[]) (n-1)
300
301 -- likewise, encode as ASCII, fill space with 0
302 enc_<xsl:value-of select="@name"/> :: String -> DecS ()
303 enc_<xsl:value-of select="@name"/> str =
304 do let rlen = (length str) - <xsl:value-of select="@length"/>
305 putS str
306 putN rlen
307 where
308 putN 0 = return ()
309 putN n = enc_U8 0 n-1
310 putS [] = return ()
311 putS (c:cs) = do enc_U8 c
312 putS cs
313 </xsl:template>
314
315 <xsl:template match="type[@multiplier]">
316 type <xsl:value-of select="@name"/> = Float
317
318 dec_<xsl:value-of select="@name"/> = do
319 n &lt;- dec_<xsl:value-of select="@type"/>
320 return $ n * (1 / <xsl:value-of select="@multiplier"/>)
321
322 enc_<xsl:value-of select="@name"/> n =
323 enc_<xsl:value-of select="@type"/> $ n * <xsl:value-of select="@multiplier"/>
324
325 <xsl:text>
326 </xsl:text>
327 </xsl:template>
328
329 <xsl:template match="member[@array = 'yes']" mode="dec">
330 $r->{<xsl:value-of select="@name"/>} = (my $array = []);
331 while (length $data) {
332 push @$array, dec_<xsl:value-of select="@type"/>
333 <xsl:text> </xsl:text>
334 <xsl:value-of select="@default"/>;
335 }
336 </xsl:template>
337
338 <xsl:template match="member" mode="dec">
339 <xsl:if test="@guard-cond">
340 <xsl:text> if </xsl:text><xsl:value-of select="@guard-member"/><xsl:text> </xsl:text> <xsl:value-of select="@guard-cond"/> then do
341 </xsl:if>
342 <xsl:text> </xsl:text><xsl:value-of select="@name"/> &lt;- dec_<xsl:value-of select="@type"/>
343 <xsl:text>
344 </xsl:text>
345 </xsl:template>
346
347 <xsl:template match="member" mode="enc">
348 <xsl:text> enc_</xsl:text><xsl:value-of select="@type"/> $ <xsl:value-of select="@name"/> s
349 </xsl:template>
350
351 <xsl:template match="member" mode="decl">
352 <xsl:text> </xsl:text><xsl:value-of select="@name"/>: !<xsl:value-of select="@type"/>
353 <xsl:text>
354 </xsl:text>
355 </xsl:template>
356
357 <xsl:template match="struct">
358 data KGS_<xsl:value-of select="@name"/> = KGS_<xsl:value-of select="@name"/>
359 {
360 <xsl:apply-templates select="member" mode="decl"/>
361 }
362
363 dec_<xsl:value-of select="@name"/> = do
364 <xsl:apply-templates select="member" mode="dec"/>
365 return KGS_<xsl:value-of select="@name"/><xsl:for-each select="member"><xsl:text> </xsl:text><xsl:value-of select="@name"/></xsl:for-each>
366
367 enc_<xsl:value-of select="@name"/> s =
368 <xsl:apply-templates select="member" mode="enc"/>
369 <xsl:text>
370 </xsl:text>
371 </xsl:template>
372
373 <xsl:template match="message">
374 -- <xsl:value-of select="@name"/>
375 dec_<xsl:value-of select="@src"/>_<xsl:value-of select="@type"/> = do
376 <xsl:apply-templates select="member" mode="dec"/>
377 return KGS_<xsl:value-of select="@name"/><xsl:for-each select="member"><xsl:text> </xsl:text><xsl:value-of select="@name"/></xsl:for-each>
378
379 enc_<xsl:value-of select="@src"/>_<xsl:value-of select="@name"/> =
380 enc_U16 0x<xsl:value-of select="@type"/>
381 <xsl:apply-templates select="member" mode="enc"/>
382
383 </xsl:template>
384
385 <xsl:template match="text()">
386 </xsl:template>
387
388 </xsl:stylesheet>
389