bool CBitcoinAddrDb::Read(CAddrMan& addr) { boost::filesystem::path pathAddr = GetDataDir() / m_addrFileName; // open input file, and associate with CAutoFile FILE *file = fopen(pathAddr.string().c_str(), "rb"); CAutoFile filein = CAutoFile(file, SER_DISK, BITCOIN_CLIENT_VERSION); if (!filein) { return false; } // use file size to size memory buffer int fileSize = GetFilesize(filein); int dataSize = fileSize - sizeof(uint256); //Don't try to resize to a negative number if file is small if ( dataSize < 0 ) dataSize = 0; vector<unsigned char> vchData; vchData.resize(dataSize); uint256 hashIn; // read data and checksum from file try { filein.read((char *)&vchData[0], dataSize); filein >> hashIn; } catch (std::exception &e) { return false; } filein.fclose(); CDataStream ssPeers(vchData, SER_DISK, BITCOIN_CLIENT_VERSION); // verify stored checksum matches input data uint256 hashTmp = Hash(ssPeers.begin(), ssPeers.end()); if (hashIn != hashTmp) { return false; } unsigned char pchMsgTmp[4]; try { // de-serialize file header (network specific magic number) and .. ssPeers >> FLATDATA(pchMsgTmp); // ... verify the network matches ours if (memcmp(pchMsgTmp, BitcoinMessageStart, sizeof(pchMsgTmp))) { return false; } // de-serialize address data into one CAddrMan object ssPeers >> addr; } catch (std::exception &e) { return false; } return true; }
// get the sizes of the ark files bool ArkFile::GetArkSizes(std::vector<s64>& rArkSizes) const { if( !IsOpen() ) return false; rArkSizes.clear(); for(int i=0; i<(int)mArkHandles.size(); i++) { s64 size = GetFilesize(mArkHandles[i]); if(size <= 0 && i > 0) return false; rArkSizes.push_back(size); } return rArkSizes.size() > 0; }
ByteArray ReadBinaryFile(const std::string& filename) { std::ifstream file(filename.c_str(), std::ios::binary); if (file.is_open()) { size_t filesize = GetFilesize(file); // Now just read in ALL the data from the file at once ByteArray data(filesize); file.read(reinterpret_cast<char*>(&data[0]), filesize); return data; } else { return ByteArray(); } }
// adds a file into the ark and hdr // // args: external filename of file on pc that you want to insert // internal filename to insert as in the ark and hdr // flag for whether to encrypt files when adding them // true: to encrypt "*.dtb" files when they are added // false: if "*.dtb" files being added are already encrypted // returns: true if inserted successfully bool ArkHdrPair::AddFile(const char* srcFilepath, const char* arkFilename, bool performEncrypts) { // ensure file doesnt already exist in ark FileEntrySetIter iter; const FileEntry* p_entry = First(iter, arkFilename); if(p_entry != NULL) return false; // file doesnt already exist, so add it bool encrypted = performEncrypts && (strstr(arkFilename, ".dtb") != 0); s64 filesize = GetFilesize(srcFilepath); if(filesize < 0) return false; FileEntry entry(arkFilename, srcFilepath, filesize, 0, encrypted); mFileCollection.Add(entry); mHasChanged = true; return true; }
/*=========================================================================== * * Class CSrResourceInstance Method - int64 GetFilesize (void); * *=========================================================================*/ int64 CSrResourceInstance::GetFilesize (void) { int64 Size; if (GetFilesize(Size)) return (Size); return (-1); }
// from a given offset into an ark, the correct file handle is returned // which is pointing at the correct offset in that file // (will seek to a new offset outside of current bounds) // // args: offset into ark // flag or whether to seek outside existing file sizes // returns: file handle if successful // NULL if error FILE* ArkFile::GetHandleFromOffset(s64 offset, bool offsetMustExist) { s64 filesize; for(int i=0; i<(int)mArkHandles.size(); i++) { if((filesize=GetFilesize(mArkHandles[i])) < 0) return NULL; if(offset >= filesize) { // if there is still another ark file after this one // then update the offset to be in that ark file if(i+1 < (int)mArkHandles.size()) offset -= filesize; else break; } else { FSEEK64(mArkHandles[i], offset, SEEK_SET); return mArkHandles[i]; } } // offset is outside of the current file space if(offsetMustExist) return NULL; // check if we need to open a new ark file char zero[1] = {0}; FILE* fd = NULL; if(offset >= mMaxArkSize && mMaxArkSize != 0) { // pad the current ark file up to the max ark size limit, // then create a new ark file and seek to the required offset in it fd = mArkHandles[mArkHandles.size()-1]; FSEEK64(fd, 0, SEEK_END); if(FTELL64(fd) < mMaxArkSize) { FSEEK64(fd, mMaxArkSize-1, SEEK_SET); fwrite(zero, 1, 1, fd); } offset -= mMaxArkSize; char new_filename[260]; sprintf(new_filename, "%s%cMAIN_%d.ARK", mDirname, DIRSEPCHAR, (int) mArkHandles.size()); FILE* fd = fopen(new_filename, "w+b"); if(fd == NULL) return NULL; mArkHandles.push_back(fd); if(offset > 0) { FSEEK64(fd, offset-1, SEEK_SET); fwrite(zero, 1, 1, fd); } return fd; } else { // according to posix specifications, an fseek followed by an fwrite // should make a file grow to the new end of file mark. // (check the file needs to grow first though) fd = mArkHandles[mArkHandles.size()-1]; FSEEK64(fd, 0, SEEK_END); if(FTELL64(fd) < offset) { FSEEK64(fd, offset-1, SEEK_SET); fwrite(zero, 1, 1, fd); } else { FSEEK64(fd, offset, SEEK_SET); } return fd; } }