1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
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 encoding)
103 (declare (ignorable str 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 encoding *default-foreign-encoding*
148 sb-impl::*default-external-format*))
149 (ife (when fe (lookup-foreign-encoding fe))))
151 (let* ((octets (sb-ext:string-to-octets str :external-format ife))
152 (size (length octets))
153 (storage (sb-alien:make-alien (sb-alien:unsigned 8) (+ size 2))))
154 (declare (fixnum size))
155 (setq storage (sb-alien:cast storage (* (sb-alien:unsigned 8))))
158 (setf (sb-alien:deref storage i) (aref octets i)))
159 ;; terminate with 2 nulls, maybe needed for some encodings
160 (setf (sb-alien:deref storage size) 0)
161 (setf (sb-alien:deref storage (1+ size)) 0)
164 (let* ((size (length str))
165 (storage (sb-alien:make-alien (sb-alien:unsigned 8) (1+ size))))
166 (declare (fixnum size))
167 (setq storage (sb-alien:cast storage (* (sb-alien:unsigned 8))))
170 (setf (sb-alien:deref storage i)
171 (char-code (char str i))))
172 (setf (sb-alien:deref storage size) 0)
175 #+(and openmcl openmcl-unicode-strings)
177 +null-cstring-pointer+
179 (declare (optimize (speed 3) (safety 0)))
180 (let* ((fe (or encoding *default-foreign-encoding*))
181 (ife (when fe (lookup-foreign-encoding fe))))
183 (let* ((octets (ccl:encode-string-to-octets str :external-format ife))
184 (size (length octets))
185 (ptr (new-ptr (+ size 2))))
186 (declare (fixnum size))
189 (setf (ccl:%get-unsigned-byte ptr i) (svref octets i)))
190 (setf (ccl:%get-unsigned-byte ptr size) 0)
191 (setf (ccl:%get-unsigned-byte ptr (1+ size)) 0)
194 (let ((ptr (new-ptr (1+ (length str)))))
195 (ccl::%put-cstring ptr str)
198 #+(or digitool (and openmcl (not openmcl-unicode-strings)))
200 +null-cstring-pointer+
201 (let ((ptr (new-ptr (1+ (length str)))))
202 (ccl::%put-cstring ptr str)
205 #+(or allegro lispworks)
209 (defmacro convert-to-foreign-string (obj &optional encoding)
210 (declare (ignorable encoding))
212 (let ((stored (gensym "STR-"))
214 (ife (gensym "IFE-")))
215 `(let* ((,stored ,obj)
216 (,fe (or encoding *default-foreign-encoding*))
218 (lookup-foreign-encoding ,fe))))
223 (values (excl:string-to-native ,stored)))
225 (values (excl:string-to-native ,stored :external-format ,ife))))))
228 (let ((stored (gensym "STR-"))
230 (ife (gensym "NEF-")))
231 `(let* ((,stored ,obj)
232 (,fe (or ,encoding *default-foreign-encoding*))
234 (lookup-foreign-encoding ,fe))))
237 +null-cstring-pointer+)
239 (fli:convert-to-foreign-string ,stored))
241 (fli:convert-to-foreign-string ,stored :external-format ,ife)))))
243 #+(or cmu scl sbcl digitool openmcl)
244 `(%convert-to-foreign-string ,obj (lookup-foreign-encoding
245 (or ,encoding *default-foreign-encoding*)))
249 ;; Either length or null-terminated-p must be non-nil
250 (defmacro convert-from-foreign-string (obj &key
253 (null-terminated-p t))
254 (declare (ignorable length encoding null-terminated-p))
256 (let ((stored-obj (gensym "STR-"))
258 (ife (gensym "IFE-")))
259 `(let ((,stored-obj ,obj))
260 (if (zerop ,stored-obj)
262 (let* ((,fe (or ,encoding *default-foreign-encoding*))
263 (,ife (when ,fe (lookup-foreign-encoding ,fe))))
266 (excl:native-to-string
268 ,@(when length (list :length length))
269 :truncate (not ,null-terminated-p)
270 :external-format ,ife))
271 (fast-native-to-string ,stored-obj ,length))))))
275 ;; FAST-NATIVE-TO-STRING (suprisingly) works just fine to make strings
276 ;; for formatted printing with Lispworks and UTF-8 multibyte character strings.
277 ;; However, without knowledge of specific-encoding, the LENGTH call in FAST-NATIVE-TO-STRING
278 ;; will be be incorrect for some encodings/strings and strings consist of octets rather
279 ;; than wide characters
280 ;; This is a stop-gap until get tech support on why the below fails.
281 (let ((stored-obj (gensym "STR-")))
282 `(let ((,stored-obj ,obj))
283 (if (fli:null-pointer-p ,stored-obj)
285 (fast-native-to-string ,stored-obj ,length))))
288 ;; Below code doesn't work on tesing with LW 6.0 testing with a UTF-8 string.
289 ;; fli:convert-from-foreign-string with :external-format of :UTF-8 doesn't
290 ;; properly code multibyte characters.
292 (let ((stored-obj (gensym "STR-"))
294 (ife (gensym "IFE-")))
295 `(let ((,stored-obj ,obj))
296 (if (fli:null-pointer-p ,stored-obj)
298 (let* ((,fe (or ,encoding *default-foreign-encoding*))
299 (,ife (when ,fe (lookup-foreign-encoding ,fe))))
301 (fli:convert-from-foreign-string
303 ,@(when length (list :length length))
304 :null-terminated-p ,null-terminated-p
305 :external-format (list ,ife :eol-style :lf))
306 (fast-native-to-string ,stored-obj ,length))))))
309 (let ((stored-obj (gensym)))
310 `(let ((,stored-obj ,obj))
311 (if (null-pointer-p ,stored-obj)
313 (cmucl-naturalize-cstring (alien:alien-sap ,stored-obj)
315 :null-terminated-p ,null-terminated-p))))
316 #+(and sbcl (not sb-unicode))
317 (let ((stored-obj (gensym)))
318 `(let ((,stored-obj ,obj))
319 (if (null-pointer-p ,stored-obj)
321 (sbcl-naturalize-cstring (sb-alien:alien-sap ,stored-obj)
323 :null-terminated-p ,null-terminated-p))))
325 #+(and sbcl sb-unicode)
326 (let ((stored-obj (gensym "STR-"))
328 (ife (gensym "IFE-")))
329 `(let ((,stored-obj ,obj))
330 (if (null-pointer-p ,stored-obj)
332 (let* ((,fe (or ,encoding *default-foreign-encoding*))
333 (,ife (when ,fe (lookup-foreign-encoding ,fe))))
334 (sb-alien::c-string-to-string (sb-alien:alien-sap ,stored-obj)
335 (or ,ife sb-impl::*default-external-format* :latin-1)
338 #+(or openmcl digitool)
339 (let ((stored-obj (gensym "STR-"))
341 `(let ((,stored-obj ,obj))
342 (if (ccl:%null-ptr-p ,stored-obj)
347 ,@(if length (list length) nil))
349 (let ((,fe (or ,encoding *default-foreign-encoding*)))
352 (ccl::%get-utf-8-cstring ,stored-obj))
354 (ccl::%get-native-utf-16-cstring ,stored-obj))
357 `((ccl:%str-from-ptr ,stored-obj ,length))
358 `((ccl:%get-cstring ,stored-obj)))))))))
362 (defmacro allocate-foreign-string (size &key (unsigned t))
363 (declare (ignorable unsigned))
365 (let ((array-def (gensym)))
366 `(let ((,array-def (list 'alien:array 'c-call:char ,size)))
367 (eval `(alien:cast (alien:make-alien ,,array-def)
369 '(* (alien:unsigned 8))
370 '(* (alien:signed 8)))))))
373 `(alien:make-alien ,(if unsigned
379 `(sb-alien:make-alien ,(if unsigned
380 '(sb-alien:unsigned 8)
381 '(sb-alien:signed 8))
385 `(fli:allocate-foreign-object :type
391 `(ff:allocate-fobject :char :c ,size)
392 #+(or openmcl digitool)
396 (defmacro foreign-string-length (foreign-string)
397 #+allegro `(ff:foreign-strlen ,foreign-string)
401 until (char= (deref-array ,foreign-string '(:array :unsigned-char) size) #\Null)
402 finally (return size)))
405 (defmacro with-foreign-string ((foreign-string lisp-string &optional encoding)
407 (let ((result (gensym))
409 `(let* ((,fe ,encoding)
410 (,foreign-string (convert-to-foreign-string ,lisp-string ,fe))
411 (,result (progn ,@body)))
412 (declare (dynamic-extent ,foreign-string))
413 (free-foreign-object ,foreign-string)
416 (defmacro with-foreign-strings (bindings &body body)
417 `(with-foreign-string ,(car bindings)
419 `((with-foreign-strings ,(cdr bindings) ,@body))
422 ;; Modified from CMUCL's source to handle non-null terminated strings
424 (defun cmucl-naturalize-cstring (sap &key length (null-terminated-p t))
425 (declare (type system:system-area-pointer sap))
427 (declare (optimize (speed 3) (safety 0)))
428 (let ((null-terminated-length
429 (when null-terminated-p
431 for offset of-type fixnum upfrom 0
432 until (zerop (system:sap-ref-8 sap offset))
433 finally (return offset)))))
435 (if (and null-terminated-length
436 (> (the fixnum length) (the fixnum null-terminated-length)))
437 (setq length null-terminated-length))
438 (setq length null-terminated-length)))
439 (let ((result (make-string length)))
440 (kernel:copy-from-system-area sap 0
441 result (* vm:vector-data-offset
443 (* length vm:byte-bits))
447 ;; kernel:copy-from-system-area doesn't work like it does on CMUCL or SBCL,
448 ;; so have to iteratively copy from sap
449 (defun cmucl-naturalize-cstring (sap &key length (null-terminated-p t))
450 (declare (type system:system-area-pointer sap))
452 (declare (optimize (speed 3) (safety 0)))
453 (let ((null-terminated-length
454 (when null-terminated-p
456 for offset of-type fixnum upfrom 0
457 until (zerop (system:sap-ref-8 sap offset))
458 finally (return offset)))))
460 (if (and null-terminated-length
461 (> (the fixnum length) (the fixnum null-terminated-length)))
462 (setq length null-terminated-length))
463 (setq length null-terminated-length)))
464 (let ((result (make-string length)))
466 (declare (type fixnum i))
467 (setf (char result i) (code-char (system:sap-ref-8 sap i))))
470 #+(and sbcl (not sb-unicode))
471 (defun sbcl-naturalize-cstring (sap &key length (null-terminated-p t))
472 (declare (type sb-sys:system-area-pointer sap)
473 (type (or null fixnum) length))
475 (declare (optimize (speed 3) (safety 0)))
476 (let ((null-terminated-length
477 (when null-terminated-p
479 for offset of-type fixnum upfrom 0
480 until (zerop (sb-sys:sap-ref-8 sap offset))
481 finally (return offset)))))
483 (if (and null-terminated-length
484 (> (the fixnum length) (the fixnum null-terminated-length)))
485 (setq length null-terminated-length))
486 (setq length null-terminated-length)))
487 (let ((result (make-string length)))
488 (funcall *system-copy-fn* sap 0 result +system-copy-offset+
489 (* length +system-copy-multiplier+))
493 (eval-when (:compile-toplevel :load-toplevel :execute)
494 (def-function "strlen"
495 ((str (* :unsigned-char)))
496 :returning :unsigned-int))
498 (def-type char-ptr-def (* :unsigned-char))
500 #+(or (and allegro (not ics)) (and lispworks (not lispworks5) (not lispworks6)))
501 (defun fast-native-to-string (s len)
502 (declare (optimize (speed 3) (space 0) (safety 0) (compilation-speed 0))
503 (type char-ptr-def s))
504 (let* ((len (or len (strlen s)))
505 (str (make-string len)))
506 (declare (fixnum len)
507 (type (simple-array #+lispworks base-char
508 #-lispworks (signed-byte 8) (*)) str))
511 (uffi:deref-array s '(:array :char) i)))))
513 #+(or (and allegro ics) lispworks5 lispworks6)
514 (defun fast-native-to-string (s len)
515 (declare (optimize (speed 3) (space 0) (safety 0) (compilation-speed 0))
516 (type char-ptr-def s))
517 (let* ((len (or len (strlen s)))
518 (str (make-string len)))
520 (setf (schar str i) (code-char (uffi:deref-array s '(:array :unsigned-byte) i))))))