UT_Error IE_Exp_WordPerfect::_writeDocument(void) { UT_DEBUGMSG(("WordPerfect Exporter: _writeDocument\n")); if (_writeHeader() != UT_OK) return UT_ERROR; m_pListener = _constructListener(); if (!m_pListener) return UT_IE_NOMEMORY; if (getDocRange()) getDoc()->tellListenerSubset(static_cast<PL_Listener *>(m_pListener),getDocRange()); else getDoc()->tellListener(static_cast<PL_Listener *>(m_pListener)); DELETEP(m_pListener); if (m_error == UT_OK) { _UT_String_overwrite(*m_buffer, PTR_TO_DOCUMENT_INDEX, m_ptrToDocument); _UT_String_overwrite(*m_buffer, PTR_TO_FILESIZE_INDEX, m_buffer->length()); _UT_String_overwrite(*m_buffer, m_ptrDesiredFontUseCount, m_desiredFontUseCount); write(m_buffer->c_str(), m_buffer->length()); DELETEP(m_buffer); } return ((m_error) ? UT_IE_COULDNOTWRITE : UT_OK); }
void CryptStream::setEncryptionKey (std::string passphrase, const char *cipherName, const char *ivParam, int keyIterationCount) { if (!_crypt_bio) throw std::logic_error ("CryptStream was not set up to use encryption"); if (!_cipher) { if (cipherName && cipherName[0] != '\0') { this->_cipher = EVP_get_cipherbyname(cipherName); } else this->_cipher = EVP_aes_256_ctr(); } if (!this->_cipher) throw std::runtime_error ("OpenSSL library does not provide requested Cipher mode"); if (ivParam) { this->_iv = ivParam; } else { if (this->_iv.size() == 0) { this->_iv.resize(_cipher->iv_len); if (!RAND_bytes((unsigned char*)&_iv[0], _cipher->iv_len)) throw std::runtime_error ("Could not generate random bytes to create initialization vector"); } } if (keyIterationCount != -1) { this->_pbkdfIterationCount = keyIterationCount; } if (_iv.length() != _cipher->iv_len) throw std::runtime_error ("Invalid initialization vector length does not match cipher"); // Use PBKDF2 to derive the encryption key from the passphrase. Use iv as Salt. unsigned char raw_key[_cipher->key_len + 1]; int r = PKCS5_PBKDF2_HMAC_SHA1(passphrase.c_str(), passphrase.size(), (const unsigned char*)_iv.c_str(), _iv.length(), _pbkdfIterationCount, _cipher->key_len, raw_key); if (r != 1) throw std::runtime_error ("PBKDF2 algorithm to derive encryption key failed"); // enc should be set to 1 for encryption and 0 for decryption. int enc = (_mode == READ) ? 0 : 1; BIO_set_cipher (_crypt_bio, _cipher, raw_key, (const unsigned char*)_iv.c_str(), enc); _bio_chain = BIO_push(_crypt_bio, _bio_chain); // Have a magic string in the beggining to find out if decryption actually works // Cipher must be secure against known-plaintext attacks const std::string encBuffer ("-- FILE -- 85gxk9d7 --"); if (_mode == WRITE) { BIO_write(_bio_chain, encBuffer.data(), encBuffer.size()); } else { std::string rBuffer (encBuffer.size(), '\0'); r = BIO_read(_bio_chain, &rBuffer[0], encBuffer.size()); if (r != encBuffer.size()) throw std::runtime_error ("Unexpected file read error"); if (encBuffer != rBuffer) throw std::runtime_error ("Invalid key"); } if (_mode == WRITE) { _writeHeader(); } this->_initialized = true; }
void CHtmlFile::sendfile(CString strFilePath) { LOG_DEBUG(_T("ファイルを送信 : %s"), strFilePath); CString strArg; int n = strFilePath.Find(_T('?')); if (n != -1) { strArg = strFilePath.Mid(n + 1); strFilePath.Delete(n + 1); } enum { HeaderSize = 1012 }; m_buf.reserve(HeaderSize); CString strExt = GetFileExt(strFilePath); strExt.MakeLower(); try { if (strExt == _T("htm") || strExt == _T("html")) { _writeHeader(MIME_HTML); _writeTemplate(strFilePath); } else if (strExt == _T("css")) { _writeHeader(MIME_CSS); _writeRawFile(strFilePath); } else if (strExt == _T("js")) { _writeHeader("application/x-javascript"); _writeRawFile(strFilePath); } else if (strExt == _T("jpg")) { _writeHeader(MIME_JPEG); _writeRawFile(strFilePath); } else if (strExt == _T("gif")) { _writeHeader(MIME_GIF); _writeRawFile(strFilePath); } else if (strExt == _T("png")) { _writeHeader(MIME_PNG); _writeRawFile(strFilePath); } else { _writeHeader(MIME_TEXT); _writeRawFile(strFilePath); } } catch (StreamException& e) { // 404 LOG_ERROR("%s : 404エラーを送信", e.msg); m_buf.clear(); _writeLine(HTTP_SC_NOTFOUND); _writeLine("%s %s", HTTP_HS_SERVER, PCX_AGENT); _writeLine("%s %s", HTTP_HS_CONNECTION, "close"); _writeLine("%s %s", HTTP_HS_CONTENT, MIME_HTML); } m_psock->send(m_buf.data(), m_buf.size()); }
Data OuterConfig::serialize() const { try { Serializer serializer(Serializer::StringSize(HEADER) + Serializer::DataSize(kdfParameters) + encryptedInnerConfig.size()); _writeHeader(&serializer); serializer.writeData(kdfParameters); serializer.writeTailData(encryptedInnerConfig); return serializer.finished(); } catch (const exception &e) { LOG(ERROR, "Error serializing CryConfigEncryptor: {}", e.what()); throw; // This is a programming logic error. Pass through exception. } }
UT_Error IE_Imp_PalmDoc::_loadFile(GsfInput * input) { m_pdfp = (GsfInput*)g_object_ref (G_OBJECT (input)); if (!m_pdfp) { return UT_ERROR; } UT_Error error; X_CleanupIfError(error,_writeHeader(m_pdfp)); X_CleanupIfError(error,_parseFile(m_pdfp)); error = UT_OK; Cleanup: g_object_unref(G_OBJECT(m_pdfp)); return error; }
bool FrameStreamWriter::_init(GeneratorIDType processedRawFrameGeneratorID, GeneratorIDType depthFrameGeneratorID, GeneratorIDType pointCloudFrameGeneratorID) { _frameCount = 0; if(!_stream.good()) { logger(LOG_ERROR) << "FrameStreamWriter: Failed to open file." << std::endl; return false; } _header.generatorIDs[0] = processedRawFrameGeneratorID; _header.generatorIDs[1] = depthFrameGeneratorID; _header.generatorIDs[2] = pointCloudFrameGeneratorID; if(!_writeHeader()) { logger(LOG_ERROR) << "FrameStreamWriter: Failed to write stream header." << std::endl; return false; } return true; }
/* Import data from a plain text file \param szFilename Name of file to import Each line terminator is taken to be a paragraph break */ UT_Error IE_Imp_Text::_loadFile(GsfInput * fp) { ImportStream *pStream = 0; UT_Error error; // First we try to determine the encoding. if (_recognizeEncoding(fp) == UT_OK) getDoc()->setEncodingName(m_szEncoding); // Call encoding dialog if (!m_bIsEncoded || m_bExplicitlySetEncoding || _doEncodingDialog(m_szEncoding)) { X_CleanupIfError(error,_constructStream(pStream,fp)); X_CleanupIfError(error,_writeHeader(fp)); X_CleanupIfError(error,_parseStream(pStream)); error = UT_OK; } else error = UT_ERROR; Cleanup: delete pStream; return error; }