| 1553 | } |
| 1554 | |
| 1555 | bool CAddrDB::Read(CAddrMan& addr) { |
| 1556 | // open input file, and associate with CAutoFile |
| 1557 | FILE* file = fopen(pathAddr.string().c_str(), "rb"); |
| 1558 | CAutoFile filein = CAutoFile(file, SER_DISK, CLIENT_VERSION); |
| 1559 | if (!filein) |
| 1560 | return ERRORMSG("Failed to open file %s", pathAddr.string()); |
| 1561 | |
| 1562 | // use file size to size memory buffer |
| 1563 | int32_t fileSize = boost::filesystem::file_size(pathAddr); |
| 1564 | int32_t dataSize = fileSize - sizeof(uint256); |
| 1565 | // Don't try to resize to a negative number if file is small |
| 1566 | if (dataSize < 0) |
| 1567 | dataSize = 0; |
| 1568 | vector<uint8_t> vchData; |
| 1569 | vchData.resize(dataSize); |
| 1570 | uint256 hashIn; |
| 1571 | |
| 1572 | // read data and checksum from file |
| 1573 | try { |
| 1574 | filein.read((char*)&vchData[0], dataSize); |
| 1575 | filein >> hashIn; |
| 1576 | } catch (std::exception& e) { |
| 1577 | return ERRORMSG("Deserialize or I/O error - %s", e.what()); |
| 1578 | } |
| 1579 | filein.fclose(); |
| 1580 | |
| 1581 | CDataStream ssPeers(vchData, SER_DISK, CLIENT_VERSION); |
| 1582 | |
| 1583 | // verify stored checksum matches input data |
| 1584 | uint256 hashTmp = Hash(ssPeers.begin(), ssPeers.end()); |
| 1585 | if (hashIn != hashTmp) |
| 1586 | return ERRORMSG("Checksum mismatch, data corrupted"); |
| 1587 | |
| 1588 | uint8_t pchMsgTmp[4]; |
| 1589 | try { |
| 1590 | // de-serialize file header (network specific magic number) and .. |
| 1591 | ssPeers >> FLATDATA(pchMsgTmp); |
| 1592 | |
| 1593 | // ... verify the network matches ours |
| 1594 | if (memcmp(pchMsgTmp, SysCfg().MessageStart(), sizeof(pchMsgTmp))) |
| 1595 | return ERRORMSG("Invalid network magic number"); |
| 1596 | |
| 1597 | // de-serialize address data into one CAddrMan object |
| 1598 | ssPeers >> addr; |
| 1599 | } catch (std::exception& e) { |
| 1600 | return ERRORMSG("Deserialize or I/O error - %s", e.what()); |
| 1601 | } |
| 1602 | |
| 1603 | return true; |
| 1604 | } |