Skip to content

Commit

Permalink
Bugfix: I accidentally swapped 'file' and 'frame' format around.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sewer56 committed Dec 13, 2024
1 parent 0667a85 commit a05c0d4
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions doc/bzip3_format.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ block count field.
```
+----------------+------------------+--------------------+
| Header | Chunk 1 | Chunk 2 |
| (13 bytes) | (variable size) | (variable size) |
| (9 bytes) | (variable size) | (variable size) |
+----------------+------------------+--------------------+
```

Expand All @@ -23,7 +23,7 @@ This is created by the CLI tool.
```
+----------------+------------------+--------------------+
| Header | Chunk 1 | Chunk 2 |
| (9 bytes) | (variable size) | (variable size) |
| (13 bytes) | (variable size) | (variable size) |
+----------------+------------------+--------------------+
```

Expand All @@ -35,28 +35,29 @@ This is created/read by `bz3_compress` and `bz3_decompress`.
| -------------- | ------ | ------------------------------- | ----------- | ------------ |
| Signature | u8[5] | Fixed "BZ3v1" ASCII string |||
| Max Block Size | u32_le | Maximum decompressed block size |||
| Block Count | u32_le | Number of blocks in the stream | | |
| Block Count | u32_le | Number of blocks in the stream | | |

### Validation Rules

1. **Signature**: Must exactly match "BZ3v1"
2. **Max Block Size**:
- Minimum: 65KiB (66,560 bytes)
- Maximum: 511MiB (535,822,336 bytes)
3. **Block Count** (File Format only):
3. **Block Count** (Frame Format only):
- Must match the actual number of blocks in the stream
- Should be greater than 0

### Example Parser

For the file header.

```c
typedef struct {
uint32_t max_block_size;
uint32_t block_count; // File Format only
bool is_file_format;
uint32_t block_count; // Frame Format only
} bzip3_header_t;

bool read_bzip3_header(FILE* fp, bzip3_header_t* header) {
bool read_bzip3_header(FILE* fp, bzip3_header_t* header, bool is_frame_format) {
char signature[6] = {0};

// Read signature
Expand All @@ -77,8 +78,8 @@ bool read_bzip3_header(FILE* fp, bzip3_header_t* header) {
header->max_block_size > 535822336)
return false;

// Read block count if File Format
if (header->is_file_format) {
// Read block count if Frame Format
if (is_frame_format) {
uint8_t count_bytes[4];
if (fread(count_bytes, 1, 4, fp) != 4)
return false;
Expand Down

0 comments on commit a05c0d4

Please sign in to comment.