# File lib/ascii85.rb, line 125
  def self.decode(str)

    input = str.to_s

    # Try to compile the regular expression for finding the input between
    # the <~ and ~> delimiters. In order to work properly with different
    # input encodings, the RegExp itself is re-encoded to the input encoding
    # if possible. Thanks to Myrddin Emrys for suggesting this approach
    # (http://is.gd/5x18O)
    begin
      regex = "<~(.*?)?~>"

      if regex.methods.include?(:encode)
        regex = regex.encode(input.encoding)
      end
      regex = Regexp.compile(regex, Regexp::MULTILINE)

      # Find the actual data to be decoded
      input = input.match(regex)

    rescue EncodingError
      raise ArgumentError, "Incompatible input encoding: #{str.encoding.inspect}"
    end

    return '' if input.nil?

    # Get the matched data as String
    input = input.captures.first

    # Decode
    result = []

    count = 0
    word = 0

    input.each_byte do |c|

      case c.chr
      when /[ \t\r\n\f\0]/
        # Ignore whitespace
        next

      when 'z'
        if count == 0
          # Expand z to 0-word
          result << 0
        else
          raise(Ascii85::DecodingError, "Found 'z' inside Ascii85 5-tuple")
        end

      when '!'..'u'
        # Decode 5 characters into a 4-byte word
        word += (c - 33) * 85**(4 - count)
        count += 1

        if count == 5

          if word >= 2**32
            raise(Ascii85::DecodingError,
                  "Invalid Ascii85 5-tuple (#{word} >= 2**32)")
          end

          result << word
          word = 0
          count = 0
        end

      else
        raise(Ascii85::DecodingError,
              "Illegal character inside Ascii85: #{c.chr.dump}")
      end

    end

    # Convert result into a String
    result = result.pack('N*')

    if count > 0
      # Finish last, partially decoded 32-bit-word

      if count == 1
        raise(Ascii85::DecodingError,
              "Last 5-tuple consists of single character")
      end

      count -= 1
      word += 85**(4 - count)

      result << ((word >> 24) & 255).chr if count >= 1
      result << ((word >> 16) & 255).chr if count >= 2
      result << ((word >>  8) & 255).chr if count == 3
    end

    return result
  end