private static void SelectStream(
     ArchiveMetadata archiveMetadata,
     ArchiveDecoderSection sectionMetadata,
     DecoderInputMetadata selector,
     ReaderNode[] streams,
     DecoderNode[] decoders,
     out ReaderNode result,
     out long length)
 {
     var decoderIndex = selector.DecoderIndex;
     if (decoderIndex.HasValue)
     {
         length = sectionMetadata.Decoders[decoderIndex.Value].OutputStreams[selector.StreamIndex].Length;
         result = decoders[decoderIndex.Value].GetOutputStream(selector.StreamIndex);
     }
     else
     {
         length = archiveMetadata.FileSections[selector.StreamIndex].Length;
         result = streams[selector.StreamIndex];
     }
 }
Exemple #2
0
        public ArchiveSectionDecoder(Stream stream, ArchiveMetadata metadata, int index, PasswordStorage password)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            if (!stream.CanRead)
            {
                throw new ArgumentException("Stream must be readable.", nameof(stream));
            }

            if (!stream.CanSeek)
            {
                throw new ArgumentException("Stream must be seekable.", nameof(stream));
            }

            if (metadata == null)
            {
                throw new ArgumentNullException(nameof(metadata));
            }

            if (index < 0 || index >= metadata.DecoderSections.Length)
            {
                throw new ArgumentOutOfRangeException(nameof(index));
            }

            var inputCoordinator = new StreamCoordinator(stream);
            var inputStreams     = new ReaderNode[metadata.FileSections.Length];

            for (int i = 0; i < inputStreams.Length; i++)
            {
                inputStreams[i] = new CoordinatedStream(inputCoordinator, metadata.FileSections[i].Offset, metadata.FileSections[i].Length);
            }

            var decoderSection     = metadata.DecoderSections[index];
            var decoderDefinitions = decoderSection.Decoders;
            var decoders           = new DecoderNode[decoderDefinitions.Length];

            for (int i = 0; i < decoders.Length; i++)
            {
                var decoderDefinition = decoderDefinitions[i];
                decoders[i] = decoderDefinition.DecoderType.CreateDecoder(decoderDefinition.Settings, decoderDefinition.OutputStreams, password);
            }

            for (int i = 0; i < decoders.Length; i++)
            {
                var decoder                 = decoders[i];
                var decoderDefinition       = decoderDefinitions[i];
                var decoderInputDefinitions = decoderDefinition.InputStreams;

                for (int j = 0; j < decoderInputDefinitions.Length; j++)
                {
                    ReaderNode inputStream;
                    long       inputLength;
                    SelectStream(metadata, decoderSection, decoderInputDefinitions[j], inputStreams, decoders, out inputStream, out inputLength);
                    decoder.SetInputStream(j, inputStream, inputLength);
                }
            }

            ReaderNode outputStream;
            long       outputLength;

            SelectStream(metadata, decoderSection, decoderSection.DecodedStream, inputStreams, decoders, out outputStream, out outputLength);

            mCoordinator  = inputCoordinator;
            mInputStreams = inputStreams;
            mDecoders     = decoders;
            mOutputStream = outputStream;
        }
        public ArchiveSectionDecoder(Stream stream, ArchiveMetadata metadata, int index, PasswordStorage password)
        {
            if (stream == null)
                throw new ArgumentNullException(nameof(stream));

            if (!stream.CanRead)
                throw new ArgumentException("Stream must be readable.", nameof(stream));

            if (!stream.CanSeek)
                throw new ArgumentException("Stream must be seekable.", nameof(stream));

            if (metadata == null)
                throw new ArgumentNullException(nameof(metadata));

            if (index < 0 || index >= metadata.DecoderSections.Length)
                throw new ArgumentOutOfRangeException(nameof(index));

            var inputCoordinator = new StreamCoordinator(stream);
            var inputStreams = new ReaderNode[metadata.FileSections.Length];
            for (int i = 0; i < inputStreams.Length; i++)
                inputStreams[i] = new CoordinatedStream(inputCoordinator, metadata.FileSections[i].Offset, metadata.FileSections[i].Length);

            var decoderSection = metadata.DecoderSections[index];
            var decoderDefinitions = decoderSection.Decoders;
            var decoders = new DecoderNode[decoderDefinitions.Length];

            for (int i = 0; i < decoders.Length; i++)
            {
                var decoderDefinition = decoderDefinitions[i];
                decoders[i] = decoderDefinition.DecoderType.CreateDecoder(decoderDefinition.Settings, decoderDefinition.OutputStreams, password);
            }

            for (int i = 0; i < decoders.Length; i++)
            {
                var decoder = decoders[i];
                var decoderDefinition = decoderDefinitions[i];
                var decoderInputDefinitions = decoderDefinition.InputStreams;

                for (int j = 0; j < decoderInputDefinitions.Length; j++)
                {
                    ReaderNode inputStream;
                    long inputLength;
                    SelectStream(metadata, decoderSection, decoderInputDefinitions[j], inputStreams, decoders, out inputStream, out inputLength);
                    decoder.SetInputStream(j, inputStream, inputLength);
                }
            }

            ReaderNode outputStream;
            long outputLength;
            SelectStream(metadata, decoderSection, decoderSection.DecodedStream, inputStreams, decoders, out outputStream, out outputLength);

            mCoordinator = inputCoordinator;
            mInputStreams = inputStreams;
            mDecoders = decoders;
            mOutputStream = outputStream;
        }