ashinn / chibi-scheme

Official chibi-scheme repository
Other
1.21k stars 142 forks source link

Image endianness #917

Closed lassik closed 3 months ago

lassik commented 1 year ago

How are byte order differences handled when loading images?

#define SEXP_IMAGE_MAJOR_VERSION 1
#define SEXP_IMAGE_MINOR_VERSION 1

struct sexp_image_header_t {
  char magic[8];
  short major, minor;
  // ...
};

It seems that the major and minor version are each meant to be restricted to one byte (1..255), and storing the number in a two-byte field (C short) will cause the reader to fail if the writer used a different endianness. Is this correct?

lassik commented 1 year ago

Here's some Unix file(1) magic:


# Chibi-Scheme
0   string  \a\achibi\n                     Chibi-Scheme memory image data
>8  ubyte   >0                              \b, big-endian
>>8     ubyte   x                               \b, version %d.
>>10    ubyte   x                               \b%d
>8  ubyte   =0                              \b, little-endian
>>9     ubyte   x                               \b, version %d.
>>11    ubyte   x                               \b%d,
>12     string  x                               \b, ABI %s

Sample output:

chibi-srfi.img: Chibi-Scheme memory image data, little-endian, version 1.1, ABI cv-gm*uh
ashinn commented 3 months ago

Yes, there's not an explicit endian flag in the header but the version won't match on the wrong endianness.

lassik commented 3 months ago

https://github.com/file/file/commit/8aeeecc8fc143b19bdf091b66d9f909ec452e7fd