public static PyString a2b_uu(String ascii_data) {
int leftbits = 0;
int leftchar = 0;
if (ascii_data.length() == 0)
return new PyString("");
StringBuilder bin_data = new StringBuilder();
char this_ch;
int i;
int ascii_len = ascii_data.length()-1;
int bin_len = (ascii_data.charAt(0) - ' ') & 077;
for (i = 0; bin_len > 0 && ascii_len > 0; i++, ascii_len--) {
this_ch = ascii_data.charAt(i+1);
if (this_ch == '\n' || this_ch == '\r' || ascii_len <= 0) {
// Whitespace. Assume some spaces got eaten at
// end-of-line. (We check this later)
this_ch = 0;
} else {
// Check the character for legality
// The 64 in stead of the expected 63 is because
// there are a few uuencodes out there that use
// '@' as zero instead of space.
if ( this_ch < ' ' || this_ch > (' ' + 64)) {
throw new PyException(Error, "Illegal char");
}
this_ch = (char)((this_ch - ' ') & 077);
}
// Shift it in on the low end, and see if there's
// a byte ready for output.
leftchar = (leftchar << 6) | (this_ch);
leftbits += 6;
if (leftbits >= 8) {
leftbits -= 8;
bin_data.append((char)((leftchar >> leftbits) & 0xff));
leftchar &= ((1 << leftbits) - 1);
bin_len--;
}
}
// Finally, check that if there's anything left on the line
// that it's whitespace only.
while (ascii_len-- > 0) {
this_ch = ascii_data.charAt(++i);
// Extra '@' may be written as padding in some cases
if (this_ch != ' ' && this_ch != '@' &&
this_ch != '\n' && this_ch != '\r') {
throw new PyException(Error, "Trailing garbage");
}
}
// finally, if we haven't decoded enough stuff, fill it up with zeros
for (; i<bin_len; i++)
bin_data.append((char)0);
return new PyString(bin_data.toString());
}