summaryrefslogtreecommitdiffstats
path: root/tests/gpgscm/lib.scm
blob: 258f6925bf8722373c7e35632074f32a86240302 (plain)
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
;; Additional library functions for TinySCHEME.
;;
;; Copyright (C) 2016 g10 Code GmbH
;;
;; This file is part of GnuPG.
;;
;; GnuPG is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3 of the License, or
;; (at your option) any later version.
;;
;; GnuPG is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program; if not, see <http://www.gnu.org/licenses/>.

(macro (assert form)
  (let ((tag (get-tag form)))
    `(if (not ,(cadr form))
	 (throw ,(if (and (pair? tag) (string? (car tag)) (number? (cdr tag)))
		     `(string-append ,(car tag) ":"
				     ,(number->string (+ 1 (cdr tag)))
				     ": Assertion failed: ")
		     "Assertion failed: ")
		(quote ,(cadr form))))))
(assert #t)
(assert (not #f))

;; Trace displays and returns the given value.  A debugging aid.
(define (trace x)
  (display x)
  (newline)
  x)

;; Stringification.
(define (stringify expression)
  (let ((p (open-output-string)))
    (write expression p)
    (get-output-string p)))

(define (filter pred lst)
  (cond ((null? lst) '())
        ((pred (car lst))
         (cons (car lst) (filter pred (cdr lst))))
        (else (filter pred (cdr lst)))))

(define (any p l)
  (cond ((null? l) #f)
        ((p (car l)) #t)
        (else (any p (cdr l)))))

(define (all p l)
  (cond ((null? l) #t)
        ((not (p (car l))) #f)
        (else (all p (cdr l)))))

;; Return the first element of a list.
(define first car)

;; Return the last element of a list.
(define (last lst)
  (if (null? (cdr lst))
      (car lst)
      (last (cdr lst))))

;; Compute the powerset of a list.
(define (powerset set)
  (if (null? set)
      '(())
      (let ((rst (powerset (cdr set))))
        (append (map (lambda (x) (cons (car set) x))
                     rst)
                rst))))

;; Is PREFIX a prefix of S?
(define (string-prefix? s prefix)
  (and (>= (string-length s) (string-length prefix))
       (string=? prefix (substring s 0 (string-length prefix)))))
(assert (string-prefix? "Scheme" "Sch"))

;; Is SUFFIX a suffix of S?
(define (string-suffix? s suffix)
  (and (>= (string-length s) (string-length suffix))
       (string=? suffix (substring s (- (string-length s)
					(string-length suffix))
				   (string-length s)))))
(assert (string-suffix? "Scheme" "eme"))

;; Locate the first occurrence of needle in haystack starting at offset.
(ffi-define (string-index haystack needle [offset]))
(assert (= 2 (string-index "Hallo" #\l)))
(assert (= 3 (string-index "Hallo" #\l 3)))
(assert (equal? #f (string-index "Hallo" #\.)))

;; Locate the last occurrence of needle in haystack starting at offset.
(ffi-define (string-rindex haystack needle [offset]))
(assert (= 3 (string-rindex "Hallo" #\l)))
(assert (equal? #f (string-rindex "Hallo" #\a 2)))
(assert (equal? #f (string-rindex "Hallo" #\.)))

;; Split HAYSTACK at each character that makes PREDICATE true at most
;; N times.
(define (string-split-pln haystack predicate lookahead n)
  (let ((length (string-length haystack)))
    (define (split acc offset n)
      (if (>= offset length)
	  (reverse! acc)
	  (let ((i (lookahead haystack offset)))
	    (if (or (eq? i #f) (= 0 n))
		(reverse! (cons (substring haystack offset length) acc))
		(split (cons (substring haystack offset i) acc)
		       (+ i 1) (- n 1))))))
    (split '() 0 n)))

(define (string-indexp haystack offset predicate)
  (cond
   ((= (string-length haystack) offset)
    #f)
   ((predicate (string-ref haystack offset))
    offset)
   (else
    (string-indexp haystack (+ 1 offset) predicate))))

;; Split HAYSTACK at each character that makes PREDICATE true at most
;; N times.
(define (string-splitp haystack predicate n)
  (string-split-pln haystack predicate
		    (lambda (haystack offset)
		      (string-indexp haystack offset predicate))
		    n))
(assert (equal? '("a" "b") (string-splitp "a b" char-whitespace? -1)))
(assert (equal? '("a" "b") (string-splitp "a\tb" char-whitespace? -1)))
(assert (equal? '("a" "" "b") (string-splitp "a \tb" char-whitespace? -1)))

;; Split haystack at delimiter at most n times.
(define (string-splitn haystack delimiter n)
  (string-split-pln haystack
		    (lambda (c) (char=? c delimiter))
		    (lambda (haystack offset)
		      (string-index haystack delimiter offset))
		    n))
(assert (= 2 (length (string-splitn "foo:bar:baz" #\: 1))))
(assert (string=? "foo" (car (string-splitn "foo:bar:baz" #\: 1))))
(assert (string=? "bar:baz" (cadr (string-splitn "foo:bar:baz" #\: 1))))

;; Split haystack at delimiter.
(define (string-split haystack delimiter)
  (string-splitn haystack delimiter -1))
(assert (= 3 (length (string-split "foo:bar:baz" #\:))))
(assert (string=? "foo" (car (string-split "foo:bar:baz" #\:))))
(assert (string=? "bar" (cadr (string-split "foo:bar:baz" #\:))))
(assert (string=? "baz" (caddr (string-split "foo:bar:baz" #\:))))

;; Split haystack at newlines.
(define (string-split-newlines haystack)
  (if *win32*
      (map (lambda (line) (if (string-suffix? line "\r")
			      (substring line 0 (- (string-length line) 1))
			      line))
	   (string-split haystack #\newline))
      (string-split haystack #\newline)))

;; Trim the prefix of S containing only characters that make PREDICATE
;; true.
(define (string-ltrim predicate s)
  (if (string=? s "")
      ""
      (let loop ((s' (string->list s)))
	(if (predicate (car s'))
	    (loop (cdr s'))
	    (list->string s')))))
(assert (string=? "" (string-ltrim char-whitespace? "")))
(assert (string=? "foo" (string-ltrim char-whitespace? "  foo")))

;; Trim the suffix of S containing only characters that make PREDICATE
;; true.
(define (string-rtrim predicate s)
  (if (string=? s "")
      ""
      (let loop ((s' (reverse! (string->list s))))
	(if (predicate (car s'))
	    (loop (cdr s'))
	    (list->string (reverse! s'))))))
(assert (string=? "" (string-rtrim char-whitespace? "")))
(assert (string=? "foo" (string-rtrim char-whitespace? "foo 	")))

;; Trim both the prefix and suffix of S containing only characters
;; that make PREDICATE true.
(define (string-trim predicate s)
  (string-ltrim predicate (string-rtrim predicate s)))
(assert (string=? "" (string-trim char-whitespace? "")))
(assert (string=? "foo" (string-trim char-whitespace? " 	foo 	")))

;; Check if needle is contained in haystack.
(ffi-define (string-contains? haystack needle))
(assert (string-contains? "Hallo" "llo"))
(assert (not (string-contains? "Hallo" "olla")))

;; Translate characters.
(define (string-translate s from to)
  (list->string (map (lambda (c)
		       (let ((i (string-index from c)))
			 (if i (string-ref to i) c))) (string->list s))))
(assert (equal? (string-translate "foo/bar" "/" ".") "foo.bar"))

;; Read a word from port P.
(define (read-word . p)
  (list->string
   (let f ()
     (let ((c (apply peek-char p)))
       (cond
	((eof-object? c) '())
	((char-alphabetic? c)
	 (apply read-char p)
	 (cons c (f)))
	(else
	 (apply read-char p)
	 '()))))))

(define (list->string-reversed lst)
  (let* ((len (length lst))
	 (str (make-string len)))
    (let loop ((i (- len 1))
	       (l lst))
      (if (< i 0)
	  (begin
	    (assert (null? l))
	    str)
	  (begin
	    (string-set! str i (car l))
	    (loop (- i 1) (cdr l)))))))

;; Read a line from port P.
(define (read-line . p)
  (let loop ((acc '()))
    (let ((c (apply peek-char p)))
      (cond
       ((eof-object? c)
	(if (null? acc)
	    c ;; #eof
	    (list->string-reversed acc)))
       ((char=? c #\newline)
	(apply read-char p)
	(list->string-reversed acc))
       (else
	(apply read-char p)
	(loop (cons c acc)))))))

;; Read everything from port P.
(define (read-all . p)
  (let loop ((acc (open-output-string)))
    (let ((c (apply peek-char p)))
      (cond
       ((eof-object? c) (get-output-string acc))
       (else
	(write-char (apply read-char p) acc)
	(loop acc))))))

;;
;; Windows support.
;;

;; Like call-with-input-file but opens the file in 'binary' mode.
(define (call-with-binary-input-file filename proc)
  (letfd ((fd (open filename (logior O_RDONLY O_BINARY))))
	 (proc (fdopen fd "rb"))))

;; Like call-with-output-file but opens the file in 'binary' mode.
(define (call-with-binary-output-file filename proc)
  (letfd ((fd (open filename (logior O_WRONLY O_CREAT O_BINARY) #o600)))
	 (proc (fdopen fd "wb"))))

;;
;; Libc functions.
;;

;; Change the read/write offset.
(ffi-define (seek fd offset whence))

;; Constants for WHENCE.
(ffi-define SEEK_SET)
(ffi-define SEEK_CUR)
(ffi-define SEEK_END)

;; Get our process id.
(ffi-define (getpid))

;; Copy data from file descriptor SOURCE to every file descriptor in
;; SINKS.
(ffi-define (splice source . sinks))

;;
;; Random numbers.
;;

;; Seed the random number generator.
(ffi-define (srandom seed))

;; Get a pseudo-random number between 0 (inclusive) and SCALE
;; (exclusive).
(ffi-define (random scale))

;; Create a string of the given SIZE containing pseudo-random data.
(ffi-define (make-random-string size))