private (LhaContent, byte[]) ReadContent(FileStream fileStream, long?position = null) { if (position.HasValue) { _fileStreamReadyForAppend = false; _fileStream.Seek(position.Value, SeekOrigin.Begin); } var streamHeaderPosition = fileStream.Position; var readBytes = fileStream.Read(_headerBuffer, 0, 22); if (readBytes == 0 || (readBytes > 1 && readBytes < 22)) { _logger.Error("Abnormal end of archive file!"); //break; return(null, null); } var headerLength = _headerBuffer[0]; if (headerLength == 0) { // End of archive //break; return(null, null); } // crc will be written later var headerCrc = _headerBuffer[1]; //var methodId = LhaEncoding.GetString(_headerBuffer, 2, 5); var methodId = _encodingConverter.ConvertIsoBytesToUtf8String(_headerBuffer, 2, 5); var length000000FF = _headerBuffer[7]; // little endian var length0000FF00 = _headerBuffer[8]; var length00FF0000 = _headerBuffer[9]; var lengthFF000000 = _headerBuffer[10]; var dateTime000000FF = _headerBuffer[15]; var dateTime0000FF00 = _headerBuffer[16]; var dateTime00FF0000 = _headerBuffer[17]; var dateTimeFF000000 = _headerBuffer[18]; var attributes = _headerBuffer[19]; var levelIdentifier = _headerBuffer[20]; var pathLength = _headerBuffer[21]; int contentLength = length000000FF; contentLength |= length0000FF00 << 8; contentLength |= length00FF0000 << 16; contentLength |= lengthFF000000 << 24; uint dateTimeInt = dateTime000000FF; dateTimeInt |= (uint)(dateTime0000FF00 << 8); dateTimeInt |= (uint)(dateTime00FF0000 << 16); dateTimeInt |= (uint)(dateTimeFF000000 << 24); var dateTime = IntToDateTime(dateTimeInt); EnsureHeaderBufferLength(headerLength + 2); fileStream.Read(_headerBuffer, 22, pathLength + 2); var path = _encodingConverter.ConvertIsoBytesToUtf8String(_headerBuffer, 22, pathLength); string comment = null; for (var i = 0; i < pathLength; i++) { if (_headerBuffer[22 + i] == 0) { path = _encodingConverter.ConvertIsoBytesToUtf8String(_headerBuffer, 22, i); comment = _encodingConverter.ConvertIsoBytesToUtf8String(_headerBuffer, 22 + i + 1, pathLength - i - 1); } } var calcHeaderCrc = CalcHeaderCrc(2, headerLength); if (methodId != "-lh0-") { throw new Exception($"Unknown Method ID in [{path}]: {methodId}"); } if (levelIdentifier != 0) { throw new Exception($"Unknown Level identifier in [{path}]: {levelIdentifier}"); } if (calcHeaderCrc != headerCrc) { throw new Exception($"Header CRC mismatch in [{path}]: headerCrc={headerCrc} calcHeaderCrc={calcHeaderCrc}"); } var streamContentPosition = fileStream.Position; fileStream.Seek(contentLength, SeekOrigin.Current); var content = new LhaContent(path, dateTime, attributes, contentLength, streamHeaderPosition, streamContentPosition); return(content, _headerBuffer); }
public LhaContentFileInfo(LhaContent content, Func <Stream> getStreamAction) { _content = content; _getStreamAction = getStreamAction; }