Ejemplo n.º 1
0
        /// <summary>
        /// This method is used to decode a value read in from a PDI data stream
        /// </summary>
        /// <param name="data">The data string to decode</param>
        /// <returns>The decoded data</returns>
        /// <remarks><para>By default, it can handle Base 64 and Quoted-Printable decoding.  For 7-bit and 8-bit
        /// decoding, it will unescape characters where necessary.  The method used depends on the current
        /// setting of the <see cref="Encoding"/> property.  It can be overridden to support other types of
        /// decoding.</para>
        ///
        /// <para>For vCard 2.1 and vCalendar 1.0 properties, if a <see cref="CharacterSet"/> parameter has been
        /// specified, the value is converted from the specified character set to the one specified by the
        /// <see cref="DefaultEncoding"/> property after decoding.</para></remarks>
        public virtual string Decode(string data)
        {
            string decoded;

            // It's only done if the value is inline, text, or binary
            if (this.ValueLocation != ValLocValue.Inline && this.ValueLocation != ValLocValue.Text &&
                this.ValueLocation != ValLocValue.Binary)
            {
                return(data);
            }

            switch (this.EncodingMethod)
            {
            case EncodingType.SevenBit:
            case EncodingType.EightBit:
                // Unescape characters for these if necessary
                decoded = EncodingUtils.Unescape(data);
                break;

            case EncodingType.QuotedPrintable:
                decoded = EncodingUtils.FromQuotedPrintable(data);
                break;

            case EncodingType.Base64:
            case EncodingType.BEncoding:
                // Base 64 decoding
                decoded = EncodingUtils.FromBase64(data);
                break;

            default:        // Custom, can't do anything with it
                decoded = data;
                break;
            }

            if (String.Compare(this.CharacterSet, CharSetValue.ASCII, StringComparison.OrdinalIgnoreCase) != 0)
            {
                // The string is already encoded.  We just want the bytes in array form
                Encoding enc      = Encoding.GetEncoding("iso-8859-1");
                byte[]   srcBytes = enc.GetBytes(decoded);

                decoded = defaultEncoding.GetString(Encoding.Convert(Encoding.GetEncoding(this.CharacterSet),
                                                                     defaultEncoding, srcBytes));
            }

            return(decoded);
        }