Esempio n. 1
0
        /// <summary>
        /// Decode the Base64-encoded data in input and return the data in
        /// a new byte array.
        /// <p/>
        /// <para>The padding '=' characters at the end are considered optional, but
        /// if any are present, there must be the correct number of them.
        /// 
        /// </para>
        /// </summary>
        /// <param name="input">  the data to decode </param>
        /// <param name="offset"> the position within the input array at which to start </param>
        /// <param name="len">    the number of bytes of input to decode </param>
        /// <param name="flags">  controls certain features of the decoded output.
        ///               Pass {@code DEFAULT} to decode standard Base64. </param>
        /// <exception cref="IllegalArgumentException"> if the input contains
        ///                                  incorrect padding </exception>
        public static byte[] decode(byte[] input, int offset, int len, int flags)
        {
            // Allocate space for the most data the input could represent.
            // (It could contain less if it contains whitespace, etc.)
            Decoder decoder = new Decoder(flags, new byte[len * 3 / 4]);

            if (!decoder.process(input, offset, len, true))
            {
                throw new System.ArgumentException("bad base-64");
            }

            // Maybe we got lucky and allocated exactly enough output space.
            if (decoder.op == decoder.output.Length)
            {
                return decoder.output;
            }

            // Need to shorten the array, so allocate a new one of the
            // right size and copy.
            byte[] temp = new byte[decoder.op];
            Array.Copy(decoder.output, 0, temp, 0, decoder.op);
            return temp;
        }