Artifact 372f4c25f1536cf499ccda2f90f7c7b90e4fd7f4:
- File netstring.sls — part of check-in [03e5fe6318] at 2016-12-11 13:57:38 on branch trunk — added missing license headers (user: aldo size: 2063)
0000: 3b 3b 0a 3b 3b 20 43 6f 70 79 72 69 67 68 74 20 ;;.;; Copyright 0010: 32 30 31 36 20 41 6c 64 6f 20 4e 69 63 6f 6c 61 2016 Aldo Nicola 0020: 73 20 42 72 75 6e 6f 0a 3b 3b 0a 3b 3b 20 4c 69 s Bruno.;;.;; Li 0030: 63 65 6e 73 65 64 20 75 6e 64 65 72 20 74 68 65 censed under the 0040: 20 41 70 61 63 68 65 20 4c 69 63 65 6e 73 65 2c Apache License, 0050: 20 56 65 72 73 69 6f 6e 20 32 2e 30 20 28 74 68 Version 2.0 (th 0060: 65 20 22 4c 69 63 65 6e 73 65 22 29 3b 0a 3b 3b e "License");.;; 0070: 20 79 6f 75 20 6d 61 79 20 6e 6f 74 20 75 73 65 you may not use 0080: 20 74 68 69 73 20 66 69 6c 65 20 65 78 63 65 70 this file excep 0090: 74 20 69 6e 20 63 6f 6d 70 6c 69 61 6e 63 65 20 t in compliance 00a0: 77 69 74 68 20 74 68 65 20 4c 69 63 65 6e 73 65 with the License 00b0: 2e 0a 3b 3b 20 59 6f 75 20 6d 61 79 20 6f 62 74 ..;; You may obt 00c0: 61 69 6e 20 61 20 63 6f 70 79 20 6f 66 20 74 68 ain a copy of th 00d0: 65 20 4c 69 63 65 6e 73 65 20 61 74 0a 3b 3b 0a e License at.;;. 00e0: 3b 3b 20 20 20 20 20 68 74 74 70 3a 2f 2f 77 77 ;; http://ww 00f0: 77 2e 61 70 61 63 68 65 2e 6f 72 67 2f 6c 69 63 w.apache.org/lic 0100: 65 6e 73 65 73 2f 4c 49 43 45 4e 53 45 2d 32 2e enses/LICENSE-2. 0110: 30 0a 3b 3b 0a 3b 3b 20 55 6e 6c 65 73 73 20 72 0.;;.;; Unless r 0120: 65 71 75 69 72 65 64 20 62 79 20 61 70 70 6c 69 equired by appli 0130: 63 61 62 6c 65 20 6c 61 77 20 6f 72 20 61 67 72 cable law or agr 0140: 65 65 64 20 74 6f 20 69 6e 20 77 72 69 74 69 6e eed to in writin 0150: 67 2c 20 73 6f 66 74 77 61 72 65 0a 3b 3b 20 64 g, software.;; d 0160: 69 73 74 72 69 62 75 74 65 64 20 75 6e 64 65 72 istributed under 0170: 20 74 68 65 20 4c 69 63 65 6e 73 65 20 69 73 20 the License is 0180: 64 69 73 74 72 69 62 75 74 65 64 20 6f 6e 20 61 distributed on a 0190: 6e 20 22 41 53 20 49 53 22 20 42 41 53 49 53 2c n "AS IS" BASIS, 01a0: 0a 3b 3b 20 57 49 54 48 4f 55 54 20 57 41 52 52 .;; WITHOUT WARR 01b0: 41 4e 54 49 45 53 20 4f 52 20 43 4f 4e 44 49 54 ANTIES OR CONDIT 01c0: 49 4f 4e 53 20 4f 46 20 41 4e 59 20 4b 49 4e 44 IONS OF ANY KIND 01d0: 2c 20 65 69 74 68 65 72 20 65 78 70 72 65 73 73 , either express 01e0: 20 6f 72 20 69 6d 70 6c 69 65 64 2e 0a 3b 3b 20 or implied..;; 01f0: 53 65 65 20 74 68 65 20 4c 69 63 65 6e 73 65 20 See the License 0200: 66 6f 72 20 74 68 65 20 73 70 65 63 69 66 69 63 for the specific 0210: 20 6c 61 6e 67 75 61 67 65 20 67 6f 76 65 72 6e language govern 0220: 69 6e 67 20 70 65 72 6d 69 73 73 69 6f 6e 73 20 ing permissions 0230: 61 6e 64 0a 3b 3b 20 6c 69 6d 69 74 61 74 69 6f and.;; limitatio 0240: 6e 73 20 75 6e 64 65 72 20 74 68 65 20 4c 69 63 ns under the Lic 0250: 65 6e 73 65 2e 0a 0a 28 6c 69 62 72 61 72 79 20 ense...(library 0260: 28 6e 65 74 73 74 72 69 6e 67 29 0a 20 20 28 65 (netstring). (e 0270: 78 70 6f 72 74 20 72 65 61 64 2d 6e 65 74 73 74 xport read-netst 0280: 72 69 6e 67 20 77 72 69 74 65 2d 6e 65 74 73 74 ring write-netst 0290: 72 69 6e 67 20 72 65 61 64 2d 6e 65 74 73 74 72 ring read-netstr 02a0: 69 6e 67 2f 73 74 72 69 6e 67 29 0a 20 20 28 69 ing/string). (i 02b0: 6d 70 6f 72 74 20 28 63 68 65 7a 73 63 68 65 6d mport (chezschem 02c0: 65 29 29 0a 20 20 0a 20 20 28 64 65 66 69 6e 65 e)). . (define 02d0: 20 28 72 65 61 64 2d 6e 65 74 73 74 72 69 6e 67 (read-netstring 02e0: 20 70 6f 72 74 29 0a 20 20 20 20 28 6c 65 74 20 port). (let 02f0: 6c 6f 6f 70 20 28 5b 6c 65 6e 20 30 5d 29 0a 20 loop ([len 0]). 0300: 20 20 20 20 20 28 6c 65 74 20 28 5b 63 20 28 67 (let ([c (g 0310: 65 74 2d 75 38 20 70 6f 72 74 29 5d 20 29 0a 09 et-u8 port)] ).. 0320: 28 77 68 65 6e 20 28 65 6f 66 2d 6f 62 6a 65 63 (when (eof-objec 0330: 74 3f 20 63 29 0a 09 20 20 20 20 28 65 72 72 6f t? c).. (erro 0340: 72 66 20 27 72 65 61 64 2d 6e 65 74 73 74 72 69 rf 'read-netstri 0350: 6e 67 20 22 75 6e 65 78 70 65 63 74 65 64 20 65 ng "unexpected e 0360: 6e 64 20 6f 66 20 66 69 6c 65 20 77 68 69 6c 65 nd of file while 0370: 20 72 65 61 64 69 6e 67 20 68 65 61 64 65 72 22 reading header" 0380: 29 29 0a 09 28 63 6f 6e 64 0a 09 20 5b 28 3c 3d ))..(cond.. [(<= 0390: 20 23 78 33 30 20 63 20 23 78 33 39 29 0a 09 20 #x30 c #x39).. 03a0: 20 28 6c 6f 6f 70 20 28 66 78 2b 20 28 66 78 2a (loop (fx+ (fx* 03b0: 20 31 30 20 6c 65 6e 29 20 28 66 78 2d 20 63 20 10 len) (fx- c 03c0: 23 78 33 30 29 29 29 5d 0a 09 20 5b 28 66 78 3d #x30)))].. [(fx= 03d0: 20 63 20 28 63 68 61 72 2d 3e 69 6e 74 65 67 65 c (char->intege 03e0: 72 20 23 5c 3a 29 29 0a 09 20 20 28 6c 65 74 20 r #\:)).. (let 03f0: 28 5b 72 20 28 67 65 74 2d 62 79 74 65 76 65 63 ([r (get-bytevec 0400: 74 6f 72 2d 6e 20 70 6f 72 74 20 6c 65 6e 29 5d tor-n port len)] 0410: 29 0a 09 20 20 20 20 28 77 68 65 6e 20 28 6f 72 ).. (when (or 0420: 20 28 65 6f 66 2d 6f 62 6a 65 63 74 3f 20 72 29 (eof-object? r) 0430: 0a 09 09 20 20 20 20 20 20 28 3c 20 28 62 79 74 ... (< (byt 0440: 65 76 65 63 74 6f 72 2d 6c 65 6e 67 74 68 20 72 evector-length r 0450: 29 20 6c 65 6e 29 29 0a 09 09 20 20 28 65 72 72 ) len))... (err 0460: 6f 72 66 20 27 72 65 61 64 2d 6e 65 74 73 74 72 orf 'read-netstr 0470: 69 6e 67 20 22 75 6e 65 78 70 65 63 74 65 64 20 ing "unexpected 0480: 65 6e 64 20 6f 66 20 66 69 6c 65 20 77 68 69 6c end of file whil 0490: 65 20 72 65 61 64 69 6e 67 20 64 61 74 61 22 29 e reading data") 04a0: 29 0a 09 20 20 20 20 28 75 6e 6c 65 73 73 20 28 ).. (unless ( 04b0: 65 71 76 3f 20 28 67 65 74 2d 75 38 20 70 6f 72 eqv? (get-u8 por 04c0: 74 29 20 28 63 68 61 72 2d 3e 69 6e 74 65 67 65 t) (char->intege 04d0: 72 20 23 5c 2c 29 29 0a 09 09 20 20 20 20 28 65 r #\,))... (e 04e0: 72 72 6f 72 66 20 27 72 65 61 64 2d 6e 65 74 73 rrorf 'read-nets 04f0: 74 72 69 6e 67 20 22 65 78 70 65 63 74 65 64 20 tring "expected 0500: 2c 20 61 74 20 65 6e 64 20 6f 66 20 6e 65 74 73 , at end of nets 0510: 74 72 69 6e 67 22 20 29 29 0a 09 20 20 20 20 72 tring" )).. r 0520: 29 5d 0a 09 20 5b 65 6c 73 65 0a 09 20 20 28 65 )].. [else.. (e 0530: 72 72 6f 72 66 20 27 72 65 61 64 2d 6e 65 74 73 rrorf 'read-nets 0540: 74 72 69 6e 67 20 22 75 6e 65 78 70 65 63 74 65 tring "unexpecte 0550: 64 20 63 68 61 72 61 63 74 65 72 20 77 68 69 6c d character whil 0560: 65 20 72 65 61 64 69 6e 67 20 68 65 61 64 65 72 e reading header 0570: 20 23 78 7e 78 22 20 63 29 5d 29 29 29 29 0a 20 #x~x" c)])))). 0580: 20 0a 20 20 28 64 65 66 69 6e 65 20 28 72 65 61 . (define (rea 0590: 64 2d 6e 65 74 73 74 72 69 6e 67 2f 73 74 72 69 d-netstring/stri 05a0: 6e 67 20 70 6f 72 74 29 0a 20 20 20 20 28 75 74 ng port). (ut 05b0: 66 38 2d 3e 73 74 72 69 6e 67 20 28 72 65 61 64 f8->string (read 05c0: 2d 6e 65 74 73 74 72 69 6e 67 20 70 6f 72 74 29 -netstring port) 05d0: 29 29 0a 20 20 0a 20 20 28 64 65 66 69 6e 65 20 )). . (define 05e0: 28 77 72 69 74 65 2d 6e 65 74 73 74 72 69 6e 67 (write-netstring 05f0: 20 70 6f 72 74 20 64 61 74 61 29 0a 20 20 20 20 port data). 0600: 28 6c 65 74 20 28 5b 64 61 74 61 20 28 69 66 20 (let ([data (if 0610: 28 73 74 72 69 6e 67 3f 20 64 61 74 61 29 20 28 (string? data) ( 0620: 73 74 72 69 6e 67 2d 3e 75 74 66 38 20 64 61 74 string->utf8 dat 0630: 61 29 20 64 61 74 61 29 5d 29 0a 20 20 20 20 20 a) data)]). 0640: 20 28 70 75 74 2d 62 79 74 65 76 65 63 74 6f 72 (put-bytevector 0650: 20 70 6f 72 74 20 28 73 74 72 69 6e 67 2d 3e 75 port (string->u 0660: 74 66 38 20 28 6e 75 6d 62 65 72 2d 3e 73 74 72 tf8 (number->str 0670: 69 6e 67 20 28 62 79 74 65 76 65 63 74 6f 72 2d ing (bytevector- 0680: 6c 65 6e 67 74 68 20 64 61 74 61 29 29 29 29 0a length data)))). 0690: 20 20 20 20 20 20 28 70 75 74 2d 75 38 20 70 6f (put-u8 po 06a0: 72 74 20 28 63 68 61 72 2d 3e 69 6e 74 65 67 65 rt (char->intege 06b0: 72 20 23 5c 3a 29 29 0a 20 20 20 20 20 20 28 70 r #\:)). (p 06c0: 75 74 2d 62 79 74 65 76 65 63 74 6f 72 20 70 6f ut-bytevector po 06d0: 72 74 20 64 61 74 61 29 0a 20 20 20 20 20 20 28 rt data). ( 06e0: 70 75 74 2d 75 38 20 70 6f 72 74 20 28 63 68 61 put-u8 port (cha 06f0: 72 2d 3e 69 6e 74 65 67 65 72 20 23 5c 2c 29 29 r->integer #\,)) 0700: 29 29 29 0a 0a 23 7c 0a 28 64 65 66 69 6e 65 20 )))..#|.(define 0710: 6d 73 67 20 22 61 62 63 64 65 66 67 68 69 6a 6b msg "abcdefghijk 0720: 6c 22 29 0a 28 64 65 66 69 6e 65 2d 76 61 6c 75 l").(define-valu 0730: 65 73 20 28 70 74 20 62 76 29 20 28 6f 70 65 6e es (pt bv) (open 0740: 2d 62 79 74 65 76 65 63 74 6f 72 2d 6f 75 74 70 -bytevector-outp 0750: 75 74 2d 70 6f 72 74 29 29 0a 28 77 72 69 74 65 ut-port)).(write 0760: 2d 6e 65 74 73 74 72 69 6e 67 20 70 74 20 6d 73 -netstring pt ms 0770: 67 29 0a 28 64 65 66 69 6e 65 20 78 20 28 62 76 g).(define x (bv 0780: 29 29 0a 28 69 66 20 28 65 71 75 61 6c 3f 20 6d )).(if (equal? m 0790: 73 67 0a 09 20 20 20 20 28 72 65 61 64 2d 6e 65 sg.. (read-ne 07a0: 74 73 74 72 69 6e 67 2f 73 74 72 69 6e 67 20 28 tstring/string ( 07b0: 6f 70 65 6e 2d 62 79 74 65 76 65 63 74 6f 72 2d open-bytevector- 07c0: 69 6e 70 75 74 2d 70 6f 72 74 20 78 29 29 29 0a input-port x))). 07d0: 20 20 20 20 28 70 72 69 6e 74 66 20 22 74 65 73 (printf "tes 07e0: 74 20 4f 4b 7e 6e 22 29 0a 20 20 20 20 28 70 72 t OK~n"). (pr 07f0: 69 6e 74 66 20 22 74 65 73 74 20 46 41 49 4c 45 intf "test FAILE 0800: 44 7e 6e 22 29 29 0a 20 20 20 20 0a 7c 23 0a D~n")). .|#.