Quick note: I didn't figure out most of the format of SB files. Trinitro21 did most of the work figuring out the file structure, and the footer was thanks to code plutooo had for smilehax. I just wrote this document, which sums everything we know.
DISCLAIMER: This is not 100% correct and may change at any time.
By the way, me and Trinitro21 (triangle) wrote an API to download programs from the SmileBASIC servers. It can also display GRPs as PNGs. It's available at http://sbapi.me/
SmileBASIC has its own format for storing its files. There are 2 main types of files, TXT and DAT. Both have a common header and a footer, but DAT files have a secondary header after the common header to store metadata like how many dimensions it has, how large each dimension is, and what type of data it stores.
All files are stored in SmileBASIC's ExtData archive stored on the SD card. The default folder is stored as ### in the ExtData, and filenames are prefixed with T or B, for Text (TXT) and Binary (DAT), respectively.
Common Header
The shared part of every SB file is the common header. This contains information such as the username of who wrote it, how large the data stored is, and when it was last modified. The common header is 80 bytes long on the 3DS and 112 bytes on Switch. All values are little-endian.
Offset | Size (bytes) | Description
-----------------------------------
&H00 | 2 | Version of SmileBASIC the file was created under (00 or 01 = 3DS/WiiU, 04 = Switch)
&H02 | 1 | File type (00 = TXT, 01 = DAT. Switch: 02 = GRP, 04 = META)
&H04 | 1 | Zlib compression (0 = no, 1 = yes)
&H06 | 1 | Icon shown in the project browser. For TXT files, 00 = TXT and 01 = PRG. For DAT, 00 = DAT and 02 = GRP
&H08 | 4 | 32-bit value storing the size of the file, not including the common header or the footer
&H0C | 2 | 16-bit value storing the year the file was last modified
&H0E | 1 | 8-bit value storing the month the file was last modified
&H0F | 1 | 8-bit value storing the day of the month the file was last modified
&H10 | 1 | 8-bit value storing the hour the file was last modified
&H11 | 1 | 8-bit value storing the minute the file was last modified
&H12 | 1 | 8-bit value storing the second the file was last modified
&H13 | 1 | Unknown, might be part of date/time
A very important difference to note is that in SB4, uploader information is slightly longer.
Offsets for 3DS:
&H14 | 18 | The first author's (the original uploader) NNID. This one isn't shown in the project browser
&H26 | 18 | The second author's (the last editor) NNID. This one is displayed in the project browser
&H38 | 4 | The first author's user ID. Used for controlling the blacklist editable in the project download area
&H3C | 4 | The second author's user ID
Offsets for Switch:
&H14 | 32 | The first author's (the original uploader) NNID. This one isn't shown in the project browser
&H34 | 32 | The second author's (the last editor) NNID. This one is displayed in the project browser
&H54 | 4 | The first author's user ID. Used for controlling the blacklist editable in the project download area
&H58 | 4 | The second author's user ID
The header ends at &H50 on 3DS and &H70 on Switch.
DAT Secondary Header (Petit Computer BiNary)
TXT and PRG files just place the text after the footer. The DAT and GRP files need more information, which is why they have a secondary header. This secondary header stores information for SB to parse the file properly.
(offset is the offset after the header, which changes depending on the version of SmileBASIC in use.)
Offset | Size (bytes) | Description
-----------------------------------
&H00 | 8 | Always the ASCII string "PCBN000n", where n is the device type (offset &H00)
&H08 | 1 | Data type (03 = 16 bit unsigned (colors as RGBA5551, used for GRPs), 04 = Signed 32 bit integers (int%), 05 = 64-bit double (real#)). GRPs in SB4 are stored as integer (data type &H04) DAT files since they use RGBA8888 encoding.
&H0A | 1 | Number of dimensions (1-4)
&H0C | 4 | 32-bit value storing the size of the first dimension
&H10 | 4 | 32-bit value storing the size of the second dimension if applicable
&H14 | 4 | 32-bit value storing the size of the third dimension if applicable
&H18 | 4 | 32-bit value storing the size of the fourth dimension if applicable
Afterward, the data is stored in row-major order (
https://en.wikipedia.org/wiki/Row-_and_column-major_order).
META Project file (Petit Computer Project Metadata
META files are used in projects to store metadata about a project, including icon, name, and description.
(offset is the offset after the header, which changes depending on the version of SmileBASIC in use.)
Offset | Size (bytes) | Description
-----------------------------------
&H00 | 8 | Always the ASCII string "PCPM0005"
&H08 | 48 | The project name (UCS-2)
&H38 | 4576 | The project description (UCS-2)
&H1218 | 4 | The width of the project icon
The data following is the icon's pixel data, encoded in BGRA8888. Icons are always square, so you can find the length of the icon data by squaring the icon width and multiplying by 4.
Projects
File type 2 in SB3 and file type 3 on SB4 are reserved for project files that are used for uploading/downloading projects from the server. They are unpacked by SB when downloaded into a proper file structure.
As such, this file format should never be encountered unless you're talking to the SB servers yourself. If you're doing that, figuring out project file format is left as an exercise for you :)
Footer
The footer is a 20 byte HMAC-SHA1 hash of the header and data using this HMAC key:
nqmby+e9S?{%U*-V]51n%^xZMk8>b{?x]&?(NmmV[,g85:%6Sqd"'U")/8u77UL2
The footer must be valid in order to download or upload a program/project, not having a valid footer will cause an error when doing either of these.