Example #1
0
        /// <summary>
        /// CertificatePart constructor
        /// </summary>
        internal CertificatePart(System.IO.Packaging.Package container, Uri partName)
        {
            if (container == null)
            {
                throw new ArgumentNullException("container");
            }

            if (partName == null)
            {
                throw new ArgumentNullException("partName");
            }

            partName = PackUriHelper.ValidatePartUri(partName);

            // create if not found
            if (container.PartExists(partName))
            {
                // open the part
                _part = container.GetPart(partName);

                // ensure the part is of the expected type
                if (_part.ValidatedContentType().AreTypeAndSubTypeEqual(_certificatePartContentType) == false)
                {
                    throw new FileFormatException(SR.CertificatePartContentTypeMismatch);
                }
            }
            else
            {
                // create the part
                _part = container.CreatePart(partName, _certificatePartContentType.ToString());
            }
        }
        private static Uri ParsePartUriAttribute(String attrValue, out ContentType contentType)
        {
            // extract the query portion - do not ask the Uri class for it because it will escape
            // characters and we want to do a simple text comparison later
            contentType = ContentType.Empty;             // out argument must always be set
            int index = attrValue.IndexOf('?');
            Uri uri   = null;

            if (index > 0)
            {
                try
                {
                    // ensure it starts with the correct query prefix
                    String query = attrValue.Substring(index);
                    if ((query.Length > _contentTypeQueryStringPrefix.Length) && (query.StartsWith(_contentTypeQueryStringPrefix, StringComparison.Ordinal)))
                    {
                        // truncate the prefix and validate
                        contentType = new ContentType(query.Substring(_contentTypeQueryStringPrefix.Length));
                    }

                    // now construct the uri without the query
                    uri = PackUriHelper.ValidatePartUri(new Uri(attrValue.Substring(0, index), UriKind.Relative));
                }
                catch (ArgumentException ae)
                {
                    // Content type or part uri is malformed so we have a bad signature.
                    // Rethrow as XmlException so outer validation loop can catch it and return validation result.
                    throw new XmlException(SR.Get(SRID.PartReferenceUriMalformed), ae);
                }
            }

            // throw if we failed
            if (contentType.ToString().Length <= 0)
            {
                throw new XmlException(SR.Get(SRID.PartReferenceUriMalformed));
            }

            return(uri);
        }