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
|
;;;
;;; Copyright (C) 2004, 2005 M. Tuexen tuexen@fh-muenster.de
;;;
;;; All rights reserved.
;;;
;;; Redistribution and use in source and binary forms, with or
;;; without modification, are permitted provided that the
;;; following conditions are met:
;;; 1. Redistributions of source code must retain the above
;;; copyright notice, this list of conditions and the
;;; following disclaimer.
;;; 2. Redistributions in binary form must reproduce the
;;; above copyright notice, this list of conditions and
;;; the following disclaimer in the documentation and/or
;;; other materials provided with the distribution.
;;; 3. Neither the name of the project nor the names of
;;; its contributors may be used to endorse or promote
;;; products derived from this software without specific
;;; prior written permission.
;;;
;;; THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS
;;; ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
;;; BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
;;; MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
;;; DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS
;;; BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
;;; EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
;;; LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
;;; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
;;; HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
;;; IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
;;; USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
;;; OF SUCH DAMAGE.
;;; $Id: common.scm,v 1.6 2007/10/15 17:11:29 lmay Exp $
;;; Load the SCTP API needed.
(use-modules (net sctp))
;;; Just have a convenient way of simple looping.
(use-modules (ice-9 syncase))
(define-syntax dotimes
(syntax-rules ()
((_ (var n res) . body)
(do ((limit n)
(var 0 (+ var 1)))
((>= var limit) res)
. body))
((_ (var n) . body)
(do ((limit n)
(var 0 (+ var 1)))
((>= var limit))
. body))))
;;; The following functions implement modulo arithmetic.
(define 2^8 (expt 2 8))
(define 2^16 (expt 2 16))
(define 2^24 (expt 2 24))
(define 2^32 (expt 2 32))
(define 2^8-1 (1- 2^8))
(define 2^16-1 (1- 2^16))
(define 2^24-1 (1- 2^24))
(define 2^32-1 (1- 2^32))
(define (+mod2^8 x y)
(modulo (+ x y) 2^8))
(define (-mod2^8 x y)
(modulo (- x y) 2^8))
(define (*mod2^8 x y)
(modulo (* x y) 2^8))
(define (+mod2^16 x y)
(modulo (+ x y) 2^16))
(define (-mod2^16 x y)
(modulo (- x y) 2^16))
(define (*mod2^16 x y)
(modulo (* x y) 2^16))
(define (+mod2^24 x y)
(modulo (+ x y) 2^24))
(define (-mod2^24 x y)
(modulo (- x y) 2^24))
(define (*mod2^24 x y)
(modulo (* x y) 2^24))
(define (+mod2^32 x y)
(modulo (+ x y) 2^32))
(define (-mod2^32 x y)
(modulo (- x y) 2^32))
(define (*mod2^32 x y)
(modulo (* x y) 2^32))
;;; The following functions convert unsigned integers into
;;; a list of bytes in network byte order.
(define (uint8->bytes n)
(if (and (exact? n) (integer? n) (<= 0 n 2^8-1))
(list n)
(error "Argument not a uint8" n)))
;;;(uint8->bytes 1)
;;;(uint8->bytes -1)
;;;(uint8->bytes 2^8)
;;;(uint8->bytes 2.0)
(define (uint16->bytes n)
(if (and (exact? n) (integer? n) (<= 0 n 2^16-1))
(list (quotient n 2^8)
(remainder n 2^8))
(error "Argument not a uint16" n)))
;;;(uint16->bytes 1)
;;;(uint16->bytes 2^8)
;;;(uint16->bytes 2^16)
;;;(uint16->bytes 2^16-1)
(define (uint24->bytes n)
(if (and (exact? n) (integer? n) (<= 0 n 2^24-1))
(list (quotient n 2^16)
(quotient (remainder n 2^16) 2^8)
(remainder n 2^8))
(error "Argument not a uint24", n)))
;;;(uint24->bytes 1)
;;;(uint24->bytes 2^8)
;;;(uint24->bytes 2^16)
;;;(uint24->bytes 2^24-1)
(define (uint32->bytes n)
(if (and (exact? n) (integer? n) (<= 0 n 2^32-1))
(list (quotient n 2^24)
(quotient (remainder n 2^24) 2^16)
(quotient (remainder n 2^16) 2^8)
(remainder n 2^8))
(error "Argument not a uint32", n)))
;;;(uint32->bytes 1)
;;;(uint32->bytes 2^8)
;;;(uint32->bytes 2^16)
;;;(uint32->bytes 2^24)
;;;(uint32->bytes 2^32-1)
(define uint8->big-endian-bytes uint8->bytes)
(define uint16->big-endian-bytes uint16->bytes)
(define uint24->big-endian-bytes uint24->bytes)
(define uint32->big-endian-bytes uint32->bytes)
(define (uint8->little-endian-bytes n)
(reverse (uint8->bytes n)))
(define (uint16->little-endian-bytes n)
(reverse (uint16->bytes n)))
(define (uint24->little-endian-bytes n)
(reverse (uint24->bytes n)))
(define (uint32->little-endian-bytes n)
(reverse (uint32->bytes n)))
;;;(uint32->little-endian-bytes 1024)
;;; The following functions converts the first bytes of the argument
;;; to an unsigned integer in host byte order.
(define (bytes->uint8 l)
(car l))
;;;(bytes->uint8 (uint8->bytes 56))
(define (bytes->uint16 l)
(+ (* 2^8 (car l))
(cadr l)))
;;;(bytes->uint16 (uint16->bytes 12345))
(define (bytes->uint24 l)
(+ (* 2^16 (car l))
(* 2^8 (cadr l))
(caddr l)))
;;;(bytes->uint24 (uint24->bytes 12345567))
(define (bytes->uint32 l)
(+ (* 2^24 (car l))
(* 2^16 (cadr l))
(* 2^8 (caddr l))
(cadddr l)))
;;;(bytes->uint32 (uint32->bytes 2^32-1))
(define (list-head l n)
(list-head-1 l n (list)))
(define (list-head-1 l n r)
(if (<= n 0)
(reverse r)
(list-head-1 (cdr l) (- n 1) (cons (car l) r))))
;;; (list-head (list 1 2 3) 4)
(define big-endian-bytes->uint8 bytes->uint8)
(define big-endian-bytes->uint16 bytes->uint16)
(define big-endian-bytes->uint24 bytes->uint24)
(define big-endian-bytes->uint32 bytes->uint32)
(define (little-endian-bytes->uint8 l)
(bytes->uint8 (reverse (list-head l 1))))
(define (little-endian-bytes->uint16 l)
(bytes->uint16 (reverse (list-head l 2))))
(define (little-endian-bytes->uint24 l)
(bytes->uint24 (reverse (list-head l 3))))
(define (little-endian-bytes->uint32 l)
(bytes->uint32 (reverse (list-head l 4))))
;;;(little-endian-bytes->uint32 (uint32->little-endian-bytes 123456))
;;; This function generates a list of bytes representing a string.
(define (string->bytes s)
(map char->integer (string->list s)))
;;;(string->bytes "Hello")
;;; Convert a list of bytes to a string which can be used by the send call
(define (bytes->string l)
(list->string (map integer->char l)))
;;; (bytes->string '(65 65 65 0 65))
;;; This function generates a list of random bytes of a given length
(define (random-bytes n)
(random-bytes-1 n (list)))
;;; This is the tail-recursive version
(define (random-bytes-1 n l)
(if (<= n 0)
l
(random-bytes-1 (- n 1) (cons (random 2^8) l))))
;;; (random-bytes 10000)
(define (zero-bytes n)
(zero-bytes-1 n (list)))
(define (zero-bytes-1 n l)
(if (<= n 0)
l
(zero-bytes-1 (- n 1) (cons 0 l))))
;;;(length (zero-bytes 3400))
;;;(zero-bytes 0)
(define (remove pred lst)
(if (null? lst)
(list)
(if (pred (car lst))
(remove pred (cdr lst))
(cons (car lst) (remove pred (cdr lst))))))
;;; (remove positive? (list 1 -32 3 -9))
;;; (remove positive? (list -9))
;;; (remove positive? (list 1 2 3))
(define (filter pred lst)
(if (null? lst)
(list)
(if (pred (car lst))
(cons (car lst) (filter pred (cdr lst)))
(filter pred (cdr lst)))))
;;; (filter positive? (list 1 -32 3 -9))
;;; (filter positive? (list -9))
;;; (filter positive? (list 1 2 3))
|