bool bi_copy_data_to_data(MbBiReader *bir, MbBiWriter *biw) { int ret; char buf[10240]; size_t n_read; size_t n_written; while ((ret = mb_bi_reader_read_data(bir, buf, sizeof(buf), &n_read)) == MB_BI_OK) { ret = mb_bi_writer_write_data(biw, buf, n_read, &n_written); if (ret != MB_BI_OK || n_read != n_written) { LOGE("Failed to write entry data: %s", mb_bi_writer_error_string(biw)); return false; } } if (ret != MB_BI_EOF) { LOGE("Failed to read boot image entry data: %s", mb_bi_reader_error_string(bir)); return false; } return true; }
void TestChecksum(const unsigned char expected[20], int types) { static const unsigned char padding[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }; MbBiHeader *header; MbBiEntry *entry; int ret; size_t n; // Write dummy header ASSERT_EQ(mb_bi_writer_get_header(_biw.get(), &header), MB_BI_OK); ASSERT_EQ(mb_bi_header_set_page_size(header, 2048), MB_BI_OK); ASSERT_EQ(mb_bi_writer_write_header(_biw.get(), header), MB_BI_OK); // Write specified dummy entries while ((ret = mb_bi_writer_get_entry(_biw.get(), &entry)) == MB_BI_OK) { ASSERT_EQ(mb_bi_writer_write_entry(_biw.get(), entry), MB_BI_OK); if (mb_bi_entry_type(entry) & types) { ASSERT_EQ(mb_bi_writer_write_data(_biw.get(), "hello", 5, &n), MB_BI_OK); ASSERT_EQ(n, 5); } } ASSERT_EQ(ret, MB_BI_EOF); // Close to write header ASSERT_EQ(mb_bi_writer_close(_biw.get()), MB_BI_OK); // Check SHA1 ASSERT_EQ(memcmp(static_cast<unsigned char *>(_buf) + 576, expected, 20), 0); // Check padding ASSERT_EQ(memcmp(static_cast<unsigned char *>(_buf) + 596, padding, sizeof(padding)), 0); }
bool bi_copy_file_to_data(const std::string &path, MbBiWriter *biw) { ScopedFILE fp(fopen(path.c_str(), "rb"), fclose); if (!fp) { LOGE("%s: Failed to open for reading: %s", path.c_str(), strerror(errno)); return false; } char buf[10240]; size_t n; while (true) { n = fread(buf, 1, sizeof(buf), fp.get()); size_t bytes_written; if (mb_bi_writer_write_data(biw, buf, n, &bytes_written) != MB_BI_OK || bytes_written != n) { LOGE("Failed to write entry data: %s", mb_bi_writer_error_string(biw)); return false; } if (n < sizeof(buf)) { if (ferror(fp.get())) { LOGE("%s: Failed to read file: %s", path.c_str(), strerror(errno)); } else { break; } } } return true; }