X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Forg%2Fibex%2Futil%2FEncode.java;h=790c526eab742e3b75193cb97c1f10cf13681fd4;hb=176228ebf5bfc64c4e305ca4d37e6e3a5c0e31bf;hp=26522d78e935f9d3d935ba6f5f29cd780fc2531d;hpb=a838b50960feb8b99a7e6d662c1e4b0ab8315f4b;p=org.ibex.util.git diff --git a/src/org/ibex/util/Encode.java b/src/org/ibex/util/Encode.java index 26522d7..790c526 100644 --- a/src/org/ibex/util/Encode.java +++ b/src/org/ibex/util/Encode.java @@ -8,6 +8,8 @@ import java.io.*; 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. * @@ -15,9 +17,63 @@ import java.util.zip.GZIPOutputStream; */ 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)); } + 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 = @@ -76,6 +132,8 @@ public final class Encode { public static byte[] toBase64(String data) { return toBase64(data.getBytes()); } + public static String toBase64String(byte[] data) { return new String(toBase64(data)); } + /** Encode the input data producong a base 64 encoded byte array. * @return A byte array containing the base 64 encoded data. */ public static byte[] toBase64(byte[] data) {