bool ApplyEnsemblePatch(const CString old_file, const CString patch_file, const CString new_file) { BYTE* old_buffer = NULL ; BYTE* patch_buffer = NULL; DWORD dwOld; DWORD dwPatch; if (!ReadFileToBuffer(old_file, old_buffer, &dwOld)) return false; if (!ReadFileToBuffer(patch_file, patch_buffer, &dwPatch)) return false; courgette::SourceStream old_stream; courgette::SourceStream patch_stream; old_stream.Init(old_buffer, dwOld); patch_stream.Init(patch_buffer, dwPatch); courgette::SinkStream new_stream; courgette::Status status = courgette::ApplyEnsemblePatch(&old_stream, &patch_stream, &new_stream); if (status != courgette::C_OK) return false; return WriteBufferToFile(new_file, reinterpret_cast<const BYTE*>(new_stream.Buffer()), new_stream.Length()); }
static void test_sparse_files_8(void) { Log(LOG_LEVEL_VERBOSE, "Special Case: File ending with few (DEV_BSIZE-1) zeroes," " at block size barrier; so the last bytes written are a hole and are seek()ed," " but the output file can't be sparse since the hole isn't block-sized"); char *buf = xmalloc(TESTFILE_SIZE + DEV_BSIZE - 1); FillBufferWithGarbage(buf, TESTFILE_SIZE); memset(&buf[TESTFILE_SIZE], 0, DEV_BSIZE - 1); WriteBufferToFile(srcfile, buf, TESTFILE_SIZE + DEV_BSIZE - 1); /* ACTUAL TEST */ bool ret = CopyRegularFileDisk(srcfile, dstfile); assert_true(ret); if (SPARSE_SUPPORT_OK) { bool is_sparse = FileIsSparse(dstfile); assert_false(is_sparse); } bool data_ok = CompareFileToBuffer(dstfile, buf, TESTFILE_SIZE + DEV_BSIZE - 1); assert_true(data_ok); free(buf); test_has_run[8] = true; success [8] = true; }
static void test_sparse_files_1(void) { Log(LOG_LEVEL_VERBOSE, "No zeros in the file, the output file must be non-sparse"); char *buf = xmalloc(TESTFILE_SIZE); FillBufferWithGarbage(buf, TESTFILE_SIZE); WriteBufferToFile(srcfile, buf, TESTFILE_SIZE); /* ACTUAL TEST */ bool ret = CopyRegularFileDisk(srcfile, dstfile); assert_true(ret); if (SPARSE_SUPPORT_OK) { bool is_sparse = FileIsSparse(dstfile); assert_false(is_sparse); } bool data_ok = CompareFileToBuffer(dstfile, buf, TESTFILE_SIZE); assert_true(data_ok); free(buf); test_has_run[1] = true; success [1] = true; }
static void test_sparse_files_7(void) { Log(LOG_LEVEL_VERBOSE, "File ending with many zeroes, the output file must be sparse"); char *buf = xmalloc(TESTFILE_SIZE); FillBufferWithGarbage(buf, TESTFILE_SIZE); memset(&buf[TESTFILE_SIZE - LONG_REGION], 0, LONG_REGION); WriteBufferToFile(srcfile, buf, TESTFILE_SIZE); /* ACTUAL TEST */ bool ret = CopyRegularFileDisk(srcfile, dstfile); assert_true(ret); if (SPARSE_SUPPORT_OK) { bool is_sparse = FileIsSparse(dstfile); assert_true(is_sparse); } bool data_ok = CompareFileToBuffer(dstfile, buf, TESTFILE_SIZE); assert_true(data_ok); free(buf); test_has_run[7] = true; success [7] = true; }
///--------------------------------------------------------------------------------- /// ///--------------------------------------------------------------------------------- void SaveFileJob::Run() { bool* success = new bool( WriteBufferToFile( m_filePath, m_buffer, m_bufferSize ) ); SetCallbackArg( (void*)success ); }
//----------------------------------------------------------------------------- // Models are already converted in a pre-pass. //----------------------------------------------------------------------------- bool CreateTargetFile_Model( const char *pSourceName, const char *pTargetName, bool bWriteToZip ) { // model component should be present CUtlBuffer targetBuffer; if ( !scriptlib->ReadFileToBuffer( pTargetName, targetBuffer ) ) { return false; } // no conversion to write, but possibly zipped bool bSuccess = WriteBufferToFile( pTargetName, targetBuffer, bWriteToZip, WRITE_TO_DISK_NEVER ); return bSuccess; }
void FetchIntegrationFromWeb() { const int MAX_SIZE = 2 * 1024 * 1024; char* buffer = new char[ MAX_SIZE ]; if( buffer != nullptr ) { DWORD nBytesRead = 0; if( DoBlockingHttpGet( "bin/RA_Integration.dll", buffer, MAX_SIZE, &nBytesRead ) ) WriteBufferToFile( "RA_Integration.dll", buffer, nBytesRead ); delete[] ( buffer ); buffer = nullptr; } }
bool CreateSceneImageFile( char const *pchModPath, bool bWriteToZip, bool bLittleEndian, bool bQuiet, DiskWriteMode_t eWriteModeForConversions ) { CUtlBuffer targetBuffer; const char *pFilename = bLittleEndian ? "scenes/scenes.image" : "scenes/scenes.360.image"; CDefaultStatus statusHelper; bool bSuccess = g_pSceneImage->CreateSceneImageFile( targetBuffer, pchModPath, bLittleEndian, bQuiet, &statusHelper ); if ( bSuccess ) { bSuccess = WriteBufferToFile( pFilename, targetBuffer, bWriteToZip, eWriteModeForConversions ); } return bSuccess; }
//----------------------------------------------------------------------------- // Purpose: Generate .360 compiled caption files //----------------------------------------------------------------------------- bool CreateTargetFile_CCDAT( const char *pSourceName, const char *pTargetName, bool bWriteToZip ) { CUtlBuffer targetBuffer; bool bOk = false; if ( !scriptlib->ReadFileToBuffer( pSourceName, targetBuffer ) ) { return false; } if ( SwapClosecaptionFile( targetBuffer.Base() ) ) { bOk = WriteBufferToFile( pTargetName, targetBuffer, bWriteToZip, g_WriteModeForConversions ); } return bOk; }
// main function to do the spkg decrypt/unpackage int do_spp_decrypt(char* pInPath, char* pOutPath, char* pKeyName) { u8* pMySpp = NULL; u64 dec_size = 0; u64 hdr_len = 0; uint32_t dwBytesRead = 0; sce_header_t* pSceHdr = NULL; int retval = -1; // validate input params if ( (pInPath == NULL) || (pOutPath == NULL) ) goto exit; // read the spp file into a buffer (alloc a new buffer) if ( ReadFileToBuffer(pInPath,(uint8_t**)&pMySpp, 0x00, &dwBytesRead, TRUE) != STATUS_SUCCESS ) { printf("failed to read in file:%s, exiting...\n", pInPath); goto exit; } /// Unpackage the "PKG" types /// if ( decrypt_spp(pMySpp, &dec_size, pKeyName) != STATUS_SUCCESS ) { printf("\n!!ERROR!! FAILED to decrypt SPP file:%s\n\n", pInPath); goto exit; } // setup the SCE header pSceHdr = (sce_header_t*)pMySpp;//SIZE_SPKG_HDR wbe64((u8*)&hdr_len, pSceHdr->header_len); // write the decrypted file to disk if ( WriteBufferToFile(pOutPath, (pMySpp+hdr_len), (uint32_t)dec_size, FALSE, 0, NULL) != STATUS_SUCCESS ) { printf("failed to write to file:%s, exiting...\n", pOutPath); goto exit; } // status success retval = STATUS_SUCCESS; exit: // free the alloc'd memory if (pMySpp != NULL) free(pMySpp); return retval; }
//----------------------------------------------------------------------------- // Purpose: Generate .360 compiled reslist files // Reslist files are processed for the unique consumption of Queued Loading. //----------------------------------------------------------------------------- bool CreateTargetFile_RESLST( const char *pSourceName, const char *pTargetName, bool bWriteToZip ) { bool bOK = false; // parse reslist CUtlRBTree< CUtlString, int > rbTree( 0, 0, ReslistLessFunc ); if ( !LoadReslist( pSourceName, &rbTree ) ) { return false; } CUtlBuffer targetBuffer( 0, 0, CUtlBuffer::TEXT_BUFFER ); for ( int iIndex = rbTree.FirstInorder(); iIndex != rbTree.InvalidIndex(); iIndex = rbTree.NextInorder( iIndex ) ) { targetBuffer.PutChar( '\"' ); targetBuffer.PutString( rbTree[iIndex].String() ); targetBuffer.PutChar( '\"' ); targetBuffer.PutString( "\n" ); } bOK = WriteBufferToFile( pTargetName, targetBuffer, bWriteToZip, g_WriteModeForConversions ); return bOK; }
void Serpent::WriteToFile(CFileIO &fIO) { fIO.WriteBinary(&RateofFire,1); fIO.WriteBinary(&bStartDirection,1); WriteBufferToFile(fIO); }
//----------------------------------------------------------------------------- // Rebuilds all of a MDL's components. //----------------------------------------------------------------------------- static bool GenerateModelFiles( const char *pMdlFilename ) { CUtlBuffer tempBuffer; int fileSize; int paddedSize; int swappedSize; // .mdl CUtlBuffer mdlBuffer; if ( !scriptlib->ReadFileToBuffer( pMdlFilename, mdlBuffer ) ) { return false; } if ( !Studio_ConvertStudioHdrToNewVersion( (studiohdr_t *)mdlBuffer.Base() )) { Msg("%s needs to be recompiled\n", pMdlFilename ); } // .vtx char szVtxFilename[MAX_PATH]; V_StripExtension( pMdlFilename, szVtxFilename, sizeof( szVtxFilename ) ); V_strncat( szVtxFilename, ".dx90.vtx", sizeof( szVtxFilename ) ); CUtlBuffer vtxBuffer; bool bHasVtx = ReadFileToBuffer( szVtxFilename, vtxBuffer, false, true ); // .vvd char szVvdFilename[MAX_PATH]; V_StripExtension( pMdlFilename, szVvdFilename, sizeof( szVvdFilename ) ); V_strncat( szVvdFilename, ".vvd", sizeof( szVvdFilename ) ); CUtlBuffer vvdBuffer; bool bHasVvd = ReadFileToBuffer( szVvdFilename, vvdBuffer, false, true ); if ( bHasVtx != bHasVvd ) { // paired resources, either mandates the other return false; } // a .mdl file that has .vtx/.vvd gets re-processed to cull lod data if ( bHasVtx && bHasVvd ) { // cull lod if needed IMdlStripInfo *pStripInfo = NULL; bool bResult = mdllib->StripModelBuffers( mdlBuffer, vvdBuffer, vtxBuffer, &pStripInfo ); if ( !bResult ) { return false; } if ( pStripInfo ) { // .vsi CUtlBuffer vsiBuffer; pStripInfo->Serialize( vsiBuffer ); pStripInfo->DeleteThis(); // save strip info for later processing char szVsiFilename[MAX_PATH]; V_StripExtension( pMdlFilename, szVsiFilename, sizeof( szVsiFilename ) ); V_strncat( szVsiFilename, ".vsi", sizeof( szVsiFilename ) ); WriteBufferToFile( szVsiFilename, vsiBuffer, false, WRITE_TO_DISK_ALWAYS ); } } // .ani processing may further update .mdl buffer char szAniFilename[MAX_PATH]; V_StripExtension( pMdlFilename, szAniFilename, sizeof( szAniFilename ) ); V_strncat( szAniFilename, ".ani", sizeof( szAniFilename ) ); CUtlBuffer aniBuffer; bool bHasAni = ReadFileToBuffer( szAniFilename, aniBuffer, false, true ); if ( bHasAni ) { // Some vestigal .ani files exist in the tree, only process valid .ani if ( ((studiohdr_t*)mdlBuffer.Base())->numanimblocks != 0 ) { // .ani processing modifies .mdl buffer fileSize = aniBuffer.TellPut(); paddedSize = fileSize + BYTESWAP_ALIGNMENT_PADDING; aniBuffer.EnsureCapacity( paddedSize ); tempBuffer.EnsureCapacity( paddedSize ); V_StripExtension( pMdlFilename, szAniFilename, sizeof( szAniFilename ) ); V_strncat( szAniFilename, ".360.ani", sizeof( szAniFilename ) ); swappedSize = StudioByteSwap::ByteswapStudioFile( szAniFilename, tempBuffer.Base(), aniBuffer.PeekGet(), fileSize, (studiohdr_t*)mdlBuffer.Base(), CompressFunc ); if ( swappedSize > 0 ) { // .ani buffer is replaced with swapped data aniBuffer.Purge(); aniBuffer.Put( tempBuffer.Base(), swappedSize ); WriteBufferToFile( szAniFilename, aniBuffer, false, WRITE_TO_DISK_ALWAYS ); } else { return false; } } } // .phy char szPhyFilename[MAX_PATH]; V_StripExtension( pMdlFilename, szPhyFilename, sizeof( szPhyFilename ) ); V_strncat( szPhyFilename, ".phy", sizeof( szPhyFilename ) ); CUtlBuffer phyBuffer; bool bHasPhy = ReadFileToBuffer( szPhyFilename, phyBuffer, false, true ); if ( bHasPhy ) { fileSize = phyBuffer.TellPut(); paddedSize = fileSize + BYTESWAP_ALIGNMENT_PADDING; phyBuffer.EnsureCapacity( paddedSize ); tempBuffer.EnsureCapacity( paddedSize ); V_StripExtension( pMdlFilename, szPhyFilename, sizeof( szPhyFilename ) ); V_strncat( szPhyFilename, ".360.phy", sizeof( szPhyFilename ) ); swappedSize = StudioByteSwap::ByteswapStudioFile( szPhyFilename, tempBuffer.Base(), phyBuffer.PeekGet(), fileSize, (studiohdr_t*)mdlBuffer.Base(), CompressFunc ); if ( swappedSize > 0 ) { // .phy buffer is replaced with swapped data phyBuffer.Purge(); phyBuffer.Put( tempBuffer.Base(), swappedSize ); WriteBufferToFile( szPhyFilename, phyBuffer, false, WRITE_TO_DISK_ALWAYS ); } else { return false; } } if ( bHasVtx ) { fileSize = vtxBuffer.TellPut(); paddedSize = fileSize + BYTESWAP_ALIGNMENT_PADDING; vtxBuffer.EnsureCapacity( paddedSize ); tempBuffer.EnsureCapacity( paddedSize ); V_StripExtension( pMdlFilename, szVtxFilename, sizeof( szVtxFilename ) ); V_strncat( szVtxFilename, ".dx90.360.vtx", sizeof( szVtxFilename ) ); swappedSize = StudioByteSwap::ByteswapStudioFile( szVtxFilename, tempBuffer.Base(), vtxBuffer.PeekGet(), fileSize, (studiohdr_t*)mdlBuffer.Base(), CompressFunc ); if ( swappedSize > 0 ) { // .vtx buffer is replaced with swapped data vtxBuffer.Purge(); vtxBuffer.Put( tempBuffer.Base(), swappedSize ); WriteBufferToFile( szVtxFilename, vtxBuffer, false, WRITE_TO_DISK_ALWAYS ); } else { return false; } } if ( bHasVvd ) { fileSize = vvdBuffer.TellPut(); paddedSize = fileSize + BYTESWAP_ALIGNMENT_PADDING; vvdBuffer.EnsureCapacity( paddedSize ); tempBuffer.EnsureCapacity( paddedSize ); V_StripExtension( pMdlFilename, szVvdFilename, sizeof( szVvdFilename ) ); V_strncat( szVvdFilename, ".360.vvd", sizeof( szVvdFilename ) ); swappedSize = StudioByteSwap::ByteswapStudioFile( szVvdFilename, tempBuffer.Base(), vvdBuffer.PeekGet(), fileSize, (studiohdr_t*)mdlBuffer.Base(), CompressFunc ); if ( swappedSize > 0 ) { // .vvd buffer is replaced with swapped data vvdBuffer.Purge(); vvdBuffer.Put( tempBuffer.Base(), swappedSize ); WriteBufferToFile( szVvdFilename, vvdBuffer, false, WRITE_TO_DISK_ALWAYS ); } else { return false; } } // swap and write final .mdl fileSize = mdlBuffer.TellPut(); paddedSize = fileSize + BYTESWAP_ALIGNMENT_PADDING; mdlBuffer.EnsureCapacity( paddedSize ); tempBuffer.EnsureCapacity( paddedSize ); char szMdlFilename[MAX_PATH]; V_StripExtension( pMdlFilename, szMdlFilename, sizeof( szMdlFilename ) ); V_strncat( szMdlFilename, ".360.mdl", sizeof( szMdlFilename ) ); swappedSize = StudioByteSwap::ByteswapStudioFile( szMdlFilename, tempBuffer.Base(), mdlBuffer.PeekGet(), fileSize, NULL, CompressFunc ); if ( swappedSize > 0 ) { // .mdl buffer is replaced with swapped data mdlBuffer.Purge(); mdlBuffer.Put( tempBuffer.Base(), swappedSize ); WriteBufferToFile( szMdlFilename, mdlBuffer, false, WRITE_TO_DISK_ALWAYS ); } else { return false; } return true; }
//----------------------------------------------------------------------------- // The DX Support file is a very fat expensive KV file, causes a run-time startup slowdown. // Becauase it normally lives in the game\bin directory, it can't be in the zip or preloaded. // Thus, it gets reprocessed into just the trivial 360 portion and placed into the platform.zip // Yes, it's evil. //----------------------------------------------------------------------------- bool ProcessDXSupportConfig( bool bWriteToZip ) { if ( !g_bIsPlatformZip ) { // only relevant when building platform zip, otherwise no-op return false; } const char *pConfigName = "dxsupport.cfg"; char szTempPath[MAX_PATH]; char szSourcePath[MAX_PATH]; V_ComposeFileName( g_szModPath, "../bin", szTempPath, sizeof( szTempPath ) ); V_ComposeFileName( szTempPath, pConfigName, szSourcePath, sizeof( szSourcePath ) ); CUtlBuffer sourceBuf( 0, 0, CUtlBuffer::TEXT_BUFFER ); if ( !g_pFullFileSystem->ReadFile( szSourcePath, NULL, sourceBuf ) ) { Msg( "Error! Couldn't open file '%s'!\n", pConfigName ); return false; } KeyValues *pKV = new KeyValues( "" ); if ( !pKV->LoadFromBuffer( "dxsupport.cfg", sourceBuf ) ) { Msg( "Error! Couldn't parse config file '%s'!\n", pConfigName ); pKV->deleteThis(); return false; } // only care about the xbox specific dxlevel 98 block KeyValues *pXboxSubKey = NULL; for ( KeyValues *pSubKey = pKV->GetFirstSubKey(); pSubKey != NULL && pXboxSubKey == NULL; pSubKey = pSubKey->GetNextKey() ) { // descend each sub block for ( KeyValues *pKey = pSubKey->GetFirstSubKey(); pKey != NULL && pXboxSubKey == NULL; pKey = pKey->GetNextKey() ) { if ( !V_stricmp( pKey->GetName(), "name" ) && pKey->GetInt( (const char *)NULL ) == 98 ) { pXboxSubKey = pSubKey; } } } if ( !pXboxSubKey ) { Msg( "Error! Couldn't find expected dxlevel 98 in config file '%s'!\n", pConfigName ); pKV->deleteThis(); return false; } CUtlBuffer kvBuffer( 0, 0, CUtlBuffer::TEXT_BUFFER ); kvBuffer.Printf( "\"dxsupport\"\n" ); kvBuffer.Printf( "{\n" ); kvBuffer.Printf( "\t\"0\"\n" ); kvBuffer.Printf( "\t{\n" ); for ( KeyValues *pKey = pXboxSubKey->GetFirstSubKey(); pKey != NULL; pKey = pKey->GetNextKey() ) { kvBuffer.Printf( "\t\t\"%s\" \"%s\"\n", pKey->GetName(), pKey->GetString( (const char *)NULL ) ); } kvBuffer.Printf( "\t}\n" ); kvBuffer.Printf( "}\n" ); CUtlBuffer targetBuf( 0, 0, CUtlBuffer::TEXT_BUFFER | CUtlBuffer::CONTAINS_CRLF ); kvBuffer.ConvertCRLF( targetBuf ); // only appears in zip file bool bSuccess = WriteBufferToFile( pConfigName, targetBuf, bWriteToZip, WRITE_TO_DISK_NEVER ); pKV->deleteThis(); return bSuccess; }
// main function to do the pkg decrypt/unpackage int do_spp_encrypt(char* pInPath, char* pOutPath, char* pType, char* pKeyName) { u8* pMySpp = NULL; u8* pMyContent = NULL; sce_header_t sce_hdr = {0}; SPP_META_HDR meta_hdr = {0}; u64 content_size_original = 0; u32 dwBytesRead = 0; u64 spp_size = 0; struct keylist* pMyKeyList = NULL; struct key MyKey = {0}; int retval = -1; // validate input params if ( (pInPath == NULL) || (pOutPath == NULL) || (pType == NULL) ) goto exit; // read in the 'input' (unencrypted) spp file (alloc a buffer) if ( ReadFileToBuffer(pInPath, (uint8_t**)&pMyContent, 0x00, &dwBytesRead, TRUE) != STATUS_SUCCESS) { printf("failed to read in file:%s, exiting...\n", pInPath); goto exit; } // assign the read-in size content_size_original = (u32)dwBytesRead; // build the SCE & META headers if ( build_spp_sce_hdr(&sce_hdr, content_size_original) != STATUS_SUCCESS ) goto exit; ///////////////// KEYS LOADING ////////////////////////////////////////// // // If we are OVERRIDING the default key from the 'keys' file, then // attempt to find it, first from the new 'keys' file, and if not, // manually by the exact 'keyname' specified if ( b_DefaultKeyListOverride == TRUE ) { if ( load_singlekey_by_name(pKeyName, &pMyKeyList) != STATUS_SUCCESS ) { // failed to find the 'override' key in 'KEYS' file, // so try 'old-style' keys printf("Error: Failed to find override SPP key(%s) in new \"KEYS\" file, trying old style keys...\n", pKeyName); if ( key_get_old(KEY_SPP, pKeyName, &MyKey) == STATUS_SUCCESS ) { if ( load_keylist_from_key(&pMyKeyList, &MyKey) != STATUS_SUCCESS ) { printf("Error: Unexpected failure loading single 'keylist' from 'key' structure....exiting....\n"); goto exit; } } else { printf("key_get() for SPP key failed"); goto exit; } } } // end if (KeyListOverride....) else { // try to get keys via the new 'keys' format first, if not, // failover to the old keys style if ( key_get_new(sce_hdr.key_revision, sce_hdr.header_type, &MyKey) != STATUS_SUCCESS ) { printf("Failed to find SPP key in new \"KEYS\" file, exiting!\n"); goto exit; } } // //////////////////////////////////////////////////////////////////////////////// // build the 'metadata' headers if ( build_spp_meta_hdr(&meta_hdr, content_size_original) != STATUS_SUCCESS ) goto exit; // build the SPP file spp_size = build_spp(&sce_hdr, &meta_hdr, &pMySpp, pMyContent, content_size_original); if (spp_size == 0) goto exit; // hash/sha1 the pkg data if ( hash_spp(pMySpp, content_size_original) != STATUS_SUCCESS ) goto exit; // ECDSA sign the pkg if ( sign_spp(pMySpp) != STATUS_SUCCESS ) goto exit; // encrypt the data if ( sce_encrypt_data_pkgtool(pMySpp) != STATUS_SUCCESS ) goto exit; // encrypt the hdrs if (sce_encrypt_header_pkgtool(pMySpp, &MyKey) != STATUS_SUCCESS ) goto exit; // write out the final .pkg file if (WriteBufferToFile(pOutPath, pMySpp, (uint32_t)spp_size, FALSE, 0, NULL) != STATUS_SUCCESS) { printf("failed to write to file:%s, exiting...\n", pOutPath); goto exit; } // status success retval = STATUS_SUCCESS; exit: // free the alloc'd memory if (pMyContent != NULL) free(pMyContent); // free the alloc'd memory if (pMySpp != NULL) free(pMySpp); // return the status return retval; }