1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10; Package: UFFI -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
5 ;;;; Name: strings.lisp
6 ;;;; Purpose: UFFI source to handle strings, cstrings, and foreigns
7 ;;;; Programmer: Kevin M. Rosenberg
8 ;;;; Date Started: Feb 2002
10 ;;;; This file, part of UFFI, is Copyright (c) 2002-2010 by Kevin M. Rosenberg
11 ;;;; *************************************************************************
16 (def-pointer-var +null-cstring-pointer+
17 #+(or cmu sbcl scl) nil
19 #+lispworks (fli:make-pointer :address 0 :type '(:unsigned :char))
20 #+(or openmcl digitool) (ccl:%null-ptr)
23 (defmacro convert-from-cstring (obj)
24 "Converts a string from a c-call. Same as convert-from-foreign-string, except
25 that LW/CMU automatically converts strings from c-calls."
26 #+(or cmu sbcl lispworks scl) obj
28 (let ((stored (gensym)))
29 `(let ((,stored ,obj))
32 (values (excl:native-to-string ,stored)))))
33 #+(or openmcl digitool)
34 (let ((stored (gensym)))
35 `(let ((,stored ,obj))
36 (if (ccl:%null-ptr-p ,stored)
38 (values (ccl:%get-cstring ,stored)))))
41 (defmacro convert-to-cstring (obj)
42 #+(or cmu sbcl scl lispworks) obj
44 (let ((stored (gensym)))
45 `(let ((,stored ,obj))
48 (values (excl:string-to-native ,stored)))))
49 #+(or openmcl digitool)
50 (let ((stored (gensym)))
51 `(let ((,stored ,obj))
53 +null-cstring-pointer+
54 (let ((ptr (new-ptr (1+ (length ,stored)))))
55 (ccl::%put-cstring ptr ,stored)
59 (defmacro free-cstring (obj)
60 (declare (ignorable obj))
62 (let ((stored (gensym)))
63 `(let ((,stored ,obj))
64 (unless (zerop ,stored)
65 (ff:free-fobject ,stored))))
66 #+(or openmcl digitool)
67 (let ((stored (gensym)))
68 `(let ((,stored ,obj))
69 (unless (ccl:%null-ptr-p ,stored)
70 (dispose-ptr ,stored))))
73 (defmacro with-cstring ((cstring lisp-string) &body body)
74 #+(or cmu sbcl scl lispworks)
75 `(let ((,cstring ,lisp-string)) ,@body)
77 (let ((acl-native (gensym))
78 (stored-lisp-string (gensym)))
79 `(let ((,stored-lisp-string ,lisp-string))
80 (excl:with-native-string (,acl-native ,stored-lisp-string)
81 (let ((,cstring (if ,stored-lisp-string ,acl-native 0)))
83 #+(or openmcl digitool)
84 (let ((stored-lisp-string (gensym)))
85 `(let ((,stored-lisp-string ,lisp-string))
86 (if (stringp ,stored-lisp-string)
87 (ccl:with-cstrs ((,cstring ,stored-lisp-string))
89 (let ((,cstring +null-cstring-pointer+))
93 (defmacro with-cstrings (bindings &rest body)
95 `(with-cstring ,(car bindings)
96 (with-cstrings ,(cdr bindings)
100 ;;; Foreign string functions
102 (defun %convert-to-foreign-string (str foreign-encoding)
103 (declare (ignorable str foreign-encoding))
107 (alien:sap-alien (system:int-sap 0) (* (alien:unsigned 8))))
110 (declare (optimize (speed 3) (safety 0)))
111 (let* ((size (length str))
112 (storage (alien:make-alien (alien:unsigned 8) (1+ size))))
113 (declare (fixnum size))
114 (setq storage (alien:cast storage (* (alien:unsigned 8))))
117 (setf (alien:deref storage i)
118 (char-code (char str i))))
119 (setf (alien:deref storage size) 0)
122 #+(and sbcl (not sb-unicode))
125 (sb-alien:sap-alien (sb-sys:int-sap 0) (* (sb-alien:unsigned 8))))
128 (declare (optimize (speed 3) (safety 0)))
129 (let* ((size (length str))
130 (storage (sb-alien:make-alien (sb-alien:unsigned 8) (1+ size))))
132 (setq storage (sb-alien:cast storage (* (sb-alien:unsigned 8))))
135 (setf (sb-alien:deref storage i)
136 (char-code (char str i))))
137 (setf (sb-alien:deref storage size) 0))
140 #+(and sbcl sb-unicode)
143 (sb-alien:sap-alien (sb-sys:int-sap 0) (* (sb-alien:unsigned 8))))
146 (declare (optimize (speed 3) (safety 0)))
147 (let* ((fe (or foreign-encoding *default-foreign-encoding*))
148 (ife (when fe (lookup-foreign-encoding fe))))
150 (let* ((octets (sb-ext:string-to-octets str :external-format ife))
151 (size (length octets))
152 (storage (sb-alien:make-alien (sb-alien:unsigned 8) (+ size 2))))
153 (declare (fixnum size))
154 (setq storage (sb-alien:cast storage (* (sb-alien:unsigned 8))))
157 (setf (sb-alien:deref storage i) (svref octets i)))
158 ;; terminate with 2 nulls, maybe needed for some encodings
159 (setf (sb-alien:deref storage size) 0)
160 (setf (sb-alien:deref storage (1+ size)) 0)
163 (let* ((size (length str))
164 (storage (sb-alien:make-alien (sb-alien:unsigned 8) (1+ size))))
165 (declare (fixnum size))
166 (setq storage (sb-alien:cast storage (* (sb-alien:unsigned 8))))
169 (setf (sb-alien:deref storage i)
170 (char-code (char str i))))
171 (setf (sb-alien:deref storage size) 0)
174 #+(and openmcl openmcl-unicode-strings)
176 +null-cstring-pointer+
178 (declare (optimize (speed 3) (safety 0)))
179 (let* ((fe (or foreign-encoding *default-foreign-encoding*))
180 (ife (when fe (lookup-foreign-encoding fe))))
182 (let* ((octets (ccl:encode-string-to-octets str :external-format ife))
183 (size (length octets))
184 (ptr (new-ptr (+ size 2))))
185 (declare (fixnum size))
188 (setf (ccl:%get-unsigned-byte ptr i) (svref octets i)))
189 (setf (ccl:%get-unsigned-byte ptr size) 0)
190 (setf (ccl:%get-unsigned-byte ptr (1+ size)) 0)
193 (let ((ptr (new-ptr (1+ (length str)))))
194 (ccl::%put-cstring ptr str)
197 #+(or digitool (and openmcl (not openmcl-unicode-strings)))
199 +null-cstring-pointer+
200 (let ((ptr (new-ptr (1+ (length str)))))
201 (ccl::%put-cstring ptr str)
204 #+(or allegro lispworks)
208 (defmacro convert-to-foreign-string (obj &optional foreign-encoding)
209 (declare (ignorable foreign-encoding))
211 (let ((stored (gensym "STR-"))
213 (ife (gensym "IFE-")))
214 `(let* ((,stored ,obj)
215 (,fe (or foreign-encoding *default-foreign-encoding*))
217 (lookup-foreign-encoding ,fe))))
222 (values (excl:string-to-native ,stored)))
224 (values (excl:string-to-native ,stored :external-format ,ife))))))
227 (let ((stored (gensym "STR-"))
229 (ife (gensym "NEF-")))
230 `(let* ((,stored ,obj)
231 (,fe (or ,foreign-encoding *default-foreign-encoding*))
233 (lookup-foreign-encoding ,fe))))
236 +null-cstring-pointer+)
238 (fli:convert-to-foreign-string ,stored))
240 (fli:convert-to-foreign-string ,stored :external-format ,ife)))))
242 #+(or cmu scl sbcl digitool openmcl)
243 `(%convert-to-foreign-string ,obj (lookup-foreign-encoding
244 (or ,foreign-encoding *default-foreign-encoding*)))
248 ;; Either length or null-terminated-p must be non-nil
249 (defmacro convert-from-foreign-string (obj &key
252 (null-terminated-p t))
253 (declare (ignorable length foreign-encoding null-terminated-p))
255 (let ((stored-obj (gensym "STR-"))
257 (ife (gensym "IFE-")))
258 `(let ((,stored-obj ,obj))
259 (if (zerop ,stored-obj)
261 (let* ((,fe (or ,foreign-encoding *default-foreign-encoding*))
262 (,ife (when ,fe (lookup-foreign-encoding ,fe))))
265 (excl:native-to-string
267 ,@(when length (list :length length))
268 :truncate (not ,null-terminated-p)
269 :external-format ,ife))
270 (fast-native-to-string ,stored-obj ,length))))))
273 ;; FAST-NATIVE-TO-STRING (suprisingly) works just fine with UTF-8 multibyte character strings
274 ;; However, without knowledge of specific-encoding, the LENGTH call in FAST-NATIVE-TO-STRING
275 ;; may not be incorrect for some encodings/strings.
276 ;; This is a stop-gap until get tech support on why the below fails.
277 (let ((stored-obj (gensym "STR-")))
278 `(let ((,stored-obj ,obj))
279 (if (fli:null-pointer-p ,stored-obj)
281 (fast-native-to-string ,stored-obj ,length))))
282 ;; Below code doesn't work on tesing with LW 6.0 testing with a UTF-8 string.
283 ;; fli:convert-from-foreign-string with :external-format of :UTF-8 doesn't
284 ;; properly code multibyte characters.
286 (let ((stored-obj (gensym "STR-"))
288 (ife (gensym "IFE-")))
289 `(let ((,stored-obj ,obj))
290 (if (fli:null-pointer-p ,stored-obj)
292 (let* ((,fe (or ,foreign-encoding *default-foreign-encoding*))
293 (,ife (when ,fe (lookup-foreign-encoding ,fe))))
295 (fli:convert-from-foreign-string
297 ,@(when length (list :length length))
298 :null-terminated-p ,null-terminated-p
299 :external-format (list ,ife :eol-style :lf))
300 (fast-native-to-string ,stored-obj ,length))))))
304 (let ((stored-obj (gensym)))
305 `(let ((,stored-obj ,obj))
306 (if (null-pointer-p ,stored-obj)
308 (cmucl-naturalize-cstring (alien:alien-sap ,stored-obj)
310 :null-terminated-p ,null-terminated-p))))
311 #+(and sbcl (not sb-unicode))
312 (let ((stored-obj (gensym)))
313 `(let ((,stored-obj ,obj))
314 (if (null-pointer-p ,stored-obj)
316 (sbcl-naturalize-cstring (sb-alien:alien-sap ,stored-obj)
318 :null-terminated-p ,null-terminated-p))))
320 #+(and sbcl sb-unicode)
321 (let ((stored-obj (gensym "STR-"))
323 (ife (gensym "IFE-")))
324 `(let ((,stored-obj ,obj))
325 (if (null-pointer-p ,stored-obj)
327 (let* ((,fe (or ,foreign-encoding *default-foreign-encoding*))
328 (,ife (when ,fe (lookup-foreign-encoding ,fe))))
329 (sb-alien::c-string-to-string (sb-alien:alien-sap ,stored-obj)
330 (or ,ife sb-impl::*default-external-format* :latin-1)
333 #+(or openmcl digitool)
334 (let ((stored-obj (gensym "STR-"))
336 `(let ((,stored-obj ,obj))
337 (if (ccl:%null-ptr-p ,stored-obj)
342 ,@(if length (list length) nil))
344 (let ((,fe (or ,foreign-encoding *default-foreign-encoding*)))
347 (ccl::%get-utf-8-cstring ,stored-obj))
349 (ccl::%get-native-utf-16-cstring ,stored-obj))
352 `((ccl:%str-from-ptr ,stored-obj ,length))
353 `((ccl:%get-cstring ,stored-obj)))))))))
357 (defmacro allocate-foreign-string (size &key (unsigned t))
358 (declare (ignorable unsigned))
360 (let ((array-def (gensym)))
361 `(let ((,array-def (list 'alien:array 'c-call:char ,size)))
362 (eval `(alien:cast (alien:make-alien ,,array-def)
364 '(* (alien:unsigned 8))
365 '(* (alien:signed 8)))))))
368 `(alien:make-alien ,(if unsigned
374 `(sb-alien:make-alien ,(if unsigned
375 '(sb-alien:unsigned 8)
376 '(sb-alien:signed 8))
380 `(fli:allocate-foreign-object :type
386 `(ff:allocate-fobject :char :c ,size)
387 #+(or openmcl digitool)
391 (defun foreign-string-length (foreign-string)
392 #+allegro `(ff:foreign-strlen ,foreign-string)
395 until (char= (deref-array ,foreign-string '(:array :unsigned-char) size) #\Null)
397 finally return size))
400 (defmacro with-foreign-string ((foreign-string lisp-string &optional foreign-encoding)
402 (let ((result (gensym))
404 `(let* ((,fe ,foreign-encoding)
405 (,foreign-string (convert-to-foreign-string ,lisp-string ,fe))
406 (,result (progn ,@body)))
407 (declare (dynamic-extent ,foreign-string))
408 (free-foreign-object ,foreign-string)
411 (defmacro with-foreign-strings (bindings &body body)
412 `(with-foreign-string ,(car bindings)
414 `((with-foreign-strings ,(cdr bindings) ,@body))
417 ;; Modified from CMUCL's source to handle non-null terminated strings
419 (defun cmucl-naturalize-cstring (sap &key length (null-terminated-p t))
420 (declare (type system:system-area-pointer sap))
422 (declare (optimize (speed 3) (safety 0)))
423 (let ((null-terminated-length
424 (when null-terminated-p
426 for offset of-type fixnum upfrom 0
427 until (zerop (system:sap-ref-8 sap offset))
428 finally (return offset)))))
430 (if (and null-terminated-length
431 (> (the fixnum length) (the fixnum null-terminated-length)))
432 (setq length null-terminated-length))
433 (setq length null-terminated-length)))
434 (let ((result (make-string length)))
435 (kernel:copy-from-system-area sap 0
436 result (* vm:vector-data-offset
438 (* length vm:byte-bits))
442 ;; kernel:copy-from-system-area doesn't work like it does on CMUCL or SBCL,
443 ;; so have to iteratively copy from sap
444 (defun cmucl-naturalize-cstring (sap &key length (null-terminated-p t))
445 (declare (type system:system-area-pointer sap))
447 (declare (optimize (speed 3) (safety 0)))
448 (let ((null-terminated-length
449 (when null-terminated-p
451 for offset of-type fixnum upfrom 0
452 until (zerop (system:sap-ref-8 sap offset))
453 finally (return offset)))))
455 (if (and null-terminated-length
456 (> (the fixnum length) (the fixnum null-terminated-length)))
457 (setq length null-terminated-length))
458 (setq length null-terminated-length)))
459 (let ((result (make-string length)))
461 (declare (type fixnum i))
462 (setf (char result i) (code-char (system:sap-ref-8 sap i))))
465 #+(and sbcl (not sb-unicode))
466 (defun sbcl-naturalize-cstring (sap &key length (null-terminated-p t))
467 (declare (type sb-sys:system-area-pointer sap)
468 (type (or null fixnum) length))
470 (declare (optimize (speed 3) (safety 0)))
471 (let ((null-terminated-length
472 (when null-terminated-p
474 for offset of-type fixnum upfrom 0
475 until (zerop (sb-sys:sap-ref-8 sap offset))
476 finally (return offset)))))
478 (if (and null-terminated-length
479 (> (the fixnum length) (the fixnum null-terminated-length)))
480 (setq length null-terminated-length))
481 (setq length null-terminated-length)))
482 (let ((result (make-string length)))
483 (funcall *system-copy-fn* sap 0 result +system-copy-offset+
484 (* length +system-copy-multiplier+))
488 (eval-when (:compile-toplevel :load-toplevel :execute)
489 (def-function "strlen"
490 ((str (* :unsigned-char)))
491 :returning :unsigned-int))
493 (def-type char-ptr-def (* :unsigned-char))
495 #+(or (and allegro (not ics)) (and lispworks (not lispworks5) (not lispworks6)))
496 (defun fast-native-to-string (s len)
497 (declare (optimize (speed 3) (space 0) (safety 0) (compilation-speed 0))
498 (type char-ptr-def s))
499 (let* ((len (or len (strlen s)))
500 (str (make-string len)))
501 (declare (fixnum len)
502 (type (simple-array #+lispworks base-char
503 #-lispworks (signed-byte 8) (*)) str))
506 (uffi:deref-array s '(:array :char) i)))))
508 #+(or (and allegro ics) lispworks5 lispworks6)
509 (defun fast-native-to-string (s len)
510 (declare (optimize (speed 3) (space 0) (safety 0) (compilation-speed 0))
511 (type char-ptr-def s))
512 (let* ((len (or len (strlen s)))
513 (str (make-string len)))
515 (setf (schar str i) (code-char (uffi:deref-array s '(:array :unsigned-byte) i))))))