Skip to content

Commit ba9208f

Browse files
committed
Refactor magic string handling and data parsing
- Use defined constants for magic strings. - Simplify data parsing with helper macros.
1 parent 70111f7 commit ba9208f

2 files changed

Lines changed: 38 additions & 44 deletions

File tree

bfc_format.c

Lines changed: 18 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ bfc_read_header(struct bfcfs_mount *bmp, struct thread *td)
3838
}
3939

4040
/* Validate magic string */
41-
if (strncmp(hdr->magic, "BFCFv1", 6) != 0) {
42-
BFCFS_ERR("invalid magic: expected 'BFCFv1', got '%.8s'",
43-
hdr->magic);
41+
if (strncmp(hdr->magic, BFC_MAGIC_STR, strlen(BFC_MAGIC_STR)) != 0) {
42+
BFCFS_ERR("invalid magic: expected '%s', got '%.8s'",
43+
BFC_MAGIC_STR, hdr->magic);
4444
return (EINVAL);
4545
}
4646

@@ -91,7 +91,7 @@ bfc_read_footer(struct bfcfs_mount *bmp, struct thread *td)
9191
}
9292

9393
/* Validate footer magic */
94-
if (strncmp(footer->magic_start, "BFCFIDX", 7) != 0) {
94+
if (strncmp(footer->magic_start, BFC_INDEX_MAGIC, strlen(BFC_INDEX_MAGIC)) != 0) {
9595
BFCFS_ERR("invalid footer magic");
9696
return (EINVAL);
9797
}
@@ -149,9 +149,7 @@ bfc_parse_index_entry(const uint8_t *buf, size_t buflen, size_t *offset,
149149
return (EINVAL);
150150

151151
/* Read path length (32-bit) */
152-
bcopy(p, &path_len, 4);
153-
path_len = LE32(path_len);
154-
p += 4;
152+
READ_LE32(p, path_len);
155153
remaining -= 4;
156154

157155
/* Sanity check path length */
@@ -170,37 +168,14 @@ bfc_parse_index_entry(const uint8_t *buf, size_t buflen, size_t *offset,
170168
return (EINVAL);
171169

172170
/* Parse metadata fields (all little-endian) */
173-
bcopy(p, &entry->obj_offset, 8);
174-
entry->obj_offset = LE64(entry->obj_offset);
175-
p += 8;
176-
177-
bcopy(p, &entry->obj_size, 8);
178-
entry->obj_size = LE64(entry->obj_size);
179-
p += 8;
180-
181-
bcopy(p, &entry->mode, 4);
182-
entry->mode = LE32(entry->mode);
183-
p += 4;
184-
185-
bcopy(p, &entry->mtime_ns, 8);
186-
entry->mtime_ns = LE64(entry->mtime_ns);
187-
p += 8;
188-
189-
bcopy(p, &entry->comp, 4);
190-
entry->comp = LE32(entry->comp);
191-
p += 4;
192-
193-
bcopy(p, &entry->enc, 4);
194-
entry->enc = LE32(entry->enc);
195-
p += 4;
196-
197-
bcopy(p, &entry->size, 8); /* orig_size */
198-
entry->size = LE64(entry->size);
199-
p += 8;
200-
201-
bcopy(p, &entry->crc32c, 4);
202-
entry->crc32c = LE32(entry->crc32c);
203-
p += 4;
171+
READ_LE64(p, entry->obj_offset);
172+
READ_LE64(p, entry->obj_size);
173+
READ_LE32(p, entry->mode);
174+
READ_LE64(p, entry->mtime_ns);
175+
READ_LE32(p, entry->comp);
176+
READ_LE32(p, entry->enc);
177+
READ_LE64(p, entry->size); /* orig_size */
178+
READ_LE32(p, entry->crc32c);
204179

205180
entry->ino = ino;
206181

@@ -240,17 +215,17 @@ bfc_read_index(struct bfcfs_mount *bmp, struct thread *td)
240215
}
241216

242217
/* Parse index header (8 bytes: version + count) */
243-
if (bmp->footer.index_size < 8) {
218+
if (bmp->footer.index_size < BFC_INDEX_HEADER_SIZE) {
244219
BFCFS_ERR("index too small for header");
245220
free(index_buf, M_BFCFS);
246221
return (EINVAL);
247222
}
248223

224+
/* Parse index header (version and count) */
249225
uint32_t version, count;
250-
bcopy(index_buf, &version, 4);
251-
version = LE32(version);
252-
bcopy(index_buf + 4, &count, 4);
253-
count = LE32(count);
226+
uint8_t *p = index_buf;
227+
READ_LE32(p, version);
228+
READ_LE32(p, count);
254229

255230
if (version != 1) {
256231
BFCFS_ERR("unsupported index version: %u", version);

bfcfs.h

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,19 @@
3131
#define BFC_FOOTER_SIZE 56
3232
#define BFC_ALIGN 16
3333

34+
/* Magic strings */
35+
#define BFC_MAGIC_STR "BFCFv1"
36+
#define BFC_INDEX_MAGIC "BFCFIDX"
37+
#define BFC_INDEX_END "BFCFEND"
38+
3439
/* Sanity limits for container validation */
3540
#define BFC_MAX_BLOCK_SIZE (1024 * 1024) /* 1MB max block size */
3641
#define BFC_MAX_INDEX_SIZE (100 * 1024 * 1024) /* 100MB max index */
3742
#define BFC_MAX_ENTRIES 1000000 /* 1M max entries */
3843
#define BFC_MAX_PATH_LEN 4096 /* Maximum path length */
3944

40-
/* BFC index entry format constants */
45+
/* BFC index format constants */
46+
#define BFC_INDEX_HEADER_SIZE 8 /* Version (4) + count (4) */
4147
#define BFC_INDEX_ENTRY_METADATA_SIZE 48 /* Size of fixed metadata fields */
4248

4349
/* Time conversion constants */
@@ -50,6 +56,19 @@
5056
#define BFC_ALIGN_TO_BLOCK(size) (((size) + (BFC_BLOCK_ROUND - 1)) & ~(BFC_BLOCK_ROUND - 1))
5157
#define BFC_ALIGN_UP(size, align) (((size) + ((align) - 1)) & ~((align) - 1))
5258

59+
/* Helper macros for reading little-endian fields */
60+
#define READ_LE32(ptr, field) do { \
61+
bcopy(ptr, &(field), 4); \
62+
(field) = LE32(field); \
63+
(ptr) += 4; \
64+
} while (0)
65+
66+
#define READ_LE64(ptr, field) do { \
67+
bcopy(ptr, &(field), 8); \
68+
(field) = LE64(field); \
69+
(ptr) += 8; \
70+
} while (0)
71+
5372
/* VFS constants */
5473
#define BFC_DEFAULT_IO_SIZE 65536 /* 64KB optimal I/O size */
5574
#define BFC_ROOT_MODE 0755 /* Synthetic root directory mode */

0 commit comments

Comments
 (0)