import java.io.*;
import java.lang.*;

public class Base64Decode
{
  int textSize;

  int textIdx;

  String text;

  int binarySize;

  byte[] binary;

  int binaryIdx = 0;

  public Base64Decode(String text) {
    int block;

    this.text = text;
    textSize = text.length();

    binarySize = ((text.length() - newlineCount()) / 4) * 3 - equalCount();
    binary = new byte[binarySize];

    int extra;
    textIdx = 0;
    while (textIdx < textSize) {
      extra = blockTestEquals();
      block =
        make6BitBlock(
          getBlockChar(),
          getBlockChar(),
          getBlockChar(),
          getBlockChar());
      if (extra == 2) {
        saveByte1(block);
      }
      else
      if (extra == 1) {
        saveByte1(block);
        saveByte2(block);
      }
      else {
        saveByte1(block);
        saveByte2(block);
        saveByte3(block);
      }
    }
  }

  int newlineCount() {
    int count = 0;
    int i;
    for (i = 0; i < text.length(); i++)
      if (text.charAt(i) == '\n' || text.charAt(i) == '\r')
        count++;
    return count;
  }

  int equalCount() {
    int count = 0;
    int size = text.length();
    if (size < 2) return 0;
    if (text.charAt(size - 1) == '=')
      count++;
    if (text.charAt(size - 2) == '=')
      count++;
    return count;
  }

  int base64CharToBinary(char ch) {
    if (ch >= 'A' && ch <= 'Z')
      return (int)ch - (int)'A';
    if (ch >= 'a' && ch <= 'z')
      return (int)ch - (int)'a' + 26;
    if (ch >= '0' && ch <= '9')
      return (int)ch - (int)'0' + 52;
    if (ch == '+')
      return 62;
    if (ch == '/')
      return 63;
    return 0; // can't happen
  }

  char getChar() {
    char ch = text.charAt(textIdx);
    textIdx++;
    return ch;
  }

  int getBlockChar() {
    char ch;
    ch = getChar();
    while (ch == '\n' || ch == '\r')
      ch = getChar();
    if (ch == '=') // pad character
      return 0;
    else
      return base64CharToBinary(ch);
  }

  int blockTestEquals() {
    int count = 0;
    if (text.charAt(textIdx + 2) == '=')
      count++;
    if (text.charAt(textIdx + 3) == '=')
      count++;
    return count;
  }

  int make6BitBlock(int a, int b, int c, int d) {
    return (a << 18) + (b << 12) + (c << 6) + d;
  }

  void saveByte(int n) {
    if (n >= 128)
      n = n - 256;
    binary[binaryIdx] = (byte)n;
    binaryIdx++;
  }

  void saveByte1(int block) {
    saveByte((block & 0x00ff0000) >> 16);
  }

  void saveByte2(int block) {
    saveByte((block & 0x0000ff00) >> 8);
  }

  void saveByte3(int block) {
    saveByte(block & 0x000000ff);
  }

  byte[] getBinary() {
    return binary;
  }
}
