public static void ReadCompressed(Stream s, List<string> classnames) { long start = s.Position; BinaryReader br = new BinaryReader(s); byte[] mem = new byte[(int)s.Length + 8]; byte[] buf = new byte[3]; s.Read(buf, 0, 3); s.Seek(start+4, SeekOrigin.Begin); int size = br.ReadInt32(); s.Seek(start+8, SeekOrigin.Begin); s.Read(mem, 0, (int)s.Length); s.Close(); br.Close(); try { s = new MemoryStream(mem); if (Encoding.Default.GetString(buf) == "CWS") { Inflater i = new Inflater(); i.SetInput(mem); byte[] mem2 = new byte[size + 8]; i.Inflate(mem2, 8, size); s = new MemoryStream(mem2); mem = new byte[0]; } s.Seek(0x15, SeekOrigin.Begin); br = new BinaryReader(s); while (br.BaseStream.Position < br.BaseStream.Length) { uint taglen = br.ReadUInt16(); uint len = taglen & 0x3f; uint tag = (taglen - len) / 64; if (len == 63) len = br.ReadUInt32(); start = br.BaseStream.Position; if (tag == 82) { FlashABC fabc = new FlashABC(br.BaseStream, len); fabc.FindClasses(classnames); } br.BaseStream.Seek(start + len, SeekOrigin.Begin); } br.Close(); } catch (Exception e) { Debug.Print(e.StackTrace); return; } }
public async ValueTask CopyDataTo(Stream output) { await using var fs = await _bsa._filename.OpenRead(); fs.Seek((long)_offset, SeekOrigin.Begin); uint len = Compressed ? _size : _realSize; var bytes = new byte[len]; fs.Read(bytes, 0, (int)len); if (!Compressed) { await output.WriteAsync(bytes, 0, bytes.Length); } else { var uncompressed = new byte[_realSize]; var inflater = new Inflater(); inflater.SetInput(bytes); inflater.Inflate(uncompressed); await output.WriteAsync(uncompressed, 0, uncompressed.Length); } }
/// <summary> /// The initial WOFF decompressor is null and throws an exception /// So we use SharpZipLib to inflate the file /// </summary> private static void SetupWoffDecompressorIfRequired() { if (WoffDefaultZlibDecompressFunc.DecompressHandler != null) { return; } WoffDefaultZlibDecompressFunc.DecompressHandler = (byte[] compressedBytes, byte[] decompressedResult) => { try { var inflater = new Inflater(); inflater.SetInput(compressedBytes); inflater.Inflate(decompressedResult); return(true); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.ToString()); return(false); } }; }
/// <summary> /// Inflate compressed swf /// </summary> private void Inflate() { // read size br.BaseStream.Position = 4; // skip signature int size = Convert.ToInt32(br.ReadUInt32()); // read swf head byte[] uncompressed = new byte[size]; br.BaseStream.Position = 0; br.Read(uncompressed, 0, 8); // header data is not compress // un-zip byte[] compressed = br.ReadBytes(size); Inflater zipInflator = new Inflater(); zipInflator.SetInput(compressed); zipInflator.Inflate(uncompressed, 8, size - 8); // new memory stream for uncompressed swf MemoryStream m = new MemoryStream(uncompressed); br = new BufferedBinaryReader(m); br.BaseStream.Position = 0; }
public void ReadBufferData(ByteBuffer buffer, int readLen) { if (zip > 0) { var zipData = buffer.ReadBytes(readLen); var decompressor = new Inflater(); decompressor.SetInput(zipData); var zipMemory = new MemoryStream(zipData.Length); //TODO alloc优化 //byte[] buf = new byte[1024]; while (!decompressor.IsFinished) { int count = decompressor.Inflate(_buffer); zipMemory.Write(_buffer, 0, count); } data = zipMemory.ToArray(); } else { data = buffer.ReadBytes(readLen); } }
public void CopyDataTo(Stream output) { using (var fs = _bsa._filename.OpenRead()) { fs.Seek((long)_offset, SeekOrigin.Begin); uint len = Compressed ? _size : _realSize; var bytes = new byte[len]; fs.Read(bytes, 0, (int)len); if (!Compressed) { output.Write(bytes, 0, bytes.Length); } else { var uncompressed = new byte[_realSize]; var inflater = new Inflater(); inflater.SetInput(bytes); inflater.Inflate(uncompressed); output.Write(uncompressed, 0, uncompressed.Length); } } }
private UnpackedObjectLoader(byte[] compressed, AnyObjectId id) { // Try to determine if this is a legacy format loose object or // a new style loose object. The legacy format was completely // compressed with zlib so the first byte must be 0x78 (15-bit // window size, deflated) and the first 16 bit word must be // evenly divisible by 31. Otherwise its a new style loose // object. // Inflater inflater = InflaterCache.Instance.get(); try { int fb = compressed[0] & 0xff; if (fb == 0x78 && (((fb << 8) | compressed[1] & 0xff) % 31) == 0) { inflater.SetInput(compressed); var hdr = new byte[64]; int avail = 0; while (!inflater.IsFinished && avail < hdr.Length) { try { avail += inflater.Inflate(hdr, avail, hdr.Length - avail); } catch (IOException dfe) { var coe = new CorruptObjectException(id, "bad stream", dfe); //inflater.end(); throw coe; } } if (avail < 5) { throw new CorruptObjectException(id, "no header"); } var p = new MutableInteger(); _objectType = Constants.decodeTypeString(id, hdr, (byte)' ', p); _objectSize = RawParseUtils.parseBase10(hdr, p.value, p); if (_objectSize < 0) { throw new CorruptObjectException(id, "negative size"); } if (hdr[p.value++] != 0) { throw new CorruptObjectException(id, "garbage after size"); } _bytes = new byte[_objectSize]; if (p.value < avail) { Array.Copy(hdr, p.value, _bytes, 0, avail - p.value); } Decompress(id, inflater, avail - p.value); } else { int p = 0; int c = compressed[p++] & 0xff; int typeCode = (c >> 4) & 7; int size = c & 15; int shift = 4; while ((c & 0x80) != 0) { c = compressed[p++] & 0xff; size += (c & 0x7f) << shift; shift += 7; } switch (typeCode) { case Constants.OBJ_COMMIT: case Constants.OBJ_TREE: case Constants.OBJ_BLOB: case Constants.OBJ_TAG: _objectType = typeCode; break; default: throw new CorruptObjectException(id, "invalid type"); } _objectSize = size; _bytes = new byte[_objectSize]; inflater.SetInput(compressed, p, compressed.Length - p); Decompress(id, inflater, 0); } } finally { InflaterCache.Instance.release(inflater); } }
static bool OnSaveUIConfig(LoginClient client, CMSG msgID, BinReader data) { if (client.Character == null) { return(true); } uint type = data.ReadUInt32(); int len = data.ReadInt32(); string conf = string.Empty; if (len > 0) { try { byte[] compressed = data.ReadBytes((int)(data.BaseStream.Length - data.BaseStream.Position)); Inflater inflater = new Inflater(); inflater.SetInput(compressed); byte[] decompressed = new byte[len]; inflater.Inflate(decompressed); conf = System.Text.ASCIIEncoding.ASCII.GetString(decompressed); } catch (Exception e) { Console.WriteLine("Failed to decompress config type " + type + ": " + e.Message); return(true); } } switch (type) { case 0: client.Character.UIConfig0 = conf; client.Character.Dirty = true; break; case 1: client.Character.UIConfig1 = conf; client.Character.Dirty = true; break; case 2: client.Character.UIConfig2 = conf; client.Character.Dirty = true; break; case 3: client.Character.UIConfig3 = conf; client.Character.Dirty = true; break; case 4: client.Character.UIConfig4 = conf; client.Character.Dirty = true; break; default: Console.WriteLine("Unknown config type: " + type); Console.WriteLine(conf); return(true); } DataServer.Database.SaveObject(client.Character); return(true); }
static void Save(BinaryReader input, FileMetadata metadata, BinaryWriter output) { input.BaseStream.Position = metadata.offset; byte[] compressedData = input.ReadBytes((int)metadata.compressedSize); byte[] uncompressedData = new byte[metadata.uncompressedSize]; if (metadata.compressionMethod == 0) // Нет сжатия { if (metadata.compressedSize != metadata.uncompressedSize) { throw new Exception("metadata.compressedSize != metadata.uncompressedSize"); } uncompressedData = compressedData; } else if (metadata.compressionMethod == 1) // Deflate { Inflater inflater = new Inflater(false); inflater.SetInput(compressedData); int length = inflater.Inflate(uncompressedData); if (length != metadata.uncompressedSize) { throw new Exception("length != metadata.uncompressedSize"); } if (!inflater.IsFinished) { // Какой-то остаток в конце сжатых данных, но он не дает дополнительных распакованных данных byte[] remain = new byte[100]; int remainLength = inflater.Inflate(remain); if (remainLength != 0 || !inflater.IsFinished) { throw new Exception("remainLength != 0 || !inflater.IsFinished"); } } } else if (metadata.compressionMethod == 2) // Snappy { SnappyDecompressor decompressor = new SnappyDecompressor(); uncompressedData = decompressor.Decompress(compressedData, 0, compressedData.Length); } else if (metadata.compressionMethod == 3) // Doboz { uncompressedData = DobozDecoder.Decode(compressedData, 0, compressedData.Length); if (uncompressedData.Length != metadata.uncompressedSize) { throw new Exception("uncompressedData.Length != metadata.uncompressedSize"); } } else if (metadata.compressionMethod == 4 || metadata.compressionMethod == 5) // LZ4 { uncompressedData = LZ4Codec.Decode32(compressedData, 0, compressedData.Length, (int)metadata.uncompressedSize); if (uncompressedData.Length != metadata.uncompressedSize) { throw new Exception("uncompressedData.Length != metadata.uncompressedSize"); } } else { throw new Exception("Unknown compression method"); } // Проверяем, что распаковали данные правильно Crc32 calculatedCrc32 = new Crc32(); calculatedCrc32.Update(uncompressedData); if (calculatedCrc32.Value != metadata.crc32) { throw new Exception("calculatedCrc32.Value != metadata.crc32"); } output.Write(uncompressedData); }
// note: might be zero /// <summary> /// Decompress the data (gzip header, deflate stream, gzip trailer) in the /// provided buffer. /// </summary> /// <returns>the number of decompressed bytes placed into b</returns> /// <exception cref="System.IO.IOException"/> public virtual int Decompress(byte[] b, int off, int len) { lock (this) { /* From the caller's perspective, this is where the state machine lives. * The code is written such that we never return from decompress() with * data remaining in userBuf unless we're in FINISHED state and there was * data beyond the current gzip member (e.g., we're within a concatenated * gzip stream). If this ever changes, {@link #needsInput()} will also * need to be modified (i.e., uncomment the userBufLen condition). * * The actual deflate-stream processing (decompression) is handled by * Java's Inflater class. Unlike the gzip header/trailer code (execute* * methods below), the deflate stream is never copied; Inflater operates * directly on the user's buffer. */ int numAvailBytes = 0; if (state != BuiltInGzipDecompressor.GzipStateLabel.DeflateStream) { ExecuteHeaderState(); if (userBufLen <= 0) { return(numAvailBytes); } } // "executeDeflateStreamState()" if (state == BuiltInGzipDecompressor.GzipStateLabel.DeflateStream) { // hand off user data (or what's left of it) to Inflater--but note that // Inflater may not have consumed all of previous bufferload (e.g., if // data highly compressed or output buffer very small), in which case // userBufLen will be zero if (userBufLen > 0) { inflater.SetInput(userBuf, userBufOff, userBufLen); userBufOff += userBufLen; userBufLen = 0; } // now decompress it into b[] try { numAvailBytes = inflater.Inflate(b, off, len); } catch (SharpZipBaseException dfe) { throw new IOException(dfe.Message); } crc.Update(b, off, numAvailBytes); // CRC-32 is on _uncompressed_ data if (inflater.IsFinished) { state = BuiltInGzipDecompressor.GzipStateLabel.TrailerCrc; int bytesRemaining = inflater.RemainingInput; System.Diagnostics.Debug.Assert((bytesRemaining >= 0), "logic error: Inflater finished; byte-count is inconsistent" ); // could save a copy of userBufLen at call to inflater.setInput() and // verify that bytesRemaining <= origUserBufLen, but would have to // be a (class) member variable...seems excessive for a sanity check userBufOff -= bytesRemaining; userBufLen = bytesRemaining; } else { // or "+=", but guaranteed 0 coming in return(numAvailBytes); } } // minor optimization ExecuteTrailerState(); return(numAvailBytes); } }
void CheckReader() { if (reader == null) { if (!record.IsCompressed) throw new InvalidOperationException(); int compressedSize = checked((int)record.Size); int uncompressedSize = checked((int)compressedReader.ReadUInt32()); byte[] compressedData = new byte[compressedSize]; byte[] uncompressedData = new byte[uncompressedSize]; int compressedRead = compressedReader.Read(compressedData, 0, compressedSize); Inflater inflater = new Inflater(); inflater.SetInput(compressedData); int uncompressedRead = inflater.Inflate(uncompressedData, 0, uncompressedSize); reader = new BinaryReader(new MemoryStream(uncompressedData, false)); endOffset = uncompressedSize; } }
public byte[] unpack() { var resources = peImage.Resources; var dir = resources.getRoot(); if ((dir = dir.getDirectory(10)) == null) { return(null); } if ((dir = dir.getDirectory("__")) == null) { return(null); } var dataEntry = dir.getData(0); if (dataEntry == null) { return(null); } var encryptedData = peImage.readBytes(dataEntry.RVA, (int)dataEntry.Size); if (encryptedData.Length != dataEntry.Size) { return(null); } var keyData = getKeyData(); if (keyData == null) { return(null); } var decrypter = new NativeFileDecrypter(keyData); decrypter.decrypt(encryptedData, 0, encryptedData.Length); byte[] inflatedData; if (isNet1x) { inflatedData = DeobUtils.inflate(encryptedData, false); } else { int inflatedSize = BitConverter.ToInt32(encryptedData, 0); inflatedData = new byte[inflatedSize]; var inflater = new Inflater(false); inflater.SetInput(encryptedData, 4, encryptedData.Length - 4); int count = inflater.Inflate(inflatedData); if (count != inflatedSize) { return(null); } } if (BitConverter.ToInt16(inflatedData, 0) != 0x5A4D) { return(null); } return(inflatedData); }
public void load_binary_waveform(Guid patient_channel_id, string patientId, Guid channel_type_id, int samplesPerSecond, string label, string filePath, string fileId, string dateTimeValue) { if (DataSource.OpenConnection()) { LabelInfo labelText = new LabelInfo(); labelText.Label = label; labelText.FileID = fileId; labelText.Letter = letter; labelText.date = dateTimeValue; bool exist = FileExists(filePath, labelText.FileName); bool newfileexist = NewFileExists(filePath, labelText.NewFileName); while (newfileexist == true) { labelText.Letter = letter; newfileexist = NewFileExists(filePath, labelText.NewFileName); } labelText.Letter = letter; SqlDataReader sqlDataReader; string LoadWaveformdata = ConfigurationManager.AppSettings["LoadBinWaveform"].ToString(); SqlCommand sqlcmd = new SqlCommand(LoadWaveformdata, DataSource.SqlConn); SqlParameter patientidParam = new SqlParameter("@patient_id", SqlDbType.UniqueIdentifier, 50); Guid guidPatientId = new Guid(patientId); patientidParam.Value = guidPatientId; sqlcmd.Parameters.Add(patientidParam); SqlParameter patientchannelParam = new SqlParameter("@patient_channel_id", SqlDbType.UniqueIdentifier, 50); patientchannelParam.Value = patient_channel_id; sqlcmd.Parameters.Add(patientchannelParam); sqlDataReader = sqlcmd.ExecuteReader(); int waveFormDataColPos = sqlDataReader.GetOrdinal("waveform_data"); while (sqlDataReader.Read()) { object waveformobj = sqlDataReader["waveform_data"]; byte[] waveformbyte = (Byte[])waveformobj; long StartFt = (long)sqlDataReader["start_ft"]; long EndFt = (long)sqlDataReader["end_ft"]; long rowSamples = ComputeFtDiff(EndFt, StartFt, samplesPerSecond); Inflater inflate = new Inflater(); long uncomprLen = rowSamples * mBytesPerSample; byte[] newBuffer = new byte[uncomprLen]; inflate.Reset(); // decompress the waveform buffer inflate.SetInput(waveformbyte); long rowBytes = inflate.Inflate(newBuffer); FileStream writeStream; try { if (exist == false && newfileexist == false) { fileExportPath = filePath + "\\" + labelText.FileName; } else { fileExportPath = filePath + "\\" + labelText.NewFileName; } writeStream = new FileStream(fileExportPath, FileMode.Append); BinaryWriter writeBinay = new BinaryWriter(writeStream); writeBinay.Write(newBuffer); writeBinay.Close(); } catch (Exception ex) { MessageBox.Show("Failed to write binary data." + Environment.NewLine + "Error: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } FileInfo f = new FileInfo(fileExportPath); labelData = f.Name; int i = labelData.IndexOf('_'); labelData = labelData.Remove(i); } }
public void SetInflaterInput(Inflater inflater) { if (this.available > 0) { inflater.SetInput(this.clearText, this.clearTextLength - this.available, this.available); this.available = 0; } }
private bool CheckSwf(bool is_comp) { this.format = MediaFormat.SWF; this.reader = new BinaryReader(this.stream); if (is_comp) { int size = -1; this.reader.BaseStream.Position = 4; // Skip head size = Convert.ToInt32(this.reader.ReadUInt32()); // Read swf head byte[] uncompressed = new byte[size]; this.reader.BaseStream.Position = 0; this.reader.Read(uncompressed, 0, 8); // Read compressed data byte[] compressed = this.reader.ReadBytes(size); this.stream.Close(); // Close the old stream // Uncompress Inflater zipInflator = new Inflater(); zipInflator.SetInput(compressed); zipInflator.Inflate(uncompressed, 8, size - 8); // Setup new uncompressed reader this.reader = new BinaryReader(new MemoryStream(uncompressed)); this.reader.BaseStream.Position = 0; } // Skip header signature/version etc etc this.reader.BaseStream.Position = 8; // Read rect uint bits = ReadUBits(5); ReadSBits(bits); // Read xmin this.width = ReadSBits(bits) / 20; // Read xmax ReadSBits(bits); // Read ymin this.height = ReadSBits(bits) / 20; // Read ymax return true; }
protected override byte[] ProcessFile(string fileName, PresetParserMetadata preset) { var chunkData = base.ProcessFile(fileName, preset); var ms = new MemoryStream(chunkData); var parameterSize = ms.ReadInt32(); ms.Seek(parameterSize, SeekOrigin.Current); var metadataSize = ms.ReadInt32(); var metadataBuf = new byte[metadataSize]; ms.Read(metadataBuf, 0, metadataSize); var inflater = new Inflater(false); inflater.SetInput(metadataBuf); var size = inflater.Inflate(_decodeBuffer); var metadata = _decodeBuffer.GetRange(0, size).ToArray(); var metadataString = Encoding.UTF8.GetString(metadata); var tokens = metadataString.Split(','); var metadataDictionary = new Dictionary <string, string>(); var isKey = true; var key = ""; foreach (var token in tokens) { if (isKey) { key = token; } else { try { metadataDictionary.Add(key, token); } catch (ArgumentException) { // Do nothing } } isKey = !isKey; } if (metadataDictionary.ContainsKey("Comment")) { preset.Comment = metadataDictionary["Comment"]; } if (metadataDictionary.ContainsKey("Author")) { preset.Author = metadataDictionary["Author"]; } ApplyType(int.Parse(metadataDictionary["Type"]), preset); return(chunkData); }
/// <summary> /// see <see cref="SwfDotNet.IO.Tags.BaseTag">base class</see> /// </summary> public override void ReadData(byte version, BufferedBinaryReader binaryReader) { RecordHeader rh = new RecordHeader(); rh.ReadData(binaryReader); int beforePos = (int)binaryReader.BaseStream.Position; int toReaded = (int)rh.TagLength - 7; _characterId = binaryReader.ReadUInt16(); _bitmapFormat = binaryReader.ReadByte(); _bitmapWidth = binaryReader.ReadUInt16(); _bitmapHeight = binaryReader.ReadUInt16(); _bitmapColorTableSize = 0; if (_bitmapFormat == 3) { _bitmapColorTableSize = binaryReader.ReadByte(); toReaded--; } if (_bitmapFormat == 3) { _colorMapData = new ColorMapData(); _colorMapData.ReadData(binaryReader, _bitmapColorTableSize, _bitmapWidth, _bitmapHeight, toReaded); } else if (_bitmapFormat == 4 || _bitmapFormat == 5) { int imageSize = _bitmapWidth * _bitmapHeight; int uncompressedSize = imageSize; if (_bitmapFormat == 4) { uncompressedSize *= 2; } else { uncompressedSize *= 4; } byte[] uncompressed = new byte[uncompressedSize]; byte[] compressed = binaryReader.ReadBytes(toReaded); Inflater zipInflator = new Inflater(); zipInflator.SetInput(compressed); zipInflator.Inflate(uncompressed, 0, uncompressedSize); _bitmapColorData = null; if (_bitmapFormat == 4) { Pix15[] bitmapPixelData = new Pix15[imageSize]; for (int i = 0, j = 0; i < imageSize; i++, j += 2) { byte[] data = new byte[2] { uncompressed[j], uncompressed[j + 1] }; bitmapPixelData[i] = new Pix15(data); } _bitmapColorData = new BitmapColorData(bitmapPixelData); } else { Pix24[] bitmapPixelData = new Pix24[imageSize]; for (int i = 0, j = 0; i < imageSize; i++, j += 4) { byte reserved = uncompressed[j]; byte red = uncompressed[j + 1]; byte green = uncompressed[j + 2]; byte blue = uncompressed[j + 3]; bitmapPixelData[i] = new Pix24(red, green, blue); } _bitmapColorData = new BitmapColorData(bitmapPixelData); } } }
static int Inflate(Inflater inf, byte [] src, byte [] dest) { int offset, length, remain; inf.Reset (); inf.SetInput (src); offset = 0; while (!inf.IsNeedingInput) { remain = Math.Min (dest.Length - offset, BlockSize); if (remain == 0) break; length = inf.Inflate (dest, offset, remain); offset += length; } return inf.TotalOut; }
public FlashReadFile(string file) { FileStream fs = new FileStream(file, FileMode.Open); BinaryReader br = new BinaryReader(fs); byte[] mem = new byte[(int)fs.Length + 8]; byte[] buf = new byte[3]; fs.Read(buf, 0, 3); fs.Seek(4, SeekOrigin.Begin); int size = br.ReadInt32(); fs.Seek(8, SeekOrigin.Begin); fs.Read(mem, 0, (int)fs.Length); fs.Close(); br.Close(); s = new MemoryStream(mem); if (Encoding.Default.GetString(buf) == "CWS") { Inflater i = new Inflater(); i.SetInput(mem); byte[] mem2 = new byte[size + 8]; i.Inflate(mem2, 8, size); s = new MemoryStream(mem2); mem = new byte[0]; } s.Seek(0x15, SeekOrigin.Begin); br = new BinaryReader(s); while (br.BaseStream.Position < br.BaseStream.Length) { uint taglen = br.ReadUInt16(); uint len = taglen & 0x3f; uint tag = (taglen - len) / 64; if (len == 63) len = br.ReadUInt32(); long start = br.BaseStream.Position; if (tag == 82) { FlashABC fabc = new FlashABC(br.BaseStream, len); List<string> classnames = new List<string>(); classnames.Add("cPlayerData"); fabc.FindClasses(classnames); } //Debug.Print("{0} {1}", tag, len+2); br.BaseStream.Seek(start + len, SeekOrigin.Begin); } fClass.InitClasses(); br.Close(); }
internal void Extract(Stream dmgStream, Stream isoStream, ref long lastOutOffset) { MemoryStream stream = new MemoryStream(Data); BinaryReader reader = new BinaryReader(stream); byte [] temp = new byte[0x40000]; byte [] otemp = new byte[0x40000]; byte [] zeroblock = new byte[4096]; int offset = 0xcc; long total_size = 0; long block_count = 0; BlockType block_type = BlockType.Zero; while (block_type != BlockType.End) { stream.Seek(offset, SeekOrigin.Begin); block_type = (BlockType)EndianSwap(reader.ReadInt32()); stream.Seek(offset + 12, SeekOrigin.Begin); long out_offset = EndianSwap(reader.ReadInt32()) * 0x200; stream.Seek(offset + 20, SeekOrigin.Begin); long out_size = EndianSwap(reader.ReadInt32()) * 0x200; stream.Seek(offset + 28, SeekOrigin.Begin); long in_offset = EndianSwap(reader.ReadInt32()); stream.Seek(offset + 36, SeekOrigin.Begin); long in_size = EndianSwap(reader.ReadInt32()); out_offset += lastOutOffset; if (block_type == BlockType.Zlib) { dmgStream.Seek(in_offset, SeekOrigin.Begin); if (temp.Length < in_size) { temp = new byte[in_size]; } long total_bytes_read = 0; while (total_bytes_read < in_size) { total_bytes_read += dmgStream.Read(temp, 0, Math.Min((int)(in_size - total_bytes_read), temp.Length)); } Inflater inflater = new Inflater(); inflater.SetInput(temp, 0, (int)total_bytes_read); if (otemp.Length < out_size + 4) { otemp = new byte[out_size + 4]; } inflater.Inflate(otemp); if (inflater.RemainingInput > 0) { throw new ApplicationException("Could not inflate entire block"); } isoStream.Write(otemp, 0, (int)out_size); } else if (block_type == BlockType.Copy) { dmgStream.Seek(in_offset, SeekOrigin.Begin); int bytes_read = dmgStream.Read(temp, 0, Math.Min((int)in_size, temp.Length)); long total_bytes_read = bytes_read; while (bytes_read != -1) { isoStream.Write(temp, 0, bytes_read); if (total_bytes_read >= in_size) { break; } bytes_read = dmgStream.Read(temp, 0, Math.Min((int)(in_size - total_bytes_read), temp.Length)); if (bytes_read > 0) { total_bytes_read += bytes_read; } } } else if (block_type == BlockType.Zero) { long zero_block_count = out_size / zeroblock.Length; for (int i = 0; i < zero_block_count; ++i) { isoStream.Write(zeroblock, 0, zeroblock.Length); } isoStream.Write(zeroblock, 0, (int)(out_size % zeroblock.Length)); } else if (block_type == BlockType.End) { lastOutOffset = out_offset; } offset += 0x28; block_count++; total_size += out_size; } }
public override TREFile Load(Stream stream) { var start = stream.Position; var tag = stream.ReadString(8); if (!tag.StartsWith(HeaderTagPrefix)) { throw new IOException("TRE File does not contain valid TRE data"); } if (!HeaderTagValidVersions.Any(htv => tag.EndsWith(htv))) { throw new IOException("TRE File does not contain valid TRE data"); } var header = new TREFile.TreHeader { ResourceCount = stream.ReadInt32(), InfoOffset = stream.ReadInt32(), InfoCompression = stream.ReadInt32(), InfoCompressedSize = stream.ReadInt32(), NameCompression = stream.ReadInt32(), NameCompressedSize = stream.ReadInt32(), NameSize = stream.ReadInt32(), }; stream.Seek(start + header.InfoOffset, SeekOrigin.Begin); byte[] infoData; if (header.InfoCompression == 0) { infoData = stream.ReadBytes(header.InfoSize); } else { var infoBuffer = stream.ReadBytes(header.InfoCompressedSize); var inflater = new Inflater(false); inflater.SetInput(infoBuffer); infoData = new Byte[header.InfoSize]; inflater.Inflate(infoData); } byte[] nameData; if (header.NameCompression == 0) { nameData = stream.ReadBytes(header.NameSize); } else { var nameBuffer = stream.ReadBytes(header.NameCompressedSize); var inflater = new Inflater(false); inflater.SetInput(nameBuffer); nameData = new Byte[header.NameSize]; inflater.Inflate(nameData); } var names = new List <String>(); using (var nameStream = new MemoryStream(nameData)) while (nameStream.Position < nameStream.Length) { names.Add(nameStream.ReadString()); } var infoList = new List <TREFile.TreInfo>(); using (var infoStream = new MemoryStream(infoData)) using (var infoReader = new BinaryReader(infoStream)) foreach (var name in names) { var info = new TREFile.TreInfo { Name = name, Checksum = infoReader.ReadInt32(), DataSize = infoReader.ReadInt32(), DataOffset = infoReader.ReadInt32(), DataCompression = infoReader.ReadInt32(), DataCompressedSize = infoReader.ReadInt32(), NameOffset = infoReader.ReadInt32() }; infoList.Add(info); } return(new TREFile { Header = header, InfoFiles = infoList }); }
public static void Trim(IntPtr hwnd, string In, string Out, ReportProgressDelegate del) { NativeMethods.ddsInit(hwnd); var br = new BinaryReader(File.OpenRead(In), Encoding.Default); var bw = new BinaryWriter(File.Create(Out), Encoding.Default); var sb = new StringBuilder(64); var inf = new Inflater(); bool Compressed, SkipName; if (br.ReadInt32() != 0x00415342) { throw new Exception("Invalid bsa"); } var version = br.ReadUInt32(); bw.Write(0x00415342); bw.Write(version); bw.Write(br.ReadInt32()); var flags = br.ReadUInt32(); if ((flags & 0x004) > 0) { Compressed = true; flags ^= 0x4; } else { Compressed = false; } if ((flags & 0x100) > 0 && version == 0x68) { SkipName = true; } else { SkipName = false; } flags ^= 0x2; var FolderCount = br.ReadInt32(); var FileCount = br.ReadInt32(); bw.Write(flags); bw.Write(FolderCount); bw.Write(FileCount); bw.Write(br.ReadInt32()); bw.Write(br.ReadInt32()); bw.Write(br.ReadInt32()); var folderFileCount = new int[FolderCount]; for (var i = 0; i < FolderCount; i++) { bw.Write(br.ReadInt64()); folderFileCount[i] = br.ReadInt32(); bw.Write(folderFileCount[i]); bw.Write(br.ReadInt32()); } var fileLengths = new int[FileCount]; var offsetOffsets = new long[FileCount]; var fileOffsets = new uint[FileCount]; var parsefiles = new bool[FileCount]; var file = 0; for (var i = 0; i < FolderCount; i++) { var len = br.ReadByte(); bw.Write(len); sb.Length = 0; while (--len > 0) { var c = br.ReadChar(); sb.Append(c); bw.Write(c); } br.ReadByte(); bw.Write((byte)0); var parse = true; if (sb.ToString().StartsWith("textures\\interface\\")) { parse = false; } for (var j = 0; j < folderFileCount[i]; j++) { bw.Write(br.ReadUInt64()); offsetOffsets[file] = br.BaseStream.Position; fileLengths[file] = br.ReadInt32(); bw.Write(fileLengths[file]); fileOffsets[file] = br.ReadUInt32(); bw.Write(fileOffsets[file]); parsefiles[file] = parse; file++; } } for (var i = 0; i < FileCount; i++) { sb.Length = 0; while (true) { var c = (char)br.ReadByte(); //bw.Write(c); if (c == '\0') { break; } sb.Append(c); } if (!sb.ToString().EndsWith(".dds", StringComparison.OrdinalIgnoreCase)) { parsefiles[i] = false; } } for (var i = 0; i < FileCount; i++) { if ((i % 100) == 0) { del("Processing file " + i + " of " + FileCount); } br.BaseStream.Position = fileOffsets[i]; var offset = bw.BaseStream.Position; var add = 0; if (SkipName) { var len = br.ReadByte(); bw.Write(len); bw.Write(br.ReadBytes(len + 1)); add = len + 2; } var compressed2 = Compressed; if ((fileLengths[i] & (1 << 30)) != 0) { compressed2 = !compressed2; fileLengths[i] ^= (1 << 30); } if (!compressed2) { var bytes = new byte[fileLengths[i]]; br.Read(bytes, 0, fileLengths[i]); Commit(bw, offsetOffsets[i], bytes, offset, add, parsefiles[i]); } else { var uncompressed = new byte[br.ReadUInt32()]; var compressed = new byte[fileLengths[i] - 4]; br.Read(compressed, 0, fileLengths[i] - 4); inf.Reset(); inf.SetInput(compressed); inf.Inflate(uncompressed); Commit(bw, offsetOffsets[i], uncompressed, offset, add, parsefiles[i]); } } br.Close(); bw.Close(); NativeMethods.ddsClose(); }
public void ProcessCommand(ref Socket soUDP, ref IPEndPoint remoteIpEndPoint, string sessionID, Encoding enc) { this.sessionID = sessionID; Encoding changeencoding = null; encoding = enc; EndPoint RemotePoint = remoteIpEndPoint; mcommandText = commandText; errorOccurred = false; StaticRateLimiter.UDP.EnsureRate(); if (commandType != enAniDBCommandType.Ping) { if (commandType != enAniDBCommandType.Login) { if (commandType != enAniDBCommandType.Logout && commandType != enAniDBCommandType.GetMyListStats) { mcommandText += "&"; } mcommandText += "s=" + sessionID; } else { encoding = Encoding.ASCII; changeencoding = enc; string encod = changeencoding.EncodingName; if (changeencoding.EncodingName.StartsWith("Unicode")) { encod = "utf-16"; } mcommandText += "&enc=" + encod; } } bool multipart = false; int part = 0; int maxpart = 1; string fulldesc = string.Empty; string decodedstring = string.Empty; DateTime start = DateTime.Now; do { if (commandType != enAniDBCommandType.Login) { string msg = string.Format("UDP_COMMAND: {0}", mcommandText); ShokoService.LogToSystem(Constants.DBLogType.APIAniDBUDP, msg); } bool repeatcmd; int received; byte[] byReceivedAdd = new byte[2000]; // max length should actually be 1400 do { repeatcmd = false; // Send Message byte[] SendByteAdd = encoding.GetBytes(mcommandText.ToCharArray()); try { ShokoService.LastAniDBMessage = DateTime.Now; ShokoService.LastAniDBUDPMessage = DateTime.Now; if (commandType != enAniDBCommandType.Ping) { ShokoService.LastAniDBMessageNonPing = DateTime.Now; } else { ShokoService.LastAniDBPing = DateTime.Now; } soUDP.SendTo(SendByteAdd, remoteIpEndPoint); } catch (Exception ex) { logger.Error(ex, ex.ToString()); //MyAnimeLog.Write(ex.ToString()); errorOccurred = true; errorMessage = ex.ToString(); } // Receive Response received = 0; try { //MyAnimeLog.Write("soUDP.ReceiveTimeout = {0}", soUDP.ReceiveTimeout.ToString()); received = soUDP.ReceiveFrom(byReceivedAdd, ref RemotePoint); ShokoService.LastAniDBMessage = DateTime.Now; ShokoService.LastAniDBUDPMessage = DateTime.Now; if (commandType != enAniDBCommandType.Ping) { ShokoService.LastAniDBMessageNonPing = DateTime.Now; } else { ShokoService.LastAniDBPing = DateTime.Now; } //MyAnimeLog.Write("Buffer length = {0}", received.ToString()); if ((received > 2) && (byReceivedAdd[0] == 0) && (byReceivedAdd[1] == 0)) { //deflate byte[] buff = new byte[65536]; byte[] input = new byte[received - 2]; Array.Copy(byReceivedAdd, 2, input, 0, received - 2); Inflater inf = new Inflater(false); inf.SetInput(input); inf.Inflate(buff); byReceivedAdd = buff; received = (int)inf.TotalOut; } } catch (SocketException sex) { // most likely we have timed out logger.Error(sex, sex.ToString()); errorOccurred = true; errorMessage = sex.ToString(); } catch (Exception ex) { logger.Error(ex, ex.ToString()); errorOccurred = true; errorMessage = ex.ToString(); } if ((commandType == enAniDBCommandType.Login) && (byReceivedAdd[0] == 0xFE) && (byReceivedAdd[1] == 0xFF) && (byReceivedAdd[3] == 53) && (byReceivedAdd[5] != 53) && !Encoding.EncodingName.ToLower().StartsWith("unicode") && (changeencoding != null) && changeencoding.EncodingName.ToLower().StartsWith("unicode")) { //Previous Session used utf-16 and was not logged out, AniDB was not yet issued a timeout. //AUTH command was not understand because it was encoded in ASCII. encoding = changeencoding; repeatcmd = true; } } while (repeatcmd); if (!errorOccurred) { if (changeencoding != null) { encoding = changeencoding; } Encoding enco; if ((byReceivedAdd[0] == 0xFE) && (byReceivedAdd[1] == 0xFF)) { enco = encoding; } else { enco = Encoding.ASCII; } decodedstring = enco.GetString(byReceivedAdd, 0, received); if (decodedstring[0] == 0xFEFF) // remove BOM { decodedstring = decodedstring.Substring(1); } if (commandType == enAniDBCommandType.GetAnimeDescription || commandType == enAniDBCommandType.GetReview) { //Lets handle multipart part++; string[] sp1 = decodedstring.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries); if (sp1[0].StartsWith("233 ANIMEDESC") || sp1[0].StartsWith("233 ANIMEDESC")) { string[] sp2 = sp1[1].Split('|'); fulldesc += sp2[2]; maxpart = int.Parse(sp2[1]); } if (sp1[0].StartsWith("234 REVIEW") || sp1[0].StartsWith("234 REVIEW")) { string[] sp2 = sp1[1].Split('|'); if (sp2.Length == 3) { fulldesc += sp2[2]; } else { for (int i = 2; i < sp2.Length; i++) { fulldesc += "|" + sp2[i]; } } maxpart = int.Parse(sp2[1]); } multipart = true; if (part == maxpart) { decodedstring = sp1[0] + "\n0|1|" + fulldesc + "\n"; multipart = false; } } } } while (multipart && !errorOccurred); if (errorOccurred) { socketResponse = string.Empty; } else { // there should be 2 newline characters in each response // the first is after the command .e.g "220 FILE" // the second is at the end of the data int i = 0, ipos = 0, foundpos = 0; foreach (char c in decodedstring) { if (c == '\n') { //MyAnimeLog.Write("NEWLINE FOUND AT: {0}", ipos); i++; foundpos = ipos; } ipos++; } if (i != 2) { socketResponse = decodedstring; TimeSpan ts = DateTime.Now - start; string msg = string.Format("UDP_RESPONSE in {0} ms - {1} ", ts.TotalMilliseconds, socketResponse); ShokoService.LogToSystem(Constants.DBLogType.APIAniDBUDP, msg); } else { socketResponse = decodedstring.Substring(0, foundpos + 1); TimeSpan ts = DateTime.Now - start; string msg = string.Format("UDP_RESPONSE_TRUNC in {0}ms - {1} ", ts.TotalMilliseconds, socketResponse); ShokoService.LogToSystem(Constants.DBLogType.APIAniDBUDP, msg); } } int val = 0; if (socketResponse.Length > 2) { int.TryParse(socketResponse.Substring(0, 3), out val); } ResponseCode = val; // if we get banned pause the command processor for a while // so we don't make the ban worse ShokoService.AnidbProcessor.IsUdpBanned = ResponseCode == 555; // 598 UNKNOWN COMMAND usually means we had connections issue // 506 INVALID SESSION // 505 ILLEGAL INPUT OR ACCESS DENIED // reset login status to start again if (ResponseCode == 598 || ResponseCode == 506 || ResponseCode == 505) { ShokoService.AnidbProcessor.IsInvalidSession = true; logger.Trace("FORCING Logout because of invalid session"); ForceReconnection(); } // 600 INTERNAL SERVER ERROR // 601 ANIDB OUT OF SERVICE - TRY AGAIN LATER // 602 SERVER BUSY - TRY AGAIN LATER // 604 TIMEOUT - DELAY AND RESUBMIT if (ResponseCode == 600 || ResponseCode == 601 || ResponseCode == 602 || ResponseCode == 604) { string errormsg = string.Empty; switch (ResponseCode) { case 600: errormsg = "600 INTERNAL SERVER ERROR"; break; case 601: errormsg = "601 ANIDB OUT OF SERVICE - TRY AGAIN LATER"; break; case 602: errormsg = "602 SERVER BUSY - TRY AGAIN LATER"; break; case 604: errormsg = "TIMEOUT - DELAY AND RESUBMIT"; break; } logger.Trace("FORCING Logout because of invalid session"); ShokoService.AnidbProcessor.ExtendPause(300, errormsg); } }
/// <summary> /// see <see cref="SwfDotNet.IO.Tags.BaseTag">base class</see> /// </summary> public override void ReadData(byte version, BufferedBinaryReader binaryReader) { RecordHeader rh = new RecordHeader(); rh.ReadData(binaryReader); int beforePos = (int)binaryReader.BaseStream.Position; int toReaded = (int)rh.TagLength - 7; _characterId = binaryReader.ReadUInt16(); _bitmapFormat = binaryReader.ReadByte(); _bitmapWidth = binaryReader.ReadUInt16(); _bitmapHeight = binaryReader.ReadUInt16(); _bitmapColorTableSize = 0; if (_bitmapFormat == 3) { _bitmapColorTableSize = binaryReader.ReadByte(); toReaded--; } int imageSize = _bitmapWidth * _bitmapHeight; if (_bitmapFormat == 3) { int uncompressedSize = imageSize + ((_bitmapColorTableSize + 1) * 4); byte[] uncompressed = new byte[uncompressedSize]; byte[] compressed = binaryReader.ReadBytes(toReaded); Inflater zipInflator = new Inflater(); zipInflator.SetInput(compressed); zipInflator.Inflate(uncompressed, 0, uncompressedSize); _alphaColorMapData = new AlphaColorMapData(); _alphaColorMapData.ColorTableRgb = new RGBA[_bitmapColorTableSize + 1]; int offset = 0; for (int i = 0; i < _bitmapColorTableSize + 1; i++, offset += 4) { byte red = uncompressed[offset]; byte green = uncompressed[offset + 1]; byte blue = uncompressed[offset + 2]; byte alpha = uncompressed[offset + 3]; _alphaColorMapData.ColorTableRgb[i] = new RGBA(red, green, blue, alpha); } _alphaColorMapData.ColorMapPixelData = new byte[uncompressedSize - offset]; for (int i = 0; i < uncompressedSize - offset; i++, offset++) { _alphaColorMapData.ColorMapPixelData[i] = uncompressed[offset]; } } else if (_bitmapFormat == 4 || _bitmapFormat == 5) { int uncompressedSize = imageSize * 4; byte[] uncompressed = new byte[uncompressedSize]; byte[] compressed = binaryReader.ReadBytes(toReaded); Inflater zipInflator = new Inflater(); zipInflator.SetInput(compressed); zipInflator.Inflate(uncompressed, 0, uncompressedSize); _alphaBitmapData = new AlphaBitmapData(); _alphaBitmapData.BitmapPixelData = new RGBA[imageSize]; for (int i = 0, j = 0; i < imageSize; i++, j += 4) { byte red = uncompressed[j]; byte green = uncompressed[j + 1]; byte blue = uncompressed[j + 2]; byte alpha = uncompressed[j + 3]; _alphaBitmapData.BitmapPixelData[i] = new RGBA(red, green, blue, alpha); } } }
public static string Decompress(String str) { // Initialize decompressor. byte[] compressedBytes = Convert.FromBase64String(str); Inflater decompressor = new Inflater(); decompressor.SetInput(compressedBytes); // Give the decompressor the // data to decompress. byte[] ret = null; using (MemoryStream memStream = new MemoryStream(compressedBytes.Length)) { // Decompress the data byte[] buf = new byte[compressedBytes.Length + 100]; while (!decompressor.IsFinished) { memStream.Write(buf, 0, decompressor.Inflate(buf)); } memStream.Close(); ret = memStream.ToArray(); } return ASCIIEncoding.UTF8.GetString(ret); }
public virtual void TestPrototypeInflaterGzip() { CompressionCodec gzip = new GzipCodec(); // used only for file extension localFs.Delete(workDir, true); // localFs = FileSystem instance System.Console.Out.WriteLine(ColorBrBlue + "testPrototypeInflaterGzip() using " + "non-native/Java Inflater and manual gzip header/trailer parsing" + ColorNormal ); // copy prebuilt (correct!) version of concat.gz to HDFS string fn = "concat" + gzip.GetDefaultExtension(); Path fnLocal = new Path(Runtime.GetProperty("test.concat.data", "/tmp"), fn); Path fnHDFS = new Path(workDir, fn); localFs.CopyFromLocalFile(fnLocal, fnHDFS); FileInputStream @in = new FileInputStream(fnLocal.ToString()); NUnit.Framework.Assert.AreEqual("concat bytes available", 148, @in.Available()); // should wrap all of this header-reading stuff in a running-CRC wrapper // (did so in BuiltInGzipDecompressor; see below) byte[] compressedBuf = new byte[256]; int numBytesRead = @in.Read(compressedBuf, 0, 10); NUnit.Framework.Assert.AreEqual("header bytes read", 10, numBytesRead); NUnit.Framework.Assert.AreEqual("1st byte", unchecked ((int)(0x1f)), compressedBuf [0] & unchecked ((int)(0xff))); NUnit.Framework.Assert.AreEqual("2nd byte", unchecked ((int)(0x8b)), compressedBuf [1] & unchecked ((int)(0xff))); NUnit.Framework.Assert.AreEqual("3rd byte (compression method)", 8, compressedBuf [2] & unchecked ((int)(0xff))); byte flags = unchecked ((byte)(compressedBuf[3] & unchecked ((int)(0xff)))); if ((flags & unchecked ((int)(0x04))) != 0) { // FEXTRA numBytesRead = @in.Read(compressedBuf, 0, 2); NUnit.Framework.Assert.AreEqual("XLEN bytes read", 2, numBytesRead); int xlen = ((compressedBuf[1] << 8) | compressedBuf[0]) & unchecked ((int)(0xffff) ); @in.Skip(xlen); } if ((flags & unchecked ((int)(0x08))) != 0) { // FNAME while ((numBytesRead = @in.Read()) != 0) { NUnit.Framework.Assert.IsFalse("unexpected end-of-file while reading filename", numBytesRead == -1); } } if ((flags & unchecked ((int)(0x10))) != 0) { // FCOMMENT while ((numBytesRead = @in.Read()) != 0) { NUnit.Framework.Assert.IsFalse("unexpected end-of-file while reading comment", numBytesRead == -1); } } if ((flags & unchecked ((int)(0xe0))) != 0) { // reserved NUnit.Framework.Assert.IsTrue("reserved bits are set??", (flags & unchecked ((int) (0xe0))) == 0); } if ((flags & unchecked ((int)(0x02))) != 0) { // FHCRC numBytesRead = @in.Read(compressedBuf, 0, 2); NUnit.Framework.Assert.AreEqual("CRC16 bytes read", 2, numBytesRead); int crc16 = ((compressedBuf[1] << 8) | compressedBuf[0]) & unchecked ((int)(0xffff )); } // ready to go! next bytes should be start of deflated stream, suitable // for Inflater numBytesRead = @in.Read(compressedBuf); // Inflater docs refer to a "dummy byte": no clue what that's about; // appears to work fine without one byte[] uncompressedBuf = new byte[256]; Inflater inflater = new Inflater(true); inflater.SetInput(compressedBuf, 0, numBytesRead); try { int numBytesUncompressed = inflater.Inflate(uncompressedBuf); string outString = Sharpen.Runtime.GetStringForBytes(uncompressedBuf, 0, numBytesUncompressed , "UTF-8"); System.Console.Out.WriteLine("uncompressed data of first gzip member = [" + outString + "]"); } catch (SharpZipBaseException ex) { throw new IOException(ex.Message); } @in.Close(); }
public void LoadFile(string path) { if (!File.Exists(path)) { throw new ArgumentException($"The provided file at {path} does not exist!"); } byte[] fileBytes = File.ReadAllBytes(path); using var ms = new MemoryStream(); ms.Write(fileBytes, 0, fileBytes.Length); ms.Seek(0, SeekOrigin.Begin); using var br = new BinaryReader(ms); //reference: https://en.uesp.net/wiki/Tes5Mod:Save_File_Format //file format conventions: https://en.uesp.net/wiki/Tes5Mod:File_Format_Conventions char[] magic = br.ReadChars(13); var magicString = new string(magic); if (magicString != SkyrimMagic) { throw new CorruptedSaveFileException($"The magic string of the save file does not equal \"TESV_SAVEGAME\" but \"{magicString}\"", br); } HeaderSize = br.ReadUInt32(); Header = new Header { Version = br.ReadUInt32(), SaveNumber = br.ReadUInt32(), PlayerName = br.ReadWString(), PlayerLevel = br.ReadUInt32(), PlayerLocation = br.ReadWString(), GameDate = br.ReadWString(), PlayerRaceEditorID = br.ReadWString(), PlayerSex = br.ReadUInt16(), PlayerCurExp = br.ReadSingle(), PlayerLevelUpExp = br.ReadSingle(), FileTime = br.ReadFileTime() }; IsSpecialEdition = Header.Version == 12; var screenshotWidth = (int)br.ReadUInt32(); var screenshotHeight = (int)br.ReadUInt32(); //for some unknown reason the type of compression comes between the image resolution and the image data Compression = IsSpecialEdition ? (SkyrimSaveFileCompression)br.ReadUInt16() : 0; Screenshot = ReadScreenshot(br, IsSpecialEdition, screenshotWidth, screenshotHeight); if (Compression != SkyrimSaveFileCompression.None) { if (Compression == SkyrimSaveFileCompression.ZLib) { //IGNORED: https://github.com/ModOrganizer2/modorganizer-game_gamebryo/blob/178c35674a8d3a6a5312065898f2e3c35fb701ac/src/gamebryo/gamebryosavegame.cpp#L226 return; } /* * 1) read the compressed data * 2) save the current position * 3) decompress the data * 4) copy the decompressed data to the main MemoryStream * 5) jump to the saved position to continue reading */ var decompressedSize = br.ReadUInt32(); var compressedSize = br.ReadUInt32(); byte[] compressed = br.ReadBytes((int)compressedSize); var currentPosition = ms.Position; var decompressed = new byte[decompressedSize]; var res = LZ4Codec.Decode(compressed, 0, compressed.Length, decompressed, 0, decompressed.Length); if (res != decompressedSize) { throw new CorruptedSaveFileException($"Could not decode compressed data at position {currentPosition} with length {compressedSize} to a decompressed byte array of length {decompressedSize}", br); } using var tempStream = new MemoryStream(decompressed); tempStream.CopyTo(ms); ms.Position = currentPosition; tempStream.Dispose(); } FormVersion = br.ReadByte(); PluginInfoSize = br.ReadUInt32(); PluginInfo = new PluginInfo { PluginCount = br.ReadByte() }; PluginInfo.Plugins = new WString[PluginInfo.PluginCount]; for (var i = 0; i < PluginInfo.PluginCount; i++) { PluginInfo.Plugins[i] = br.ReadWString(); } if (IsSpecialEdition) { LightPluginInfo = new LightPluginInfo { LightPluginCount = br.ReadUInt16() }; LightPluginInfo.LightPlugins = new WString[LightPluginInfo.LightPluginCount]; for (var i = 0; i < LightPluginInfo.LightPluginCount; i++) { LightPluginInfo.LightPlugins[i] = br.ReadWString(); } } FileLocationTable = new FileLocationTable { FormIDArrayCountOffset = br.ReadUInt32(), UnknownTable3Offset = br.ReadUInt32(), GlobalDataTable1Offset = br.ReadUInt32(), GlobalDataTable2Offset = br.ReadUInt32(), ChangeFormsOffset = br.ReadUInt32(), GlobalDataTable3Offset = br.ReadUInt32(), GlobalDataTable1Count = br.ReadUInt32(), GlobalDataTable2Count = br.ReadUInt32(), GlobalDataTable3Count = br.ReadUInt32(), ChangeFormCount = br.ReadUInt32(), Unused = ReadUInt32Array(br, 15) }; if (FileLocationTable.GlobalDataTable1Count > 9) { throw new CorruptedSaveFileException($"The GlobalDataTable1Count can not be greater than 9 but is {FileLocationTable.GlobalDataTable1Count}!", br); } if (FileLocationTable.GlobalDataTable2Count > 14) { throw new CorruptedSaveFileException($"The GlobalDataTable2Count can not be greater than 14 but is {FileLocationTable.GlobalDataTable2Count}!", br); } if (FileLocationTable.GlobalDataTable3Count > 5) { throw new CorruptedSaveFileException($"The GlobalDataTable3Count can not be greater than 5 but is {FileLocationTable.GlobalDataTable3Count}!", br); } if (FileLocationTable.Unused.Any(x => x != 0)) { throw new CorruptedSaveFileException("The unused array got filled with non-zero values! Please open a new issue on GitHub with this save file.", br); } // types 0 to 8 GlobalDataTable1 = new IGlobalData[FileLocationTable.GlobalDataTable1Count]; // types 100 to 114 GlobalDataTable2 = new IGlobalData[FileLocationTable.GlobalDataTable2Count]; ChangeForms = new ChangeForm[FileLocationTable.ChangeFormCount]; // types 1000 to 1005 GlobalDataTable3 = new IGlobalData[FileLocationTable.GlobalDataTable3Count]; for (var i = 0; i < GlobalDataTable1.Length; i++) { GlobalDataTable1[i] = br.ReadGlobalData(0, 8); } for (var i = 0; i < GlobalDataTable2.Length; i++) { GlobalDataTable2[i] = br.ReadGlobalData(100, 114); } for (var i = 0; i < ChangeForms.Length; i++) { var changeForm = br.ReadChangeForm(); //check if data is compressed if (changeForm.Length2 != 0) { byte[] compressed = br.ReadBytes((int)changeForm.Length1); var decompressed = new byte[changeForm.Length2]; var inflater = new Inflater(); inflater.SetInput(compressed); var res = inflater.Inflate(decompressed); if (res != changeForm.Length2) { throw new CorruptedSaveFileException($"Result of inflation resulted in a return value of {res} instead of {changeForm.Length2}!", br); } changeForm.Compression = SkyrimSaveFileCompression.ZLib; changeForm.Data = decompressed; } else { changeForm.Data = br.ReadBytes((int)changeForm.Length1); } ChangeForms[i] = changeForm; } for (var i = 0; i < GlobalDataTable3.Length; i++) { GlobalDataTable3[i] = br.ReadGlobalData(1000, 1005); } return; }
byte[] decrypt(byte[] encryptedData) { var reader = new BinaryReader(new MemoryStream(encryptedData)); int headerMagic = reader.ReadInt32(); if (headerMagic == 0x04034B50) { throw new NotImplementedException("Not implemented yet since I haven't seen anyone use it."); } byte encryption = (byte)(headerMagic >> 24); if ((headerMagic & 0x00FFFFFF) != 0x007D7A7B) // Check if "{z}" { throw new ApplicationException(string.Format("Invalid SA header magic 0x{0:X8}", headerMagic)); } switch (encryption) { case 1: int totalInflatedLength = reader.ReadInt32(); if (totalInflatedLength < 0) { throw new ApplicationException("Invalid length"); } var inflatedBytes = new byte[totalInflatedLength]; int partInflatedLength; for (int inflateOffset = 0; inflateOffset < totalInflatedLength; inflateOffset += partInflatedLength) { int partLength = reader.ReadInt32(); partInflatedLength = reader.ReadInt32(); if (partLength < 0 || partInflatedLength < 0) { throw new ApplicationException("Invalid length"); } var inflater = new Inflater(true); inflater.SetInput(encryptedData, checked ((int)reader.BaseStream.Position), partLength); reader.BaseStream.Seek(partLength, SeekOrigin.Current); int realInflatedLen = inflater.Inflate(inflatedBytes, inflateOffset, inflatedBytes.Length - inflateOffset); if (realInflatedLen != partInflatedLength) { throw new ApplicationException("Could not inflate"); } } return(inflatedBytes); case 2: if (resourceDecrypterInfo.DES_Key == null || resourceDecrypterInfo.DES_IV == null) { throw new ApplicationException("DES key / iv have not been set yet"); } using (var provider = new DESCryptoServiceProvider()) { provider.Key = resourceDecrypterInfo.DES_Key; provider.IV = resourceDecrypterInfo.DES_IV; using (var transform = provider.CreateDecryptor()) { return(decrypt(transform.TransformFinalBlock(encryptedData, 4, encryptedData.Length - 4))); } } case 3: if (resourceDecrypterInfo.AES_Key == null || resourceDecrypterInfo.AES_IV == null) { throw new ApplicationException("AES key / iv have not been set yet"); } using (var provider = new RijndaelManaged()) { provider.Key = resourceDecrypterInfo.AES_Key; provider.IV = resourceDecrypterInfo.AES_IV; using (var transform = provider.CreateDecryptor()) { return(decrypt(transform.TransformFinalBlock(encryptedData, 4, encryptedData.Length - 4))); } } default: throw new ApplicationException(string.Format("Unknown encryption type 0x{0:X2}", encryption)); } }
public static int DecompressBlock(byte[] inBuffer, int inLength, byte[] outBuffer, bool multi) { byte[] tempBuffer; if (!multi) { return(DclCompression.DecompressBlock(inBuffer, 0, inLength, outBuffer)); } else // Examinate first byte for finding compression methods used { switch (inBuffer[0]) { case 0x01: // Huffman throw new MpqCompressionNotSupportedException(0x01, "Huffman"); case 0x02: // Zlib (Deflate/Inflate) #if USE_SHARPZIPLIB // Use SharpZipLib's Deflate implementation Inflater.Reset(); // The first property read will initialize the field… inflater.SetInput(inBuffer, 1, inLength - 1); return(inflater.Inflate(outBuffer)); #else // Use .NET 2.0's built-in inflate algorithm using (var inStream = new MemoryStream(inBuffer, 3, inLength - 7, false, false)) using (var outStream = new DeflateStream(inStream, CompressionMode.Decompress)) outStream.Read(outBuffer, 0, outBuffer.Length); #endif case 0x08: // PKWare DCL (Implode/Explode) return(DclCompression.DecompressBlock(inBuffer, 1, inLength - 1, outBuffer)); case 0x10: // BZip2 #if USE_SHARPZIPLIB // Use SharpZipLib for decompression using (var inStream = new MemoryStream(inBuffer, 1, inLength - 1, false, false)) using (var outStream = new BZip2InputStream(inStream)) return(outStream.Read(outBuffer, 0, outBuffer.Length)); #else throw new CompressionNotSupportedException("BZip2"); #endif case 0x12: // LZMA using (var inStream = new MemoryStream(inBuffer, 1, inLength - 1, false, false)) using (var outStream = new MemoryStream(outBuffer, true)) { lzmaDecoder.Code(inStream, outStream, inStream.Length, outStream.Length, null); return(checked ((int)outStream.Position)); } case 0x20: // Sparse return(SparseCompression.DecompressBlock(inBuffer, 1, inLength - 1, outBuffer)); case 0x22: // Sparse + Deflate #if USE_SHARPZIPLIB // Use SharpZipLib's Deflate implementation Inflater.Reset(); // The first property read will initialize the field… inflater.SetInput(inBuffer, 1, inLength - 1); tempBuffer = CommonMethods.GetSharedBuffer(outBuffer.Length); return(SparseCompression.DecompressBlock(tempBuffer, 0, inflater.Inflate(tempBuffer), outBuffer)); #else // Use .NET 2.0's built-in inflate algorithm using (var inStream = new MemoryStream(inBuffer, 3, inLength - 7, false, false)) using (var inoutStream = new DeflateStream(inStream, CompressionMode.Decompress)) using (var outStream = new SparseInputStream(inoutStream)) return(outStream.Read(outBuffer, 0, outBuffer.Length)); #endif case 0x30: // Sparse + BZip2 #if USE_SHARPZIPLIB // Use SharpZipLib for decompression using (var inStream = new MemoryStream(inBuffer, 1, inLength - 1, false, false)) using (var inoutStream = new BZip2InputStream(inStream)) using (var outStream = new SparseInputStream(inoutStream)) return(outStream.Read(outBuffer, 0, outBuffer.Length)); #else throw new CompressionNotSupportedException("Sparse + BZip2"); #endif case 0x40: // Mono IMA ADPCM throw new MpqCompressionNotSupportedException(0x40, "Mono IMA ADPCM"); case 0x41: // Mono IMA ADPCM + Huffman throw new MpqCompressionNotSupportedException(0x41, "Mono IMA ADPCM + Huffman"); case 0x48: // Mono IMA ADPCM + Implode throw new MpqCompressionNotSupportedException(0x48, "Mono IMA ADPCM + Implode"); case 0x80: // Stereo IMA ADPCM throw new MpqCompressionNotSupportedException(0x80, "Stereo IMA ADPCM"); case 0x81: // Stereo IMA ADPCM + Huffman throw new MpqCompressionNotSupportedException(0x81, "Stereo IMA ADPCM + Huffman"); case 0x88: // Stereo IMA ADPCM + Implode throw new MpqCompressionNotSupportedException(0x88, "Stereo IMA ADPCM + Implode"); default: throw new MpqCompressionNotSupportedException(inBuffer[0]); } } }
/// <exception cref="System.IO.IOException"></exception> /// <exception cref="NGit.Errors.StoredObjectRepresentationNotAvailableException"></exception> private void CopyAsIs2(PackOutputStream @out, LocalObjectToPack src, bool validate , WindowCursor curs) { CRC32 crc1 = validate ? new CRC32() : null; CRC32 crc2 = validate ? new CRC32() : null; byte[] buf = @out.GetCopyBuffer(); // Rip apart the header so we can discover the size. // ReadFully(src.offset, buf, 0, 20, curs); int c = buf[0] & unchecked ((int)(0xff)); int typeCode = (c >> 4) & 7; long inflatedLength = c & 15; int shift = 4; int headerCnt = 1; while ((c & unchecked ((int)(0x80))) != 0) { c = buf[headerCnt++] & unchecked ((int)(0xff)); inflatedLength += ((long)(c & unchecked ((int)(0x7f)))) << shift; shift += 7; } if (typeCode == Constants.OBJ_OFS_DELTA) { do { c = buf[headerCnt++] & unchecked ((int)(0xff)); }while ((c & 128) != 0); if (validate) { crc1.Update(buf, 0, headerCnt); crc2.Update(buf, 0, headerCnt); } } else { if (typeCode == Constants.OBJ_REF_DELTA) { if (validate) { crc1.Update(buf, 0, headerCnt); crc2.Update(buf, 0, headerCnt); } ReadFully(src.offset + headerCnt, buf, 0, 20, curs); if (validate) { crc1.Update(buf, 0, 20); crc2.Update(buf, 0, 20); } headerCnt += 20; } else { if (validate) { crc1.Update(buf, 0, headerCnt); crc2.Update(buf, 0, headerCnt); } } } long dataOffset = src.offset + headerCnt; long dataLength = src.length; long expectedCRC; ByteArrayWindow quickCopy; // Verify the object isn't corrupt before sending. If it is, // we report it missing instead. // try { quickCopy = curs.QuickCopy(this, dataOffset, dataLength); if (validate && Idx().HasCRC32Support()) { // Index has the CRC32 code cached, validate the object. // expectedCRC = Idx().FindCRC32(src); if (quickCopy != null) { quickCopy.Crc32(crc1, dataOffset, (int)dataLength); } else { long pos = dataOffset; long cnt = dataLength; while (cnt > 0) { int n = (int)Math.Min(cnt, buf.Length); ReadFully(pos, buf, 0, n, curs); crc1.Update(buf, 0, n); pos += n; cnt -= n; } } if (crc1.GetValue() != expectedCRC) { SetCorrupt(src.offset); throw new CorruptObjectException(MessageFormat.Format(JGitText.Get().objectAtHasBadZlibStream , Sharpen.Extensions.ValueOf(src.offset), GetPackFile())); } } else { if (validate) { // We don't have a CRC32 code in the index, so compute it // now while inflating the raw data to get zlib to tell us // whether or not the data is safe. // Inflater inf = curs.Inflater(); byte[] tmp = new byte[1024]; if (quickCopy != null) { quickCopy.Check(inf, tmp, dataOffset, (int)dataLength); } else { long pos = dataOffset; long cnt = dataLength; while (cnt > 0) { int n = (int)Math.Min(cnt, buf.Length); ReadFully(pos, buf, 0, n, curs); crc1.Update(buf, 0, n); inf.SetInput(buf, 0, n); while (inf.Inflate(tmp, 0, tmp.Length) > 0) { continue; } pos += n; cnt -= n; } } if (!inf.IsFinished || inf.TotalIn != dataLength) { SetCorrupt(src.offset); throw new EOFException(MessageFormat.Format(JGitText.Get().shortCompressedStreamAt , Sharpen.Extensions.ValueOf(src.offset))); } expectedCRC = crc1.GetValue(); } else { expectedCRC = -1; } } } catch (SharpZipBaseException dataFormat) { SetCorrupt(src.offset); CorruptObjectException corruptObject = new CorruptObjectException(MessageFormat.Format (JGitText.Get().objectAtHasBadZlibStream, Sharpen.Extensions.ValueOf(src.offset) , GetPackFile())); Sharpen.Extensions.InitCause(corruptObject, dataFormat); StoredObjectRepresentationNotAvailableException gone; gone = new StoredObjectRepresentationNotAvailableException(src); Sharpen.Extensions.InitCause(gone, corruptObject); throw gone; } catch (IOException ioError) { StoredObjectRepresentationNotAvailableException gone; gone = new StoredObjectRepresentationNotAvailableException(src); Sharpen.Extensions.InitCause(gone, ioError); throw gone; } if (quickCopy != null) { // The entire object fits into a single byte array window slice, // and we have it pinned. Write this out without copying. // @out.WriteHeader(src, inflatedLength); quickCopy.Write(@out, dataOffset, (int)dataLength, null); } else { if (dataLength <= buf.Length) { // Tiny optimization: Lots of objects are very small deltas or // deflated commits that are likely to fit in the copy buffer. // if (!validate) { long pos = dataOffset; long cnt = dataLength; while (cnt > 0) { int n = (int)Math.Min(cnt, buf.Length); ReadFully(pos, buf, 0, n, curs); pos += n; cnt -= n; } } @out.WriteHeader(src, inflatedLength); @out.Write(buf, 0, (int)dataLength); } else { // Now we are committed to sending the object. As we spool it out, // check its CRC32 code to make sure there wasn't corruption between // the verification we did above, and us actually outputting it. // @out.WriteHeader(src, inflatedLength); long pos = dataOffset; long cnt = dataLength; while (cnt > 0) { int n = (int)Math.Min(cnt, buf.Length); ReadFully(pos, buf, 0, n, curs); if (validate) { crc2.Update(buf, 0, n); } @out.Write(buf, 0, n); pos += n; cnt -= n; } if (validate && crc2.GetValue() != expectedCRC) { throw new CorruptObjectException(MessageFormat.Format(JGitText.Get().objectAtHasBadZlibStream , Sharpen.Extensions.ValueOf(src.offset), GetPackFile())); } } } }
/// <summary> /// Writes an array of bytes to the uncompressed output stream. /// </summary> /// <param name="b"> buffer containing compressed data to decompress and write to /// the output stream </param> /// <param name="off"> starting offset of the compressed data within {@code b} </param> /// <param name="len"> number of bytes to decompress from {@code b} </param> /// <exception cref="IndexOutOfBoundsException"> if {@code off < 0}, or if /// {@code len < 0}, or if {@code len > b.length - off} </exception> /// <exception cref="IOException"> if an I/O error occurs or this stream is already /// closed </exception> /// <exception cref="NullPointerException"> if {@code b} is null </exception> /// <exception cref="ZipException"> if a compression (ZIP) format error occurs </exception> //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: public void write(byte[] b, int off, int len) throws java.io.IOException public override void Write(sbyte[] b, int off, int len) { // Sanity checks EnsureOpen(); if (b == null) { throw new NullPointerException("Null buffer for read"); } else if (off < 0 || len < 0 || len > b.Length - off) { throw new IndexOutOfBoundsException(); } else if (len == 0) { return; } // Write uncompressed data to the output stream try { for (;;) { int n; // Fill the decompressor buffer with output data if (Inf.NeedsInput()) { int part; if (len < 1) { break; } part = (len < 512 ? len : 512); Inf.SetInput(b, off, part); off += part; len -= part; } // Decompress and write blocks of output data do { n = Inf.Inflate(Buf, 0, Buf.Length); if (n > 0) { @out.Write(Buf, 0, n); } } while (n > 0); // Check the decompressor if (Inf.Finished()) { break; } if (Inf.NeedsDictionary()) { throw new ZipException("ZLIB dictionary missing"); } } } catch (DataFormatException ex) { // Improperly formatted compressed (ZIP) data String msg = ex.Message; if (msg == null) { msg = "Invalid ZLIB data format"; } throw new ZipException(msg); } }
/// <summary> /// Call <see cref="Inflater.SetInput(byte[], int, int)"/> passing the current clear text buffer contents. /// </summary> /// <param name="inflater">The inflater to set input for.</param> public void SetInflaterInput(Inflater inflater) { if ( available > 0 ) { inflater.SetInput(clearText, clearTextLength - available, available); available = 0; } }
public void PackText(FileStream fileStream, uint off, uint zsize, uint size, Dictionary <ulong, string> dbd, uint hash2, uint hash1, int endtable, int somework) { BinaryReader binaryReader = new BinaryReader(fileStream); binaryReader.BaseStream.Position = off; binaryReader.ReadBytes(36); byte[] input = binaryReader.ReadBytes((int)zsize); byte[] buffer = new byte[size]; Inflater inflater = new Inflater(); inflater.SetInput(input); inflater.Inflate(buffer); BinaryReader binaryReader2 = new BinaryReader(new MemoryStream(buffer)); binaryReader2.ReadBytes(3); int num = binaryReader2.ReadInt32(); int num2 = num * 26 + 7; Dictionary <ulong, string> dictionary = new Dictionary <ulong, string>(); int num3 = 0; MemoryStream memoryStream = new MemoryStream(); BinaryWriter binaryWriter = new BinaryWriter(memoryStream); binaryWriter.Write((byte)1); binaryWriter.Write((byte)0); binaryWriter.Write((byte)0); binaryWriter.Write(num); while (num3 < num) { long num4 = binaryReader2.ReadInt64(); byte b = binaryReader2.ReadByte(); byte value = binaryReader2.ReadByte(); float value2 = binaryReader2.ReadSingle(); int num5 = binaryReader2.ReadInt32(); int num6 = binaryReader2.ReadInt32(); binaryReader2.ReadInt32(); long position = binaryReader2.BaseStream.Position; string change_value = "0"; if (num5 > 0) { binaryReader2.BaseStream.Seek(num6, SeekOrigin.Begin); byte[] bytes = binaryReader2.ReadBytes(num5); string text = Encoding.UTF8.GetString(bytes).Replace("\n", "\\n"); ulong key = UniqueId(num4, b); if (changes == "1") { if (dictionary_en.TryGetValue(key, out string _)) { if (dictionary_en[key] != text) { dbd.Remove(key); dictionary_xml_m.Remove(key); dictionary_xml_w.Remove(key); dictionary_xml_translator_m.Remove(key); dictionary_xml_translator_w.Remove(key); change_value = "1"; } else { change_value = "0"; } } } if (dbd.TryGetValue(key, out string _)) { dictionary.Add(key, dbd[key]); _ = dbd[key]; num5 = Encoding.UTF8.GetBytes(dbd[key].Replace("\\n", "\n")).Length; } else { transl_a = ""; if (ConfigurationManager.AppSettings["a_translate"] == "1" && somework == 1) { if (File.Exists(@"C:\\Program Files\\Mozilla Firefox\\firefox.exe") || File.Exists(@"C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe")) { transl_a = Translator(text, "Deepl"); /*string instrrr = "INSERT INTO Translated (fileinfo,hash,key_unic,text_en,text_ru_m,translator_m) VALUES ('" + hash1 + "','" + hash1 + "','" + key + "','" + WebUtility.HtmlEncode(text) + "','" + WebUtility.HtmlEncode(transl_a) + "','Deepl');"; * using (StreamWriter file_for_exam = new StreamWriter("db\\deepl_trans.txt", true)) * { * file_for_exam.WriteLine(instrrr); * } * transl_a = "";*/ } else { transl_a = Translator(text, "Promt"); } } if (transl_a == "") { dictionary.Add(key, text); dictionary_xml_m.Add(key, text); dictionary_xml_w.Add(key, text); dictionary_xml_translator_m.Add(key, "Deepl"); dictionary_xml_translator_w.Add(key, "Deepl"); num5 = Encoding.UTF8.GetBytes(text.Replace("\\n", "\n")).Length; } else { dictionary.Add(key, transl_a); dictionary_xml_m.Add(key, transl_a); dictionary_xml_w.Add(key, transl_a); dictionary_xml_translator_m.Add(key, "Deepl"); dictionary_xml_translator_w.Add(key, "Deepl"); num5 = Encoding.UTF8.GetBytes(transl_a.Replace("\\n", "\n")).Length; using (SQLiteConnection sqlite_conn = new SQLiteConnection("Data Source=db\\translate.db3; Version = 3; New = True; Compress = True; ")) { sqlite_conn.Open(); using (SQLiteCommand sqlite_cmd = new SQLiteCommand(sqlite_conn)) { if (change_value == "1") { sqlite_cmd.CommandText = "UPDATE Translated SET text_en='" + WebUtility.HtmlEncode(text) + "', text_ru_m='" + WebUtility.HtmlEncode(transl_a) + "',translator_m='Deepl',text_ru_w=NULL,translator_w=NULL WHERE key_unic ='" + key + "'"; } else { sqlite_cmd.CommandText = "INSERT INTO Translated (fileinfo,hash,key_unic,text_en,text_ru_m,translator_m) VALUES ('" + hash1 + "','" + hash1 + "','" + key + "','" + WebUtility.HtmlEncode(text) + "','" + WebUtility.HtmlEncode(transl_a) + "','Deepl')"; } sqlite_cmd.ExecuteNonQuery(); } } } /*string xml_text = "<hash>" + hash1 + "</hash><key>" + key + "</key><text_en>" + WebUtility.HtmlEncode(text) + "</text_en>"; * using (StreamWriter file_for_exam = * new StreamWriter("db\\new_eng.txt", true)) * { * file_for_exam.WriteLine(xml_text); * } * * string xml_text = "<filesinfo></filesinfo><hash>" + hash1 + "</hash><key>" + key + "</key><text_en>" + WebUtility.HtmlEncode(text) + "</text_en><text_ru_m transl=\"3\">" + WebUtility.HtmlEncode(dbd[key]) + "</text_ru_m><text_ru_w transl=\"3\"></text_ru_w>"; * using (StreamWriter file_for_exam = * new StreamWriter(@"C:\Users\Tidus\source\repos\SWToR_RUS\bin\Debug\db\all2.txt", true, encoding: Encoding.UTF8)) * { * file_for_exam.WriteLine(xml_text); * } * SQLiteConnection sqlite_conn; * sqlite_conn = new SQLiteConnection("Data Source=db\\translate.db3; Version = 3; New = True; Compress = True; "); * sqlite_conn.Open(); * SQLiteCommand sqlite_cmd; * sqlite_cmd = sqlite_conn.CreateCommand(); * string sql_insert = "INSERT INTO Translated (hash,key_unic,text_en,text_ru_m,translator_m) VALUES ('" + hash1.ToString() + "','" + key.ToString() + "','" + WebUtility.HtmlEncode(text) + "','" + WebUtility.HtmlEncode(dbd[key]) + "','3')"; * sqlite_cmd.CommandText = sql_insert; * sqlite_cmd.ExecuteNonQuery(); * sqlite_conn.Close();*/ } /*if (somework == 1) * { * string tmp_base; * if (dictionary_xml_translator_w.TryGetValue(key, out string sdfg) && dictionary_xml_m[key] != dictionary_xml_w[key]) * { * tmp_base = "INSERT INTO Translated (fileinfo,hash,key_unic,text_en,text_ru_m,translator_m,text_ru_w,translator_w) VALUES ('" + hash1 + "','" + hash1 + "','" + key + "','" + WebUtility.HtmlEncode(text) + "','" + WebUtility.HtmlEncode(dictionary_xml_m[key]).Replace("\n \n", "\n\n").Replace("\n", "\\n") + "','" + dictionary_xml_translator_m[key] + "','" + WebUtility.HtmlEncode(dictionary_xml_w[key]).Replace("\n \n", "\n\n").Replace("\n", "\\n") + "','" + dictionary_xml_translator_w[key] + "');"; * } * else * { * tmp_base = "INSERT INTO Translated (fileinfo,hash,key_unic,text_en,text_ru_m,translator_m) VALUES ('" + hash1 + "','" + hash1 + "','" + key + "','" + WebUtility.HtmlEncode(text) + "','" + WebUtility.HtmlEncode(dictionary_xml_m[key]).Replace("\n \n", "\n\n").Replace("\n", "\\n") + "','" + dictionary_xml_translator_m[key] + "');"; * } * tmp_base = "'" + nu88 + "','" + num4 + "','" + b + "','" + value + "','" + value2 + "','" + num6 + "','" + hash1 + "','" + key + "','" + WebUtility.HtmlEncode(text) + "','" + WebUtility.HtmlEncode(dictionary_xml_m[key]).Replace("\n \n", "\n\n").Replace("\n", "\\n") + "','" + dictionary_xml_translator_m[key] + "'"; * using (StreamWriter file_for_exam = new StreamWriter("db\\allbase.txt", true)) * { * file_for_exam.WriteLine(tmp_base); * } * }*/ } binaryReader2.BaseStream.Position = position; num3++; binaryWriter.Write(num4); binaryWriter.Write(b); binaryWriter.Write(value); binaryWriter.Write(value2); binaryWriter.Write(num5); binaryWriter.Write(num2); num2 += num5; binaryWriter.Write(num5); } foreach (KeyValuePair <ulong, string> item in dictionary) { Encoding.UTF8.GetBytes(item.Value.Replace("\\n", "\n")); binaryWriter.Write(Encoding.UTF8.GetBytes(item.Value.Replace("\\n", "\n"))); } MemoryStream memoryStream2 = new MemoryStream(); Deflater deflater = new Deflater(); deflater.SetInput(memoryStream.ToArray()); deflater.Finish(); byte[] array = new byte[size * 3]; while (!deflater.IsNeedingInput) { int count = deflater.Deflate(array); memoryStream2.Write(array, 0, count); if (deflater.IsFinished) { break; } } deflater.Reset(); int newsize = memoryStream.ToArray().Length; int newzsize = memoryStream2.ToArray().Length; byte[] array2 = new byte[36]; array2[0] = 2; array2[2] = 32; byte[] array3 = new byte[array2.Length + memoryStream2.ToArray().Length]; array2.CopyTo(array3, 0); memoryStream2.ToArray().CopyTo(array3, array2.Length); if (somework == 1) { using (StreamReader Hashes_Names = new StreamReader("db\\hashes_filename.txt", Encoding.Default)) { while (!Hashes_Names.EndOfStream) { string line = Hashes_Names.ReadLine(); if (line.IndexOf(hash1.ToString("X")) != -1) { string rez = line.Substring(line.IndexOf("/resources/en-us/str/") + 21); string rez2 = rez.Substring(0, rez.IndexOf("#")); if (File_Name_List.Contains(hash1.ToString())) { using (SQLiteConnection sqlite_conn = new SQLiteConnection("Data Source=db\\translate.db3; Version = 3; New = True; Compress = True; ")) { sqlite_conn.Open(); using (SQLiteCommand sqlite_cmd = new SQLiteCommand(sqlite_conn)) { sqlite_cmd.CommandText = "UPDATE Translated SET fileinfo='" + WebUtility.HtmlEncode(rez2) + "' WHERE hash ='" + hash1 + "'"; sqlite_cmd.ExecuteNonQuery(); } } } } } } } Hhh(fileStream, array3, newzsize, newsize, hash2, hash1, endtable); }
/// <summary> /// Reads a MPK from a binary reader /// </summary> /// <param name="rdr">The binary reader pointing to the MPK</param> private void ReadArchive(BinaryReader rdr) { _files.Clear(); _crc.Value = 0; _sizeDir = 0; _sizeName = 0; _numFiles = 0; var buf = new byte[16]; rdr.Read(buf, 0, 16); for (byte i = 0; i < 16; ++i) { buf[i] ^= i; } _crc.Value = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]; _sizeDir = (buf[4] << 24) | (buf[5] << 16) | (buf[6] << 8) | buf[7]; _sizeName = (buf[8] << 24) | (buf[9] << 16) | (buf[10] << 8) | buf[11]; _numFiles = (buf[12] << 24) | (buf[13] << 16) | (buf[14] << 8) | buf[15]; buf = new byte[_sizeName]; rdr.Read(buf, 0, _sizeName); var inf = new Inflater(); inf.SetInput(buf); buf = new byte[1024]; inf.Inflate(buf); buf[inf.TotalOut] = 0; _name = Marshal.ConvertToString(buf); long totalin = 0; buf = ReadDirectory(rdr, ref totalin); using (var directory = new MemoryStream(buf)) { long pos = rdr.BaseStream.Position; long len = rdr.BaseStream.Seek(0, SeekOrigin.End); rdr.BaseStream.Position = pos; buf = new byte[len - pos]; rdr.Read(buf, 0, buf.Length); using (var files = new MemoryStream(buf)) { rdr.BaseStream.Position = pos - totalin; buf = new byte[totalin]; rdr.Read(buf, 0, buf.Length); var crc = new Crc32(); crc.Reset(); crc.Update(buf); if (crc.Value != _crc.Value) { throw new Exception("Invalid or corrupt MPK"); } while (directory.Position < directory.Length && files.Position < files.Length) { crc.Reset(); buf = new byte[MPKFileHeader.MaxSize]; directory.Read(buf, 0, MPKFileHeader.MaxSize); MPKFileHeader hdr; using (var hdrStream = new MemoryStream(buf)) { using (var hdrRdr = new BinaryReader(hdrStream, Encoding.UTF8)) { hdr = new MPKFileHeader(hdrRdr); } } var compbuf = new byte[hdr.CompressedSize]; files.Read(compbuf, 0, compbuf.Length); crc.Update(compbuf, 0, compbuf.Length); inf.Reset(); inf.SetInput(compbuf, 0, compbuf.Length); buf = new byte[hdr.UncompressedSize]; inf.Inflate(buf, 0, buf.Length); var file = new MPKFile(compbuf, buf, hdr); if (crc.Value != hdr.CRC.Value) { OnInvalidFile(file); continue; } _files.Add(hdr.Name.ToLower(), file); } } } }
public byte[] аспаковать(byte[] байты, int длина, out byte[] ответСерверу) { byte[] буфер = new byte[длина * 16]; int смещение = 0; List <byte> результат = new List <byte>(буфер.Length); List <byte> ответ = new List <byte>(8); Начало: if (inflater != null) { inflater.SetInput(байты, смещение, длина - смещение); while (true) { int числоБайт = inflater.Inflate(буфер); if (числоБайт > 0) { for (int i = 0; i < числоБайт; i++) { результат.Add(буфер[i]); } continue; } if (inflater.IsFinished) { смещение = длина - inflater.RemainingInput; inflater = null; версияMccp = 0; goto Начало; } break; } } else { while (смещение < длина) { byte байт = байты[смещение]; последовательность.Add(байт); смещение++; if (состояние == состояние_Обычное && байт == Telnet.Iac) { состояние = состояние_Iac; continue; } if (состояние == состояние_Iac && байт == Telnet.Will) { состояние = состояние_IacWill; continue; } if (состояние == состояние_IacWill && байт == mccp_Compress) { состояние = состояние_Обычное; последовательность = new List <byte>(8); ответ.Add(Telnet.Iac); ответ.Add(Telnet.Do); ответ.Add(mccp_Compress); continue; } if (состояние == состояние_IacWill && байт == mccp_Compress2) { состояние = состояние_Обычное; последовательность = new List <byte>(8); ответ.Add(Telnet.Iac); ответ.Add(Telnet.Do); ответ.Add(mccp_Compress2); continue; } if (состояние == состояние_Iac && байт == Telnet.Sb) { состояние = состояние_IacSb; continue; } if (состояние == состояние_IacSb && байт == mccp_Compress) { состояние = состояние_IacSbCompress; continue; } if (состояние == состояние_IacSb && байт == mccp_Compress2) { состояние = состояние_IacSbCompress2; continue; } if (состояние == состояние_IacSbCompress && байт == Telnet.Will) { состояние = состояние_IacSbCompressWill; continue; } if (состояние == состояние_IacSbCompress2 && байт == Telnet.Iac) { состояние = состояние_IacSbCompress2Iac; continue; } if (состояние == состояние_IacSbCompressWill && байт == Telnet.Se) { состояние = состояние_Обычное; последовательность = new List <byte>(8); версияMccp = 1; inflater = new Inflater(); goto Начало; } if (состояние == состояние_IacSbCompress2Iac && байт == Telnet.Se) { состояние = состояние_Обычное; последовательность = new List <byte>(8); версияMccp = 2; inflater = new Inflater(); goto Начало; } состояние = состояние_Обычное; результат.AddRange(последовательность); последовательность = new List <byte>(8); } } ответСерверу = ответ.ToArray(); return(результат.ToArray()); }