📄 binascii.java
字号:
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"); } } return bin_data.toString(); } public static PyString __doc__b2a_uu = new PyString( "(bin) -> ascii. Uuencode line of data" ); /** * Convert binary data to a line of ASCII characters, the return value * is the converted line, including a newline char. The length of * <i>data</i> should be at most 45. */ public static String b2a_uu(String bin_data) { int leftbits = 0; char this_ch; int leftchar = 0; int bin_len = bin_data.length(); if (bin_len > 45) { // The 45 is a limit that appears in all uuencode's throw new PyException(Error, "At most 45 bytes at once"); } StringBuffer ascii_data = new StringBuffer(); // Store the length */ ascii_data.append((char)(' ' + (bin_len & 077))); for (int i = 0; bin_len > 0 || leftbits != 0; i++, bin_len--) { // Shift the data (or padding) into our buffer if (bin_len > 0) // Data leftchar = (leftchar << 8) | bin_data.charAt(i); else // Padding leftchar <<= 8; leftbits += 8; // See if there are 6-bit groups ready while (leftbits >= 6) { this_ch = (char)((leftchar >> (leftbits-6)) & 0x3f); leftbits -= 6; ascii_data.append((char)(this_ch + ' ')); } } ascii_data.append('\n'); // Append a courtesy newline return ascii_data.toString(); } private static int binascii_find_valid(String s, int offset, int num) { int slen = s.length() - offset; /* Finds & returns the (num+1)th ** valid character for base64, or -1 if none. */ int ret = -1; while ((slen > 0) && (ret == -1)) { int c = (int)s.charAt(offset); short b64val = table_a2b_base64[c & 0x7f]; if (((c <= 0x7f) && (b64val != -1)) ) { if (num == 0) ret = c; num--; } offset++; slen--; } return ret; } public static PyString __doc__a2b_base64 = new PyString( "(ascii) -> bin. Decode a line of base64 data" ); /** * Convert a block of base64 data back to binary and return the * binary data. More than one line may be passed at a time. */ public static String a2b_base64(String ascii_data) { int leftbits = 0; char this_ch; int leftchar = 0; int quad_pos = 0; int ascii_len = ascii_data.length(); int bin_len = 0; StringBuffer bin_data = new StringBuffer(); for(int i = 0; ascii_len > 0 ; ascii_len--, i++) { // Skip some punctuation this_ch = ascii_data.charAt(i); if ((int) this_ch > 0x7F || this_ch == '\r' || this_ch == '\n' || this_ch == ' ') continue; if (this_ch == BASE64_PAD) { if (quad_pos < 2 || (quad_pos == 2 && binascii_find_valid(ascii_data, i, 1) != BASE64_PAD)) continue; else { // A pad sequence means no more input. // We've already interpreted the data // from the quad at this point. leftbits = 0; break; } } short this_v = table_a2b_base64[(int) this_ch]; if (this_v == -1) continue; // Shift it in on the low end, and see if there's // a byte ready for output. quad_pos = (quad_pos + 1) & 0x03; leftchar = (leftchar << 6) | (this_v); leftbits += 6; if (leftbits >= 8) { leftbits -= 8; bin_data.append((char)((leftchar >> leftbits) & 0xff)); bin_len++; leftchar &= ((1 << leftbits) - 1); } } // Check that no bits are left if (leftbits != 0) { throw new PyException(Error, "Incorrect padding"); } return bin_data.toString(); } public static PyString __doc__b2a_base64 = new PyString( "(bin) -> ascii. Base64-code line of data" ); /** * Convert binary data to a line of ASCII characters in base64 coding. * The return value is the converted line, including a newline char. * The length of <i>data</i> should be at most 57 to adhere to the base64 * standard. */ public static String b2a_base64(String bin_data) { int leftbits = 0; char this_ch; int leftchar = 0; StringBuffer ascii_data = new StringBuffer(); int bin_len = bin_data.length(); if (bin_len > BASE64_MAXBIN) { throw new PyException(Error,"Too much data for base64 line"); } for (int i = 0; bin_len > 0 ; bin_len--, i++) { // Shift the data into our buffer leftchar = (leftchar << 8) | bin_data.charAt(i); leftbits += 8; // See if there are 6-bit groups ready while (leftbits >= 6) { this_ch = (char)((leftchar >> (leftbits-6)) & 0x3f); leftbits -= 6; ascii_data.append((char)table_b2a_base64[this_ch]); } } if (leftbits == 2) { ascii_data.append((char)table_b2a_base64[(leftchar&3) << 4]); ascii_data.append(BASE64_PAD); ascii_data.append(BASE64_PAD); } else if (leftbits == 4) { ascii_data.append((char)table_b2a_base64[(leftchar&0xf) << 2]); ascii_data.append(BASE64_PAD); } ascii_data.append('\n'); // Append a courtesy newline return ascii_data.toString(); } public static PyString __doc__a2b_hqx = new PyString( "ascii -> bin, done. Decode .hqx coding" ); /** * Convert binhex4 formatted ASCII data to binary, without doing * RLE-decompression. The string should contain a complete number of * binary bytes, or (in case of the last portion of the binhex4 data) * have the remaining bits zero. */ public static PyTuple a2b_hqx(String ascii_data) { int leftbits = 0; char this_ch; int leftchar = 0; boolean done = false; int len = ascii_data.length(); StringBuffer bin_data = new StringBuffer(); for(int i = 0; len > 0 ; len--, i++) { // Get the byte and look it up this_ch = (char) table_a2b_hqx[ascii_data.charAt(i)]; if (this_ch == SKIP) continue; if (this_ch == FAIL) { throw new PyException(Error, "Illegal char"); } if (this_ch == DONE) { // The terminating colon done = true; break; } // Shift it into the buffer and see if any bytes are ready leftchar = (leftchar << 6) | (this_ch); leftbits += 6; if (leftbits >= 8) { leftbits -= 8; bin_data.append((char)((leftchar >> leftbits) & 0xff)); leftchar &= ((1 << leftbits) - 1); } } if (leftbits != 0 && !done) { throw new PyException(Incomplete, "String has incomplete number of bytes"); } return new PyTuple(new PyObject[] { Py.java2py(bin_data.toString()), Py.newInteger(done ? 1 : 0) }); } public static PyString __doc__rlecode_hqx = new PyString( "Binhex RLE-code binary data" ); /** * Perform binhex4 style RLE-compression on <i>data</i> and return the * result. */ static public String rlecode_hqx(String in_data) { int len = in_data.length(); StringBuffer out_data = new StringBuffer(); for (int in=0; in < len; in++) { char ch = in_data.charAt(in); if (ch == RUNCHAR) { // RUNCHAR. Escape it. out_data.append(RUNCHAR); out_data.append(0); } else { // Check how many following are the same int inend; for (inend=in+1; inend < len && in_data.charAt(inend) == ch && inend < in+255; inend++) ; if (inend - in > 3) { // More than 3 in a row. Output RLE. out_data.append(ch); out_data.append(RUNCHAR); out_data.append((char) (inend-in)); in = inend-1; } else { // Less than 3. Output the byte itself out_data.append(ch); } } } return out_data.toString();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -