Skip to content

Commit 80377ca

Browse files
author
Dmitrii Kuvaiskii
committed
[common] Refactor Protected Files, part 2
This commit refactors PF code without changing functionality (part 2 in a series of commits). In particular, this commit simplifies function signatures of `ipf_read_node()` and `ipf_write_node()`. Signed-off-by: Dmitrii Kuvaiskii <[email protected]>
1 parent b7ffa83 commit 80377ca

File tree

1 file changed

+16
-24
lines changed

1 file changed

+16
-24
lines changed

common/src/protected_files/protected_files.c

+16-24
Original file line numberDiff line numberDiff line change
@@ -321,11 +321,10 @@ static bool ipf_update_all_data_and_mht_nodes(pf_context_t* pf) {
321321
return ret;
322322
}
323323

324-
static bool ipf_read_node(pf_context_t* pf, pf_handle_t handle, uint64_t node_number, void* buffer,
325-
uint32_t node_size) {
326-
uint64_t offset = node_number * node_size;
324+
static bool ipf_read_node(pf_context_t* pf, uint64_t node_number, void* buffer) {
325+
uint64_t offset = node_number * PF_NODE_SIZE;
327326

328-
pf_status_t status = g_cb_read(handle, buffer, offset, node_size);
327+
pf_status_t status = g_cb_read(pf->file, buffer, offset, PF_NODE_SIZE);
329328
if (PF_FAILURE(status)) {
330329
pf->last_error = status;
331330
return false;
@@ -334,9 +333,10 @@ static bool ipf_read_node(pf_context_t* pf, pf_handle_t handle, uint64_t node_nu
334333
return true;
335334
}
336335

337-
static bool ipf_write_file(pf_context_t* pf, pf_handle_t handle, uint64_t offset, void* buffer,
338-
uint32_t size) {
339-
pf_status_t status = g_cb_write(handle, buffer, offset, size);
336+
static bool ipf_write_node(pf_context_t* pf, uint64_t node_number, void* buffer) {
337+
uint64_t offset = node_number * PF_NODE_SIZE;
338+
339+
pf_status_t status = g_cb_write(pf->file, buffer, offset, PF_NODE_SIZE);
340340
if (PF_FAILURE(status)) {
341341
pf->last_error = status;
342342
return false;
@@ -345,11 +345,6 @@ static bool ipf_write_file(pf_context_t* pf, pf_handle_t handle, uint64_t offset
345345
return true;
346346
}
347347

348-
static bool ipf_write_node(pf_context_t* pf, pf_handle_t handle, uint64_t node_number, void* buffer,
349-
uint32_t node_size) {
350-
return ipf_write_file(pf, handle, node_number * node_size, buffer, node_size);
351-
}
352-
353348
// this is a very 'specific' function, tied to the architecture of the file layout,
354349
// returning the node numbers according to the data offset in the file
355350
static void get_node_numbers(uint64_t offset, uint64_t* mht_node_number, uint64_t* data_node_number,
@@ -408,22 +403,21 @@ static bool ipf_write_all_changes_to_disk(pf_context_t* pf) {
408403
data_to_write = (uint8_t*)&file_node->encrypted;
409404
node_number = file_node->physical_node_number;
410405

411-
if (!ipf_write_node(pf, pf->file, node_number, data_to_write, PF_NODE_SIZE)) {
406+
if (!ipf_write_node(pf, node_number, data_to_write)) {
412407
return false;
413408
}
414409

415410
file_node->need_writing = false;
416411
}
417412

418-
if (!ipf_write_node(pf, pf->file, /*node_number=*/1, &pf->root_mht.encrypted,
419-
PF_NODE_SIZE)) {
413+
if (!ipf_write_node(pf, /*node_number=*/1, &pf->root_mht.encrypted)) {
420414
return false;
421415
}
422416

423417
pf->root_mht.need_writing = false;
424418
}
425419

426-
if (!ipf_write_node(pf, pf->file, /*node_number=*/0, &pf->file_metadata, PF_NODE_SIZE)) {
420+
if (!ipf_write_node(pf, /*node_number=*/0, &pf->file_metadata)) {
427421
return false;
428422
}
429423

@@ -666,8 +660,8 @@ static file_node_t* ipf_read_data_node(pf_context_t* pf, uint64_t offset) {
666660
file_data_node->physical_node_number = physical_node_number;
667661
file_data_node->parent = file_mht_node;
668662

669-
if (!ipf_read_node(pf, pf->file, file_data_node->physical_node_number,
670-
file_data_node->encrypted.cipher, PF_NODE_SIZE)) {
663+
if (!ipf_read_node(pf, file_data_node->physical_node_number,
664+
file_data_node->encrypted.cipher)) {
671665
free(file_data_node);
672666
return NULL;
673667
}
@@ -731,8 +725,8 @@ static file_node_t* ipf_read_mht_node(pf_context_t* pf, uint64_t mht_node_number
731725
file_mht_node->physical_node_number = physical_node_number;
732726
file_mht_node->parent = parent_file_mht_node;
733727

734-
if (!ipf_read_node(pf, pf->file, file_mht_node->physical_node_number,
735-
file_mht_node->encrypted.cipher, PF_NODE_SIZE)) {
728+
if (!ipf_read_node(pf, file_mht_node->physical_node_number,
729+
file_mht_node->encrypted.cipher)) {
736730
free(file_mht_node);
737731
return NULL;
738732
}
@@ -805,8 +799,7 @@ static bool ipf_init_existing_file(pf_context_t* pf, const char* path) {
805799
pf_status_t status;
806800

807801
// read meta-data node
808-
if (!ipf_read_node(pf, pf->file, /*node_number=*/0, (uint8_t*)&pf->file_metadata,
809-
PF_NODE_SIZE)) {
802+
if (!ipf_read_node(pf, /*node_number=*/0, (uint8_t*)&pf->file_metadata)) {
810803
return false;
811804
}
812805

@@ -850,8 +843,7 @@ static bool ipf_init_existing_file(pf_context_t* pf, const char* path) {
850843

851844
if (pf->encrypted_part_plain.size > MD_USER_DATA_SIZE) {
852845
// read the root node of the mht
853-
if (!ipf_read_node(pf, pf->file, /*node_number=*/1, &pf->root_mht.encrypted.cipher,
854-
PF_NODE_SIZE))
846+
if (!ipf_read_node(pf, /*node_number=*/1, &pf->root_mht.encrypted.cipher))
855847
return false;
856848

857849
// this also verifies the root mht gmac against the gmac in the meta-data encrypted part

0 commit comments

Comments
 (0)