#|-------------------- 0.9.3 |# "./chicken-dump.scm" 4585 ;;;; chicken-dump.scm (use srfi-4 srfi-69 lolevel matchable posix) (include "constants.scm") #> #include "s11n-c.c" <# (define bytes_to_block (##core#primitive "bytes_to_block")) (define (dump port) (let ((indent 0) (items 0) (header "") (backref-count 0) ) (define (getb) (char->integer (read-char port)) ) (define (getw) (##core#inline "bytes_to_word" (read-string +sizeof-ulong+ port)) ) (define (fetchslots x i) (fluid-let ((indent (add1 indent))) (let ((len (##sys#size x))) (do ((i i (add1 i))) ((>= i len) x) (fetchh (sprintf "[~a]" i))) ) ) ) (define (out i? fstr . args) (display (make-string indent)) (when i? (printf "#~a:" (sub1 items))) (printf "~a ~?~%" header fstr args) ) (define (outh fstr . args) (printf "~a[~?]~%" (make-string indent) fstr args) ) (define (fetchh hd) (fluid-let ((header hd)) (fetch))) (define (fetch) (let ((tag (getb))) (select tag ((sixtyfour-bit-tag) (out #f "64-bit mark") (fetch)) ((thirtytwo-bit-tag) (out #f "32-bit mark") (fetch)) ((big-endian-tag) (out #f "big endian mark") (fetch)) ((little-endian-tag) (out #f "little endian mark") (fetch)) ((void-tag) (out #f "void")) ((null-tag) (out #f "()")) ((eof-tag) (out #f "#!eof")) ((true-tag) (out #f "#t")) ((false-tag) (out #f "#f")) ((backref-tag) (let ((w (getw))) (out #f "back reference -> #~a" w) ) ) ((fixnum-tag) (out #f "fixnum ~a" (##core#inline "bytes_to_fixnum" (read-string +sizeof-word+ port)) )) ((char-tag) (out #f "char ~s" (integer->char (getw)) )) ((gensym-tag) (set! items (add1 items)) (out #t "uninterned symbol:") (fetchh "(name)") ) ((symbol-tag) (set! items (add1 items)) (out #t "symbol:") (fetchh "(name)")) ((stdport-tag) (set! items (add1 items)) (let* ((n (getb)) (r (sub1 backref-count))) (case n ((0) (out #t "stdin port")) ((1) (out #t "stdout port")) ((2) (out #t "stderr port")) (else (out #t "invalid standard port - file possibly corrupt") ) ) ) ) ((hash-table-tag) (set! items (add1 items)) (out #t "hash table:") (fluid-let ((indent (+ 2 indent))) (fetchh "(items)") ) (fluid-let ((indent (+ 2 indent))) (fetchh "(comparison procedure)") ) (fluid-let ((indent (+ 2 indent))) (fetchh "(hash function)") )) (else (call/cc (lambda (return) (set! items (add1 items)) (let* ((h (read-string +sizeof-header+ port)) (x (bytes_to_block h))) (##core#inline "set_header" x h) (select tag ((bytevector-tag) (##core#inline "insert_bytes" x (read-string (##sys#size x) port)) (cond ((string? x) (out #t "string ~s" x)) ((number? x) (out #t "float ~s" x)) ((##core#inline "C_lambdainfop" x) (out #t "lambda info ~a" x) ) (else (out #t "byte-vector ~s" (byte-vector->u8vector x)) ) ) ) ((vector-tag) (let ((off 0)) (cond ((vector? x) (out #t "vector")) ((pair? x) (out #t "pair:") (fluid-let ((indent (add1 indent))) (fetchh "(car)") (fetchh "(cdr)") ) (return #f) ) ((##sys#generic-structure? x) (out #t "structure:") (fetchh "(tag)") (set! off 1) ) (else (out #t "unknown vector-like object:")) ) (fetchslots x off))) ((special-tag) (out #t "(invalid) special vector-like object:") (##core#inline "insert_bytes" x (read-string +sizeof-word+ port)) (fetchslots x 1) ) ((procedure-tag) (out #t "procedure:") (fluid-let ((indent (add1 indent))) (fetchh "(id)") (fetchslots x 1) ) ) (else (out #t "invalid tag ~a - file possibly corrupt" tag)) ) ) ) ) ) ) ) ) (let loop () (unless (eof-object? (peek-char port)) (printf "~a:~%" (file-position port)) (fetch) (newline) (loop))))) (define (version) (printf "chicken-dump ~a~%" +version+) (exit 0) ) (define (usage) (printf "usage: chicken-dump [FILE | OPTION ...] -v -version show version and exit -h -help show this message and exit~%") (exit 0) ) (define *dumped* #f) (define (dump-file fn) (printf "~%File: ~a~%~%" fn) (set! *dumped* #t) (call-with-input-file fn dump) ) (let loop ((args (command-line-arguments))) (match args (() (unless *dumped* (usage))) (((or "-v" "-version") . _) (version)) (((or "-h" "-help" "--help") . _) (usage)) ((file . more) (dump-file file) (loop more)) ) ) #|-------------------- 0.9.3 |# "./constants.scm" 1066 ;;;; constants.scm (define-constant +version+ 0.7) (define-constant void-tag 0) (define-constant null-tag 1) (define-constant eof-tag 2) (define-constant fixnum-tag 3) (define-constant bytevector-tag 4) (define-constant vector-tag 5) (define-constant special-tag 6) (define-constant true-tag 7) (define-constant false-tag 8) (define-constant char-tag 9) (define-constant backref-tag 10) (define-constant gensym-tag 11) (define-constant procedure-tag 12) (define-constant symbol-tag 13) (define-constant stdport-tag 14) (define-constant hash-table-tag 15) (define-constant big-endian-tag 16) (define-constant little-endian-tag 17) (define-constant thirtytwo-bit-tag 18) (define-constant sixtyfour-bit-tag 19) (define +sizeof-ulong+ (foreign-value "sizeof(unsigned long)" int)) (define +sizeof-word+ (foreign-value "sizeof(C_word)" int)) (define +sizeof-header+ (foreign-value "sizeof(C_header)" int)) #> static int check_endianness() { int i = 99; return *((char *)&i) != 99 ? 16 : 17; } <# (define +endianness-tag+ (foreign-value "check_endianness()" int)) #|-------------------- 0.9.3 |# "./s11n-c.c" 1962 /* s11n-c.c */ static C_word fixnum_to_bytes(C_word c, C_word self, C_word k, C_word x) { C_word *a = C_alloc(C_SIZEOF_STRING(sizeof(C_word))); C_kontinue(k, C_string(&a, sizeof(C_word), (C_char *)&x)); } static C_word word_to_bytes(C_word c, C_word self, C_word k, C_word x) { C_word *a = C_alloc(C_SIZEOF_STRING(sizeof(unsigned long))); unsigned long n = C_num_to_unsigned_long(x); C_kontinue(k, C_string(&a, sizeof(unsigned long), (C_char *)&n)); } static C_word header_to_bytes(C_word c, C_word self, C_word k, C_word x) { C_word *a = C_alloc(C_SIZEOF_STRING(sizeof(C_header))); C_kontinue(k, C_string(&a, sizeof(C_header), (C_char *)x)); } static void bytes_to_block(C_word c, C_word self, C_word k, C_word str) { C_header h = *((C_header *)C_data_pointer(str)); int size = h & C_HEADER_SIZE_MASK; C_allocate_vector(6, C_SCHEME_UNDEFINED, k, C_fix(size), C_mk_bool((h & C_BYTEBLOCK_BIT) != 0), C_SCHEME_UNDEFINED, C_mk_bool((h & C_8ALIGN_BIT) != 0)); } static ___scheme_value bytes_to_size(C_word str) { C_header h = *((C_header *)C_data_pointer(str)); return C_fix(h & C_HEADER_SIZE_MASK); } static ___scheme_value bytes_to_word(___scheme_value str) { return C_fix(*((unsigned long *)C_data_pointer(str))); } static ___scheme_value insert_bytes(___scheme_value x, ___scheme_value str) { C_memcpy(C_data_pointer(x), C_data_pointer(str), C_header_size(str)); return C_SCHEME_UNDEFINED; } static ___scheme_value set_procedure_ptr(___scheme_value x, ___scheme_value pid) { void *ptr = C_lookup_procedure_ptr(C_c_string(pid)); if(ptr != NULL) { C_block_item(x, 0) = (C_word)ptr; return C_SCHEME_TRUE; } else return C_SCHEME_FALSE; } static ___scheme_value bytes_to_fixnum(___scheme_value str) { return *((C_word *)C_data_pointer(str)); } static ___scheme_value set_header(___scheme_value x, ___scheme_value str) { C_block_header(x) = *((C_header *)C_data_pointer(str)); return x; } #|-------------------- 0.9.3 |# "./s11n.html" 9003 Eggs Unlimited - s11n

Description

Serialization and deserialization of arbitrary objects

Author

felix

Version

Usage

(require-extension s11n)

Download

s11n.egg

Documentation

This extension allows serializing and deserializing arbitrary data (including procedures and continuations) into/from ports. Circular data is support as well as uninterned symbols. Foreign-pointer objects and ports other than the default input-, output- and error-ports can not be serialized. Threads may be serialized provided they are not running or ready (this means suspended or created but not yet started threads).

The serialized data is endianness- and word-size dependent.

To enable serialization of procedures and continuations, CHICKEN has to be built with the --enable-procedure-tables configuration option. In particular, any compiled module that is referenced in serialized data has to be compiled with a CHICKEN version that has procedure tables enabled.

Compiled procedures and continuations can be deserialized, provided that the same executable or libraries are loaded/linked - the compiled code must be available so that the deserialization process can find the associated code.

Continuations can be serialized but care has to be taken about what exactly is stored in such an object. Every invocation of call-with-current-continuation includes the complete list of pending dynamic-wind thunks (and their dynamic environment) which is likely to include non-serializable state.

Since the deserialization process breaks any assumptions about a unique identity of objects, TinyCLOS instances and classes can currently not be deserialized.

CHICKEN version 2.207 or higher is required to use this extension.

procedure: (serialize X [PORT [SERIALIZER]])

Writes a binary representation of X into PORT which defaults to the value of (current-output-port). If the serialization is unable to handle some object, the procedure SERIALIZER is invoked with that object as it's sole argument. The procedure should either signal an error or return a placeholder object that is to be serialized instead. If no serializer is specified, an error will be signalled.

procedure: (deserialize [PORT [DESERIALIZER]])

Reads a binary representation of a Scheme data object from PORT which defaults to the value of (current-input-port). The deserialized object is returned.

If DESERIALIZER is given, then it will be called for procedure objects that can not be deserialized (normally because a serialized file was generated by a different version of CHICKEN) with the procedure-id (internal name) and the deserialized closure object as argument.

A separate tool is also available, named chicken-dump that writes a description about the contents of a serialized file. Just invoke

chicken-dump FILENAME

on the command prompt.

License

Copyright (c) 2005, Felix Winkelmann.  All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the Software),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ASIS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
#|-------------------- 0.9.3 |# "./s11n.meta" 291 ;;; s11n.meta -*- Scheme -*- ((egg "s11n.egg") (synopsis "Serialization of arbitrary data.") (category parsing) (license "BSD") (author "felix") (depends matchable) (files "constants.scm" "s11n.meta" "s11n-c.c" "s11n.html" "s11n.setup" "s11n.scm" "tests/run.scm" "chicken-dump.scm")) #|-------------------- 0.9.3 |# "./s11n.scm" 7319 ;;;; s11n.scm (declare (fixnum)) (module s11n (serialize deserialize serialization-mode) (import scheme chicken foreign) (use srfi-69 extras) (include "constants.scm") #> #include "s11n-c.c" <# (define procedure_to_id (foreign-lambda* c-string ((scheme-object p)) "return(C_lookup_procedure_id((void *)C_block_item(p, 0)));") ) (define fixnum_to_bytes (##core#primitive "fixnum_to_bytes")) (define word_to_bytes (##core#primitive "word_to_bytes")) (define header_to_bytes (##core#primitive "header_to_bytes")) (define serialization-mode (make-parameter 'host)) (define (serialize x #!optional (port (current-output-port)) serializer) (let ((table (make-hash-table eq?)) (pos 0) ) (define (emitb b) (write-char (integer->char b) port) ) (define (emit s) (display s port) ) (define (emitw w) (display (word_to_bytes w) port) ) (define (walkslots x i) (let ((len (##sys#size x))) (do ((i i (add1 i))) ((>= i len)) (walk (##sys#slot x i)) ) ) ) (define (emitbytes x) (let ((len (##sys#size x))) (do ((i 0 (add1 i))) ((>= i len)) (emitb (##sys#byte x i)) ) ) ) (define (fail msg x) (if serializer (walk (serializer x)) (error 'serialize (string-append "unable to serialize object - " msg) x))) (define (walk x) #+debug (fprintf (current-error-port) "Emitting: ~a" (##sys#with-print-length-limit 80 (lambda () (with-output-to-string (cut pp x))))) (cond ((fixnum? x) (emitb fixnum-tag) (emit (fixnum_to_bytes x)) ) ((char? x) (emitb char-tag) (emitw (char->integer x)) ) ((##sys#immediate? x) (select x (((##core#undefined)) (emitb void-tag)) ((#t) (emitb true-tag)) ((#f) (emitb false-tag)) ((#!eof) (emitb eof-tag)) (('()) (emitb null-tag)) (else (fail "can not serialize immediate value" x))) ) ((hash-table-ref/default table x #f) => (lambda (pos) (emitb backref-tag) (emitw pos) ) ) (else (hash-table-set! table x pos) (set! pos (add1 pos)) (cond ((procedure? x) (emitb procedure-tag) (emit (header_to_bytes x)) (let ((id (procedure_to_id x))) (cond ((not id) (set! pos (sub1 pos)) (fail "unable to serialize procedure (no table entry found)" x) ) (else (walk id) (walkslots x 1) ) ) ) ) ((##core#inline "C_specialp" x) (cond ((port? x) (let ((name (##sys#slot x 3))) (cond ((string=? "(stdin)" name) (emitb stdport-tag) (emitb 0) ) ((string=? "(stdout)" name) (emitb stdport-tag) (emitb 1) ) ((string=? "(stderr)" name) (emitb stdport-tag) (emitb 2) ) ((not (zero? (##sys#pointer->address x))) (fail "can not serialize stream port" x) ) (else (emitb vector-tag) (emit (header_to_bytes x)) (emitw 0) (walkslots x 1) ) ) ) ) (else (fail "can not serialize pointer-like object" x) #| (emitb special-tag) (emit (header_to_bytes x)) (walkslots x 1) |#) ) ) ((##core#inline "C_byteblockp" x) (emitb bytevector-tag) (emit (header_to_bytes x)) (emitbytes x) ) ((symbol? x) (emitb (if (not (##core#inline "C_lookup_symbol" x)) gensym-tag symbol-tag) ) (walk (##sys#slot x 1)) ) ((hash-table? x) (emitb hash-table-tag) (walk (hash-table->alist x)) (walkslots x 3) ) (else (emitb vector-tag) (emit (header_to_bytes x)) (walkslots x 0) ) ) ) ) ) (when (eq? (serialization-mode) 'portable) (emitb (if (##sys#fudge 3) sixtyfour-bit-tag thirtytwo-bit-tag)) (emitb +endianness-tag+) ) (walk x) ) ) (define bytes_to_block (##core#primitive "bytes_to_block")) (define stdin-port ##sys#standard-input) (define stdout-port ##sys#standard-output) (define stderr-port ##sys#standard-error) (define (deserialize #!optional (port (current-input-port)) fallback (safe #t)) (let ((backrefs (make-vector 100)) (backref-count 0) ) (define (getb) (let ((c (read-char port))) (if (eof-object? c) (error 'deserialize "unexpected end of input" port) (char->integer c)))) (define (getw) (##core#inline "bytes_to_word" (read-string +sizeof-ulong+ port)) ) (define (addref x) (when (>= backref-count (vector-length backrefs)) (set! backrefs (vector-resize backrefs (* 2 backref-count))) ) (vector-set! backrefs backref-count x) (set! backref-count (add1 backref-count)) x) (define (fetchslots x i) (let ((len (##sys#size x))) (do ((i i (add1 i))) ((>= i len) x) (##sys#setslot x i (fetch)) ) ) ) (define (fetch) (let ((tag (getb))) (select tag ((sixtyfour-bit-tag) (fetch)) ((thirtytwo-bit-tag) (fetch)) ((big-endian-tag) (fetch)) ((little-endian-tag) (fetch)) ((void-tag) (void)) ((null-tag) '()) ((eof-tag) #!eof) ((true-tag) #t) ((false-tag) #f) ((backref-tag) (let ((w (getw))) (vector-ref backrefs w)) ) ((fixnum-tag) (##core#inline "bytes_to_fixnum" (read-string +sizeof-word+ port)) ) ((char-tag) (integer->char (getw)) ) ((gensym-tag) (addref #f) (let* ((r (sub1 backref-count)) (x (##sys#make-symbol (fetch))) ) (vector-set! backrefs r x) x) ) ((symbol-tag) (addref #f) (let* ((r (sub1 backref-count)) (x (##sys#intern-symbol (fetch)) )) (vector-set! backrefs r x) x) ) ((stdport-tag) (addref #f) (let* ((n (getb)) (r (sub1 backref-count)) (p (case n ((0) stdin-port) ((1) stdout-port) ((2) stderr-port) (else (error 'deserialize "invalid standard-port number" n)) ) ) ) (vector-set! backrefs r p) p) ) ((hash-table-tag) (addref #f) (let ((r (sub1 backref-count)) (ht (##sys#make-structure 'hash-table #f 0 #f #f)) ) (vector-set! backrefs r ht) (let* ((table (fetch)) (test (fetch)) (hashf (fetch)) (ht2 (alist->hash-table table)) ) (##sys#setslot ht 1 (##sys#slot ht2 1)) (##sys#setslot ht 2 (##sys#slot ht2 2)) (##sys#setslot ht 3 test) (##sys#setslot ht 4 hashf) ht) ) ) (else (let* ((h (read-string +sizeof-header+ port)) (x (addref (bytes_to_block h))) (r (sub1 backref-count)) ) (##core#inline "set_header" x h) (select tag ((bytevector-tag) (##core#inline "insert_bytes" x (read-string (##sys#size x) port)) x) ((vector-tag) (fetchslots x 0)) #;((special-tag) (when safe (error 'deserialize "unable to deserialize pointer-like object" x) ) (##core#inline "insert_bytes" x (read-string +sizeof-word+ port)) (fetchslots x 1) ) ((procedure-tag) (let ((id (fetch))) (cond ((##core#inline "set_procedure_ptr" x (##sys#make-c-string id)) (fetchslots x 1) ) (fallback (let ((proc (fallback id (fetchslots x 1)))) (vector-set! backrefs r proc) proc) ) (else (error 'deserialize "unable to deserialize procedure - no table entry found" id))) ) ) (else (error 'deserialize "invalid tag" tag)) ) ) ) ) ) ) (fetch) ) ) ) #|-------------------- 0.9.3 |# "./s11n.setup" 264 (compile s11n.scm -O2 -d1 -s -j s11n) (compile s11n.import.scm -O2 -d0 -s) (compile chicken-dump.scm -O2 -d1 -disable-interrupts -b) (install-extension 's11n `("s11n.import.so" "s11n.so") `((version "0.9.3"))) (install-program 'chicken-dump "chicken-dump")