internal static void SaveFileUCFB(string name, byte[] data) { byte[] encodedLen = BinUtils.EncodeNumber(data.Length); byte[] buffer = new byte[8]; buffer[0] = (byte)'u'; buffer[1] = (byte)'c'; buffer[2] = (byte)'f'; buffer[3] = (byte)'b'; buffer[4] = encodedLen[0]; buffer[5] = encodedLen[1]; buffer[6] = encodedLen[2]; buffer[7] = encodedLen[3]; FileStream fs = null; try { fs = new FileStream(name, FileMode.OpenOrCreate); fs.Write(buffer, 0, buffer.Length); fs.Write(data, 0, data.Length); } catch (Exception ex) { Program.MessageUser("Error Saving file: " + name + "\n" + ex.Message); } finally { if (fs != null) { fs.Close(); } } }
public void ReplaceUcfbChunk(Chunk chk, byte[] mungedFileBytes, bool replaceFirstOnly) { uint bytesLeftInFile = InitializeRead(); string curType = PeekChunkType(); string curName = PeekName(mCurrentPos + 8, curType); string cur = curName + "." + curType; string target = chk.Name + "." + chk.Type; //while ( RipChunk(false) != null ) do { curType = PeekChunkType(); if (string.IsNullOrEmpty(curType)) { break; } curName = PeekName(mCurrentPos + 8, curType); cur = curName + "." + curType; //} if (target.Equals(cur, StringComparison.CurrentCultureIgnoreCase)) { // we've advanced to the correct place uint chunk_start = mCurrentPos; uint chunk_end = PeekNumber(mCurrentPos + 4) + mCurrentPos + 8; int mungedContentLength = mungedFileBytes.Length - 8; uint chunk_len = chunk_end - chunk_start; int difference = (int)(chunk_len - (mungedContentLength)); int newLength = mData.Length - difference; byte[] newData = new byte[newLength]; Array.Copy(mData, 0, newData, 0, chunk_start /*- 1*/); // copy first chunk Array.Copy(mungedFileBytes, 8, newData, chunk_start, mungedFileBytes.Length - 8); //splice long next_start = chunk_start + mungedFileBytes.Length - 8; // MAYBE +1 ? if (mData.Length - chunk_end != 0) { Array.Copy(mData, chunk_end, newData, next_start, (mData.Length - chunk_end) - 1); } mData = newData; //Update UCFB Header byte[] encodedLen = BinUtils.EncodeNumber(mData.Length - 8); mData[4] = encodedLen[0]; mData[5] = encodedLen[1]; mData[6] = encodedLen[2]; mData[7] = encodedLen[3]; if (replaceFirstOnly) { break; } } } while (RipChunk(false) != null); }