/usr/share/common-lisp/source/kmrcl/byte-stream.lisp is in cl-kmrcl 1.106-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | ;;; -*- Syntax: Ansi-Common-Lisp; Base: 10; Mode: lisp -*-
;;;; *************************************************************************
;;;; FILE IDENTIFICATION
;;;;
;;;; Name: byte-stream.lisp
;;;; Purpose: Byte array input/output streams
;;;; Programmer: Kevin M. Rosenberg
;;;; Date Started: June 2003
;;;;
;;;; Works for CMUCL, SBCL, and AllergoCL only
;;;;
;;;; This file, part of KMRCL, is Copyright (c) 2003 by Kevin M. Rosenberg
;;;; and by onShore Development, Inc.
;;;;
;;;; KMRCL users are granted the rights to distribute and use this software
;;;; as governed by the terms of the Lisp Lesser GNU Public License
;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
;;;; *************************************************************************
(in-package #:kmrcl)
;; Intial CMUCL version by OnShored. Ported to AllegroCL, SBCL by Kevin Rosenberg
#+sbcl
(eval-when (:compile-toplevel :load-toplevel :execute)
(when (sb-ext:without-package-locks
(sb-pcl::structure-class-p
(find-class (intern "FILE-STREAM" "SB-IMPL"))))
(push :old-sb-file-stream cl:*features*)))
#+(or cmu sbcl)
(progn
(defstruct (byte-array-output-stream
(:include #+cmu system:lisp-stream
#+(and sbcl old-sb-file-stream) sb-impl::file-stream
#+(and sbcl (not old-sb-file-stream)) sb-sys:fd-stream
(bout #'byte-array-bout)
(misc #'byte-array-out-misc))
(:print-function %print-byte-array-output-stream)
(:constructor make-byte-array-output-stream ()))
;; The buffer we throw stuff in.
(buffer (make-array 128 :element-type '(unsigned-byte 8)))
;; Index of the next location to use.
(index 0 :type fixnum))
(defun %print-byte-array-output-stream (s stream d)
(declare (ignore s d))
(write-string "#<Byte-Array-Output Stream>" stream))
(setf (documentation 'make-binary-output-stream 'function)
"Returns an Output stream which will accumulate all output given it for
the benefit of the function Get-Output-Stream-Data.")
(defun byte-array-bout (stream byte)
(let ((current (byte-array-output-stream-index stream))
(workspace (byte-array-output-stream-buffer stream)))
(if (= current (length workspace))
(let ((new-workspace (make-array (* current 2) :element-type '(unsigned-byte 8))))
(replace new-workspace workspace)
(setf (aref new-workspace current) byte)
(setf (byte-array-output-stream-buffer stream) new-workspace))
(setf (aref workspace current) byte))
(setf (byte-array-output-stream-index stream) (1+ current))))
(defun byte-array-out-misc (stream operation &optional arg1 arg2)
(declare (ignore arg2))
(case operation
(:file-position
(if (null arg1)
(byte-array-output-stream-index stream)))
(:element-type '(unsigned-byte 8))))
(defun get-output-stream-data (stream)
"Returns an array of all data sent to a stream made by
Make-Byte-Array-Output-Stream since the last call to this function and
clears buffer."
(declare (type byte-array-output-stream stream))
(prog1
(dump-output-stream-data stream)
(setf (byte-array-output-stream-index stream) 0)))
(defun dump-output-stream-data (stream)
"Returns an array of all data sent to a stream made by
Make-Byte-Array-Output-Stream since the last call to this function."
(declare (type byte-array-output-stream stream))
(let* ((length (byte-array-output-stream-index stream))
(result (make-array length :element-type '(unsigned-byte 8))))
(replace result (byte-array-output-stream-buffer stream))
result))
) ; progn
#+sbcl
(eval-when (:compile-toplevel :load-toplevel :execute)
(sb-ext:without-package-locks
(defvar *system-copy-fn* (if (fboundp (intern "COPY-SYSTEM-AREA" "SB-KERNEL"))
(intern "COPY-SYSTEM-AREA" "SB-KERNEL")
(intern "COPY-SYSTEM-UB8-AREA" "SB-KERNEL")))
(defconstant +system-copy-multiplier+ (if (fboundp (intern "COPY-FROM-SYSTEM-AREA" "SB-KERNEL"))
sb-vm:n-byte-bits
1))))
#+(or cmu sbcl)
(progn
(defstruct (byte-array-input-stream
(:include #+cmu system:lisp-stream
;;#+sbcl sb-impl::file-stream
#+(and sbcl old-sb-file-stream) sb-impl::file-stream
#+(and sbcl (not old-sb-file-stream)) sb-sys:fd-stream
(in #'byte-array-inch)
(bin #'byte-array-binch)
(n-bin #'byte-array-stream-read-n-bytes)
(misc #'byte-array-in-misc))
(:print-function %print-byte-array-input-stream)
;(:constructor nil)
(:constructor internal-make-byte-array-input-stream
(byte-array current end)))
(byte-array nil :type vector)
(current nil)
(end nil))
(defun %print-byte-array-input-stream (s stream d)
(declare (ignore s d))
(write-string "#<Byte-Array-Input Stream>" stream))
(defun byte-array-inch (stream eof-errorp eof-value)
(let ((byte-array (byte-array-input-stream-byte-array stream))
(index (byte-array-input-stream-current stream)))
(cond ((= index (byte-array-input-stream-end stream))
#+cmu
(eof-or-lose stream eof-errorp eof-value)
#+sbcl
(sb-impl::eof-or-lose stream eof-errorp eof-value)
)
(t
(setf (byte-array-input-stream-current stream) (1+ index))
(aref byte-array index)))))
(defun byte-array-binch (stream eof-errorp eof-value)
(let ((byte-array (byte-array-input-stream-byte-array stream))
(index (byte-array-input-stream-current stream)))
(cond ((= index (byte-array-input-stream-end stream))
#+cmu
(eof-or-lose stream eof-errorp eof-value)
#+sbcl
(sb-impl::eof-or-lose stream eof-errorp eof-value)
)
(t
(setf (byte-array-input-stream-current stream) (1+ index))
(aref byte-array index)))))
(defun byte-array-stream-read-n-bytes (stream buffer start requested eof-errorp)
(declare (type byte-array-input-stream stream))
(let* ((byte-array (byte-array-input-stream-byte-array stream))
(index (byte-array-input-stream-current stream))
(available (- (byte-array-input-stream-end stream) index))
(copy (min available requested)))
(when (plusp copy)
(setf (byte-array-input-stream-current stream)
(+ index copy))
#+cmu
(system:without-gcing
(system::system-area-copy (system:vector-sap byte-array)
(* index vm:byte-bits)
(if (typep buffer 'system::system-area-pointer)
buffer
(system:vector-sap buffer))
(* start vm:byte-bits)
(* copy vm:byte-bits)))
#+sbcl
(sb-sys:without-gcing
(funcall *system-copy-fn* (sb-sys:vector-sap byte-array)
(* index +system-copy-multiplier+)
(if (typep buffer 'sb-sys::system-area-pointer)
buffer
(sb-sys:vector-sap buffer))
(* start +system-copy-multiplier+)
(* copy +system-copy-multiplier+))))
(if (and (> requested copy) eof-errorp)
(error 'end-of-file :stream stream)
copy)))
(defun byte-array-in-misc (stream operation &optional arg1 arg2)
(declare (ignore arg2))
(case operation
(:file-position
(if arg1
(setf (byte-array-input-stream-current stream) arg1)
(byte-array-input-stream-current stream)))
(:file-length (length (byte-array-input-stream-byte-array stream)))
(:unread (decf (byte-array-input-stream-current stream)))
(:listen (or (/= (the fixnum (byte-array-input-stream-current stream))
(the fixnum (byte-array-input-stream-end stream)))
:eof))
(:element-type 'base-char)))
(defun make-byte-array-input-stream (buffer &optional (start 0) (end (length buffer)))
"Returns an input stream which will supply the bytes of BUFFER between
Start and End in order."
(internal-make-byte-array-input-stream buffer start end))
) ;; progn
(eval-when (:compile-toplevel :load-toplevel :execute)
(setq cl:*features* (delete :old-sb-file-stream cl:*features*)))
;;; Simple streams implementation by Kevin Rosenberg
#+allegro
(progn
(defclass extendable-buffer-output-stream (excl:buffer-output-simple-stream)
()
)
(defun make-byte-array-output-stream ()
"Returns an Output stream which will accumulate all output given it for
the benefit of the function Get-Output-Stream-Data."
(make-instance 'extendable-buffer-output-stream
:buffer (make-array 128 :element-type '(unsigned-byte 8))
:external-form :octets))
(defun get-output-stream-data (stream)
"Returns an array of all data sent to a stream made by
Make-Byte-Array-Output-Stream since the last call to this function
and clears buffer."
(prog1
(dump-output-stream-data stream)
(file-position stream 0)))
(defun dump-output-stream-data (stream)
"Returns an array of all data sent to a stream made by
Make-Byte-Array-Output-Stream since the last call to this function."
(force-output stream)
(let* ((length (file-position stream))
(result (make-array length :element-type '(unsigned-byte 8))))
(replace result (slot-value stream 'excl::buffer))
result))
(excl::without-package-locks
(defmethod excl:device-extend ((stream extendable-buffer-output-stream)
need action)
(declare (ignore action))
(let* ((len (file-position stream))
(new-len (max (+ len need) (* 2 len)))
(old-buf (slot-value stream 'excl::buffer))
(new-buf (make-array new-len :element-type '(unsigned-byte 8))))
(declare (fixnum len)
(optimize (speed 3) (safety 0)))
(dotimes (i len)
(setf (aref new-buf i) (aref old-buf i)))
(setf (slot-value stream 'excl::buffer) new-buf)
(setf (slot-value stream 'excl::buffer-ptr) new-len)
)
t))
)
#+allegro
(progn
(defun make-byte-array-input-stream (buffer &optional (start 0)
(end (length buffer)))
(excl:make-buffer-input-stream buffer start end :octets))
) ;; progn
|