Exemple #1
0
 private WebSocket(HttpConnection connection)
 {
     _closeConnection = connection.ForceClose;
     _stream          = connection.Stream;
     _readyState      = WebSocketState.Open;
 }
Exemple #2
0
 public static ValueTask <TValue?> FromStreamAsync <TValue>(this Stream?stream,
                                                            MessagePackSerializerOptions?options = null, CancellationToken cancellationToken = default) =>
 MessagePackHelper.FromStreamAsync <TValue>(stream, options, cancellationToken);
 public static RawInboundEnvelope CloneReplacingStream(this IRawInboundEnvelope envelope, Stream?rawMessage) =>
Exemple #4
0
        static async Task <PhpValue> ProcessResponse(Context ctx, CURLResource ch, HttpWebResponse response)
        {
            // in case we are returning the response value
            var returnstream = ch.ProcessingResponse.Method == ProcessMethodEnum.RETURN
                ? new MemoryStream()
                : null;

            // handle headers
            if (!ch.ProcessingHeaders.IsEmpty)
            {
                var    statusHeaders       = HttpHeaders.StatusHeader(response) + HttpHeaders.HeaderSeparator; // HTTP/1.1 xxx xxx\r\n
                Stream?outputHeadersStream = null;

                switch (ch.ProcessingHeaders.Method)
                {
                case ProcessMethodEnum.RETURN:
                case ProcessMethodEnum.STDOUT:
                    outputHeadersStream = (returnstream ?? ctx.OutputStream);
                    goto default;

                case ProcessMethodEnum.FILE:
                    outputHeadersStream = ch.ProcessingHeaders.Stream.RawStream;
                    goto default;

                case ProcessMethodEnum.USER:
                    // pass headers one by one,
                    // in original implementation we should pass them as they are read from socket:

                    ch.ProcessingHeaders.User.Invoke(ctx, new[] {
                        PhpValue.FromClass(ch),
                        PhpValue.Create(statusHeaders)
                    });

                    for (int i = 0; i < response.Headers.Count; i++)
                    {
                        var key   = response.Headers.GetKey(i);
                        var value = response.Headers.Get(i);

                        if (key == null || key.Length != 0)
                        {
                            // header
                            ch.ProcessingHeaders.User.Invoke(ctx, new[] {
                                PhpValue.FromClr(ch),
                                PhpValue.Create(key + ": " + value + HttpHeaders.HeaderSeparator),
                            });
                        }
                    }

                    // \r\n
                    ch.ProcessingHeaders.User.Invoke(ctx, new[] {
                        PhpValue.FromClr(ch),
                        PhpValue.Create(HttpHeaders.HeaderSeparator)
                    });

                    break;

                default:
                    if (outputHeadersStream != null)
                    {
                        await outputHeadersStream.WriteAsync(Encoding.ASCII.GetBytes(statusHeaders));

                        await outputHeadersStream.WriteAsync(response.Headers.ToByteArray());
                    }
                    else
                    {
                        Debug.Fail("Unexpected ProcessingHeaders " + ch.ProcessingHeaders.Method);
                    }
                    break;
                }
            }

            var stream = response.GetResponseStream();

            // gzip decode if necessary
            if (response.ContentEncoding == "gzip") // TODO: // && ch.AcceptEncoding.Contains("gzip") ??
            {
                ch.VerboseOutput("Decompressing the output stream using GZipStream.");
                stream = new GZipStream(stream, CompressionMode.Decompress, leaveOpen: false);
            }

            // read into output stream:
            switch (ch.ProcessingResponse.Method)
            {
            case ProcessMethodEnum.STDOUT: await stream.CopyToAsync(ctx.OutputStream); break;

            case ProcessMethodEnum.RETURN: stream.CopyTo(returnstream); break;

            case ProcessMethodEnum.FILE: await stream.CopyToAsync(ch.ProcessingResponse.Stream.RawStream); break;

            case ProcessMethodEnum.USER:
                if (response.ContentLength != 0)
                {
                    // preallocate a buffer to read to,
                    // this should be according to PHP's behavior and slightly more effective than memory stream
                    byte[] buffer = new byte[ch.BufferSize > 0 ? ch.BufferSize : 2048];
                    int    bufferread;

                    while ((bufferread = stream.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        ch.ProcessingResponse.User.Invoke(ctx, new[]
                        {
                            PhpValue.FromClr(ch),
                            PhpValue.Create(new PhpString(buffer.AsSpan(0, bufferread).ToArray())),     // clone the array and pass to function
                        });
                    }
                }
                break;

            case ProcessMethodEnum.IGNORE: break;
            }

            //
            stream.Dispose();

            //
            if (response.ResponseUri != null)
            {
                ch.Url = response.ResponseUri.AbsoluteUri;
            }

            return((returnstream != null)
                ? PhpValue.Create(new PhpString(returnstream.ToArray()))
                : PhpValue.True);
        }
Exemple #5
0
        public async Task <DocumentDebugInfoReader?> GetDocumentDebugInfoReaderAsync(string dllPath, bool useDefaultSymbolServers, TelemetryMessage telemetry, CancellationToken cancellationToken)
        {
            var dllStream = IOUtilities.PerformIO(() => File.OpenRead(dllPath));

            if (dllStream is null)
            {
                return(null);
            }

            Stream?pdbStream = null;
            DocumentDebugInfoReader?result = null;
            var peReader = new PEReader(dllStream);

            try
            {
                // Try to load the pdb file from disk, or embedded
                if (peReader.TryOpenAssociatedPortablePdb(dllPath, pdbPath => File.OpenRead(pdbPath), out var pdbReaderProvider, out var pdbFilePath))
                {
                    Contract.ThrowIfNull(pdbReaderProvider);

                    if (pdbFilePath is null)
                    {
                        telemetry.SetPdbSource("embedded");
                        _logger?.Log(FeaturesResources.Found_embedded_PDB_file);
                    }
                    else
                    {
                        telemetry.SetPdbSource("ondisk");
                        _logger?.Log(FeaturesResources.Found_PDB_file_at_0, pdbFilePath);
                    }

                    result = new DocumentDebugInfoReader(peReader, pdbReaderProvider);
                }

                if (result is null)
                {
                    if (_sourceLinkService is null)
                    {
                        _logger?.Log(FeaturesResources.Could_not_find_PDB_on_disk_or_embedded);
                    }
                    else
                    {
                        var delay = Task.Delay(SymbolLocatorTimeout, cancellationToken);
                        // Call the debugger to find the PDB from a symbol server etc.
                        var pdbResultTask = _sourceLinkService.GetPdbFilePathAsync(dllPath, peReader, useDefaultSymbolServers, cancellationToken);

                        var winner = await Task.WhenAny(pdbResultTask, delay).ConfigureAwait(false);

                        if (winner == pdbResultTask)
                        {
                            var pdbResult = await pdbResultTask.ConfigureAwait(false);

                            if (pdbResult is not null)
                            {
                                pdbStream = IOUtilities.PerformIO(() => File.OpenRead(pdbResult.PdbFilePath));
                                if (pdbStream is not null)
                                {
                                    var readerProvider = MetadataReaderProvider.FromPortablePdbStream(pdbStream);
                                    telemetry.SetPdbSource("symbolserver");
                                    result = new DocumentDebugInfoReader(peReader, readerProvider);
                                    _logger?.Log(FeaturesResources.Found_PDB_on_symbol_server);
                                }
                                else
                                {
                                    _logger?.Log(FeaturesResources.Found_PDB_on_symbol_server_but_could_not_read_file);
                                }
                            }
                            else
                            {
                                _logger?.Log(FeaturesResources.Could_not_find_PDB_on_disk_or_embedded_or_server);
                            }
                        }
                        else
                        {
                            telemetry.SetPdbSource("timeout");
                            _logger?.Log(FeaturesResources.Timeout_symbol_server);
                        }
                    }
                }
            }
            catch (BadImageFormatException ex)
            {
                // If the PDB is corrupt in some way we can just ignore it, and let the system fall through to another provider
                _logger?.Log(FeaturesResources.Error_reading_PDB_0, ex.Message);
                result = null;
            }
            finally
            {
                // If we're returning a result then it will own the disposal of the reader, but if not
                // then we need to do it ourselves.
                if (result is null)
                {
                    pdbStream?.Dispose();
                    peReader.Dispose();
                }
            }

            return(result);
        }
        /// <summary>
        /// Serializes the formplot. The header information is written to the <paramref name="metaDataWriter" />, whereas the points are stored as a
        /// blob in the <paramref name="pointDataStream" /></summary>
        /// <param name="plot">The plot.</param>
        /// <param name="metaDataWriter">An XML writer to store the header data, such as tolerances and segments.</param>
        /// <param name="pointDataStream">A stream to store the binary point data.</param>
        private static void Serialize <TPoint, TGeometry>(this Formplot <TPoint, TGeometry> plot, XmlWriter metaDataWriter, Stream?pointDataStream)
            where TPoint : Point <TPoint, TGeometry>, new()
            where TGeometry : Geometry, new()
        {
            if (metaDataWriter == null)
            {
                throw new ArgumentNullException(nameof(metaDataWriter));
            }

            if (plot.FormplotType != FormplotTypes.None)
            {
                metaDataWriter.WriteAttributeString("Type", plot.FormplotType.ToString());
            }

            metaDataWriter.WriteStartElement("CreatorSoftware");
            metaDataWriter.WriteValue(plot.CreatorSoftware);
            metaDataWriter.WriteEndElement();

            metaDataWriter.WriteStartElement("CreatorSoftwareVersion");
            metaDataWriter.WriteValue(plot.CreatorSoftwareVersion.ToString());
            metaDataWriter.WriteEndElement();

            if (plot.Properties.Count > 0)
            {
                foreach (var property in plot.Properties)
                {
                    metaDataWriter.WriteStartElement("Property");
                    property.Serialize(metaDataWriter);
                    metaDataWriter.WriteEndElement();
                }
            }

            if (plot.FormplotType != FormplotTypes.None && pointDataStream != null)
            {
                WriteFormplotSpecificProperties(plot, metaDataWriter, pointDataStream);
            }
        }
        //    This is called by underlying base class code, each time a new response is received from the wire or a protocol stage is resumed.
        //    This function controls the setting up of a data socket/connection, and of saving off the server responses.
        protected override PipelineInstruction PipelineCallback(PipelineEntry?entry, ResponseDescription?response, bool timeout, ref Stream?stream)
        {
            if (NetEventSource.Log.IsEnabled())
            {
                NetEventSource.Info(this, $"Command:{entry?.Command} Description:{response?.StatusDescription}");
            }

            // null response is not expected
            if (response == null)
            {
                return(PipelineInstruction.Abort);
            }

            FtpStatusCode status = (FtpStatusCode)response.Status;

            //
            // Update global "current status" for FtpWebRequest
            //
            if (status != FtpStatusCode.ClosingControl)
            {
                // A 221 status won't be reflected on the user FTP response
                // Anything else will (by design?)
                StatusCode = status;
                StatusLine = response.StatusDescription;
            }

            // If the status code is outside the range defined in RFC (1xx to 5xx) throw
            if (response.InvalidStatusCode)
            {
                throw new WebException(SR.net_InvalidStatusCode, WebExceptionStatus.ProtocolError);
            }

            // Update the banner message if any, this is a little hack because the "entry" param is null
            if (_index == -1)
            {
                if (status == FtpStatusCode.SendUserCommand)
                {
                    _bannerMessage = new StringBuilder();
                    _bannerMessage.Append(StatusLine);
                    return(PipelineInstruction.Advance);
                }
                else if (status == FtpStatusCode.ServiceTemporarilyNotAvailable)
                {
                    return(PipelineInstruction.Reread);
                }
                else
                {
                    throw GenerateException(status, response.StatusDescription, null);
                }
            }

            //
            // Check for the result of our attempt to use UTF8
            //
            if (entry !.Command == "OPTS utf8 on\r\n")
            {
                if (response.PositiveCompletion)
                {
                    Encoding = Encoding.UTF8;
                }
                else
                {
                    Encoding = Encoding.Default;
                }
                return(PipelineInstruction.Advance);
            }

            // If we are already logged in and the server returns 530 then
            // the server does not support re-issuing a USER command,
            // tear down the connection and start all over again
            if (entry.Command.IndexOf("USER", StringComparison.Ordinal) != -1)
            {
                // The server may not require a password for this user, so bypass the password command
                if (status == FtpStatusCode.LoggedInProceed)
                {
                    _loginState = FtpLoginState.LoggedIn;
                    _index++;
                }
            }

            //
            // Throw on an error with possible recovery option
            //
            if (response.TransientFailure || response.PermanentFailure)
            {
                if (status == FtpStatusCode.ServiceNotAvailable)
                {
                    MarkAsRecoverableFailure();
                }
                throw GenerateException(status, response.StatusDescription, null);
            }

            if (_loginState != FtpLoginState.LoggedIn &&
                entry.Command.IndexOf("PASS", StringComparison.Ordinal) != -1)
            {
                // Note the fact that we logged in
                if (status == FtpStatusCode.NeedLoginAccount || status == FtpStatusCode.LoggedInProceed)
                {
                    _loginState = FtpLoginState.LoggedIn;
                }
                else
                {
                    throw GenerateException(status, response.StatusDescription, null);
                }
            }

            //
            // Parse special cases
            //
            if (entry.HasFlag(PipelineEntryFlags.CreateDataConnection) && (response.PositiveCompletion || response.PositiveIntermediate))
            {
                bool isSocketReady;
                PipelineInstruction result = QueueOrCreateDataConection(entry, response, timeout, ref stream, out isSocketReady);
                if (!isSocketReady)
                {
                    return(result);
                }
                // otherwise we have a stream to create
            }
            //
            // This is part of the above case and it's all about giving data stream back
            //
            if (status == FtpStatusCode.OpeningData || status == FtpStatusCode.DataAlreadyOpen)
            {
                if (_dataSocket == null)
                {
                    return(PipelineInstruction.Abort);
                }
                if (!entry.HasFlag(PipelineEntryFlags.GiveDataStream))
                {
                    _abortReason = SR.Format(SR.net_ftp_invalid_status_response, status, entry.Command);
                    return(PipelineInstruction.Abort);
                }

                // Parse out the Content length, if we can
                TryUpdateContentLength(response.StatusDescription !);

                // Parse out the file name, when it is returned and use it for our ResponseUri
                FtpWebRequest request = (FtpWebRequest)_request !;
                if (request.MethodInfo.ShouldParseForResponseUri)
                {
                    TryUpdateResponseUri(response.StatusDescription !, request);
                }

                return(QueueOrCreateFtpDataStream(ref stream));
            }


            //
            // Parse responses by status code exclusivelly
            //

            // Update welcome message
            if (status == FtpStatusCode.LoggedInProceed)
            {
                _welcomeMessage !.Append(StatusLine);
            }
            // OR set the user response ExitMessage
            else if (status == FtpStatusCode.ClosingControl)
            {
                _exitMessage !.Append(response.StatusDescription);
                // And close the control stream socket on "QUIT"
                CloseSocket();
            }
            // OR set us up for SSL/TLS, after this we'll be writing securely
            else if (status == FtpStatusCode.ServerWantsSecureSession)
            {
                // If NetworkStream is a TlsStream, then this must be in the async callback
                // from completing the SSL handshake.
                // So just let the pipeline continue.
                if (!(NetworkStream is TlsStream))
                {
                    FtpWebRequest request   = (FtpWebRequest)_request !;
                    TlsStream     tlsStream = new TlsStream(NetworkStream, Socket, request.RequestUri.Host, request.ClientCertificates);

                    if (_isAsync)
                    {
                        tlsStream.BeginAuthenticateAsClient(ar =>
                        {
                            try
                            {
                                tlsStream.EndAuthenticateAsClient(ar);
                                NetworkStream = tlsStream;
                                this.ContinueCommandPipeline();
                            }
                            catch (Exception e)
                            {
                                this.CloseSocket();
                                this.InvokeRequestCallback(e);
                            }
                        }, null);

                        return(PipelineInstruction.Pause);
                    }
                    else
                    {
                        tlsStream.AuthenticateAsClient();
                        NetworkStream = tlsStream;
                    }
                }
            }
            // OR parse out the file size or file time, usually a result of sending SIZE/MDTM commands
            else if (status == FtpStatusCode.FileStatus)
            {
                if (entry.Command.StartsWith("SIZE ", StringComparison.Ordinal))
                {
                    _contentLength = GetContentLengthFrom213Response(response.StatusDescription !);
                }
                else if (entry.Command.StartsWith("MDTM ", StringComparison.Ordinal))
                {
                    _lastModified = GetLastModifiedFrom213Response(response.StatusDescription !);
                }
            }
            // OR parse out our login directory
            else if (status == FtpStatusCode.PathnameCreated)
            {
                if (entry.Command == "PWD\r\n" && !entry.HasFlag(PipelineEntryFlags.UserCommand))
                {
                    _loginDirectory = GetLoginDirectory(response.StatusDescription !);
                }
            }
            // Asserting we have some positive response
            else
            {
                // We only use CWD to reset ourselves back to the login directory.
                if (entry.Command.IndexOf("CWD", StringComparison.Ordinal) != -1)
                {
                    _establishedServerDirectory = _requestedServerDirectory;
                }
            }

            // Intermediate responses require rereading
            if (response.PositiveIntermediate || (!UsingSecureStream && entry.Command == "AUTH TLS\r\n"))
            {
                return(PipelineInstruction.Reread);
            }

            return(PipelineInstruction.Advance);
        }
Exemple #8
0
    public void ReadAsync_ReturnsSynchronously()
    {
        Stream?stream = SimpleSequence.AsStream();

        Assert.True(stream.ReadAsync(new byte[1], 0, 1).IsCompleted);
    }
Exemple #9
0
        /// <summary>
        /// Load from EOP files
        /// <parameter name="eopC04File">File from https://datacenter.iers.org/data/latestVersion/EOP_14_C04_IAU1980_one_file_1962-now.txt</parameter>
        /// <parameter name="eopFinalsFile">File from https://datacenter.iers.org/data/latestVersion/finals.all.iau1980.txt</parameter>
        /// </summary>
        public static EarthOrientationParameters LoadFrom(
            Stream eopC04File,
            Stream?eopFinalsFile)
        {
            const double    TJDOFS = 2400000.5;
            string          s;
            JulianDayNumber beginDate = default;
            var             dpsi      = new List <double>();
            var             deps      = new List <double>();
            int             mjdsv     = 0;

            using (var reader = new StreamReader(eopC04File))
            {
                while ((s = reader.ReadLine()) != null)
                {
                    if (!s.StartsWith("1962"))
                    {
                        continue;
                    }
                    var values = s.Split(' ', StringSplitOptions.RemoveEmptyEntries);
                    var mjd    = int.Parse(values[3]);
                    // is file in one-day steps?
                    if (mjdsv > 0 && mjd - mjdsv != 1)
                    {
                        throw new FormatException("File must be in one-day steps");
                    }
                    if (mjdsv == 0)
                    {
                        beginDate = JulianDayNumber.FromRaw(mjd + TJDOFS);
                    }
                    dpsi.Add(double.Parse(values[8], CultureInfo.InvariantCulture));
                    deps.Add(double.Parse(values[9], CultureInfo.InvariantCulture));
                    mjdsv = mjd;
                }
            }
            var endDate = JulianDayNumber.FromRaw(mjdsv + TJDOFS);

            // file finals.all may have some more data, and especially estimations for the near future
            if (eopFinalsFile != null)
            {
                mjdsv            = 0;
                using var reader = new StreamReader(eopFinalsFile);
                while ((s = reader.ReadLine()) != null)
                {
                    var mjd = int.Parse(GetDigits(s, 7));
                    var jd  = JulianDayNumber.FromRaw(mjd + TJDOFS);
                    if (jd <= endDate)
                    {
                        continue;
                    }
                    // are data in one-day steps?
                    if (mjdsv > 0 && mjd - mjdsv != 1)
                    {
                        throw new FormatException("File must be in one-day steps");
                    }

                    // dpsi, deps Bulletin B
                    var dPsi = GetDoubleFromStr(s, 168);
                    var dEps = GetDoubleFromStr(s, 178);
                    if (dPsi == 0.0)
                    {
                        // try dpsi, deps Bulletin A
                        dPsi = GetDoubleFromStr(s, 99);
                        dEps = GetDoubleFromStr(s, 118);
                    }
                    if (dPsi == 0.0)
                    {
                        break;
                    }
                    dpsi.Add(dPsi / 1000.0);
                    deps.Add(dEps / 1000.0);
                    mjdsv = mjd;
                }
            }

            return(new EarthOrientationParameters(beginDate, endDate, dpsi, deps));
        }
 public static Task <TValue?> FromStreamAsync <TValue>(this Stream?stream,
                                                       IJsonFormatterResolver?resolver = null) =>
 Utf8JsonHelper.FromStreamAsync <TValue>(stream, resolver);
 public static Task <object?> FromStreamAsync(this Stream?stream, Type type,
                                              IJsonFormatterResolver?resolver = null) =>
 Utf8JsonHelper.FromStreamAsync(type, stream, resolver);
Exemple #12
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MpqArchive"/> class.
        /// </summary>
        /// <param name="sourceStream">The <see cref="Stream"/> containing pre-archive data. Can be <see langword="null"/>.</param>
        /// <param name="inputFiles">The <see cref="MpqFile"/>s that should be added to the archive.</param>
        /// <param name="createOptions"></param>
        /// <param name="leaveOpen">If <see langword="false"/>, the given <paramref name="sourceStream"/> will be disposed when the <see cref="MpqArchive"/> is disposed.</param>
        /// <exception cref="ArgumentNullException">Thrown when the <paramref name="mpqFiles"/> collection is <see langword="null"/>.</exception>
        public MpqArchive(Stream?sourceStream, IEnumerable <MpqFile> inputFiles, MpqArchiveCreateOptions createOptions, bool leaveOpen = false)
        {
            if (inputFiles is null)
            {
                throw new ArgumentNullException(nameof(inputFiles));
            }

            if (createOptions is null)
            {
                throw new ArgumentNullException(nameof(createOptions));
            }

            _isStreamOwner = !leaveOpen;
            _baseStream    = AlignStream(sourceStream);

            _headerOffset         = _baseStream.Position;
            _blockSize            = BlockSizeModifier << createOptions.BlockSize;
            _archiveFollowsHeader = createOptions.WriteArchiveFirst;

            var signatureName  = Signature.FileName.GetStringHash();
            var listFileName   = ListFile.FileName.GetStringHash();
            var attributesName = Attributes.FileName.GetStringHash();

            var signatureCreateMode  = createOptions.SignatureCreateMode.GetValueOrDefault(MpqFileCreateMode.Prune);
            var listFileCreateMode   = createOptions.ListFileCreateMode.GetValueOrDefault(MpqFileCreateMode.Overwrite);
            var attributesCreateMode = createOptions.AttributesCreateMode.GetValueOrDefault(MpqFileCreateMode.Overwrite);
            var haveSignature        = false;
            var haveListFile         = false;
            var haveAttributes       = false;
            var mpqFiles             = new HashSet <MpqFile>(MpqFileComparer.Default);

            foreach (var mpqFile in inputFiles)
            {
                if (mpqFile is MpqOrphanedFile)
                {
                    continue;
                }

                if (mpqFile.Name == signatureName)
                {
                    if (signatureCreateMode.HasFlag(MpqFileCreateMode.RemoveFlag))
                    {
                        continue;
                    }
                    else
                    {
                        haveSignature = true;
                    }
                }

                if (mpqFile.Name == listFileName)
                {
                    if (listFileCreateMode.HasFlag(MpqFileCreateMode.RemoveFlag))
                    {
                        continue;
                    }
                    else
                    {
                        haveListFile = true;
                    }
                }

                if (mpqFile.Name == attributesName)
                {
                    if (attributesCreateMode.HasFlag(MpqFileCreateMode.RemoveFlag))
                    {
                        continue;
                    }
                    else
                    {
                        haveAttributes = true;
                    }
                }

                if (!mpqFiles.Add(mpqFile))
                {
                    // todo: logging?
                }
            }

            var fileCount = (uint)mpqFiles.Count;

            var wantGenerateSignature = !haveSignature && signatureCreateMode.HasFlag(MpqFileCreateMode.AddFlag);
            var signature             = wantGenerateSignature ? new Signature() : null;

            if (wantGenerateSignature)
            {
                fileCount++;
            }

            var wantGenerateListFile = !haveListFile && listFileCreateMode.HasFlag(MpqFileCreateMode.AddFlag);
            var listFile             = wantGenerateListFile ? new ListFile() : null;

            if (wantGenerateListFile)
            {
                fileCount++;
            }

            var wantGenerateAttributes = !haveAttributes && attributesCreateMode.HasFlag(MpqFileCreateMode.AddFlag);
            var attributes             = wantGenerateAttributes ? new Attributes(createOptions) : null;

            if (wantGenerateAttributes)
            {
                fileCount++;
            }

            _hashTable  = new HashTable(Math.Max(createOptions.HashTableSize ?? fileCount * 8, fileCount));
            _blockTable = new BlockTable();

            using (var writer = new BinaryWriter(_baseStream, new UTF8Encoding(false, true), true))
            {
                // Skip the MPQ header, since its contents will be calculated afterwards.
                writer.Seek((int)MpqHeader.Size, SeekOrigin.Current);

                // Write Archive
                var fileIndex  = 0U;
                var fileOffset = _archiveFollowsHeader ? MpqHeader.Size : throw new NotImplementedException();

                // var gaps = new List<(long Start, long Length)>();
                var endOfStream = _baseStream.Position;

                void InsertMpqFile(MpqFile mpqFile, bool updateEndOfStream, bool allowMultiple = true)
                {
                    if (listFile is not null && mpqFile is MpqKnownFile knownFile)
                    {
                        listFile.FileNames.Add(knownFile.FileName);
                    }

                    mpqFile.AddToArchive(this, fileIndex, out var mpqEntry, out var mpqHash);
                    var hashTableEntries = _hashTable.Add(mpqHash, mpqFile.HashIndex, mpqFile.HashCollisions);

                    if (!allowMultiple && hashTableEntries > 1)
                    {
                        throw new Exception();
                    }

                    var crc32 = 0;

                    if (attributes is not null && attributes.Flags.HasFlag(AttributesFlags.Crc32) && allowMultiple)
                    {
                        mpqFile.MpqStream.Position = 0;
                        crc32 = new Ionic.Crc.CRC32().GetCrc32(mpqFile.MpqStream);
                    }

                    for (var i = 0; i < hashTableEntries; i++)
                    {
                        _blockTable.Add(mpqEntry);
                        if (attributes is not null)
                        {
                            if (attributes.Flags.HasFlag(AttributesFlags.Crc32))
                            {
                                attributes.Crc32s.Add(crc32);
                            }

                            if (attributes.Flags.HasFlag(AttributesFlags.DateTime))
                            {
                                attributes.DateTimes.Add(DateTime.Now);
                            }

                            if (attributes.Flags.HasFlag(AttributesFlags.Unk0x04))
                            {
                                attributes.Unk0x04s.Add(new byte[16]);
                            }
                        }
                    }

                    mpqFile.Dispose();

                    fileIndex += hashTableEntries;
                    if (updateEndOfStream)
                    {
                        endOfStream = _baseStream.Position;
                    }
                }

                // Find files that cannot be decrypted, and need to have a specific position in the archive, because that position is used to calculate the encryption seed.
                var mpqFixedPositionFiles = mpqFiles.Where(mpqFile => mpqFile.IsFilePositionFixed).OrderBy(mpqFile => mpqFile.MpqStream.FilePosition).ToArray();
                if (mpqFixedPositionFiles.Length > 0)
                {
                    if (mpqFixedPositionFiles.First() !.MpqStream.FilePosition < 0)
                    {
                        throw new NotSupportedException($"Cannot place files in front of the header.");
                    }

                    foreach (var mpqFixedPositionFile in mpqFixedPositionFiles)
                    {
                        var position = mpqFixedPositionFile.MpqStream.FilePosition;
                        if (position < endOfStream)
                        {
                            throw new ArgumentException($"Fixed position files overlap with each other and/or the header. Archive cannot be created.", nameof(inputFiles));
                        }

                        if (position > endOfStream)
                        {
                            var gapSize = position - endOfStream;
                            // gaps.Add((endOfStream, gapSize));
                            writer.Seek((int)gapSize, SeekOrigin.Current);
                        }

                        InsertMpqFile(mpqFixedPositionFile, true);
                    }
                }

                foreach (var mpqFile in mpqFiles.Where(mpqFile => !mpqFile.IsFilePositionFixed))
                {
                    // TODO: insert files into the gaps
                    // need to know compressed size of file first, and if file is also encrypted with blockoffsetadjustedkey, encryption needs to happen after gap selection
                    // therefore, can't use current AddToArchive method, which does both compression and encryption at same time

                    // var availableGaps = gaps.Where(gap => gap.Length >= )
                    var selectedPosition = endOfStream;
                    var selectedGap      = false;
                    _baseStream.Position = selectedPosition;

                    InsertMpqFile(mpqFile, !selectedGap);
                }

                var signaturePosition = endOfStream + 8;
                if (signature is not null)
                {
                    _baseStream.Position = endOfStream;

                    using var signatureStream = new MemoryStream();
                    using var signatureWriter = new BinaryWriter(signatureStream);
                    signatureWriter.Write(signature);
                    signatureWriter.Flush();

                    using var signatureMpqFile   = MpqFile.New(signatureStream, Signature.FileName);
                    signatureMpqFile.TargetFlags = MpqFileFlags.Exists;
                    InsertMpqFile(signatureMpqFile, true);
                }

                if (listFile is not null)
                {
                    _baseStream.Position = endOfStream;

                    using var listFileStream = new MemoryStream();
                    using var listFileWriter = new StreamWriter(listFileStream);
                    listFileWriter.WriteListFile(listFile);
                    listFileWriter.Flush();

                    using var listFileMpqFile   = MpqFile.New(listFileStream, ListFile.FileName);
                    listFileMpqFile.TargetFlags = MpqFileFlags.Exists | MpqFileFlags.CompressedMulti | MpqFileFlags.Encrypted | MpqFileFlags.BlockOffsetAdjustedKey;
                    InsertMpqFile(listFileMpqFile, true);
                }

                if (attributes is not null)
                {
                    _baseStream.Position = endOfStream;

                    if (attributes.Flags.HasFlag(AttributesFlags.Crc32))
                    {
                        attributes.Crc32s.Add(0);
                    }

                    if (attributes.Flags.HasFlag(AttributesFlags.DateTime))
                    {
                        attributes.DateTimes.Add(DateTime.Now);
                    }

                    if (attributes.Flags.HasFlag(AttributesFlags.Unk0x04))
                    {
                        attributes.Unk0x04s.Add(new byte[16]);
                    }

                    using var attributesStream = new MemoryStream();
                    using var attributesWriter = new BinaryWriter(attributesStream);
                    attributesWriter.Write(attributes);
                    attributesWriter.Flush();

                    using var attributesMpqFile   = MpqFile.New(attributesStream, Attributes.FileName);
                    attributesMpqFile.TargetFlags = MpqFileFlags.Exists | MpqFileFlags.CompressedMulti | MpqFileFlags.Encrypted | MpqFileFlags.BlockOffsetAdjustedKey;
                    InsertMpqFile(attributesMpqFile, true, false);
                }

                _baseStream.Position = endOfStream;
                _hashTable.WriteTo(writer);
                _blockTable.WriteTo(writer);

                /*if (!_archiveFollowsHeader)
                 * {
                 *  foreach (var mpqFile in mpqFiles)
                 *  {
                 *      mpqFile.WriteTo(writer, true);
                 *  }
                 * }*/

                writer.Seek((int)_headerOffset, SeekOrigin.Begin);

                _mpqHeader = new MpqHeader((uint)_headerOffset, (uint)(endOfStream - fileOffset), _hashTable.Size, _blockTable.Size, createOptions.BlockSize, _archiveFollowsHeader);
                _mpqHeader.WriteTo(writer);

                if (wantGenerateSignature)
                {
                    var archiveBytes = new byte[_mpqHeader.ArchiveSize];
                    _baseStream.Position = _headerOffset;
                    _baseStream.Read(archiveBytes);

                    using var rsa = RSA.Create();

                    rsa.ImportFromPem(createOptions.SignaturePrivateKey);
                    var signatureBytes = rsa.SignData(archiveBytes, HashAlgorithmName.MD5, RSASignaturePadding.Pkcs1);

                    _baseStream.Position = signaturePosition;
                    _baseStream.Write(signatureBytes.Reverse().ToArray());
                }
            }
        }
Exemple #13
0
 /// <summary>
 /// Creates a new <see cref="MpqArchive"/>.
 /// </summary>
 /// <param name="sourceStream">The <see cref="Stream"/> containing pre-archive data. Can be <see langword="null"/>.</param>
 /// <param name="mpqFiles">The <see cref="MpqFile"/>s that should be added to the archive.</param>
 /// <param name="createOptions"></param>
 /// <param name="leaveOpen">If <see langword="false"/>, the given <paramref name="sourceStream"/> will be disposed when the <see cref="MpqArchive"/> is disposed.</param>
 /// <returns>An <see cref="MpqArchive"/> that is created.</returns>
 /// <exception cref="ArgumentNullException">Thrown when the <paramref name="mpqFiles"/> collection is <see langword="null"/>.</exception>
 public static MpqArchive Create(Stream?sourceStream, IEnumerable <MpqFile> mpqFiles, MpqArchiveCreateOptions createOptions, bool leaveOpen = false)
 {
     return(new MpqArchive(sourceStream, mpqFiles, createOptions, leaveOpen));
 }
Exemple #14
0
            internal override Stream?TryCreateContentReadStream()
            {
                Stream?originalStream = _originalContent.TryReadAsStream();

                return(originalStream is null ? null : GetDecompressedStream(originalStream));
            }
Exemple #15
0
        /// <summary>Makes a new HawkFile based on the provided path.</summary>
        /// <param name="delayIOAndDearchive">Pass <see langword="true"/> to only populate a few fields (those that can be computed from the string <paramref name="path"/>), which is less computationally expensive.</param>
        /// <param name="nonArchiveExtensions">
        /// These file extensions are assumed to not be archives. Include the leading period in each, and use lowercase.<br/>
        /// Does not apply when <paramref name="delayIOAndDearchive"/> is <see langword="true"/>.<br/>
        /// If <see langword="null"/> is passed (the default), uses <see cref="CommonNonArchiveExtensions"/> which should mitigate false positives caused by weak archive detection signatures.
        /// </param>
        public HawkFile([HawkFilePath] string path, bool delayIOAndDearchive = false, IReadOnlyCollection <string>?nonArchiveExtensions = null)
        {
            if (delayIOAndDearchive)
            {
                var split = SplitArchiveMemberPath(path);
                if (split != null)
                {
                    (path, ArchiveMemberPath) = split.Value;
                    IsArchive = true;                     // we'll assume that the '|' is only used for archives
                }
                FullPathWithoutMember = path;
                return;
            }

            string?autobind = null;
            var    split1   = SplitArchiveMemberPath(path);

            if (split1 != null)
            {
                (path, autobind) = split1.Value;
            }
            FullPathWithoutMember = path;
            Exists = _rootExists = !string.IsNullOrEmpty(path) && new FileInfo(path).Exists;
            if (!_rootExists)
            {
                return;
            }

            if (DearchivalMethod != null &&
                !(nonArchiveExtensions ?? CommonNonArchiveExtensions).Contains(Path.GetExtension(path).ToLowerInvariant()) &&
                DearchivalMethod.CheckSignature(path, out _, out _))
            {
                _extractor = DearchivalMethod.Construct(path);
                try
                {
                    _archiveItems = _extractor.Scan();
                    IsArchive     = true;
                }
                catch
                {
                    _archiveItems = null;
                    _extractor.Dispose();
                    _extractor = null;
                }
            }
            if (_extractor == null)
            {
                _rootStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
                // we could autobind here, but i don't want to
                // bind it later with the desired extensions.
            }

            if (autobind == null)
            {
                // non-archive files can be automatically bound this way
                BindRoot();
            }
            else
            {
                if (_extractor != null)
                {
                    var scanResults = _extractor.Scan();
                    for (int i = 0, l = scanResults.Count; i < l; i++)
                    {
                        if (string.Equals(scanResults[i].Name, autobind, StringComparison.InvariantCultureIgnoreCase))
                        {
                            BindArchiveMember(i);
                            return;
                        }
                    }
                }

                Exists = false;
            }
        }
Exemple #16
0
        private async ValueTask ProcessAsync(HttpMessage message, ReadOnlyMemory <HttpPipelinePolicy> pipeline, bool async)
        {
            CancellationToken oldToken = message.CancellationToken;

            using CancellationTokenSource cts = CancellationTokenSource.CreateLinkedTokenSource(oldToken);

            var networkTimeout = _networkTimeout;

            if (message.NetworkTimeout is TimeSpan networkTimeoutOverride)
            {
                networkTimeout = networkTimeoutOverride;
            }

            cts.CancelAfter(networkTimeout);
            try
            {
                message.CancellationToken = cts.Token;
                if (async)
                {
                    await ProcessNextAsync(message, pipeline).ConfigureAwait(false);
                }
                else
                {
                    ProcessNext(message, pipeline);
                }
            }
            finally
            {
                message.CancellationToken = oldToken;
                cts.CancelAfter(Timeout.Infinite);
            }

            Stream?responseContentStream = message.Response.ContentStream;

            if (responseContentStream == null || responseContentStream.CanSeek)
            {
                return;
            }

            if (message.BufferResponse)
            {
                if (networkTimeout != Timeout.InfiniteTimeSpan)
                {
                    cts.Token.Register(state => ((Stream?)state)?.Dispose(), responseContentStream);
                }

                try
                {
                    var bufferedStream = new MemoryStream();
                    if (async)
                    {
                        await CopyToAsync(responseContentStream, bufferedStream, cts).ConfigureAwait(false);
                    }
                    else
                    {
                        CopyTo(responseContentStream, bufferedStream, cts);
                    }

                    responseContentStream.Dispose();
                    bufferedStream.Position        = 0;
                    message.Response.ContentStream = bufferedStream;
                }
                // We dispose stream on timeout so catch and check if cancellation token was cancelled
                catch (ObjectDisposedException)
                {
                    cts.Token.ThrowIfCancellationRequested();
                    throw;
                }
            }
            else if (networkTimeout != Timeout.InfiniteTimeSpan)
            {
                message.Response.ContentStream = new ReadTimeoutStream(responseContentStream, networkTimeout);
            }
        }
Exemple #17
0
 public void SetPendingImport(Stream import)
 {
     PendingImport?.Dispose();
     PendingImport = import;
 }
Exemple #18
0
 public DataReader(Stream stream, Span <byte> buffer)
 {
     m_stream    = stream;
     m_buffer    = buffer;
     m_remaining = default;
 }
 public static RawInboundEnvelope CloneReplacingStream(this IRawInboundEnvelope envelope, Stream?rawMessage) =>
 new RawInboundEnvelope(
     rawMessage,
     envelope.Headers,
     envelope.Endpoint,
     envelope.ActualEndpointName,
     envelope.Offset,
     envelope.AdditionalLogData);
Exemple #20
0
 public DataReader(ReadOnlySpan <byte> data)
 {
     m_stream    = null;
     m_buffer    = null;
     m_remaining = data;
 }
        private PipelineInstruction QueueOrCreateDataConection(PipelineEntry entry, ResponseDescription response, bool timeout, ref Stream?stream, out bool isSocketReady)
        {
            isSocketReady = false;
            if (_dataHandshakeStarted)
            {
                isSocketReady = true;
                return(PipelineInstruction.Pause); //if we already started then this is re-entering into the callback where we proceed with the stream
            }

            _dataHandshakeStarted = true;

            // Handle passive responses by parsing the port and later doing a Connect(...)
            bool isPassive = false;
            int  port      = -1;

            if (entry.Command == "PASV\r\n" || entry.Command == "EPSV\r\n")
            {
                if (!response.PositiveCompletion)
                {
                    _abortReason = SR.Format(SR.net_ftp_server_failed_passive, response.Status);
                    return(PipelineInstruction.Abort);
                }
                if (entry.Command == "PASV\r\n")
                {
                    port = GetPortV4(response.StatusDescription !);
                }
                else
                {
                    port = GetPortV6(response.StatusDescription !);
                }

                isPassive = true;
            }

            if (isPassive)
            {
                Debug.Assert(port != -1, "'port' not set.");

                try
                {
                    _dataSocket = CreateFtpDataSocket((FtpWebRequest)_request !, Socket);
                }
                catch (ObjectDisposedException)
                {
                    throw ExceptionHelper.RequestAbortedException;
                }

                IPEndPoint localEndPoint = new IPEndPoint(((IPEndPoint)Socket.LocalEndPoint !).Address, 0);
                _dataSocket.Bind(localEndPoint);

                _passiveEndPoint = new IPEndPoint(ServerAddress, port);
            }

            PipelineInstruction result;

            if (_passiveEndPoint != null)
            {
                IPEndPoint passiveEndPoint = _passiveEndPoint;
                _passiveEndPoint = null;
                if (NetEventSource.Log.IsEnabled())
                {
                    NetEventSource.Info(this, "starting Connect()");
                }
                if (_isAsync)
                {
                    _dataSocket !.BeginConnect(passiveEndPoint, s_connectCallbackDelegate, this);
                    result = PipelineInstruction.Pause;
                }
                else
                {
                    _dataSocket !.Connect(passiveEndPoint);
                    result = PipelineInstruction.Advance; // for passive mode we end up going to the next command
                }
            }
            else
            {
                if (NetEventSource.Log.IsEnabled())
                {
                    NetEventSource.Info(this, "starting Accept()");
                }

                if (_isAsync)
                {
                    _dataSocket !.BeginAccept(s_acceptCallbackDelegate, this);
                    result = PipelineInstruction.Pause;
                }
                else
                {
                    Socket listenSocket = _dataSocket !;
                    try
                    {
                        _dataSocket = _dataSocket !.Accept();
                        if (!ServerAddress.Equals(((IPEndPoint)_dataSocket.RemoteEndPoint !).Address))
                        {
                            _dataSocket.Close();
                            throw new WebException(SR.net_ftp_active_address_different, WebExceptionStatus.ProtocolError);
                        }
                        isSocketReady = true;   // for active mode we end up creating a stream before advancing the pipeline
                        result        = PipelineInstruction.Pause;
                    }
                    finally
                    {
                        listenSocket.Close();
                    }
                }
            }
            return(result);
        }
Exemple #22
0
    public static DefaultHttpContext CreateHttpContext(CancellationToken cancellationToken = default, Stream?bodyStream = null)
    {
        var serviceCollection = new ServiceCollection();

        serviceCollection.AddSingleton(typeof(IGrpcInterceptorActivator <>), typeof(TestInterceptorActivator <>));
        var serviceProvider = serviceCollection.BuildServiceProvider();
        var httpContext     = new DefaultHttpContext();

        httpContext.Request.Host               = new HostString("localhost");
        httpContext.RequestServices            = serviceProvider;
        httpContext.Response.Body              = bodyStream ?? new MemoryStream();
        httpContext.Connection.RemoteIpAddress = IPAddress.Parse("127.0.0.1");
        httpContext.Features.Set <IHttpRequestLifetimeFeature>(new HttpRequestLifetimeFeature(cancellationToken));
        return(httpContext);
    }
 /// <inheritdoc cref="Producer.ProduceCore(object,Stream,IReadOnlyCollection{MessageHeader},string)" />
 protected override IBrokerMessageIdentifier ProduceCore(
     object?message,
     Stream?messageStream,
     IReadOnlyCollection <MessageHeader>?headers,
     string actualEndpointName) =>
 throw new InvalidOperationException("Only asynchronous operations are supported.");
 public static void PackTo <TValue>(this TValue?value, Stream?stream, MessagePackSerializerOptions?options = null,
                                    CancellationToken cancellationToken = default) =>
 MessagePackHelper.Pack(value, stream, options, cancellationToken);
Exemple #25
0
        /// <summary>
        /// Initializes a new instance of ZipArchive on the given stream in the specified mode, specifying whether to leave the stream open.
        /// </summary>
        /// <exception cref="ArgumentException">The stream is already closed. -or- mode is incompatible with the capabilities of the stream.</exception>
        /// <exception cref="ArgumentNullException">The stream is null.</exception>
        /// <exception cref="ArgumentOutOfRangeException">mode specified an invalid value.</exception>
        /// <exception cref="InvalidDataException">The contents of the stream could not be interpreted as a Zip file. -or- mode is Update and an entry is missing from the archive or is corrupt and cannot be read. -or- mode is Update and an entry is too large to fit into memory.</exception>
        /// <param name="stream">The input or output stream.</param>
        /// <param name="mode">See the description of the ZipArchiveMode enum. Read requires the stream to support reading, Create requires the stream to support writing, and Update requires the stream to support reading, writing, and seeking.</param>
        /// <param name="leaveOpen">true to leave the stream open upon disposing the ZipArchive, otherwise false.</param>
        /// <param name="entryNameEncoding">The encoding to use when reading or writing entry names in this ZipArchive.
        ///         ///     <para>NOTE: Specifying this parameter to values other than <c>null</c> is discouraged.
        ///         However, this may be necessary for interoperability with ZIP archive tools and libraries that do not correctly support
        ///         UTF-8 encoding for entry names.<br />
        ///         This value is used as follows:</para>
        ///     <para><strong>Reading (opening) ZIP archive files:</strong></para>
        ///     <para>If <c>entryNameEncoding</c> is not specified (<c>== null</c>):</para>
        ///     <list>
        ///         <item>For entries where the language encoding flag (EFS) in the general purpose bit flag of the local file header is <em>not</em> set,
        ///         use the current system default code page (<c>Encoding.Default</c>) in order to decode the entry name.</item>
        ///         <item>For entries where the language encoding flag (EFS) in the general purpose bit flag of the local file header <em>is</em> set,
        ///         use UTF-8 (<c>Encoding.UTF8</c>) in order to decode the entry name.</item>
        ///     </list>
        ///     <para>If <c>entryNameEncoding</c> is specified (<c>!= null</c>):</para>
        ///     <list>
        ///         <item>For entries where the language encoding flag (EFS) in the general purpose bit flag of the local file header is <em>not</em> set,
        ///         use the specified <c>entryNameEncoding</c> in order to decode the entry name.</item>
        ///         <item>For entries where the language encoding flag (EFS) in the general purpose bit flag of the local file header <em>is</em> set,
        ///         use UTF-8 (<c>Encoding.UTF8</c>) in order to decode the entry name.</item>
        ///     </list>
        ///     <para><strong>Writing (saving) ZIP archive files:</strong></para>
        ///     <para>If <c>entryNameEncoding</c> is not specified (<c>== null</c>):</para>
        ///     <list>
        ///         <item>For entry names that contain characters outside the ASCII range,
        ///         the language encoding flag (EFS) will be set in the general purpose bit flag of the local file header,
        ///         and UTF-8 (<c>Encoding.UTF8</c>) will be used in order to encode the entry name into bytes.</item>
        ///         <item>For entry names that do not contain characters outside the ASCII range,
        ///         the language encoding flag (EFS) will not be set in the general purpose bit flag of the local file header,
        ///         and the current system default code page (<c>Encoding.Default</c>) will be used to encode the entry names into bytes.</item>
        ///     </list>
        ///     <para>If <c>entryNameEncoding</c> is specified (<c>!= null</c>):</para>
        ///     <list>
        ///         <item>The specified <c>entryNameEncoding</c> will always be used to encode the entry names into bytes.
        ///         The language encoding flag (EFS) in the general purpose bit flag of the local file header will be set if and only
        ///         if the specified <c>entryNameEncoding</c> is a UTF-8 encoding.</item>
        ///     </list>
        ///     <para>Note that Unicode encodings other than UTF-8 may not be currently used for the <c>entryNameEncoding</c>,
        ///     otherwise an <see cref="ArgumentException"/> is thrown.</para>
        /// </param>
        /// <exception cref="ArgumentException">If a Unicode encoding other than UTF-8 is specified for the <code>entryNameEncoding</code>.</exception>
        public ZipArchive(Stream stream, ZipArchiveMode mode, bool leaveOpen, Encoding?entryNameEncoding)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            EntryNameEncoding = entryNameEncoding;
            Stream?extraTempStream = null;

            try
            {
                _backingStream = null;

                // check stream against mode
                switch (mode)
                {
                case ZipArchiveMode.Create:
                    if (!stream.CanWrite)
                    {
                        throw new ArgumentException(SR.CreateModeCapabilities);
                    }
                    break;

                case ZipArchiveMode.Read:
                    if (!stream.CanRead)
                    {
                        throw new ArgumentException(SR.ReadModeCapabilities);
                    }
                    if (!stream.CanSeek)
                    {
                        _backingStream  = stream;
                        extraTempStream = stream = new MemoryStream();
                        _backingStream.CopyTo(stream);
                        stream.Seek(0, SeekOrigin.Begin);
                    }
                    break;

                case ZipArchiveMode.Update:
                    if (!stream.CanRead || !stream.CanWrite || !stream.CanSeek)
                    {
                        throw new ArgumentException(SR.UpdateModeCapabilities);
                    }
                    break;

                default:
                    // still have to throw this, because stream constructor doesn't do mode argument checks
                    throw new ArgumentOutOfRangeException(nameof(mode));
                }

                _mode = mode;
                if (mode == ZipArchiveMode.Create && !stream.CanSeek)
                {
                    _archiveStream = new PositionPreservingWriteOnlyStreamWrapper(stream);
                }
                else
                {
                    _archiveStream = stream;
                }
                _archiveStreamOwner = null;
                if (mode == ZipArchiveMode.Create)
                {
                    _archiveReader = null;
                }
                else
                {
                    _archiveReader = new BinaryReader(_archiveStream);
                }
                _entries               = new List <ZipArchiveEntry>();
                _entriesCollection     = new ReadOnlyCollection <ZipArchiveEntry>(_entries);
                _entriesDictionary     = new Dictionary <string, ZipArchiveEntry>();
                _readEntries           = false;
                _leaveOpen             = leaveOpen;
                _centralDirectoryStart = 0; // invalid until ReadCentralDirectory
                _isDisposed            = false;
                _numberOfThisDisk      = 0; // invalid until ReadCentralDirectory
                _archiveComment        = null;

                switch (mode)
                {
                case ZipArchiveMode.Create:
                    _readEntries = true;
                    break;

                case ZipArchiveMode.Read:
                    ReadEndOfCentralDirectory();
                    break;

                case ZipArchiveMode.Update:
                default:
                    Debug.Assert(mode == ZipArchiveMode.Update);
                    if (_archiveStream.Length == 0)
                    {
                        _readEntries = true;
                    }
                    else
                    {
                        ReadEndOfCentralDirectory();
                        EnsureCentralDirectoryRead();
                        foreach (ZipArchiveEntry entry in _entries)
                        {
                            entry.ThrowIfNotOpenable(needToUncompress: false, needToLoadIntoMemory: true);
                        }
                    }
                    break;
                }
            }
            catch
            {
                if (extraTempStream != null)
                {
                    extraTempStream.Dispose();
                }

                throw;
            }
        }
 public static void PackTo(this object?value, Type type, Stream?stream,
                           MessagePackSerializerOptions?options = null, CancellationToken cancellationToken = default) =>
 MessagePackHelper.Pack(type, value, stream, options, cancellationToken);
Exemple #27
0
 public static ValueTask <object?> FromStreamAsync(this Stream?stream, Type type,
                                                   MessagePackSerializerOptions?options = null, CancellationToken cancellationToken = default) =>
 MessagePackHelper.FromStreamAsync(type, stream, options, cancellationToken);
Exemple #28
0
 /// <summary>causes the root to be bound (in the case of non-archive files)</summary>
 private void BindRoot()
 {
     _boundStream = _rootStream;
     Debug.WriteLine($"{nameof(HawkFile)} bound {CanonicalFullPath}");
 }
Exemple #29
0
 public abstract bool TryOpen(long offset, [NotNullWhen(true)] out Stream?stream);
Exemple #30
0
 public OutgoingDelayedMessage(DateTime due, string?headers, Stream?bodyStream) :
     this(due, headers)
 {
     Body = bodyStream;
 }