2424
2525// read_lib_db: rebuild one NLDM LibertyLibrary from a .libdb cache.
2626//
27- // File layout: [header][string_count][ string table][ body]
27+ // File layout: [header][db block: string table then body]
2828// Body field order must match LibWriter in LibDbWriter.cc.
2929// Shared axes/tables/attrs: first use reads id + full data; later uses reuse by id.
3030// Strings: body stores an index; the string table holds the text once.
@@ -749,11 +749,29 @@ LibLoader::read()
749749 return lib_;
750750}
751751
752+ LibertyLibrary *
753+ readLibDbBytes (const uint8_t *data,
754+ size_t size,
755+ std::string_view label,
756+ Network *network)
757+ {
758+ // Split the block into the string list and body, then rebuild the library.
759+ Report *report = network->report ();
760+ std::vector<std::string> strings;
761+ std::vector<uint8_t > body;
762+ if (!dbUnpack (data, size, strings, body))
763+ report->error (1356 , " {} has a corrupt string table." , label);
764+
765+ DbReader reader (body.data (), body.size (), &strings);
766+ LibLoader loader (reader, label, network);
767+ return loader.read ();
768+ }
769+
752770LibertyLibrary *
753771readLibDbFile (std::string_view filename,
754772 Network *network)
755773{
756- // Validate .libdb + version, load string table, then LibLoader::read ().
774+ // Validate .libdb + version, then hand the block to readLibDbBytes ().
757775 Report *report = network->report ();
758776 std::string path (filename);
759777
@@ -764,7 +782,7 @@ readLibDbFile(std::string_view filename,
764782 if (f == nullptr )
765783 report->error (1366 , " cannot open {}." , path);
766784
767- // File layout: [header][string_count][string bytes][body bytes ]
785+ // File layout: [header][block ]
768786 LibDbHeader hdr{};
769787 if (fread (&hdr, sizeof hdr, 1 , f) != 1 ) {
770788 fclose (f);
@@ -776,39 +794,15 @@ readLibDbFile(std::string_view filename,
776794 path, hdr.version , lib_db_version);
777795 }
778796
779- uint32_t string_count = 0 ;
780- bool ok = fread (&string_count, sizeof string_count, 1 , f) == 1 ;
781- std::vector<uint8_t > string_bytes (hdr.string_bytes );
782- if (ok && hdr.string_bytes )
783- ok = fread (string_bytes.data (), hdr.string_bytes , 1 , f) == 1 ;
784- std::vector<uint8_t > body (hdr.body_bytes );
785- if (ok && hdr.body_bytes )
786- ok = fread (body.data (), hdr.body_bytes , 1 , f) == 1 ;
797+ std::vector<uint8_t > block (hdr.block_bytes );
798+ bool ok = true ;
799+ if (hdr.block_bytes )
800+ ok = fread (block.data (), hdr.block_bytes , 1 , f) == 1 ;
787801 fclose (f);
788802 if (!ok)
789803 report->error (1363 , " {} is truncated." , path);
790804
791- // Unpack (length, characters)* into the string list DbReader::str() uses.
792- std::vector<std::string> strings;
793- strings.reserve (string_count);
794- size_t pos = 0 ;
795- for (uint32_t i = 0 ; i < string_count; i++) {
796- uint32_t len = 0 ;
797- bool len_ok = pos + sizeof len <= string_bytes.size ();
798- if (len_ok) {
799- std::memcpy (&len, string_bytes.data () + pos, sizeof len);
800- pos += sizeof len;
801- }
802- if (!len_ok || pos + len > string_bytes.size ())
803- report->error (1356 , " {} has a corrupt string table." , path);
804- strings.emplace_back (reinterpret_cast <const char *>(string_bytes.data () + pos), len);
805- pos += len;
806- }
807-
808- // Hand body + string list to the loader; it rebuilds the library object.
809- DbReader reader (body.data (), body.size (), &strings);
810- LibLoader loader (reader, filename, network);
811- return loader.read ();
805+ return readLibDbBytes (block.data (), block.size (), filename, network);
812806}
813807
814808} // namespace sta
0 commit comments