Low-Level Byte-Ops with Common Lisp

Discussion of Common Lisp
Post Reply
schoppenhauer
Posts: 99
Joined: Sat Jul 26, 2008 2:30 pm
Location: Germany
Contact:

Low-Level Byte-Ops with Common Lisp

Post by schoppenhauer » Sat Sep 12, 2009 8:36 pm

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?
Sorry for my bad english.
Visit my blog http://blog.uxul.de/

ramarren
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland
Contact:

Re: Low-Level Byte-Ops with Common Lisp

Post by ramarren » Sat Sep 12, 2009 11:48 pm

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.

schoppenhauer
Posts: 99
Joined: Sat Jul 26, 2008 2:30 pm
Location: Germany
Contact:

Re: Low-Level Byte-Ops with Common Lisp

Post by schoppenhauer » Sun Sep 13, 2009 4:04 am

Thank you. This looks exactly like what I wanted to have.
Sorry for my bad english.
Visit my blog http://blog.uxul.de/

dmitry_vk
Posts: 96
Joined: Sat Jun 28, 2008 8:01 am
Location: Russia, Kazan
Contact:

Re: Low-Level Byte-Ops with Common Lisp

Post by dmitry_vk » Sun Sep 13, 2009 5:20 am

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.

Post Reply