public bool UpdateWordform(IWfiWordform wordform, ParserPriority priority) { CheckDisposed(); uint crcWordform = 0; ITsString form = null; int hvo = 0; using (new WorkerThreadReadHelper(m_cache.ServiceLocator.GetInstance <IWorkerThreadReadHandler>())) { if (wordform.IsValidObject) { crcWordform = (uint)wordform.Checksum; form = wordform.Form.VernacularDefaultWritingSystem; hvo = wordform.Hvo; } } // 'form' will now be null, if it could not find the wordform for whatever reason. // uiCRCWordform will also now be 0, if 'form' is null. if (form == null || string.IsNullOrEmpty(form.Text)) { return(false); } CheckNeedsUpdate(); string result = GetOneWordformResult(hvo, form.Text.Replace(' ', '.')); // LT-7334 to allow for phrases uint crc = CrcStream.GetCrc(result); if (crcWordform == crc) { return(false); } return(m_parseFiler.ProcessParse(priority, result, crc)); }
public static string GetCrc(this object obj) { using (var crc = new CrcStream(new MemoryStream(Encoding.UTF8.GetBytes(obj.ToString())))) { using (var rdr = new StreamReader(crc)) { rdr.ReadToEnd(); return(crc.ReadCrc.ToString("X8")); } } }
/// <summary> /// Implements the Dispose pattern /// </summary> /// <param name="disposing">Whether this object is being disposed via a call to Dispose /// or garbage collected.</param> protected virtual void Dispose(bool disposing) { if (!this.disposed) { if (disposing) { if (this.CrcStream != null) { CrcStream.Dispose(); CrcStream = null; } if (this.WrappingStream != null) { WrappingStream.Dispose(); WrappingStream = null; } } this.disposed = true; } }
private byte[] GetCrc(Stream stream) { using (CrcStream cStream = new CrcStream(stream)) { byte[] buffer = new byte[4096]; int length; while ((length = cStream.Read(buffer, 0, buffer.Length)) != 0) { // Do whatever you need to do in here } stream.Seek(0, SeekOrigin.Begin); return BitConverter.GetBytes(cStream.ReadCrc); } }
// Copies all source file into storage file private void Store(ref ZipEntry entry, Stream source) { uint totalRead = 0; Stream outStream; long posStart = this.zipFileStream.Position; long sourceStart = source.Position; outStream = this.zipFileStream; var inStream = new CrcStream(source); switch (entry.Method) { case Compression.LZMA: var encoder = new SevenZip.Compression.LZMA.Encoder(); // LZMA "magic bytes" (7-zip requires them) var magicBytes = BitConverter.GetBytes(0x00051409); outStream.Write(magicBytes, 0, magicBytes.Length); encoder.WriteCoderProperties(outStream); encoder.Code(inStream, outStream, -1, -1, null); break; case Compression.Deflate: //using (var outPutStream = new ZOutputStream(outStream, zlibConst.Z_DEFAULT_COMPRESSION)) var outPutStream = new Ionic.Zlib.DeflateStream(outStream, Ionic.Zlib.CompressionMode.Compress, CompressionLevel.BestCompression, true); { outPutStream.FlushMode = FlushType.Full; int buffSize = ZlibConstants.WorkingBufferSizeDefault; byte[] buff = new byte[buffSize]; int size = 0; do { size = inStream.Read(buff, 0, buffSize); if (size > 0) { outPutStream.Write(buff, 0, size); } } while (size > 0); } break; default: //case Compression.Store: inStream.CopyTo(outStream); break; } entry.Crc32 = inStream.ReadCrc; outStream.Flush(); entry.FileSize = (uint)(source.Position - sourceStart); entry.CompressedSize = (uint)(this.zipFileStream.Position - posStart); // Verify for real compression if (entry.Method != Compression.Store && !this.ForceDeflating && source.CanSeek && entry.CompressedSize > entry.FileSize) { // Start operation again with Store algorithm entry.Method = Compression.Store; this.zipFileStream.Position = posStart; this.zipFileStream.SetLength(posStart); source.Position = sourceStart; this.Store(ref entry, source); } }
/// <summary> /// Copy the contents of a stored file into an opened stream /// </summary> /// <param name="entry">Entry information of file to extract</param> /// <param name="_stream">Stream to store the uncompressed data</param> /// <returns>True if success, false if not.</returns> /// <remarks>Unique compression methods are Store and Deflate</remarks> public bool ExtractFile(ZipEntry entry, Stream stream) { if (!stream.CanWrite) { throw new InvalidOperationException("Stream cannot be written"); } // check signature byte[] signature = new byte[4]; this.zipFileStream.Seek(entry.HeaderOffset, SeekOrigin.Begin); this.zipFileStream.Read(signature, 0, 4); if (BitConverter.ToUInt32(signature, 0) != 0x04034b50) { return(false); } // Prepare streams stream.SetLength(entry.FileSize); var outStream = new CrcStream(stream); this.zipFileStream.Seek(entry.FileOffset, SeekOrigin.Begin); // Select input stream for inflating or just reading Stream inStream = new SegmentStream(this.zipFileStream, entry.CompressedSize); if (entry.Method == Compression.Deflate) { using (var intPutStream = new Ionic.Zlib.DeflateStream(inStream, Ionic.Zlib.CompressionMode.Decompress)) { intPutStream.FlushMode = FlushType.Full; int buffSize = 4096; byte[] buff = new byte[buffSize]; int size = 0; do { size = intPutStream.Read(buff, 0, buffSize); if (size > 0) { outStream.Write(buff, 0, size); } } while (size > 0); } //inStream = new DeflateStream(inStream, CompressionMode.Decompress, true); } // Decompress if (entry.Method == Compression.LZMA) { var decoder = new SevenZip.Compression.LZMA.Decoder(); const int PropsLength = 5; var buffer = new byte[PropsLength]; inStream.Read(buffer, 0, sizeof(int)); if (BitConverter.ToInt32(buffer, 0) != 0x00051409) { throw new Exception("Invalid LZMA stream signature"); } if (inStream.Read(buffer, 0, PropsLength) != PropsLength) { throw new Exception("Invalid LZMA properties length"); } decoder.SetDecoderProperties(buffer); decoder.Code(inStream, outStream, entry.CompressedSize, entry.FileSize, null); } else { inStream.CopyTo(outStream); } stream.Flush(); //if (entry.Method == Compression.Deflate) inStream.Dispose(); if (entry.Crc32 != outStream.WriteCrc) { throw new Exception("Uncompressed file CRC mismatch"); } return(true); }
internal void UpdateWordform(int hvoWordform) { CheckDisposed(); //the WFI DLL, created with FieldWorks COM code, can only be accessed by an STA // REVIEW JohnH(RandyR): Is this still relevant, now that ParseFiler isn't in its own DLL? //Debug.Assert( Thread.CurrentThread.ApartmentState == ApartmentState.STA, "Calling thread must set the apartment state to STA"); uint uiCRCWordform; string form = GetWordformStringAndCRC(hvoWordform, out uiCRCWordform); // 'form' will now be null, if it could not find the wordform for whatever reason. // uiCRCWordform will also now be 0, if 'form' is null. // Cf. LT-7203 for how it could be null. if (form == null) { return; } TaskReport task = new TaskReport(String.Format(ParserCoreStrings.ksUpdateX, form), m_taskUpdateHandler); using (task) { Debug.Assert(m_parserFiler != null); string result = ""; try { string sResult = GetOneWordformResult(hvoWordform, form); // GetOneWordformResult can throw an exception //Debug.WriteLine("ParserWorker: Ample result = " + sAmpResult); uint uiCRC = CrcStream.GetCrc(sResult); if (uiCRCWordform != uiCRC) { StringBuilder sb = new StringBuilder(); // use StringBuilder for efficiency sb.Append("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"); // REMOVED THIS FROM THE SAMPLE DOC: xmlns:xsi=\"http://www.w3.org/2000/10/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"C:\ww\code\SampleData\XMLFieldWorksParse.xsd"> sb.Append(" <AnalyzingAgentResults>\n<AnalyzingAgent name=\"" + m_agentInfo.m_name + "\" human=\"" + m_agentInfo.m_isHuman.ToString() + "\" version=\"" + m_agentInfo.m_version + "\">\n"); sb.Append(" <StateInformation />\n</AnalyzingAgent>\n<WordSet>\n"); sb.Append(sResult); sb.Append("</WordSet>\n</AnalyzingAgentResults>\n"); result = sb.ToString(); DateTime startTime = DateTime.Now; m_parserFiler.ProcessParse(result); // ProcessParse can throw exceptions. long ttlTicks = DateTime.Now.Ticks - startTime.Ticks; m_ticksFiler += ttlTicks; Trace.WriteLineIf(tracingSwitch.TraceInfo, "parser filer(" + form + ") took : " + ttlTicks.ToString()); SetChecksum(hvoWordform, uiCRC); } } catch (Exception error) { DebugMsg(error.Message); task.NotificationMessage = error.Message; throw; } } }