LCOV - code coverage report
Current view: top level - src/core - header.c (source / functions) Coverage Total Hit
Test: lcov.info Lines: 100.0 % 63 63
Test Date: 2026-09-17 22:59:44 Functions: 100.0 % 2 2
Branches: 100.0 % 42 42

             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                 :         275 : tp_result tp_header_write(tp_bitstream_writer *w, const tp_header *h)
      12                 :             : {
      13   [ +  +  +  + ]:         275 :     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                 :         273 :     rc = tp_bs_write_u8(w, h->magic[0]);
      20                 :         273 :     if (rc != TP_OK) /* LCOV_EXCL_BR_LINE */
      21                 :             :         return rc;   /* LCOV_EXCL_LINE */
      22                 :         273 :     rc = tp_bs_write_u8(w, h->magic[1]);
      23                 :         273 :     if (rc != TP_OK) /* LCOV_EXCL_BR_LINE */
      24                 :             :         return rc;   /* LCOV_EXCL_LINE */
      25                 :         273 :     rc = tp_bs_write_u8(w, h->magic[2]);
      26                 :         273 :     if (rc != TP_OK) /* LCOV_EXCL_BR_LINE */
      27                 :             :         return rc;   /* LCOV_EXCL_LINE */
      28                 :         273 :     rc = tp_bs_write_u8(w, h->magic[3]);
      29                 :         273 :     if (rc != TP_OK) /* LCOV_EXCL_BR_LINE */
      30                 :             :         return rc;   /* LCOV_EXCL_LINE */
      31                 :             :     /* Version (2) */
      32                 :         273 :     rc = tp_bs_write_u8(w, h->version_major);
      33                 :         273 :     if (rc != TP_OK) /* LCOV_EXCL_BR_LINE */
      34                 :             :         return rc;   /* LCOV_EXCL_LINE */
      35                 :         273 :     rc = tp_bs_write_u8(w, h->version_minor);
      36                 :         273 :     if (rc != TP_OK) /* LCOV_EXCL_BR_LINE */
      37                 :             :         return rc;   /* LCOV_EXCL_LINE */
      38                 :             :     /* Flags (2) */
      39                 :         273 :     rc = tp_bs_write_u16(w, h->flags);
      40                 :         273 :     if (rc != TP_OK) /* LCOV_EXCL_BR_LINE */
      41                 :             :         return rc;   /* LCOV_EXCL_LINE */
      42                 :             :     /* num_keys (4) */
      43                 :         273 :     rc = tp_bs_write_u32(w, h->num_keys);
      44                 :         273 :     if (rc != TP_OK) /* LCOV_EXCL_BR_LINE */
      45                 :             :         return rc;   /* LCOV_EXCL_LINE */
      46                 :             :     /* trie_data_offset (4) */
      47                 :         273 :     rc = tp_bs_write_u32(w, h->trie_data_offset);
      48                 :         273 :     if (rc != TP_OK) /* LCOV_EXCL_BR_LINE */
      49                 :             :         return rc;   /* LCOV_EXCL_LINE */
      50                 :             :     /* value_store_offset (4) */
      51                 :         273 :     rc = tp_bs_write_u32(w, h->value_store_offset);
      52                 :         273 :     if (rc != TP_OK) /* LCOV_EXCL_BR_LINE */
      53                 :             :         return rc;   /* LCOV_EXCL_LINE */
      54                 :             :     /* suffix_table_offset (4) */
      55                 :         273 :     rc = tp_bs_write_u32(w, h->suffix_table_offset);
      56                 :         273 :     if (rc != TP_OK) /* LCOV_EXCL_BR_LINE */
      57                 :             :         return rc;   /* LCOV_EXCL_LINE */
      58                 :             :     /* total_data_bits (4) */
      59                 :         273 :     rc = tp_bs_write_u32(w, h->total_data_bits);
      60                 :         273 :     if (rc != TP_OK) /* LCOV_EXCL_BR_LINE */
      61                 :             :         return rc;   /* LCOV_EXCL_LINE */
      62                 :             :     /* reserved (4) */
      63                 :         273 :     rc = tp_bs_write_u32(w, h->reserved);
      64                 :         273 :     return rc;
      65                 :             : }
      66                 :             : 
      67                 :        3469 : tp_result tp_header_read(tp_bitstream_reader *r, tp_header *h)
      68                 :             : {
      69   [ +  +  +  + ]:        3469 :     if (!r || !h)
      70                 :           2 :         return TP_ERR_INVALID_PARAM;
      71                 :             : 
      72                 :             :     tp_result rc;
      73                 :        3467 :     rc = tp_bs_read_u8(r, &h->magic[0]);
      74         [ +  + ]:        3467 :     if (rc != TP_OK)
      75                 :             :         return rc; /* LCOV_EXCL_LINE */
      76                 :        3466 :     rc = tp_bs_read_u8(r, &h->magic[1]);
      77         [ +  + ]:        3466 :     if (rc != TP_OK)
      78                 :             :         return rc; /* LCOV_EXCL_LINE */
      79                 :        3465 :     rc = tp_bs_read_u8(r, &h->magic[2]);
      80         [ +  + ]:        3465 :     if (rc != TP_OK)
      81                 :             :         return rc; /* LCOV_EXCL_LINE */
      82                 :        3464 :     rc = tp_bs_read_u8(r, &h->magic[3]);
      83         [ +  + ]:        3464 :     if (rc != TP_OK)
      84                 :             :         return rc; /* LCOV_EXCL_LINE */
      85                 :             : 
      86                 :             :     /* Validate magic */
      87   [ +  +  +  +  :        3463 :     if (h->magic[0] != TP_MAGIC_0 || h->magic[1] != TP_MAGIC_1 || h->magic[2] != TP_MAGIC_2 ||
                   +  + ]
      88         [ +  + ]:        3456 :         h->magic[3] != TP_MAGIC_3)
      89                 :           8 :         return TP_ERR_BAD_MAGIC;
      90                 :             : 
      91                 :        3455 :     rc = tp_bs_read_u8(r, &h->version_major);
      92         [ +  + ]:        3455 :     if (rc != TP_OK)
      93                 :             :         return rc; /* LCOV_EXCL_LINE */
      94                 :        3454 :     rc = tp_bs_read_u8(r, &h->version_minor);
      95         [ +  + ]:        3454 :     if (rc != TP_OK)
      96                 :             :         return rc; /* LCOV_EXCL_LINE */
      97                 :             : 
      98                 :             :     /* Validate version */
      99         [ +  + ]:        3453 :     if (h->version_major != TP_FORMAT_VERSION_MAJOR)
     100                 :          11 :         return TP_ERR_VERSION;
     101                 :             : 
     102                 :        3442 :     rc = tp_bs_read_u16(r, &h->flags);
     103         [ +  + ]:        3442 :     if (rc != TP_OK)
     104                 :             :         return rc; /* LCOV_EXCL_LINE */
     105                 :        3441 :     rc = tp_bs_read_u32(r, &h->num_keys);
     106         [ +  + ]:        3441 :     if (rc != TP_OK)
     107                 :             :         return rc; /* LCOV_EXCL_LINE */
     108                 :        3440 :     rc = tp_bs_read_u32(r, &h->trie_data_offset);
     109         [ +  + ]:        3440 :     if (rc != TP_OK)
     110                 :             :         return rc; /* LCOV_EXCL_LINE */
     111                 :        3439 :     rc = tp_bs_read_u32(r, &h->value_store_offset);
     112         [ +  + ]:        3439 :     if (rc != TP_OK)
     113                 :             :         return rc; /* LCOV_EXCL_LINE */
     114                 :        3438 :     rc = tp_bs_read_u32(r, &h->suffix_table_offset);
     115         [ +  + ]:        3438 :     if (rc != TP_OK)
     116                 :             :         return rc; /* LCOV_EXCL_LINE */
     117                 :        3437 :     rc = tp_bs_read_u32(r, &h->total_data_bits);
     118         [ +  + ]:        3437 :     if (rc != TP_OK)
     119                 :             :         return rc; /* LCOV_EXCL_LINE */
     120                 :        3436 :     rc = tp_bs_read_u32(r, &h->reserved);
     121                 :        3436 :     return rc;
     122                 :             : }
        

Generated by: LCOV version 2.0-1