X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Forg%2Fibex%2Futil%2FEncode.java;h=790c526eab742e3b75193cb97c1f10cf13681fd4;hb=176228ebf5bfc64c4e305ca4d37e6e3a5c0e31bf;hp=9334d46abac27dd464d0c30432b248353f7cc835;hpb=fc73b258af00744953b5098c1edb30f871039ef1;p=org.ibex.util.git
diff --git a/src/org/ibex/util/Encode.java b/src/org/ibex/util/Encode.java
index 9334d46..790c526 100644
--- a/src/org/ibex/util/Encode.java
+++ b/src/org/ibex/util/Encode.java
@@ -1,9 +1,336 @@
+// Copyright 2000-2005 the Contributors, as shown in the revision logs.
+// Licensed under the Apache Public Source License 2.0 ("the License").
+// You may not use this file except in compliance with the License.
+
package org.ibex.util;
+
import java.io.*;
-import java.util.*;
-import java.util.zip.*;
+import java.util.zip.GZIPInputStream;
+import java.util.zip.GZIPOutputStream;
+
+// FEATURE: add ASCII85Encoding (see pdf spec)
+
+/** General String and byte[] processing functions,
+ * including Base64 and a safe filename transform.
+ *
+ * @author adam@ibex.org
+ */
+public final class Encode {
+
+ public static class QuotedPrintable {
+ public static String decode(String s, boolean lax) {
+ //
+ // =XX -> hex representation, must be uppercase
+ // 9, 32, 33-60, 62-126 can be literal
+ // 9, 32 at end-of-line must get encoded
+ // trailing whitespace must be deleted when decoding
+ // =\n = soft line break
+ // lines cannot be more than 76 chars long
+ //
+
+ // lax is used for RFC2047 headers; removes restrictions on which chars you can encode
+ return s;
+ }
+ }
+
+
+ public static class RFC2047 {
+ public static String decode(String s) {
+ /*
+ try { while (s.indexOf("=?") != -1) {
+ String pre = s.substring(0, s.indexOf("=?"));
+ s = s.substring(s.indexOf("=?") + 2);
+
+ // MIME charset; FIXME use this
+ String charset = s.substring(0, s.indexOf('?')).toLowerCase();
+ s = s.substring(s.indexOf('?') + 1);
+
+ String encoding = s.substring(0, s.indexOf('?')).toLowerCase();
+ s = s.substring(s.indexOf('?') + 1);
+
+ String encodedText = s.substring(0, s.indexOf("?="));
+
+ if (encoding.equals("b")) encodedText = new String(Base64.decode(encodedText));
+
+ // except that ANY char can be endoed (unlike real qp)
+ else if (encoding.equals("q")) encodedText = MIME.QuotedPrintable.decode(encodedText, true);
+ else Log.warn(MIME.class, "unknown RFC2047 encoding \""+encoding+"\"");
+
+ String post = s.substring(s.indexOf("?=") + 2);
+ s = pre + encodedText + post;
+
+ // FIXME re-encode when transmitting
+
+ } } catch (Exception e) {
+ Log.warn(MIME.class, "error trying to decode RFC2047 encoded-word: \""+s+"\"");
+ Log.warn(MIME.class, e);
+ }
+ */
+ return s;
+ }
+ }
+
+
+ public static long twoFloatsToLong(float a, float b) {
+ return ((Float.floatToIntBits(a) & 0xffffffffL) << 32) | (Float.floatToIntBits(b) & 0xffffffffL); }
+ public static float longToFloat1(long l) { return Float.intBitsToFloat((int)((l >> 32) & 0xffffffff)); }
+ public static float longToFloat2(long l) { return Float.intBitsToFloat((int)(l & 0xffffffff)); }
+
+ private static final char[] fn =
+ new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
+
+ public static String toFilename(String s) {
+ StringBuffer sb = new StringBuffer();
+ try {
+ byte[] b = s.getBytes("UTF-8");
+ for(int i=0; i 126 || c == '%' || (i == 0 && c == '.'))
+ sb.append("%" + fn[(b[i] & 0xf0) >> 8] + fn[b[i] & 0xf]);
+ else sb.append(c);
+ }
+ return sb.toString();
+ } catch (UnsupportedEncodingException uee) {
+ throw new Error("this should never happen; Java spec mandates UTF-8 support");
+ }
+ }
+
+ public static String fromFilename(String s) {
+ StringBuffer sb = new StringBuffer();
+ byte[] b = new byte[s.length() * 2];
+ int bytes = 0;
+ for(int i=0; i>> 2) & 0x3f];
+ bytes[j + 1] = encB64[((a1 << 4) | (a2 >>> 4)) & 0x3f];
+ bytes[j + 2] = encB64[((a2 << 2) | (a3 >>> 6)) & 0x3f];
+ bytes[j + 3] = encB64[a3 & 0x3f];
+ }
+
+ int b1, b2, b3;
+ int d1, d2;
+ switch (modulus) {
+ case 0: /* nothing left to do */
+ break;
+ case 1:
+ d1 = data[data.length - 1] & 0xff;
+ b1 = (d1 >>> 2) & 0x3f;
+ b2 = (d1 << 4) & 0x3f;
+
+ bytes[bytes.length - 4] = encB64[b1];
+ bytes[bytes.length - 3] = encB64[b2];
+ bytes[bytes.length - 2] = (byte)'=';
+ bytes[bytes.length - 1] = (byte)'=';
+ break;
+ case 2:
+ d1 = data[data.length - 2] & 0xff;
+ d2 = data[data.length - 1] & 0xff;
+
+ b1 = (d1 >>> 2) & 0x3f;
+ b2 = ((d1 << 4) | (d2 >>> 4)) & 0x3f;
+ b3 = (d2 << 2) & 0x3f;
+
+ bytes[bytes.length - 4] = encB64[b1];
+ bytes[bytes.length - 3] = encB64[b2];
+ bytes[bytes.length - 2] = encB64[b3];
+ bytes[bytes.length - 1] = (byte)'=';
+ break;
+ }
+
+ return bytes;
+ }
+
+
+ private static final byte[] decB64 = new byte[128];
+ static {
+ for (int i = 'A'; i <= 'Z'; i++) decB64[i] = (byte)(i - 'A');
+ for (int i = 'a'; i <= 'z'; i++) decB64[i] = (byte)(i - 'a' + 26);
+ for (int i = '0'; i <= '9'; i++) decB64[i] = (byte)(i - '0' + 52);
+ decB64['+'] = 62;
+ decB64['/'] = 63;
+ }
+
+ /** Decode base 64 encoded input data.
+ * @return A byte array representing the decoded data. */
+ public static byte[] fromBase64(byte[] data) {
+ byte[] bytes;
+ byte b1, b2, b3, b4;
+
+ if (data[data.length - 2] == '=') bytes = new byte[(((data.length / 4) - 1) * 3) + 1];
+ else if (data[data.length - 1] == '=') bytes = new byte[(((data.length / 4) - 1) * 3) + 2];
+ else bytes = new byte[((data.length / 4) * 3)];
+
+ for (int i = 0, j = 0; i < data.length - 4; i += 4, j += 3) {
+ b1 = decB64[data[i]];
+ b2 = decB64[data[i + 1]];
+ b3 = decB64[data[i + 2]];
+ b4 = decB64[data[i + 3]];
+
+ bytes[j] = (byte)((b1 << 2) | (b2 >> 4));
+ bytes[j + 1] = (byte)((b2 << 4) | (b3 >> 2));
+ bytes[j + 2] = (byte)((b3 << 6) | b4);
+ }
+
+ if (data[data.length - 2] == '=') {
+ b1 = decB64[data[data.length - 4]];
+ b2 = decB64[data[data.length - 3]];
+ bytes[bytes.length - 1] = (byte)((b1 << 2) | (b2 >> 4));
+ } else if (data[data.length - 1] == '=') {
+ b1 = decB64[data[data.length - 4]];
+ b2 = decB64[data[data.length - 3]];
+ b3 = decB64[data[data.length - 2]];
+ bytes[bytes.length - 2] = (byte)((b1 << 2) | (b2 >> 4));
+ bytes[bytes.length - 1] = (byte)((b2 << 4) | (b3 >> 2));
+ } else {
+ b1 = decB64[data[data.length - 4]];
+ b2 = decB64[data[data.length - 3]];
+ b3 = decB64[data[data.length - 2]];
+ b4 = decB64[data[data.length - 1]];
+ bytes[bytes.length - 3] = (byte)((b1 << 2) | (b2 >> 4));
+ bytes[bytes.length - 2] = (byte)((b2 << 4) | (b3 >> 2));
+ bytes[bytes.length - 1] = (byte)((b3 << 6) | b4);
+ }
+ return bytes;
+ }
+
+ /** Decode a base 64 encoded String.
+ * @return A byte array representing the decoded data. */
+ public static byte[] fromBase64(String data) {
+ byte[] bytes;
+ byte b1, b2, b3, b4;
+
+ if (data.charAt(data.length() - 2) == '=')
+ bytes = new byte[(((data.length() / 4) - 1) * 3) + 1];
+ else if (data.charAt(data.length() - 1) == '=')
+ bytes = new byte[(((data.length() / 4) - 1) * 3) + 2];
+ else
+ bytes = new byte[((data.length() / 4) * 3)];
+
+ for (int i = 0, j = 0; i < data.length() - 4; i += 4, j += 3) {
+ b1 = decB64[data.charAt(i)];
+ b2 = decB64[data.charAt(i + 1)];
+ b3 = decB64[data.charAt(i + 2)];
+ b4 = decB64[data.charAt(i + 3)];
+
+ bytes[j] = (byte)((b1 << 2) | (b2 >> 4));
+ bytes[j + 1] = (byte)((b2 << 4) | (b3 >> 2));
+ bytes[j + 2] = (byte)((b3 << 6) | b4);
+ }
+
+ if (data.charAt(data.length() - 2) == '=') {
+ b1 = decB64[data.charAt(data.length() - 4)];
+ b2 = decB64[data.charAt(data.length() - 3)];
+ bytes[bytes.length - 1] = (byte)((b1 << 2) | (b2 >> 4));
+ } else if (data.charAt(data.length() - 1) == '=') {
+ b1 = decB64[data.charAt(data.length() - 4)];
+ b2 = decB64[data.charAt(data.length() - 3)];
+ b3 = decB64[data.charAt(data.length() - 2)];
+ bytes[bytes.length - 2] = (byte)((b1 << 2) | (b2 >> 4));
+ bytes[bytes.length - 1] = (byte)((b2 << 4) | (b3 >> 2));
+ } else {
+ b1 = decB64[data.charAt(data.length() - 4)];
+ b2 = decB64[data.charAt(data.length() - 3)];
+ b3 = decB64[data.charAt(data.length() - 2)];
+ b4 = decB64[data.charAt(data.length() - 1)];
+ bytes[bytes.length - 3] = (byte)((b1 << 2) | (b2 >> 4));
+ bytes[bytes.length - 2] = (byte)((b2 << 4) | (b3 >> 2));
+ bytes[bytes.length - 1] = (byte)((b3 << 6) | b4);
+ }
+ return bytes;
+ }
+
+
+ /** Packs 8-bit bytes into a String of 7-bit chars.
+ * @throws IllegalArgumentException when len is not a multiple of 7.
+ * @return A String representing the processed bytes. */
+ public static String toStringFrom8bit(byte[] b, int off, int len) throws IllegalArgumentException {
+ if (len % 7 != 0) throw new IllegalArgumentException("len must be a multiple of 7");
+ StringBuffer ret = new StringBuffer();
+ for(int i=off; i=0; j--) {
+ l <<= 8;
+ l |= (b[i + j] & 0xff);
+ }
+ for(int j=0; j<8; j++) {
+ ret.append((char)(l & 0x7f));
+ l >>= 7;
+ }
+ }
+ return ret.toString();
+ }
+
+ /** Packs a String of 7-bit chars into 8-bit bytes.
+ * @throws IllegalArgumentException when s.length() is not a multiple of 8.
+ * @return A byte array representing the processed String. */
+ public static byte[] fromStringTo8bit(String s) throws IllegalArgumentException {
+ if (s.length() % 8 != 0) throw new IllegalArgumentException("string length must be a multiple of 8");
+ byte[] ret = new byte[(s.length() / 8) * 7];
+ for(int i=0; i=0; j--) {
+ l <<= 7;
+ l |= (s.charAt(i + j) & 0x7fL);
+ }
+ for(int j=0; j<7; j++) {
+ ret[(i / 8) * 7 + j] = (byte)(l & 0xff);
+ l >>= 8;
+ }
+ }
+ return ret;
+ }
public static class JavaSourceCode {