Debian Codegraph @

guile/modules/gnutls/build/smobs.scm

raw
1 ;;; Help produce Guile wrappers for GnuTLS types.
2 ;;;
3 ;;; GnuTLS --- Guile bindings for GnuTLS.
4 ;;; Copyright (C) 2007-2025 Free Software Foundation, Inc.
5 ;;;
6 ;;; This file is part of Guile-GnuTLS.
7 ;;;
8 ;;; This library is free software; you can redistribute it and/or
9 ;;; modify it under the terms of the GNU Lesser General Public
10 ;;; License as published by the Free Software Foundation; either
11 ;;; version 2.1 of the License, or (at your option) any later version.
12 ;;;
13 ;;; This library is distributed in the hope that it will be useful,
14 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 ;;; Lesser General Public License for more details.
17 ;;;
18 ;;; You should have received a copy of the GNU Lesser General Public
19 ;;; License along with this library; if not, see <https://www.gnu.org/licenses/>.
20
21 ;;; Written by Ludovic Courtès <[email protected]>
22
23 (define-module (gnutls build smobs)
24 :use-module (srfi srfi-9)
25 :use-module (srfi srfi-13)
26 :use-module (gnutls build utils)
27 :export (make-smob-type
28 smob-type-tag
29 smob-free-function
30 smob-type-predicate-scheme-name
31 smob-type-from-c-function
32 smob-type-to-c-function
33
34 output-smob-type-definition
35 output-smob-type-declaration
36 output-smob-type-predicate
37 output-c->smob-converter
38 output-smob->c-converter
39
40 %gnutls-smobs))
41
42
43 ;;;
44 ;;; SMOB types.
45 ;;;
46
47 (define-record-type <smob-type>
48 (%make-smob-type c-name scm-name free-function)
49 smob-type?
50 (c-name smob-type-c-name)
51 (scm-name smob-type-scheme-name)
52 (free-function smob-type-free-function))
53
54 (define (make-smob-type c-name scm-name . free-function)
55 (%make-smob-type c-name scm-name
56 (if (null? free-function)
57 (string-append "gnutls_"
58 (scheme-symbol->c-name scm-name)
59 "_deinit")
60 (car free-function))))
61
62 (define (smob-type-tag type)
63 ;; Return the name of the C variable holding the type tag for TYPE.
64 (string-append "scm_tc16_gnutls_"
65 (scheme-symbol->c-name (smob-type-scheme-name type))))
66
67 (define (smob-type-predicate-scheme-name type)
68 ;; Return a string denoting the Scheme name of TYPE's type predicate.
69 (string-append (symbol->string (smob-type-scheme-name type)) "?"))
70
71 (define (smob-type-to-c-function type)
72 ;; Return the name of the C `scm_to_' function for SMOB.
73 (string-append "scm_to_gnutls_"
74 (scheme-symbol->c-name (smob-type-scheme-name type))))
75
76 (define (smob-type-from-c-function type)
77 ;; Return the name of the C `scm_from_' function for SMOB.
78 (string-append "scm_from_gnutls_"
79 (scheme-symbol->c-name (smob-type-scheme-name type))))
80
81
82 ;;;
83 ;;; C code generation.
84 ;;;
85
86 (define (output-smob-type-definition type port)
87 (format port "SCM_GLOBAL_SMOB (~a, \"~a\", 0);~%"
88 (smob-type-tag type)
89 (smob-type-scheme-name type))
90
91 (format port "SCM_SMOB_FREE (~a, ~a_free, obj)~%{~%"
92 (smob-type-tag type)
93 (scheme-symbol->c-name (smob-type-scheme-name type)))
94 (format port " ~a c_obj;~%"
95 (smob-type-c-name type))
96 (format port " c_obj = (~a) SCM_SMOB_DATA (obj);~%"
97 (smob-type-c-name type))
98 (format port " ~a (c_obj);~%"
99 (smob-type-free-function type))
100 (format port " return 0;~%")
101 (format port "}~%"))
102
103 (define (output-smob-type-declaration type port)
104 ;; Issue a header file declaration for the SMOB type tag of TYPE.
105 (format port "SCM_API scm_t_bits ~a;~%"
106 (smob-type-tag type)))
107
108 (define (output-smob-type-predicate type port)
109 (define (texi-doc-string)
110 (string-append "Return true if @var{obj} is of type @code{"
111 (symbol->string (smob-type-scheme-name type))
112 "}."))
113
114 (let ((c-name (string-append "scm_gnutls_"
115 (string-map (lambda (chr)
116 (if (char=? chr #\-)
117 #\_
118 chr))
119 (symbol->string
120 (smob-type-scheme-name type)))
121 "_p")))
122 (format port "SCM_DEFINE (~a, \"~a\", 1, 0, 0,~%"
123 c-name (smob-type-predicate-scheme-name type))
124 (format port " (SCM obj),~%")
125 (format port " \"~a\")~%"
126 (texi-doc-string))
127 (format port "#define FUNC_NAME s_~a~%"
128 c-name)
129 (format port "{~%")
130 (format port " return (scm_from_bool (SCM_SMOB_PREDICATE (~a, obj)));~%"
131 (smob-type-tag type))
132 (format port "}~%#undef FUNC_NAME~%")))
133
134 (define (output-c->smob-converter type port)
135 (format port "static inline SCM~%~a (~a c_obj)~%{~%"
136 (smob-type-from-c-function type)
137 (smob-type-c-name type))
138 (format port " SCM_RETURN_NEWSMOB (~a, (scm_t_bits) c_obj);~%"
139 (smob-type-tag type))
140 (format port "}~%"))
141
142 (define (output-smob->c-converter type port)
143 (format port "static inline ~a~%~a (SCM obj, "
144 (smob-type-c-name type)
145 (smob-type-to-c-function type))
146 (format port "unsigned pos, const char *func)~%")
147 (format port "#define FUNC_NAME func~%")
148 (format port "{~%")
149 (format port " SCM_VALIDATE_SMOB (pos, obj, ~a);~%"
150 (string-append "gnutls_"
151 (scheme-symbol->c-name (smob-type-scheme-name type))))
152 (format port " return ((~a) SCM_SMOB_DATA (obj));~%"
153 (smob-type-c-name type))
154 (format port "}~%")
155 (format port "#undef FUNC_NAME~%"))
156
157
158 ;;;
159 ;;; Actual SMOB types.
160 ;;;
161
162 (define %session-smob
163 (make-smob-type "gnutls_session_t" 'session
164 "gnutls_deinit"))
165
166 (define %anonymous-client-credentials-smob
167 (make-smob-type "gnutls_anon_client_credentials_t" 'anonymous-client-credentials
168 "gnutls_anon_free_client_credentials"))
169
170 (define %anonymous-server-credentials-smob
171 (make-smob-type "gnutls_anon_server_credentials_t" 'anonymous-server-credentials
172 "gnutls_anon_free_server_credentials"))
173
174 (define %dh-parameters-smob
175 (make-smob-type "gnutls_dh_params_t" 'dh-parameters
176 "gnutls_dh_params_deinit"))
177
178 (define %certificate-credentials-smob
179 (make-smob-type "gnutls_certificate_credentials_t" 'certificate-credentials
180 "gnutls_certificate_free_credentials"))
181
182 (define %srp-server-credentials-smob
183 (make-smob-type "gnutls_srp_server_credentials_t" 'srp-server-credentials
184 "gnutls_srp_free_server_credentials"))
185
186 (define %srp-client-credentials-smob
187 (make-smob-type "gnutls_srp_client_credentials_t" 'srp-client-credentials
188 "gnutls_srp_free_client_credentials"))
189
190 (define %psk-server-credentials-smob
191 (make-smob-type "gnutls_psk_server_credentials_t" 'psk-server-credentials
192 "gnutls_psk_free_server_credentials"))
193
194 (define %psk-client-credentials-smob
195 (make-smob-type "gnutls_psk_client_credentials_t" 'psk-client-credentials
196 "gnutls_psk_free_client_credentials"))
197
198 (define %x509-certificate-smob
199 (make-smob-type "gnutls_x509_crt_t" 'x509-certificate
200 "gnutls_x509_crt_deinit"))
201
202 (define %x509-private-key-smob
203 (make-smob-type "gnutls_x509_privkey_t" 'x509-private-key
204 "gnutls_x509_privkey_deinit"))
205
206 (define %openpgp-certificate-smob
207 (make-smob-type "gnutls_openpgp_crt_t" 'openpgp-certificate
208 "gnutls_openpgp_crt_deinit"))
209
210 (define %openpgp-private-key-smob
211 (make-smob-type "gnutls_openpgp_privkey_t" 'openpgp-private-key
212 "gnutls_openpgp_privkey_deinit"))
213
214 (define %openpgp-keyring-smob
215 (make-smob-type "gnutls_openpgp_keyring_t" 'openpgp-keyring
216 "gnutls_openpgp_keyring_deinit"))
217
218 (define %public-key-smob
219 (make-smob-type "gnutls_pubkey_t" 'public-key
220 "gnutls_pubkey_deinit"))
221
222 (define %private-key-smob
223 (make-smob-type "gnutls_privkey_t" 'private-key
224 "gnutls_privkey_deinit"))
225
226 (define %hmac-smob
227 ;; The hmac object in gnutls does not contain enough information to be used
228 ;; safely. The output length of the hmac algorithm cannot be found from an
229 ;; hmac state. So, we define our own compound structure that packs the hmac
230 ;; state and the algorithm.
231 (make-smob-type "scm_gnutls_hmac_and_algorithm_t" 'hmac
232 "hmac_and_algorithm_deinit"))
233
234 (define %hash-smob
235 ;; The hash object suffers from the same problem as the hmac object.
236 (make-smob-type "scm_gnutls_hash_and_algorithm_t" 'hash
237 "hash_and_algorithm_deinit"))
238
239 (define %aead-cipher-smob
240 (make-smob-type "scm_gnutls_aead_cipher_and_algorithm_t" 'aead-cipher
241 "aead_cipher_and_algorithm_deinit"))
242
243 (define %cipher-smob
244 (make-smob-type "scm_gnutls_cipher_and_algorithm_t" 'cipher-hd
245 "cipher_and_algorithm_deinit"))
246
247
248 (define %gnutls-smobs
249 ;; All SMOB types.
250 (list %session-smob %anonymous-client-credentials-smob
251 %anonymous-server-credentials-smob %dh-parameters-smob
252 %certificate-credentials-smob
253 %srp-server-credentials-smob %srp-client-credentials-smob
254 %psk-server-credentials-smob %psk-client-credentials-smob
255 %x509-certificate-smob %x509-private-key-smob %hmac-smob
256 %hash-smob %aead-cipher-smob %cipher-smob
257 %public-key-smob %private-key-smob
258
259 %openpgp-certificate-smob %openpgp-private-key-smob
260 %openpgp-keyring-smob))
261
262
263 ;;; Local Variables:
264 ;;; mode: scheme
265 ;;; coding: utf-8
266 ;;; End:
267
268 ;;; arch-tag: 26bf79ef-6dee-45f2-9e9d-2d209c518278