void sys::File::readInto(char *buffer, Size_T size) { static const size_t MAX_READ_SIZE = std::numeric_limits<DWORD>::max(); size_t bytesRead = 0; size_t bytesRemaining = size; while (bytesRead < size) { // Determine how many bytes to read const DWORD bytesToRead = static_cast<DWORD>( std::min(MAX_READ_SIZE, bytesRemaining)); // Read from file DWORD bytesThisRead = 0; if (!ReadFile(mHandle, buffer + bytesRead, bytesToRead, &bytesThisRead, NULL)) { throw sys::SystemException(Ctxt("Error reading from file")); } else if (bytesThisRead == 0) { //! ReadFile does not fail when finding the EOF -- // instead it reports 0 bytes read, so this stops an infinite loop // from Unexpected EOF throw sys::SystemException(Ctxt("Unexpected end of file")); } bytesRead += bytesThisRead; bytesRemaining -= bytesThisRead; } }
double Utilities::remapZeroTo360(double degree) { double delta = degree; while (delta < 0.) { delta += 360.; if (degree == delta) { throw except::Exception(Ctxt( "Value [" + str::toString(degree) + "] is too small to remap into the [0:360] range")); } } while (delta > 360.) { delta -= 360.; if (degree == delta) { throw except::Exception(Ctxt( "Value [" + str::toString(degree) + "] is too large to remap into the [0:360] range")); } } return delta; }
void mt::ThreadGroup::joinAll() { bool failed = false; // Keep track of which threads we've already joined. for (; mLastJoined < mThreads.size(); mLastJoined++) { try { mThreads[mLastJoined]->join(); } catch (...) { failed = true; } } if (!mExceptions.empty()) { std::string messageString("Exceptions thrown from ThreadGroup in the following order:\n"); for (size_t ii=0; ii<mExceptions.size(); ++ii) { messageString += mExceptions.at(ii).toString(); } throw except::Exception(Ctxt(messageString)); } if (failed) throw except::Error(Ctxt("ThreadGroup could not be joined")); }
const ScratchMemory::Segment& ScratchMemory::lookupSegment( const std::string& key, size_t indexBuffer) const { if (mBuffer.data == NULL) { std::ostringstream oss; oss << "Tried to get scratch memory for \"" << key << "\" before running setup."; throw except::Exception(Ctxt(oss.str())); } std::map<std::string, Segment>::const_iterator iterSeg = mSegments.find(key); if (iterSeg == mSegments.end()) { std::ostringstream oss; oss << "Scratch memory segment was not found for \"" << key << "\""; throw except::Exception(Ctxt(oss.str())); } const Segment& segment = iterSeg->second; if (indexBuffer >= segment.buffers.size()) { std::ostringstream oss; oss << "Trying to get buffer index " << indexBuffer << " for \"" << key << "\", which has only " << segment.buffers.size() << " buffers"; throw except::Exception(Ctxt(oss.str())); } return segment; }
std::auto_ptr<scene::ProjectionModel> Utilities::getProjectionModel(const DerivedData* data) { const int lookDir = getSideOfTrack(data); scene::Errors errors; ::getErrors(*data, errors); std::auto_ptr<scene::SceneGeometry> geom(getSceneGeometry(data)); const six::ProjectionType gridType = data->measurement->projection->projectionType; std::auto_ptr<scene::ProjectionModel> projModel; switch (gridType) { case six::ProjectionType::PLANE: { const six::sidd::PlaneProjection* const plane = reinterpret_cast<six::sidd::PlaneProjection*>( data->measurement->projection.get()); projModel.reset(new scene::PlaneProjectionModel( geom->getSlantPlaneZ(), plane->productPlane.rowUnitVector, plane->productPlane.colUnitVector, plane->referencePoint.ecef, data->measurement->arpPoly, plane->timeCOAPoly, lookDir, errors)); break; } case six::ProjectionType::GEOGRAPHIC: { const six::sidd::MeasurableProjection* const geo = reinterpret_cast<six::sidd::MeasurableProjection*>( data->measurement->projection.get()); projModel.reset(new scene::GeodeticProjectionModel( geom->getSlantPlaneZ(), geo->referencePoint.ecef, data->measurement->arpPoly, geo->timeCOAPoly, lookDir, errors)); break; } case six::ProjectionType::POLYNOMIAL: case six::ProjectionType::CYLINDRICAL: case six::ProjectionType::NOT_SET: throw except::Exception(Ctxt("Grid type not supported: " + gridType.toString())); default: throw except::Exception(Ctxt("Invalid grid type: " + gridType.toString())); } return projModel; }
nitf::Pair nitf::HashTable::find(const std::string& key) throw(except::NoSuchKeyException) { if (key.length() == 0) throw except::NoSuchKeyException(Ctxt("Empty key value")); nitf_Pair* x = nitf_HashTable_find(getNative(), key.c_str()); if (!x) throw except::NoSuchKeyException(Ctxt(key)); return nitf::Pair(x); }
void net::ssl::SSLConnectionClientFactory::initializeContext() { #if defined(USE_OPENSSL) SSL_library_init(); SSL_load_error_strings(); #if defined(OPENSSL_0_9_8) SSL_METHOD *method = SSLv23_client_method(); #else const SSL_METHOD *method = SSLv23_client_method(); #endif if(method == NULL) { throw net::ssl::SSLException(Ctxt(FmtX("SSLv23_client_method failed"))); } mCtx = SSL_CTX_new(method); if(mCtx == NULL) { throw net::ssl::SSLException(Ctxt(FmtX("SSL_CTX_new failed"))); } if(mClientAuthentication) { // Load our keys and certificates if(!(SSL_CTX_use_certificate_file(mCtx, mKeyfile.c_str(), SSL_FILETYPE_PEM))) throw net::ssl::SSLException(Ctxt("Can't read certificate file")); //SSL_CTX_set_default_passwd_cb(mCtx, password_cb); if(!(SSL_CTX_use_PrivateKey_file(mCtx, mKeyfile.c_str(), SSL_FILETYPE_PEM))) throw net::ssl::SSLException(Ctxt("Can't read key file")); // Load the CAs we trust if(!(SSL_CTX_load_verify_locations(mCtx, mCAList.c_str(), 0))) throw net::ssl::SSLException(Ctxt("Can't read CA list")); // Set our cipher list if(mCiphers) { SSL_CTX_set_cipher_list(mCtx, mCiphers); } } // Load randomness /* if(!RAND_load_file("random.pem", 1024*1024)) throw net::ssl::SSLException(Ctxt("Couldn't load randomness")); */ #if (OPENSSL_VERSION_NUMBER < 0x00905100L) SSL_CTX_set_verify_depth(mCtx, 1); #endif #endif }
void FileHeader::blockReadHeader(io::SeekableInputStream& inStream, size_t blockSize, std::string& headerBlock) { static const char ERROR_MSG[] = "CPHD file malformed: Header must terminate with '\\f\\n'"; mem::ScopedArray<sys::byte> buf(new sys::byte[blockSize + 1]); std::fill_n(buf.get(), blockSize + 1, 0); headerBlock.clear(); // read each block in succession size_t terminatorLoc = std::string::npos; size_t totalBytesRead = 0; while (inStream.read(buf.get(), blockSize) != io::InputStream::IS_EOF && terminatorLoc == std::string::npos) { std::string thisBlock = buf.get(); // find the terminator in the block terminatorLoc = thisBlock.find('\f'); if (terminatorLoc != std::string::npos) { // read one more byte if our block missed the trailing '\n' if (terminatorLoc == thisBlock.length() - 1) { sys::byte c(0); inStream.read(&c, 1); thisBlock.insert(thisBlock.length(), &c, 1); } // verify proper formatting if (thisBlock[terminatorLoc + 1] != '\n') { throw except::Exception(Ctxt(ERROR_MSG)); } // trim off anything after the terminator thisBlock = thisBlock.substr(0, terminatorLoc); } // build the header headerBlock += thisBlock; // verify we aren't past 10MB in the header -- // this stops processing before we start reading into the // image data and potentially run out of memory totalBytesRead += thisBlock.length(); if (totalBytesRead > MAX_HEADER_SIZE) { throw except::Exception(Ctxt(ERROR_MSG)); } } }
void ImageBlocker::findSegmentRange(size_t startRow, size_t numRows, size_t& firstSegIdx, size_t& startBlockWithinFirstSeg, size_t& lastSegIdx, size_t& lastBlockWithinLastSeg) const { if (numRows == 0) { firstSegIdx = startBlockWithinFirstSeg = lastSegIdx = lastBlockWithinLastSeg = std::numeric_limits<size_t>::max(); } else { // Figure out which segment we're starting in size_t startRowWithinFirstSeg; findSegment(startRow, firstSegIdx, startRowWithinFirstSeg, startBlockWithinFirstSeg); if (!isFirstRowInBlock(startRowWithinFirstSeg)) { std::ostringstream ostr; ostr << "Start row " << startRow << " is local row " << startRowWithinFirstSeg << " within segment " << firstSegIdx << ". The local row must be a multiple of " << mNumRowsPerBlock << "."; throw except::Exception(Ctxt(ostr.str())); } // Figure out which segment we're ending in const size_t lastRow = startRow + numRows - 1; size_t lastRowWithinLastSeg; findSegment(lastRow, lastSegIdx, lastRowWithinLastSeg, lastBlockWithinLastSeg); const size_t endRowWithinLastSeg = lastRowWithinLastSeg + 1; // Make sure we're ending on a full block if (!(endRowWithinLastSeg == mNumRows[lastSegIdx] || isFirstRowInBlock(endRowWithinLastSeg))) { std::ostringstream ostr; ostr << "Last row " << lastRow << " is local row " << lastRowWithinLastSeg << " within segment " << lastSegIdx << ". This must land on a full block."; throw except::Exception(Ctxt(ostr.str())); } } }
int sys::ExecPipe::closePipe() { if (!mOutStream) { throw except::IOException( Ctxt("The stream is already closed")); } // in case it fails FILE* tmp = mOutStream; mOutStream = NULL; int exitStatus = 0; const int encodedStatus = pclose(tmp); if (WIFEXITED(encodedStatus)) { // get exit code from child process exitStatus = WEXITSTATUS(encodedStatus); } else { //! unix gives a little better granularity on errors // due to uncaught signal (ex. segFault) if (WIFSIGNALED(encodedStatus)) { throw except::IOException( Ctxt("The child process was terminated by " \ "an uncaught signal: " + str::toString<int>(WTERMSIG(encodedStatus)))); } // due to unplanned stoppage if (WIFSTOPPED(encodedStatus)) { throw except::IOException( Ctxt("The child process was unexpectedly stopped: " + str::toString<int>(WSTOPSIG(encodedStatus)))); } // all other errors sys::SocketErr err; throw except::IOException( Ctxt("Failure while closing stream to child process: " + err.toString())); } return exitStatus; }
std::string sys::OSUnix::getTempName(const std::string& path, const std::string& prefix) const { std::string name; #if defined(_USE_MKSTEMP) || defined(__linux__) || defined(__linux) || defined(linux__) std::string pathname(path); pathname += "/" + prefix + "XXXXXX"; std::vector<char> fullPath(pathname.size() + 1); strcpy(&fullPath[0], pathname.c_str()); int ret = mkstemp(&fullPath[0]); if (ret == -1) name = ""; else { name = &fullPath[0]; } #else CharWrapper tempname = tempnam(path.c_str(), prefix.c_str()); if (tempname.get() == NULL) name = ""; else { name = tempname.get(); sys::File (name, sys::File::WRITE_ONLY, sys::File::CREATE); } #endif if (name.empty()) { throw except::Exception(Ctxt("Unable to create a temporary file")); } return name; }
// Use packed bootstrapping, so we can bootstrap all in just one go. void packedRecrypt(const CtPtrs& cPtrs, const std::vector<zzX>& unpackConsts, const EncryptedArray& ea) { FHEPubKey& pKey = (FHEPubKey&)cPtrs[0]->getPubKey(); // Allocate temporary ciphertexts for the recryption int nPacked = divc(cPtrs.size(), ea.getDegree()); // ceil(totoalNum/d) std::vector<Ctxt> cts(nPacked, Ctxt(pKey)); repack(CtPtrs_vectorCt(cts), cPtrs, ea); // pack ciphertexts // cout << "@"<< lsize(cts)<<std::flush; for (Ctxt& c: cts) { // then recrypt them c.reducePtxtSpace(2); // we only have recryption data for binary ctxt #ifdef DEBUG_PRINTOUT ZZX ptxt; decryptAndPrint((cout<<" before recryption "), c, *dbgKey, *dbgEa); dbgKey->Decrypt(ptxt, c); c.DummyEncrypt(ptxt); decryptAndPrint((cout<<" after recryption "), c, *dbgKey, *dbgEa); #else pKey.reCrypt(c); #endif } unpack(cPtrs, CtPtrs_vectorCt(cts), ea, unpackConsts); }
Regex& Regex::compile(const std::string& pattern) { mPattern = pattern; destroy(); // TODO: So, I would like an RAII object for this. But, this object would // need to call pcre2_compile() in its constructor rather than take // ownership of a pointer (so that it has access to the error code // information on failure) and its copy constructor / assignment // operator would need to hold onto mPattern. At that point the // class is close to being this Regex class itself. static const int FLAGS = PCRE2_DOTALL; int errorCode; PCRE2_SIZE errorOffset; mPCRE = pcre2_compile(reinterpret_cast<PCRE2_SPTR>(mPattern.c_str()), mPattern.length(), FLAGS, &errorCode, &errorOffset, NULL); // Use default compile context if (mPCRE == NULL) { std::ostringstream ostr; ostr << "PCRE compilation failed at offset " << errorOffset << ": " << getErrorMessage(errorCode); throw RegexException(Ctxt(ostr.str())); } return *this; }
void CPHDWriter::addImage(const T* image, const types::RowCol<size_t>& dims, const sys::ubyte* vbmData) { if (mElementSize != sizeof(T)) { throw except::Exception(Ctxt( "Incorrect buffer data type used for metadata!")); } //! If this is the first time you called addImage. We will clear // out the metadata image here and start adding it manually. if (mCPHDData.empty()) { mMetadata.data.arraySize.clear(); mMetadata.data.numCPHDChannels = 0; } mVBMData.push_back(vbmData); mCPHDData.push_back(reinterpret_cast<const sys::ubyte*>(image)); mCPHDSize += dims.normL1() * mElementSize; mVBMSize += dims.row * mMetadata.data.getNumBytesVBP(); mMetadata.data.numCPHDChannels += 1; mMetadata.data.arraySize.push_back(ArraySize(dims.row, dims.col)); }
void ImageSubheader::setPixelInformation(std::string pvtype, nitf::Uint32 nbpp, nitf::Uint32 abpp, std::string justification, std::string irep, std::string icat, std::vector<nitf::BandInfo>& bands) throw(nitf::NITFException) { const size_t bandCount = bands.size(); nitf_BandInfo ** bandInfo = (nitf_BandInfo **)NITF_MALLOC( sizeof(nitf_BandInfo*) * bandCount); if (!bandInfo) { throw nitf::NITFException(Ctxt(FmtX("Out of Memory"))); } for (size_t i = 0; i < bandCount; i++) { bandInfo[i] = nitf_BandInfo_clone(bands[i].getNative(), &error); if (!bandInfo[i]) throw nitf::NITFException(&error); } NITF_BOOL x = nitf_ImageSubheader_setPixelInformation(getNativeOrThrow(), pvtype.c_str(), nbpp, abpp, justification.c_str(), irep.c_str(), icat.c_str(), static_cast<nitf::Uint32>(bandCount), bandInfo, &error); if (!x) throw nitf::NITFException(&error); }
void sys::File::create(const std::string& str, int accessFlags, int creationFlags) { // If the truncate bit is on AND the file does exist, // we need to set the mode to TRUNCATE_EXISTING if ((creationFlags & sys::File::TRUNCATE) && sys::OS().exists(str) ) { creationFlags = TRUNCATE_EXISTING; } else { creationFlags = ~sys::File::TRUNCATE & creationFlags; } mHandle = CreateFile(str.c_str(), accessFlags, FILE_SHARE_READ, NULL, creationFlags, FILE_ATTRIBUTE_NORMAL, NULL); if (mHandle == SYS_INVALID_HANDLE) { throw sys::SystemException(Ctxt(FmtX("Error opening file: [%s]", str.c_str()))); } mPath = str; }
void sys::File::flush() { if (!FlushFileBuffers(mHandle)) { throw sys::SystemException(Ctxt("Error flushing file " + mPath)); } }
BufferedReader::BufferedReader(const std::string& file, void* buffer, size_t size, bool adopt) : mMaxBufferSize(size), mScopedBuffer(adopt ? static_cast<char*>(buffer) : NULL), mBuffer(static_cast<char*>(buffer)), mPosition(0), mBufferSize(0), mTotalRead(0), mBlocksRead(0), mPartialBlocks(0), mElapsedTime(0), mFile(file, sys::File::READ_ONLY, sys::File::EXISTING), mFileLen(mFile.length()) { if (mMaxBufferSize == 0) { throw except::Exception(Ctxt( "BufferedReaders must have a buffer size greater than zero")); } //! Start off by reading a block readNextBuffer(); }
void BufferedReader::readImpl(void* buf, size_t size) { //! Ensure there is enough data to read if (tell() + static_cast<nitf::Off>(size) > getSize()) { throw except::Exception(Ctxt( "Attempting to read past the end of a buffered reader.")); } char* bufPtr = static_cast<char*>(buf); size_t amountLeftToRead = size; size_t offset = 0; while (amountLeftToRead) { const size_t readSize = std::min(amountLeftToRead, mBufferSize - mPosition); memcpy(bufPtr + offset, mBuffer + mPosition, readSize); mPosition += readSize; offset += readSize; amountLeftToRead -= readSize; if (mPosition >= mBufferSize) { readNextBuffer(); } } }
void sys::OSUnix::setEnv(const std::string& var, const std::string& val, bool overwrite) { int ret; #ifdef HAVE_SETENV ret = setenv(var.c_str(), val.c_str(), overwrite); #else // putenv() will overwrite the value if it already exists, so if we don't // want to overwrite, we do nothing when getenv() indicates the variable's // already set if (overwrite || getenv(var.c_str()) == NULL) { // putenv() isn't guaranteed to make a copy of the string, so we need // to allocate it and let it leak. Ugh. char* const strBuffer = new char[var.length() + 1 + val.length() + 1]; ::sprintf(strBuffer, "%s=%s", var.c_str(), val.c_str()); ret = putenv(strBuffer); } else { ret = 0; } #endif if(ret != 0) { throw sys::SystemException(Ctxt( "Unable to set unix environment variable " + var)); } }
void sys::File::writeFrom(const char *buffer, Size_T size) { static const size_t MAX_WRITE_SIZE = std::numeric_limits<DWORD>::max(); size_t bytesRemaining = size; size_t bytesWritten = 0; while (bytesWritten < size) { // Determine how many bytes to write const DWORD bytesToWrite = static_cast<DWORD>( std::min(MAX_WRITE_SIZE, bytesRemaining)); // Write the data DWORD bytesThisWrite = 0; if (!WriteFile(mHandle, buffer + bytesWritten, bytesToWrite, &bytesThisWrite, NULL)) { throw sys::SystemException(Ctxt("Writing from file")); } // Accumulate this write until we are done bytesRemaining -= bytesThisWrite; bytesWritten += bytesThisWrite; } }
void io::FileUtils::forceMkdir(std::string dirname) throw (except::IOException) { sys::OS os; if (os.exists(dirname)) { if (!os.isDirectory(dirname)) throw except::IOException( Ctxt( "Cannot create directory - file already exists")); } else { if (!os.makeDirectory(dirname)) throw except::IOException(Ctxt("Cannot create directory")); } }
//! Get native object virtual T * getNativeOrThrow() const throw(nitf::NITFException) { T* val = getNative(); if (val) return val; throw nitf::NITFException(Ctxt("Invalid handle")); }
XMLElem ComplexXMLParser10x::convertRMAToXML( const RMA* rma, XMLElem parent) const { XMLElem rmaXML = newElement("RMA", parent); createString("RMAlgoType", six::toString<six::RMAlgoType>(rma->algoType), rmaXML); if (rma->rmat.get() && !rma->rmcr.get() && !rma->inca.get()) { convertRMATToXML(rma->rmat.get(), rmaXML); } else if (!rma->rmat.get() && rma->rmcr.get() && !rma->inca.get()) { convertRMCRToXML(rma->rmcr.get(), rmaXML); } else if (!rma->rmat.get() && !rma->rmcr.get() && rma->inca.get()) { convertINCAToXML(rma->inca.get(), rmaXML); } else { throw except::Exception(Ctxt( "One of RMAT, RMCR or INCA must be defined in SICD 1.0.")); } return rmaXML; }
XMLElem ComplexXMLParser10x::convertImageFormationAlgoToXML( const PFA* pfa, const RMA* rma, const RgAzComp* rgAzComp, XMLElem parent) const { if (pfa && !rma && !rgAzComp) { return convertPFAToXML(pfa, parent); } else if (!pfa && rma && !rgAzComp) { return convertRMAToXML(rma, parent); } else if (!pfa && !rma && rgAzComp) { return convertRgAzCompToXML(rgAzComp, parent); } else if (!pfa && !rma && !rgAzComp) { //! This will occur when set to OTHER. We do not want to include // a specialized image formation algorithm so we return NULL. return NULL; } else { throw except::Exception(Ctxt( "Only one PFA, RMA, or RgAzComp can be defined in SICD 1.0")); } }
void byteSwapAndScale(const void* input, size_t elementSize, const types::RowCol<size_t>& dims, const double* scaleFactors, size_t numThreads, std::complex<float>* output) { switch (elementSize) { case 2: ::byteSwapAndScale<sys::Int8_T>(input, dims, scaleFactors, numThreads, output); break; case 4: ::byteSwapAndScale<sys::Int16_T>(input, dims, scaleFactors, numThreads, output); break; case 8: ::byteSwapAndScale<float>(input, dims, scaleFactors, numThreads, output); break; default: throw except::Exception(Ctxt( "Unexpected element size " + str::toString(elementSize))); } }
void sys::OSWin32::setEnv(const std::string& var, const std::string& val, bool overwrite) { BOOL ret = SetEnvironmentVariable(var.c_str(), val.c_str()); if(!ret) throw sys::SystemException(Ctxt(FmtX("Unable to set windows environment variable %s", var.c_str()))); }
std::string net::URLParams::getFirst(std::string key) const throw (except::NoSuchKeyException) { net::URLParams::Params::const_iterator it = mParams.find(key); if (it == mParams.end() || it->second.size() == 0) throw except::NoSuchKeyException(Ctxt(key)); return it->second.front(); }
std::string sys::OSUnix::getEnv(const std::string& s) const { const char* envVal = getenv(s.c_str()); if (envVal == NULL) throw sys::SystemException( Ctxt("Unable to get unix environment variable " + s)); return std::string(envVal); }
size_t sys::OSUnix::getNumCPUs() const { #ifdef _SC_NPROCESSORS_ONLN return sysconf(_SC_NPROCESSORS_ONLN); #else throw except::NotImplementedException(Ctxt("Unable to get the number of CPUs")); #endif }