bool OleMainStream::readParagraphStyleTable(const char *headerBuffer, const OleEntry &tableEntry) { //PlcBtePapx structure is table with formatting for all paragraphs unsigned int beginParagraphInfo = OleUtil::getU4Bytes(headerBuffer, 0x102); // address of PlcBtePapx structure std::size_t paragraphInfoLength = (std::size_t)OleUtil::getU4Bytes(headerBuffer, 0x106); // length of PlcBtePapx structure if (paragraphInfoLength < 4) { return false; } OleStream tableStream(myStorage, tableEntry, myBaseStream); std::string buffer; if (!readToBuffer(buffer, beginParagraphInfo, paragraphInfoLength, tableStream)) { return false; } static const unsigned int PAPX_SIZE = 4; std::size_t size = calcCountOfPLC(paragraphInfoLength, PAPX_SIZE); std::vector<unsigned int> paragraphBlocks; for (std::size_t index = 0, tOffset = (size + 1) * 4; index < size; ++index, tOffset += PAPX_SIZE) { paragraphBlocks.push_back(OleUtil::getU4Bytes(buffer.c_str(), tOffset)); } char *formatPageBuffer = new char[OleStorage::BBD_BLOCK_SIZE]; for (std::size_t index = 0; index < paragraphBlocks.size(); ++index) { seek(paragraphBlocks.at(index) * OleStorage::BBD_BLOCK_SIZE, true); if (read(formatPageBuffer, OleStorage::BBD_BLOCK_SIZE) != OleStorage::BBD_BLOCK_SIZE) { return false; } const unsigned int paragraphsCount = OleUtil::getU1Byte(formatPageBuffer, 0x1ff); //offset with 'cpara' value (count of paragraphs) for (unsigned int index2 = 0; index2 < paragraphsCount; ++index2) { const unsigned int offset = OleUtil::getU4Bytes(formatPageBuffer, index2 * 4); unsigned int papxOffset = OleUtil::getU1Byte(formatPageBuffer, (paragraphsCount + 1) * 4 + index2 * 13) * 2; if (papxOffset <= 0) { continue; } unsigned int len = OleUtil::getU1Byte(formatPageBuffer, papxOffset) * 2; if (len == 0) { ++papxOffset; len = OleUtil::getU1Byte(formatPageBuffer, papxOffset) * 2; } const unsigned int styleId = OleUtil::getU2Bytes(formatPageBuffer, papxOffset + 1); Style styleInfo = getStyleFromStylesheet(styleId, myStyleSheet); if (len >= 3) { getStyleInfo(papxOffset, formatPageBuffer + 3, len - 3, styleInfo); } unsigned int charPos = 0; if (!offsetToCharPos(offset, charPos, myPieces)) { continue; } myStyleInfoList.push_back(CharPosToStyle(charPos, styleInfo)); } } delete[] formatPageBuffer; return true; }
size_t IO::readToBuffer(const WString & fileName, char ** data) { std::fstream file(fileName.getData(),std::ios::in | std::ios::binary); if ( file.is_open() ) { size_t nbChars = readToBuffer(&file, data); file.close(); return nbChars; } return -1; }
bool OleMainStream::readCharInfoTable(const char *headerBuffer, const OleEntry &tableEntry) { //PlcfbteChpx structure is table with formatting for particular run of text unsigned int beginCharInfo = OleUtil::getU4Bytes(headerBuffer, 0xfa); // address of PlcfbteChpx structure std::size_t charInfoLength = (std::size_t)OleUtil::getU4Bytes(headerBuffer, 0xfe); // length of PlcfbteChpx structure if (charInfoLength < 4) { return false; } OleStream tableStream(myStorage, tableEntry, myBaseStream); std::string buffer; if (!readToBuffer(buffer, beginCharInfo, charInfoLength, tableStream)) { return false; } static const unsigned int CHPX_SIZE = 4; std::size_t size = calcCountOfPLC(charInfoLength, CHPX_SIZE); std::vector<unsigned int> charBlocks; for (std::size_t index = 0, offset = (size + 1) * 4; index < size; ++index, offset += CHPX_SIZE) { charBlocks.push_back(OleUtil::getU4Bytes(buffer.c_str(), offset)); } char *formatPageBuffer = new char[OleStorage::BBD_BLOCK_SIZE]; for (std::size_t index = 0; index < charBlocks.size(); ++index) { seek(charBlocks.at(index) * OleStorage::BBD_BLOCK_SIZE, true); if (read(formatPageBuffer, OleStorage::BBD_BLOCK_SIZE) != OleStorage::BBD_BLOCK_SIZE) { return false; } unsigned int crun = OleUtil::getU1Byte(formatPageBuffer, 0x1ff); //offset with crun (count of 'run of text') for (unsigned int index2 = 0; index2 < crun; ++index2) { unsigned int offset = OleUtil::getU4Bytes(formatPageBuffer, index2 * 4); unsigned int chpxOffset = 2 * OleUtil::getU1Byte(formatPageBuffer, (crun + 1) * 4 + index2); unsigned int len = OleUtil::getU1Byte(formatPageBuffer, chpxOffset); unsigned int charPos = 0; if (!offsetToCharPos(offset, charPos, myPieces)) { continue; } unsigned int styleId = getStyleIdByCharPos(charPos, myStyleInfoList); CharInfo charInfo = getStyleFromStylesheet(styleId, myStyleSheet).CurrentCharInfo; if (chpxOffset != 0) { getCharInfo(chpxOffset, styleId, formatPageBuffer + 1, len - 1, charInfo); } myCharInfoList.push_back(CharPosToCharInfo(charPos, charInfo)); if (chpxOffset != 0) { InlineImageInfo pictureInfo; if (getInlineImageInfo(chpxOffset, formatPageBuffer + 1, len - 1, pictureInfo)) { myInlineImageInfoList.push_back(CharPosToInlineImageInfo(charPos, pictureInfo)); } } } } delete[] formatPageBuffer; return true; }
bool KGpgMe::encrypt(const QByteArray& inBuffer, Q_ULONG length, QByteArray* outBuffer, QString keyid /* = QString::null */) { gpgme_error_t err = 0; gpgme_data_t in = 0, out = 0; gpgme_key_t keys[2] = { NULL, NULL }; gpgme_key_t* key = NULL; gpgme_encrypt_result_t result = 0; outBuffer->resize(0); if(m_ctx) { err = gpgme_data_new_from_mem(&in, inBuffer.data(), length, 1); if(!err) { err = gpgme_data_new(&out); if(!err) { if(keyid.isNull()) { key = NULL; } else { err = gpgme_get_key(m_ctx, keyid.ascii(), &keys[0], 0); key = keys; } if(!err) { err = gpgme_op_encrypt(m_ctx, key, GPGME_ENCRYPT_ALWAYS_TRUST, in, out); if(!err) { result = gpgme_op_encrypt_result(m_ctx); if (result->invalid_recipients) { KMessageBox::error(kapp->activeWindow(), QString("%1: %2") .arg(i18n("That public key is not meant for encryption")) .arg(result->invalid_recipients->fpr)); } else { err = readToBuffer(out, outBuffer); } } } } } } if(err != GPG_ERR_NO_ERROR && err != GPG_ERR_CANCELED) { KMessageBox::error(kapp->activeWindow(), QString("%1: %2") .arg(gpgme_strsource(err)).arg(gpgme_strerror(err))); } if(err != GPG_ERR_NO_ERROR) clearCache(); if(keys[0]) gpgme_key_unref(keys[0]); if(in) gpgme_data_release(in); if(out) gpgme_data_release(out); return (err == GPG_ERR_NO_ERROR); }
std::size_t JavaInputStream::read(char *buffer, std::size_t maxSize) { JNIEnv *env = AndroidUtil::getEnv(); if (myNeedRepositionToStart) { rewind(env); myNeedRepositionToStart = false; } if (buffer != 0) { return readToBuffer(env, buffer, maxSize); } else { return skip(env, maxSize); } }
bool OleMainStream::readFloatingImages(const char *headerBuffer, const OleEntry &tableEntry) { //Plcspa structure is a table with information for FSPA (File Shape Address) unsigned int beginPicturesInfo = OleUtil::getU4Bytes(headerBuffer, 0x01DA); // address of Plcspa structure if (beginPicturesInfo == 0) { return true; //there's no office art objects } unsigned int picturesInfoLength = OleUtil::getU4Bytes(headerBuffer, 0x01DE); // length of Plcspa structure if (picturesInfoLength < 4) { return false; } OleStream tableStream(myStorage, tableEntry, myBaseStream); std::string buffer; if (!readToBuffer(buffer, beginPicturesInfo, picturesInfoLength, tableStream)) { return false; } static const unsigned int SPA_SIZE = 26; size_t size = calcCountOfPLC(picturesInfoLength, SPA_SIZE); std::vector<unsigned int> picturesBlocks; for (size_t index = 0, tOffset = 0; index < size; ++index, tOffset += 4) { picturesBlocks.push_back(OleUtil::getU4Bytes(buffer.c_str(), tOffset)); } for (size_t index = 0, tOffset = (size + 1) * 4; index < size; ++index, tOffset += SPA_SIZE) { unsigned int spid = OleUtil::getU4Bytes(buffer.c_str(), tOffset); FloatImageInfo info; unsigned int charPos = picturesBlocks.at(index); info.ShapeId = spid; myFloatImageInfoList.push_back(CharPosToFloatImageInfo(charPos, info)); } //DggInfo structure is office art object table data unsigned int beginOfficeArtContent = OleUtil::getU4Bytes(headerBuffer, 0x22A); // address of DggInfo structure if (beginOfficeArtContent == 0) { return true; //there's no office art objects } unsigned int officeArtContentLength = OleUtil::getU4Bytes(headerBuffer, 0x022E); // length of DggInfo structure if (officeArtContentLength < 4) { return false; } shared_ptr<OleStream> newTableStream = new OleStream(myStorage, tableEntry, myBaseStream); shared_ptr<OleStream> newMainStream = new OleStream(myStorage, myOleEntry, myBaseStream); if (newTableStream->open() && newMainStream->open()) { myFLoatImageReader = new DocFloatImageReader(beginOfficeArtContent, officeArtContentLength, newTableStream, newMainStream); myFLoatImageReader->readAll(); } return true; }
bool BinaryReader::readBit() { bool res = (buffer_[curByte_] & (1 << (BITS_IN_BYTE - 1 - curBit_))) != 0; ++curBit_; if(curBit_ == BITS_IN_BYTE) { curBit_ = 0; ++curByte_; if(curByte_ == bytesInBuffer_) readToBuffer(); } return res; }
uint8_t BinaryReader::readByte() { int bitsInNextByte = curBit_; uint8_t res = (buffer_[curByte_] << curBit_); ++curByte_; if(curByte_ == bytesInBuffer_) readToBuffer(); if(bitsInNextByte > 0) res |= (buffer_[curByte_] >> (BITS_IN_BYTE - bitsInNextByte)); curBit_ = bitsInNextByte; return res; }
bool ConfigurationReader::readHome(File* file, char* fileContentBuffer, Actor* actor, Configuration* config) { if (!readToBuffer(file, fileContentBuffer)) { return false; } //DiagnosticOutputStream.sendln(fileContentBuffer); StaticJsonBuffer<READ_FILE_BUFFOR_SIZE> jsonBuffer; JsonObject& root = jsonBuffer.parseObject(fileContentBuffer); if (!root.success()) { DiagnosticOutputStream.sendln("Config parse failed"); return false; } readConfig(root, config); JsonArray& jsonRooms = root[JSON_HOME][JSON_ROOMS].asArray(); readRooms(jsonRooms, actor); }
String Socket::receive(size_t length) { if(file < 0) throw InvalidState("Socket is closed."); if(readBuffer.length < length){ readToBuffer(); if(readBuffer.length < length) throw SocketError("Can not yet read % byte long " "string from socket.", length); } String ret(readBuffer.read(0, length)); readBuffer.chopFront(length); return ret; }
bool KGpgMe::decrypt(const QByteArray& inBuffer, QByteArray* outBuffer) { gpgme_error_t err = 0; gpgme_data_t in = 0, out = 0; gpgme_decrypt_result_t result = 0; outBuffer->resize(0); if(m_ctx) { err = gpgme_data_new_from_mem(&in, inBuffer.data(), inBuffer.size(), 1); if(!err) { err = gpgme_data_new(&out); if(!err) { err = gpgme_op_decrypt(m_ctx, in, out); if(!err) { result = gpgme_op_decrypt_result(m_ctx); if(result->unsupported_algorithm) { KMessageBox::error(kapp->activeWindow(), QString("%1: %2") .arg(i18n("Unsupported algorithm")) .arg(result->unsupported_algorithm)); } else { err = readToBuffer(out, outBuffer); } } } } } if(err != GPG_ERR_NO_ERROR && err != GPG_ERR_CANCELED) { KMessageBox::error(kapp->activeWindow(), QString("%1: %2") .arg(gpgme_strsource(err)).arg(gpgme_strerror(err))); } if(err != GPG_ERR_NO_ERROR) clearCache(); if(in) gpgme_data_release(in); if(out) gpgme_data_release(out); return (err == GPG_ERR_NO_ERROR); }
BOOL RemoteFilesList(SOCKET sd) { printf("list remote files\n"); strcpy_s(request.cmdName, LIST_FILES); if (!WriteRequest(sd, &request)) return IO_ERROR; if (!ReadResponse(sd, &response)) return IO_ERROR; int status = atoi(response.status); if (status != STATUS_OK) return status; int nEntries = atoi(response.dataSize); FSCloudFileEntry *entries = (PFSCloudFileEntry)malloc(sizeof(FSCloudFileEntry)*nEntries); readToBuffer(sd, entries, sizeof(FSCloudFileEntry)*nEntries); for (int i = 0; i < nEntries; ++i) { printf("%-30s %-10s bytes\n", entries[i].name, entries[i].size); } free(entries); return STATUS_OK; }
bool OleMainStream::readSectionsInfoTable(const char *headerBuffer, const OleEntry &tableEntry) { //PlcfSed structure is a section table unsigned int beginOfText = OleUtil::getU4Bytes(headerBuffer, 0x18); //address of text's begin in main stream unsigned int beginSectInfo = OleUtil::getU4Bytes(headerBuffer, 0xca); //address if PlcfSed structure std::size_t sectInfoLen = (std::size_t)OleUtil::getU4Bytes(headerBuffer, 0xce); //length of PlcfSed structure if (sectInfoLen < 4) { return false; } OleStream tableStream(myStorage, tableEntry, myBaseStream); std::string buffer; if (!readToBuffer(buffer, beginSectInfo, sectInfoLen, tableStream)) { return false; } static const unsigned int SED_SIZE = 12; std::size_t decriptorsCount = calcCountOfPLC(sectInfoLen, SED_SIZE); //saving the section offsets (in character positions) std::vector<unsigned int> charPos; for (std::size_t index = 0, tOffset = 0; index < decriptorsCount; ++index, tOffset += 4) { unsigned int ulTextOffset = OleUtil::getU4Bytes(buffer.c_str(), tOffset); charPos.push_back(beginOfText + ulTextOffset); } //saving sepx offsets std::vector<unsigned int> sectPage; for (std::size_t index = 0, tOffset = (decriptorsCount + 1) * 4; index < decriptorsCount; ++index, tOffset += SED_SIZE) { sectPage.push_back(OleUtil::getU4Bytes(buffer.c_str(), tOffset + 2)); } //reading the section properties char tmpBuffer[2]; for (std::size_t index = 0; index < sectPage.size(); ++index) { if (sectPage.at(index) == 0xffffffffUL) { //check for invalid record, to make default section info SectionInfo sectionInfo; sectionInfo.CharPosition = charPos.at(index); mySectionInfoList.push_back(sectionInfo); continue; } //getting number of bytes to read if (!seek(sectPage.at(index), true)) { continue; } if (read(tmpBuffer, 2) != 2) { continue; } std::size_t bytes = 2 + (std::size_t)OleUtil::getU2Bytes(tmpBuffer, 0); if (!seek(sectPage.at(index), true)) { continue; } char *formatPageBuffer = new char[bytes]; if (read(formatPageBuffer, bytes) != bytes) { delete[] formatPageBuffer; continue; } SectionInfo sectionInfo; sectionInfo.CharPosition = charPos.at(index); getSectionInfo(formatPageBuffer + 2, bytes - 2, sectionInfo); mySectionInfoList.push_back(sectionInfo); delete[] formatPageBuffer; } return true; }
void BinaryReader::reset() { ifs_.clear(); ifs_.seekg(0, ifs_.beg); readToBuffer(); }
BinaryReader::BinaryReader(std::istream &ifs) : ifs_(ifs) { buffer_.assign(READER_BUFFER_SIZE, 0); readToBuffer(); }
bool OleMainStream::readBookmarks(const char *headerBuffer, const OleEntry &tableEntry) { //SttbfBkmk structure is a table of bookmark name strings unsigned int beginNamesInfo = OleUtil::getU4Bytes(headerBuffer, 0x142); // address of SttbfBkmk structure std::size_t namesInfoLength = (std::size_t)OleUtil::getU4Bytes(headerBuffer, 0x146); // length of SttbfBkmk structure if (namesInfoLength == 0) { return true; //there's no bookmarks } OleStream tableStream(myStorage, tableEntry, myBaseStream); std::string buffer; if (!readToBuffer(buffer, beginNamesInfo, namesInfoLength, tableStream)) { return false; } unsigned int recordsNumber = OleUtil::getU2Bytes(buffer.c_str(), 0x2); //count of records std::vector<std::string> names; unsigned int offset = 0x6; //initial offset for (unsigned int i = 0; i < recordsNumber; ++i) { if (buffer.size() < offset + 2) { ZLLogger::Instance().println("DocPlugin", "problmes with reading bookmarks names"); break; } unsigned int length = OleUtil::getU2Bytes(buffer.c_str(), offset) * 2; //length of string in bytes ZLUnicodeUtil::Ucs2String name; for (unsigned int j = 0; j < length; j+=2) { char ch1 = buffer.at(offset + 2 + j); char ch2 = buffer.at(offset + 2 + j + 1); ZLUnicodeUtil::Ucs2Char ucs2Char = (unsigned int)ch1 | ((unsigned int)ch2 << 8); name.push_back(ucs2Char); } std::string utf8Name; ZLUnicodeUtil::ucs2ToUtf8(utf8Name, name); names.push_back(utf8Name); offset += length + 2; } //plcfBkmkf structure is table recording beginning CPs of bookmarks unsigned int beginCharPosInfo = OleUtil::getU4Bytes(headerBuffer, 0x14A); // address of plcfBkmkf structure std::size_t charPosInfoLen = (std::size_t)OleUtil::getU4Bytes(headerBuffer, 0x14E); // length of plcfBkmkf structure if (charPosInfoLen == 0) { return true; //there's no bookmarks } if (!readToBuffer(buffer, beginCharPosInfo, charPosInfoLen, tableStream)) { return false; } static const unsigned int BKF_SIZE = 4; std::size_t size = calcCountOfPLC(charPosInfoLen, BKF_SIZE); std::vector<unsigned int> charPage; for (std::size_t index = 0, offset = 0; index < size; ++index, offset += 4) { charPage.push_back(OleUtil::getU4Bytes(buffer.c_str(), offset)); } for (std::size_t i = 0; i < names.size(); ++i) { if (i >= charPage.size()) { break; //for the case if something in these structures goes wrong, to not to lose all bookmarks } Bookmark bookmark; bookmark.CharPosition = charPage.at(i); bookmark.Name = names.at(i); myBookmarks.push_back(bookmark); } return true; }
// auxiliary function to get server response BOOL ReadResponse(SOCKET sd, PFSCloudAnswer response) { return readToBuffer(sd, response, sizeof(FSCloudAnswer)); }