Branch data Line data Source code
1 : : /**
2 : : * @file header.c
3 : : * @brief Header parsing and writing for the .trp binary format.
4 : : *
5 : : * Copyright (c) 2026 M. A. Chatterjee <deftio at deftio dot com>
6 : : * BSD-2-Clause — see LICENSE.txt
7 : : */
8 : :
9 : : #include "core_internal.h"
10 : :
11 : 211 : tp_result tp_header_write(tp_bitstream_writer *w, const tp_header *h)
12 : : {
13 [ + + + + ]: 211 : if (!w || !h)
14 : 2 : return TP_ERR_INVALID_PARAM;
15 : :
16 : : /* Allocation failure paths are excluded from coverage (LCOV_EXCL). */
17 : : tp_result rc;
18 : : /* Magic bytes (4) */
19 : 209 : rc = tp_bs_write_u8(w, h->magic[0]);
20 [ - + ]: 209 : if (rc != TP_OK)
21 : : return rc; /* LCOV_EXCL_LINE */
22 : 209 : rc = tp_bs_write_u8(w, h->magic[1]);
23 [ - + ]: 209 : if (rc != TP_OK)
24 : : return rc; /* LCOV_EXCL_LINE */
25 : 209 : rc = tp_bs_write_u8(w, h->magic[2]);
26 [ - + ]: 209 : if (rc != TP_OK)
27 : : return rc; /* LCOV_EXCL_LINE */
28 : 209 : rc = tp_bs_write_u8(w, h->magic[3]);
29 [ - + ]: 209 : if (rc != TP_OK)
30 : : return rc; /* LCOV_EXCL_LINE */
31 : : /* Version (2) */
32 : 209 : rc = tp_bs_write_u8(w, h->version_major);
33 [ - + ]: 209 : if (rc != TP_OK)
34 : : return rc; /* LCOV_EXCL_LINE */
35 : 209 : rc = tp_bs_write_u8(w, h->version_minor);
36 [ - + ]: 209 : if (rc != TP_OK)
37 : : return rc; /* LCOV_EXCL_LINE */
38 : : /* Flags (2) */
39 : 209 : rc = tp_bs_write_u16(w, h->flags);
40 [ - + ]: 209 : if (rc != TP_OK)
41 : : return rc; /* LCOV_EXCL_LINE */
42 : : /* num_keys (4) */
43 : 209 : rc = tp_bs_write_u32(w, h->num_keys);
44 [ - + ]: 209 : if (rc != TP_OK)
45 : : return rc; /* LCOV_EXCL_LINE */
46 : : /* trie_data_offset (4) */
47 : 209 : rc = tp_bs_write_u32(w, h->trie_data_offset);
48 [ - + ]: 209 : if (rc != TP_OK)
49 : : return rc; /* LCOV_EXCL_LINE */
50 : : /* value_store_offset (4) */
51 : 209 : rc = tp_bs_write_u32(w, h->value_store_offset);
52 [ - + ]: 209 : if (rc != TP_OK)
53 : : return rc; /* LCOV_EXCL_LINE */
54 : : /* suffix_table_offset (4) */
55 : 209 : rc = tp_bs_write_u32(w, h->suffix_table_offset);
56 [ - + ]: 209 : if (rc != TP_OK)
57 : : return rc; /* LCOV_EXCL_LINE */
58 : : /* total_data_bits (4) */
59 : 209 : rc = tp_bs_write_u32(w, h->total_data_bits);
60 [ - + ]: 209 : if (rc != TP_OK)
61 : : return rc; /* LCOV_EXCL_LINE */
62 : : /* reserved (4) */
63 : 209 : rc = tp_bs_write_u32(w, h->reserved);
64 : 209 : return rc;
65 : : }
66 : :
67 : 641 : tp_result tp_header_read(tp_bitstream_reader *r, tp_header *h)
68 : : {
69 [ + + + + ]: 641 : if (!r || !h)
70 : 2 : return TP_ERR_INVALID_PARAM;
71 : :
72 : : tp_result rc;
73 : 639 : rc = tp_bs_read_u8(r, &h->magic[0]);
74 [ + + ]: 639 : if (rc != TP_OK)
75 : : return rc; /* LCOV_EXCL_LINE */
76 : 638 : rc = tp_bs_read_u8(r, &h->magic[1]);
77 [ + + ]: 638 : if (rc != TP_OK)
78 : : return rc; /* LCOV_EXCL_LINE */
79 : 637 : rc = tp_bs_read_u8(r, &h->magic[2]);
80 [ + + ]: 637 : if (rc != TP_OK)
81 : : return rc; /* LCOV_EXCL_LINE */
82 : 636 : rc = tp_bs_read_u8(r, &h->magic[3]);
83 [ + + ]: 636 : if (rc != TP_OK)
84 : : return rc; /* LCOV_EXCL_LINE */
85 : :
86 : : /* Validate magic */
87 [ + + + - : 635 : if (h->magic[0] != TP_MAGIC_0 || h->magic[1] != TP_MAGIC_1 || h->magic[2] != TP_MAGIC_2 ||
+ + ]
88 [ - + ]: 633 : h->magic[3] != TP_MAGIC_3)
89 : 2 : return TP_ERR_BAD_MAGIC;
90 : :
91 : 633 : rc = tp_bs_read_u8(r, &h->version_major);
92 [ + + ]: 633 : if (rc != TP_OK)
93 : : return rc; /* LCOV_EXCL_LINE */
94 : 632 : rc = tp_bs_read_u8(r, &h->version_minor);
95 [ + + ]: 632 : if (rc != TP_OK)
96 : : return rc; /* LCOV_EXCL_LINE */
97 : :
98 : : /* Validate version */
99 [ + + ]: 631 : if (h->version_major != TP_FORMAT_VERSION_MAJOR)
100 : 2 : return TP_ERR_VERSION;
101 : :
102 : 629 : rc = tp_bs_read_u16(r, &h->flags);
103 [ + + ]: 629 : if (rc != TP_OK)
104 : : return rc; /* LCOV_EXCL_LINE */
105 : 628 : rc = tp_bs_read_u32(r, &h->num_keys);
106 [ + + ]: 628 : if (rc != TP_OK)
107 : : return rc; /* LCOV_EXCL_LINE */
108 : 627 : rc = tp_bs_read_u32(r, &h->trie_data_offset);
109 [ + + ]: 627 : if (rc != TP_OK)
110 : : return rc; /* LCOV_EXCL_LINE */
111 : 626 : rc = tp_bs_read_u32(r, &h->value_store_offset);
112 [ + + ]: 626 : if (rc != TP_OK)
113 : : return rc; /* LCOV_EXCL_LINE */
114 : 625 : rc = tp_bs_read_u32(r, &h->suffix_table_offset);
115 [ + + ]: 625 : if (rc != TP_OK)
116 : : return rc; /* LCOV_EXCL_LINE */
117 : 624 : rc = tp_bs_read_u32(r, &h->total_data_bits);
118 [ + + ]: 624 : if (rc != TP_OK)
119 : : return rc; /* LCOV_EXCL_LINE */
120 : 623 : rc = tp_bs_read_u32(r, &h->reserved);
121 : 623 : return rc;
122 : : }
|