Page 1 of 1

Low-Level Byte-Ops with Common Lisp

Posted: Sat Sep 12, 2009 8:36 pm
by schoppenhauer
Many protocols like FastCGI, Socks and VNC use Little/Big-Endian-Stuff and low-level byte-operations. Often, there are numbers to be read directly from a byte-sequence, etc.

In C, you just read that stuff into a struct and then mostly have the proper contents in it.

Common Lisp has unsigned-byte and a few other types, but to me its rather complicated to keep track of this (and seems like some operations are not very efficient). What is the best way to handle such data in Common Lisp?

Re: Low-Level Byte-Ops with Common Lisp

Posted: Sat Sep 12, 2009 11:48 pm
by ramarren
schoppenhauer wrote:In C, you just read that stuff into a struct and then mostly have the proper contents in it.

Common Lisp has unsigned-byte and a few other types, but to me its rather complicated to keep track of this (and seems like some operations are not very efficient). What is the best way to handle such data in Common Lisp?
C is a weakly typed language, and CL strongly typed, so you can't do that directly. On the other hand, octet operations can be implemented efficiently, and in SBCL they usually are. You may want binary-types library, and possibly ieee-floats if you are dealing with floating numbers.

Re: Low-Level Byte-Ops with Common Lisp

Posted: Sun Sep 13, 2009 4:04 am
by schoppenhauer
Thank you. This looks exactly like what I wanted to have.

Re: Low-Level Byte-Ops with Common Lisp

Posted: Sun Sep 13, 2009 5:20 am
by dmitry_vk
If the data that you want to process lies in foreign memory (e.g., comes from a C library or you can mmap the file with the data) then you can use CFFI to access the memory in C-like way: you define structures (with all semantics of C: data types, member alignments, etc) and then access the memory with these structures.