long IOSeek( int seekhandle, int seekmode, long seekpos ) { long pos; int index; // if( !MayRelinquishControl ) return( -1 ); index = seekhandle - FIRST_HANDLE; if( index < 0 || index >= NUM_FILES || Files[ index ].file_type == FILE_INVALID ) { BadFile(); } pos = Files[ index ].seekpos; switch( seekmode ) { case SEEK_SET: pos = seekpos; break; case SEEK_CUR: pos += seekpos; break; case SEEK_END: pos = Files[ index ].filesize + seekpos; break; } if( pos < 0 ) { return( ccode ? ErrorCode() : -1 ); } else { Files[ index ].seekpos = pos; return( pos ); } }
int IOClose( int closehandle ) { long handle; int index; // if( !MayRelinquishControl ) return( -1 ); index = closehandle - FIRST_HANDLE; if( index < 0 || index > NUM_FILES ) return( 0 ); handle = Files[ index ].handle; Files[ index ].handle = 0; _DBG_IO(( "Closing file %8x", handle )); if( index < 0 || index >= NUM_FILES || Files[ index ].file_type == FILE_INVALID ) { BadFile(); } switch( Files[ index ].file_type ) { case FILE_DOS: ccode = INWDOSClose( handle ); break; case FILE_SERVER: ccode = CloseFile(0, 1, handle ); break; } _DBG_IO(( " RC(%d)\r\n", ccode )); return( ccode ? ErrorCode() : 0 ); }
std::vector<Node> LoadAllFromFile(const std::string& filename) { std::ifstream fin(filename.c_str()); if (!fin) { throw BadFile(); } return LoadAll(fin); }
Node LoadFile(const std::string& filename) { std::ifstream fin(filename.c_str()); if (!fin) { throw BadFile(); } return Load(fin); }
void PrivateKey::LoadPem(std::string filename, std::string password) { FILE* f = fopen(filename.c_str(), "r"); if(!f) throw BadFile(); // Get file size if(fseek(f, 0, SEEK_END)) throw BadFile(); size_t file_size = ftell(f); rewind(f); // Read the file char buf[file_size]; if(fread(buf, file_size, 1, f) <= 0) throw BadFile(); fclose(f); LoadBuf(buf, file_size); }
void Certificate::LoadPem(std::string filename, std::string password) { if(ssl_cert) { X509_free(ssl_cert); ssl_cert = NULL; } FILE* f = fopen(filename.c_str(), "r"); if(!f) throw BadFile(); // Get file size if(fseek(f, 0, SEEK_END)) throw BadFile(); size_t file_size = ftell(f); rewind(f); // Read the file char buf[file_size]; if(fread(buf, file_size, 1, f) <= 0) throw BadFile(); fclose(f); if(file_size > UINT_MAX) throw BadFile(); BIO* raw_cert_bio = BIO_new_mem_buf(buf, (uint32_t)file_size); ssl_cert = PEM_read_bio_X509(raw_cert_bio, NULL, NULL, NULL); BIO_free(raw_cert_bio); if(!ssl_cert) { std::string str = std::string(ERR_error_string( ERR_get_error(), NULL)); throw BadCertificate(str); } }
int IORead( int readhandle, char *buff, int len ) { my_file *p; int index; int amt_read; // if( !MayRelinquishControl ) return( -1 ); index = readhandle - FIRST_HANDLE; if( index < 0 || index >= NUM_FILES || Files[ index ].file_type == FILE_INVALID ) { BadFile(); } p = &Files[ index ]; amt_read = p->routine( p->handle, p->seekpos, buff, len ); p->seekpos += amt_read; return( amt_read ); }
int IOWrite( int writehandle, char *buff, int buff_len ) { my_file *p; int index; int written; // if( !MayRelinquishControl ) return( -1 ); index = writehandle - FIRST_HANDLE; if( index < 0 || index >= NUM_FILES || Files[ index ].file_type != FILE_SERVER ) { BadFile(); } p = &Files[ index ]; written = WriteServer( p->handle, p->seekpos, buff, buff_len ); if( written != buff_len ) { written = ErrorCode(); } return( written ); }
std::vector<uint8_t> readBin(const std::wstring& filename) { std::vector<uint8_t> bin; g_hFile = CreateFile(filename.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (g_hFile != INVALID_HANDLE_VALUE) { Guard([]() {CloseHandle(g_hFile); g_hFile = INVALID_HANDLE_VALUE; }); } else { std::cerr << "ERROR: " << GetLastError() << "\n"; throw BadFile("Can't Open File\n"); } bin.resize(GetFileSize(g_hFile, nullptr)); DWORD bytesRead = 0; ReadFile(g_hFile, bin.data(), bin.size(), &bytesRead, nullptr); processBin(bin); return bin; }
// Start the importer! int AsciiImp::DoImport(const TCHAR *name,ImpInterface *ii,Interface *i, BOOL suppressPrompts) { int status = 1; int lastProgress = 0; int progress; BOOL fileValid = FALSE; mtlTab.ZeroCount(); mtlTab.Shrink(); // Grab the interface pointer. ip = i; impip = ii; // Prompt the user with our dialogbox. if (!DialogBoxParam(hInstance, MAKEINTRESOURCE(IDD_ASCIIIMPORT_DLG), ip->GetMAXHWnd(), ImportDlgProc, (LPARAM)this)) { return 1; } // Open the stream pStream = _tfopen(name,_T("rt")); if (!pStream) { return 0; } // Get the file size fseek(pStream, 0, SEEK_END); long fsize = ftell(pStream); fseek(pStream, 0, SEEK_SET); if(resetScene) { impip->NewScene(); } TCHAR* token = NULL; token = GetToken(); if (token) { if (Compare(token, ID_FILEID)) { fileVersion = (int)GetFloat(); if (fileVersion >= 200 && fileVersion <= 200) { fileValid = TRUE; } } } if (!fileValid) { BadFile(); fclose(pStream); return FALSE; } #ifndef USE_IMPNODES ip->DisableSceneRedraw(); #endif // Startup the progress bar. ip->ProgressStart(_T("Importing file..."), TRUE, fn, NULL); long fpos = 0; while ((token = GetToken()) != NULL) { if (ip->GetCancel()) { status = 0; break; } // Update the progress meter // We will eliminate flicker by only calling this when it changes. fpos = ftell(pStream); progress = 100*fpos/fsize; if (progress != lastProgress) { ip->ProgressUpdate(progress); } lastProgress = progress; DebugPrint(_T("Token: %s\n"), token); if (Compare(token, ID_SCENE)) { ImportSceneParams(); } else if (Compare(token, ID_MATERIAL_LIST)) { ImportMaterialList(); } else if (Compare(token, ID_GEOMETRY)) { ImportGeomObject(); } else if (Compare(token, ID_SHAPE)) { ImportShape(); } else if (Compare(token, ID_HELPER)) { ImportHelper(); } else if (Compare(token, ID_CAMERA)) { ImportCamera(); } else if (Compare(token, ID_LIGHT)) { ImportLight(); } else if (Compare(token, ID_COMMENT)) { GetToken(); } #ifndef USE_IMPNODES // ImpNodes doesn't support group creation! else if (Compare(token, ID_GROUP)) { groupMgr.BeginGroup(GetString()); GetToken(); // BlockBegin GetToken(); // GroupDummy HelperObject GetToken(); // GroupDummy BlockBegin SkipBlock(); // GroupDummy } else if (Compare(token, _T("}"))) { groupMgr.EndGroup(ip); } #endif } ip->ProgressEnd(); #ifndef USE_IMPNODES ip->EnableSceneRedraw(); #endif // Close the stream fclose(pStream); return status; }
// prior to running, verify length is over 5 bytes short getRegName(char* szNameBuffer, unsigned char* szHolder, short nLength) { unsigned char nLen1, nLen2, nHighVal, nChar2, nLastChar, nSpaceCount, nTot; long lPos = 0; class BadFile { public: short nReason, nTwo, nThree; BadFile(short n1, short n2=0, short n3=0) {nReason = n1;nTwo=n2;nThree=n3;} }; try { nLen1 = decode(szHolder[lPos++], 0); nSpaceCount = decode(szHolder[lPos++], 1); nChar2 = decode(szHolder[lPos++], 2); nLastChar = decode(szHolder[lPos++], 3); nHighVal = decode(szHolder[lPos++], 4); nLen2 = decode(szHolder[lPos++], 5); nTot = decode(szHolder[lPos++], 6); if ( nLen1 != nLen2 ) throw BadFile(1, nLen1, nLen2); short n = 0; while ( lPos < nLength && n < 80 ) { szNameBuffer[n++] = decode(szHolder[lPos], lPos); lPos++; long lToSkip = (unsigned char)(~szHolder[lPos++]) / 50; short nSum = 7; for ( short k = 0; k < lToSkip; k++ ) { nSum += szHolder[lPos++]/6; } if ( szHolder[lPos++] != nSum ) throw BadFile(7, szHolder[lPos++], nSum); } szNameBuffer[n] = '\0'; strrev(szNameBuffer); short nMyHigh = 0; for ( short n = 0; n < strlen(szNameBuffer); n++ ) { if ( szNameBuffer[n] == ' ' ) nSpaceCount--; if ( szNameBuffer[n] > nMyHigh ) nMyHigh = szNameBuffer[n]; nTot -= szNameBuffer[n]/2; } if ( strlen(szNameBuffer) != nLen1 ) throw BadFile(2, strlen(szNameBuffer), nLen1); if ( nHighVal != nMyHigh ) throw BadFile(3, nHighVal, nMyHigh); if ( nSpaceCount != 0 ) throw BadFile(4, nSpaceCount); if ( szNameBuffer[2] != nChar2 ) throw BadFile(5, szNameBuffer[2], nChar2); if ( szNameBuffer[strlen(szNameBuffer)-1] != nLastChar ) throw BadFile(6, szNameBuffer[strlen(szNameBuffer)-1], nLastChar); if ( nTot != 0 ) throw BadFile(8, nTot); return 1; } catch ( BadFile bf ) { szNameBuffer[0] = '\0'; printf(" >> FAIL on %d because %d != %d << ", bf.nReason, bf.nTwo, bf.nThree); return 0; } }
void CMainWindow::LoadFromIVO(const char* filename) { FILE* f = std::fopen(filename, "rb"); if(!f) BadFile(f); char ivo[4]; int version = -1; SAFE_FREAD(ivo, sizeof(char), 3, f); ivo[3] = '\0'; if(strcmp(ivo, "IVO") != 0) { QMessageBox::information(this, "Error", "Selected file is not a valid IVO model!"); std::fclose(f); return; } SAFE_FREAD(&version, sizeof(version), 1, f); switch(version) { case 1 : { unsigned char renFlags; unsigned paperWid; unsigned paperHei; float resScale; int imageFmt; unsigned char imageQlt; float lineWidt; unsigned stipplLp; SAFE_FREAD(&renFlags, sizeof(renFlags), 1, f); SAFE_FREAD(&paperWid, sizeof(paperWid), 1, f); SAFE_FREAD(&paperHei, sizeof(paperHei), 1, f); SAFE_FREAD(&resScale, sizeof(resScale), 1, f); SAFE_FREAD(&imageFmt, sizeof(imageFmt), 1, f); SAFE_FREAD(&imageQlt, sizeof(imageQlt), 1, f); SAFE_FREAD(&lineWidt, sizeof(lineWidt), 1, f); SAFE_FREAD(&stipplLp, sizeof(stipplLp), 1, f); std::unique_ptr<CMesh> newModelUP(new CMesh()); newModelUP->Deserialize(f); CMesh* newModel = newModelUP.release(); m_openedModel = filename; m_rw2->SetModel(newModel); ((IRenWin*)m_rw3)->SetModel(newModel); if(m_model) delete m_model; m_model = newModel; ClearTextures(); m_rw2->DeserializeSheets(f); m_rw2->UpdateSheetsSize(); std::unordered_map<unsigned, std::string> materials; unsigned char numTextures = 0; SAFE_FREAD(&numTextures, sizeof(numTextures), 1, f); for(int i=numTextures-1; i>=0; i--) { unsigned key; int nameLen; int pathLen; SAFE_FREAD(&key, sizeof(key), 1, f); SAFE_FREAD(&nameLen, sizeof(nameLen), 1, f); std::unique_ptr<char[]> name(new char[nameLen+1]); name[nameLen] = '\0'; SAFE_FREAD(name.get(), sizeof(char), nameLen, f); SAFE_FREAD(&pathLen, sizeof(pathLen), 1, f); std::unique_ptr<char[]> path(new char[pathLen+1]); path[pathLen] = '\0'; SAFE_FREAD(path.get(), sizeof(char), pathLen, f); int hasTexture = 0; SAFE_FREAD(&hasTexture, sizeof(hasTexture), 1, f); if(hasTexture == 1) { int texWidth = 0; int texHeight = 0; int texByteCount = 0; int texFormat = 0; SAFE_FREAD(&texWidth, sizeof(texWidth), 1, f); SAFE_FREAD(&texHeight, sizeof(texHeight), 1, f); SAFE_FREAD(&texByteCount, sizeof(texByteCount), 1, f); SAFE_FREAD(&texFormat, sizeof(texFormat), 1, f); std::unique_ptr<unsigned char[]> imgBits(new unsigned char[texByteCount]); SAFE_FREAD(imgBits.get(), sizeof(unsigned char), texByteCount, f); m_textureImages[key].reset(new QImage(imgBits.get(), texWidth, texHeight, (QImage::Format)texFormat)); (*m_textureImages[key]) = m_textureImages[key]->copy(); } materials[key] = name.get(); m_textures[key] = path.get(); m_rw2->ReserveTextureID(key); ((IRenWin*)m_rw3)->ReserveTextureID(key); if(hasTexture == 1) emit UpdateTexture(m_textureImages[key].get(), key); } m_model->SetMaterials(materials); CSettings& sett = CSettings::GetInstance(); sett.SetRenderFlags( renFlags ); sett.SetPaperWidth( paperWid ); sett.SetPaperHeight( paperHei ); sett.SetResolutionScale( resScale ); sett.SetImageFormat( (CSettings::ImageFormat)imageFmt ); sett.SetImageQuality( imageQlt ); sett.SetLineWidth( lineWidt ); sett.SetStippleLoop( stipplLp ); m_rw2->UpdateSheetsSize(); break; } default : { QMessageBox::information(this, "Error", "Ivo format version " + QString::number(version) + " is not supported by this version of program!"); } } std::fclose(f); }