subpart_hdr_2.c
1 /* Subpart directory header version 2 support */ 2 /* SPDX-License-Identifier: GPL-2.0-only */ 3 4 #include <sys/types.h> 5 6 #include "cse_serger.h" 7 8 struct subpart_hdr { 9 uint32_t signature; /* SUBPART_SIGNATURE */ 10 uint32_t count; 11 uint8_t hdr_version; /* Header version = 2 */ 12 uint8_t entry_version; /* Entry version = 1 */ 13 uint8_t length; 14 uint8_t reserved; 15 uint8_t name[4]; 16 uint32_t checksum; 17 } __packed; 18 19 static subpart_hdr_ptr subpart_hdr_read(struct buffer *buff) 20 { 21 struct subpart_hdr *hdr = malloc(sizeof(*hdr)); 22 23 if (!hdr) 24 return NULL; 25 26 READ_MEMBER(buff, hdr->signature); 27 READ_MEMBER(buff, hdr->count); 28 READ_MEMBER(buff, hdr->hdr_version); 29 READ_MEMBER(buff, hdr->entry_version); 30 READ_MEMBER(buff, hdr->length); 31 READ_MEMBER(buff, hdr->reserved); 32 READ_MEMBER(buff, hdr->name); 33 READ_MEMBER(buff, hdr->checksum); 34 35 return hdr; 36 } 37 38 static void subpart_hdr_print(const subpart_hdr_ptr ptr) 39 { 40 const struct subpart_hdr *hdr = ptr; 41 42 printf("%-25s %.4s\n", "Signature", (const char *)&hdr->signature); 43 printf("%-25s %-25d\n", "Count", hdr->count); 44 printf("%-25s %-25d\n", "Header Version", hdr->hdr_version); 45 printf("%-25s %-25d\n", "Entry Version", hdr->entry_version); 46 printf("%-25s 0x%-23x\n", "Header Length", hdr->length); 47 printf("%-25s 0x%-23x\n", "Reserved", hdr->reserved); 48 printf("%-25s ", "Name"); 49 for (size_t i = 0; i < sizeof(hdr->name); i++) 50 printf("%c", hdr->name[i]); 51 printf("\n"); 52 printf("%-25s 0x%-23x\n", "Checksum", hdr->checksum); 53 } 54 55 static size_t subpart_get_count(const subpart_hdr_ptr ptr) 56 { 57 const struct subpart_hdr *hdr = ptr; 58 59 return hdr->count; 60 } 61 62 static void subpart_hdr_free(subpart_hdr_ptr ptr) 63 { 64 free(ptr); 65 } 66 67 const struct subpart_hdr_ops subpart_hdr_2_ops = { 68 .read = subpart_hdr_read, 69 .print = subpart_hdr_print, 70 .get_entry_count = subpart_get_count, 71 .free = subpart_hdr_free, 72 };