SBR.c
Go to the documentation of this file.
2 
3 #include "assert.h"
4 #include "stddef.h"
5 #include "string.h"
6 
7 static uint32_t read_uint32_t(const void* buffer) {
8  if(buffer == NULL) return 0;
9  uint8_t* p = (uint8_t*)buffer;
10  uint32_t val = 0;
11  val = (p[0]) + (p[1] << 8) + (p[2] << 16) + (p[3] << 24);
12  return val;
13 }
14 
15 static void write_uint32_t(void* buffer, uint32_t val) {
16  if(buffer == NULL) return;
17  uint8_t* p = (uint8_t*)buffer;
18  for(int i = 0; i < 4; i++) {
19  *p++ = val & 0xFF;
20  val = val >> 8;
21  }
22 }
23 
24 static int read_section(const void* buffer, uint32_t size, SBR_SECTION* section) {
25  if(section == NULL) return -1;
26  if(size < sizeof(SBR_SECTION_RAW)) return -1;
27 
28  const uint8_t* p = (const uint8_t*)buffer;
29 
30  // Read name
31  memcpy(section->name, p, sizeof(section->name));
32  p += sizeof(section->name);
33 
34  // Read size
35  section->size = read_uint32_t(p);
36  p += sizeof(section->size);
37 
38  // Read offset
39  section->offset = read_uint32_t(p);
40  p += sizeof(section->offset);
41 
42  // Read checksum
43  section->checksum = read_uint32_t(p);
44  p += sizeof(section->checksum);
45 
46  // Read type
47  section->type = read_uint32_t(p);
48  p += sizeof(section->type);
49 
50  // Read flags
51  section->flags = read_uint32_t(p);
52  p += sizeof(section->flags);
53 
54  return 0;
55 }
56 
57 int sbr_parse(const void* buffer, uint32_t size, SBR* sbr) {
58  if(buffer == NULL) return -1;
59  if(sbr == NULL) return -1;
60  if(size < SBR_RAW_SIZE) return -1;
61 
62  // pointer to be moved to current data being parsed
63  const uint8_t* p = (uint8_t*)buffer;
64 
65  // copy identifier
66  memcpy(sbr->identifier, p, SBR_IDENTIFIER_SIZE);
67  p += sizeof(sbr->identifier);
68 
69  // check identifier
70  if(memcmp(sbr->identifier, SBR_IDENTIFIER, SBR_IDENTIFIER_SIZE) != 0) {
71  return -1;
72  }
73 
74  for(unsigned int i = 0; i < SBR_MAX_NUM_SECTIONS; i++) {
75  if(read_section(p, sizeof(SBR_SECTION_RAW), &sbr->sections[i])) {
76  return -1;
77  }
78  p += sizeof(SBR_SECTION_RAW);
79  }
80 
81  return 0;
82 }
83 
84 int sbr_serialize(const SBR* sbr, void* buffer, uint32_t max_size) {
85  if(buffer == NULL) return -1;
86  if(sbr == NULL) return -1;
87  if(max_size < SBR_RAW_SIZE) return -1;
88 
89  // pointer to be moved to location of serialized data
90  uint8_t* p = (uint8_t*)buffer;
91 
92  // write out identifier
93  memcpy(p, SBR_IDENTIFIER, sizeof(SBR_IDENTIFIER));
94  p += sizeof(SBR_IDENTIFIER);
95 
96  // write out sections
97  for(unsigned int i = 0; i < SBR_MAX_NUM_SECTIONS; i++) {
98  // write name
99  memcpy(p, sbr->sections[i].name, sizeof(sbr->sections[i].name));
100  p += sizeof(sbr->sections[i].name);
101 
102  // write size
103  write_uint32_t(p, sbr->sections[i].size);
104  p += sizeof(sbr->sections[i].size);
105 
106  // write offset
107  write_uint32_t(p, sbr->sections[i].offset);
108  p += sizeof(sbr->sections[i].offset);
109 
110  // write checksum
111  write_uint32_t(p, sbr->sections[i].checksum);
112  p += sizeof(sbr->sections[i].checksum);
113 
114  // write type
115  *p++ = sbr->sections[i].type;
116 
117  // write flags
118  *p++ = sbr->sections[i].flags;
119  }
120 
121  // write out identifier
122  memcpy(p, SBR_IDENTIFIER, sizeof(SBR_IDENTIFIER));
123  p += sizeof(SBR_IDENTIFIER);
124 
125  return 0;
126 }
127 
128 bool sbr_section_get_bootable(const SBR_SECTION* sbr_section) {
129  if(sbr_section == NULL) return false;
130  if(sbr_section->flags & SBR_SECTION_FLAG_BOOTABLE) return true;
131  return false;
132 }
133 
135  if(sbr_section == NULL) return false;
136  if(sbr_section->flags & SBR_SECTION_FLAG_IGNORE_CHECKSUM) return true;
137  return false;
138 }
139 
141  if(sbr_section == NULL) return SBR_NO_COMPRESSION;
142  return sbr_section->flags & SBR_SECTION_FLAG_COMPRESSION_MASK;
143 }
144 
145 bool sbr_section_is_valid(const SBR_SECTION* sbr_section) {
146  // Valid SBR section must have a name set, as well as a non zero size
147  if(sbr_section->name[0] == 0 || ((uint8_t)sbr_section->name[0]) == 0xFF) {
148  return false;
149  }
150  return true;
151 }
152 
154  return 5381;
155 }
156 
157 uint32_t sbr_compute_checksum_prev(const void* buffer, uint32_t size, uint32_t prev_checksum) {
158  uint32_t checksum = prev_checksum;
159  uint8_t* p = (uint8_t*)buffer;
160 
161  for(unsigned int i = 0; i < size; i++) {
162  checksum = ((checksum << 5) + checksum) + p[i]; /* hash * 33 + p[i] */
163  }
164 
165  return checksum;
166 }
167 
168 uint32_t sbr_compute_checksum(const void* buffer, uint32_t size) {
170 }
171 
172 void sbr_section_set_name(SBR_SECTION* sbr_section, const char* name) {
173  assert(sbr_section != NULL);
174  strncpy(sbr_section->name, name, SBR_SECTION_NAME_MAX_SIZE);
175 }
176 
177 void sbr_section_set_size(SBR_SECTION* sbr_section, uint32_t size) {
178  assert(sbr_section != NULL);
179  sbr_section->size = size;
180 }
181 
182 void sbr_section_set_offset(SBR_SECTION* sbr_section, uint32_t offset) {
183  assert(sbr_section != NULL);
184  sbr_section->offset = offset;
185 }
186 
187 void sbr_section_set_checksum(SBR_SECTION* sbr_section, uint32_t checksum) {
188  assert(sbr_section != NULL);
189  sbr_section->checksum = checksum;
190 }
191 
192 void sbr_section_set_type(SBR_SECTION* sbr_section, uint8_t type) {
193  assert(sbr_section != NULL);
194  sbr_section->type = type;
195 }
196 
197 void sbr_section_set_bootable(SBR_SECTION* sbr_section, bool bootable) {
198  assert(sbr_section != NULL);
199  if(bootable) {
200  sbr_section->flags |= SBR_SECTION_FLAG_BOOTABLE;
201  } else {
202  sbr_section->flags &= ~SBR_SECTION_FLAG_BOOTABLE;
203  }
204 }
205 
206 void sbr_section_set_ignore_checksum(SBR_SECTION* sbr_section, bool ignore_checksum) {
207  assert(sbr_section != NULL);
208  if(ignore_checksum) {
209  sbr_section->flags |= SBR_SECTION_FLAG_IGNORE_CHECKSUM;
210  } else {
211  sbr_section->flags &= ~SBR_SECTION_FLAG_IGNORE_CHECKSUM;
212  }
213 }
214 
215 void sbr_section_set_compression(SBR_SECTION* sbr_section, SBR_COMPRESSION compression) {
216  assert(sbr_section != NULL);
217 
218  // Clear previous compression
219  sbr_section->flags &= ~SBR_SECTION_FLAG_COMPRESSION_MASK;
220 
221  // Set new compression
222  sbr_section->flags |= compression;
223 }
SBR_SECTION::flags
uint8_t flags
Definition: SBR.h:34
SBR_MAX_NUM_SECTIONS
#define SBR_MAX_NUM_SECTIONS
Definition: SBR.h:11
read_uint32_t
static uint32_t read_uint32_t(const void *buffer)
Definition: SBR.c:7
SBR_SECTION
Definition: SBR.h:28
sbr_section_set_offset
void sbr_section_set_offset(SBR_SECTION *sbr_section, uint32_t offset)
Definition: SBR.c:182
sbr_section_set_ignore_checksum
void sbr_section_set_ignore_checksum(SBR_SECTION *sbr_section, bool ignore_checksum)
Definition: SBR.c:206
sbr_section_get_ignore_checksum
bool sbr_section_get_ignore_checksum(const SBR_SECTION *sbr_section)
Definition: SBR.c:134
SBR_IDENTIFIER
static const uint8_t SBR_IDENTIFIER[SBR_IDENTIFIER_SIZE]
Definition: SBR.h:25
sbr_initial_checksum
uint32_t sbr_initial_checksum()
Definition: SBR.c:153
SBR_SECTION::offset
uint32_t offset
Definition: SBR.h:31
sbr_section_set_type
void sbr_section_set_type(SBR_SECTION *sbr_section, uint8_t type)
Definition: SBR.c:192
SBR.h
dai::utility::checksum
std::uint32_t checksum(const void *buffer, std::size_t size, uint32_t prevChecksum)
Definition: Checksum.cpp:6
SBR_SECTION::checksum
uint32_t checksum
Definition: SBR.h:32
SBR_SECTION_FLAG_COMPRESSION_MASK
#define SBR_SECTION_FLAG_COMPRESSION_MASK
Definition: SBR.h:17
SBR_SECTION::type
uint8_t type
Definition: SBR.h:33
sbr_section_get_compression
SBR_COMPRESSION sbr_section_get_compression(const SBR_SECTION *sbr_section)
Definition: SBR.c:140
sbr_section_set_bootable
void sbr_section_set_bootable(SBR_SECTION *sbr_section, bool bootable)
Definition: SBR.c:197
nanorpc::core::type::buffer
std::vector< std::uint8_t > buffer
Definition: type.h:28
SBR::sections
SBR_SECTION sections[SBR_MAX_NUM_SECTIONS]
Definition: SBR.h:52
sbr_section_is_valid
bool sbr_section_is_valid(const SBR_SECTION *sbr_section)
Definition: SBR.c:145
sbr_section_set_checksum
void sbr_section_set_checksum(SBR_SECTION *sbr_section, uint32_t checksum)
Definition: SBR.c:187
DAI_SPAN_NAMESPACE_NAME::detail::size
constexpr auto size(const C &c) -> decltype(c.size())
Definition: span.hpp:167
SBR_SECTION_NAME_MAX_SIZE
#define SBR_SECTION_NAME_MAX_SIZE
Definition: SBR.h:12
sbr_compute_checksum_prev
uint32_t sbr_compute_checksum_prev(const void *buffer, uint32_t size, uint32_t prev_checksum)
Definition: SBR.c:157
SBR_NO_COMPRESSION
@ SBR_NO_COMPRESSION
Definition: SBR.h:19
sbr_section_get_bootable
bool sbr_section_get_bootable(const SBR_SECTION *sbr_section)
Definition: SBR.c:128
sbr_section_set_name
void sbr_section_set_name(SBR_SECTION *sbr_section, const char *name)
Definition: SBR.c:172
SBR_SECTION::size
uint32_t size
Definition: SBR.h:30
SBR_IDENTIFIER_SIZE
#define SBR_IDENTIFIER_SIZE
Definition: SBR.h:13
SBR_SECTION::name
char name[SBR_SECTION_NAME_MAX_SIZE]
Definition: SBR.h:29
SBR
Definition: SBR.h:50
write_uint32_t
static void write_uint32_t(void *buffer, uint32_t val)
Definition: SBR.c:15
nanorpc::core::detail::pack::meta::type
type
Definition: pack_meta.h:26
sbr_compute_checksum
uint32_t sbr_compute_checksum(const void *buffer, uint32_t size)
Definition: SBR.c:168
SBR_SECTION_FLAG_BOOTABLE
#define SBR_SECTION_FLAG_BOOTABLE
Definition: SBR.h:15
SBR_SECTION_FLAG_IGNORE_CHECKSUM
#define SBR_SECTION_FLAG_IGNORE_CHECKSUM
Definition: SBR.h:16
sbr_section_set_size
void sbr_section_set_size(SBR_SECTION *sbr_section, uint32_t size)
Definition: SBR.c:177
sbr_section_set_compression
void sbr_section_set_compression(SBR_SECTION *sbr_section, SBR_COMPRESSION compression)
Definition: SBR.c:215
sbr_parse
int sbr_parse(const void *buffer, uint32_t size, SBR *sbr)
Definition: SBR.c:57
SBR_SECTION_RAW
Definition: SBR.h:39
SBR::identifier
uint8_t identifier[SBR_IDENTIFIER_SIZE]
Definition: SBR.h:51
SBR_RAW_SIZE
#define SBR_RAW_SIZE
Definition: SBR.h:55
SBR_COMPRESSION
SBR_COMPRESSION
Definition: SBR.h:18
sbr_serialize
int sbr_serialize(const SBR *sbr, void *buffer, uint32_t max_size)
Definition: SBR.c:84
read_section
static int read_section(const void *buffer, uint32_t size, SBR_SECTION *section)
Definition: SBR.c:24


depthai
Author(s): Martin Peterlin
autogenerated on Sat Mar 22 2025 02:58:19