Beispiel #1
0
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            BinaryResource resource = value as BinaryResource;

            if (resource == null)
            {
                return(null);
            }

            MemoryStream tempStream = new MemoryStream(resource.Buffer);
            var          tempIcon   = new System.Windows.Media.Imaging.BitmapImage();

            tempIcon.BeginInit();
            tempIcon.StreamSource = tempStream;
            tempIcon.EndInit();

            return(tempIcon);
        }
        private void _TestBinaryResource
        (
            int mfn
        )
        {
            IrbisConnection connection = Connection
                                         .ThrowIfNull("Connection");

            MarcRecord record = connection.ReadRecord(mfn);

            BinaryResource[] resources
                = BinaryResource.Parse(record);
            for (int i = 0; i < resources.Length; i++)
            {
                BinaryResource resource = resources[i];

                string fileName = string.Format
                                  (
                    "res{0:00000}-{1}.{2}",
                    mfn,
                    (i + 1),
                    resource.Kind
                                  );
                string filePath = Path.Combine
                                  (
                    Path.GetTempPath(),
                    fileName
                                  );
                byte[] array = resource.Decode();
                File.WriteAllBytes
                (
                    filePath,
                    array
                );

                Write
                (
                    "{0} ",
                    filePath
                );
            }
        }
        public bool Read(Stream stream)
        {
            try
            {
                BinaryReader reader = new BinaryReader(stream);

                // read header
                ulong magicNumber = reader.ReadUInt64();
                byte formatVersion = reader.ReadByte();
                byte[] md5hash = reader.ReadBytes(16);
                uint reserved = reader.ReadUInt32();
                uint fileLength = reader.ReadUInt32();
                uint offsetToContent = reader.ReadUInt32();
                uint numContentItems = reader.ReadUInt32();

                // Signing
                CheckSignature(stream, reader, offsetToContent);

                // Construct BinaryReadInfo
                BinaryReadInfo readInfo = new BinaryReadInfo()
                {
                     FormatVersion = formatVersion,
                     IsSigned = isSigned,
                     IsSignatureValid = validSignature,
                     IsCertificateTrusted = certificateTrusted
                };

                var componentDescriptions = new List<ComponentResource>();

                // Load content
                for (uint contentCounter = 0; contentCounter < numContentItems; contentCounter++)
                {
                    var itemType = (BinaryConstants.ContentItemType)reader.ReadUInt16();

                    if (itemType == BinaryConstants.ContentItemType.Resource)
                    {
                        var item = new BinaryResource();
                        item.Read(reader, readInfo);
                        Items.Add(item);
                        Resources.Add(item);
                    }
                    else if (itemType == BinaryConstants.ContentItemType.Component)
                    {
                        var item = new ComponentResource();
                        item.Read(reader, readInfo);
                        Items.Add(item);
                        componentDescriptions.Add(item);
                    }
                    else
                    {
                        // Unknown item type
                        continue;
                    }
                }

                // Process images
                Dictionary<uint, MultiResolutionImage> images = new Dictionary<uint, MultiResolutionImage>();
                foreach(var resource in Resources)
                {
                    if (resource.ResourceType == BinaryResourceType.BitmapImage
                        || resource.ResourceType == BinaryResourceType.JPEGImage
                        || resource.ResourceType == BinaryResourceType.PNGImage
                        || true)
                    {
                        if (!images.ContainsKey(resource.ID))
                            images.Add(resource.ID, new MultiResolutionImage());

                        images[resource.ID].Add(new SingleResolutionImage()
                        {
                            MimeType = BinaryConstants.ResourceMimeTypeToString((uint)resource.ResourceType),
                            Data = resource.Buffer
                        });
                    }
                }

                // Add component descriptions
                foreach(var componentDescription in componentDescriptions)
                {
                    componentDescription.SetIcons(images);
                    ComponentDescriptions.Add(componentDescription.ComponentDescription);
                }

                return true;
            }
            catch (Exception)
            {
                // Invalid binary file
                return false;
            }
        }
        public bool Read(Stream stream)
        {
            try
            {
                BinaryReader reader = new BinaryReader(stream);

                // read header
                ulong  magicNumber     = reader.ReadUInt64();
                byte   formatVersion   = reader.ReadByte();
                byte[] md5hash         = reader.ReadBytes(16);
                uint   reserved        = reader.ReadUInt32();
                uint   fileLength      = reader.ReadUInt32();
                uint   offsetToContent = reader.ReadUInt32();
                uint   numContentItems = reader.ReadUInt32();

                // Signing
                CheckSignature(stream, reader, offsetToContent);

                // Construct BinaryReadInfo
                BinaryReadInfo readInfo = new BinaryReadInfo()
                {
                    FormatVersion        = formatVersion,
                    IsSigned             = isSigned,
                    IsSignatureValid     = validSignature,
                    IsCertificateTrusted = certificateTrusted
                };

                var componentDescriptions = new List <ComponentResource>();

                // Load content
                for (uint contentCounter = 0; contentCounter < numContentItems; contentCounter++)
                {
                    var itemType = (BinaryConstants.ContentItemType)reader.ReadUInt16();

                    if (itemType == BinaryConstants.ContentItemType.Resource)
                    {
                        var item = new BinaryResource();
                        item.Read(reader, readInfo);
                        Items.Add(item);
                        Resources.Add(item);
                    }
                    else if (itemType == BinaryConstants.ContentItemType.Component)
                    {
                        var item = new ComponentResource();
                        item.Read(reader, readInfo);
                        Items.Add(item);
                        componentDescriptions.Add(item);
                    }
                    else
                    {
                        // Unknown item type
                        continue;
                    }
                }

                // Process images
                Dictionary <uint, MultiResolutionImage> images = new Dictionary <uint, MultiResolutionImage>();
                foreach (var resource in Resources)
                {
                    if (resource.ResourceType == BinaryResourceType.BitmapImage ||
                        resource.ResourceType == BinaryResourceType.JPEGImage ||
                        resource.ResourceType == BinaryResourceType.PNGImage ||
                        true)
                    {
                        if (!images.ContainsKey(resource.ID))
                        {
                            images.Add(resource.ID, new MultiResolutionImage());
                        }

                        images[resource.ID].Add(new SingleResolutionImage()
                        {
                            MimeType = BinaryConstants.ResourceMimeTypeToString((uint)resource.ResourceType),
                            Data     = resource.Buffer
                        });
                    }
                }

                // Add component descriptions
                foreach (var componentDescription in componentDescriptions)
                {
                    componentDescription.SetIcons(images);
                    ComponentDescriptions.Add(componentDescription.ComponentDescription);
                }

                return(true);
            }
            catch (Exception)
            {
                // Invalid binary file
                return(false);
            }
        }