From dee6e16e6c1cf73b696a25662ee63132903ef9f6 Mon Sep 17 00:00:00 2001 From: zzz Date: Thu, 21 Aug 2014 11:49:58 +0000 Subject: [PATCH] * i2psnark: - Escape control chars in encodePath() - Increase max piece size to 8 MB (ticket #1347) --- .../java/src/org/klomp/snark/Storage.java | 2 +- .../java/src/org/klomp/snark/web/URIUtil.java | 35 +++++++++++++++---- 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/apps/i2psnark/java/src/org/klomp/snark/Storage.java b/apps/i2psnark/java/src/org/klomp/snark/Storage.java index efae9baad..0e5365fa5 100644 --- a/apps/i2psnark/java/src/org/klomp/snark/Storage.java +++ b/apps/i2psnark/java/src/org/klomp/snark/Storage.java @@ -74,7 +74,7 @@ public class Storage /** The default piece size. */ private static final int DEFAULT_PIECE_SIZE = 256*1024; /** bigger than this will be rejected */ - public static final int MAX_PIECE_SIZE = 4*1024*1024; + public static final int MAX_PIECE_SIZE = 8*1024*1024; /** The maximum number of pieces in a torrent. */ public static final int MAX_PIECES = 10*1024; public static final long MAX_TOTAL_SIZE = MAX_PIECE_SIZE * (long) MAX_PIECES; diff --git a/apps/i2psnark/java/src/org/klomp/snark/web/URIUtil.java b/apps/i2psnark/java/src/org/klomp/snark/web/URIUtil.java index 3a9c3088e..aae95b515 100644 --- a/apps/i2psnark/java/src/org/klomp/snark/web/URIUtil.java +++ b/apps/i2psnark/java/src/org/klomp/snark/web/URIUtil.java @@ -56,6 +56,10 @@ class URIUtil } /** Encode a URI path. + * + * Somewhat oddly, this encodes all chars >= 0x80 if buf is null, (strict RFC 2396) + * but only the control, space, and special chars if buf is non-null. + * * @param path The path the encode * @param buf StringBuilder to encode path into (or null) * @return The StringBuilder or null if no substitutions required. @@ -83,7 +87,7 @@ class URIUtil buf=new StringBuilder(path.length()*2); break loop; default: - if (c>127) + if (c >= 0x7f || c <= 0x1f) { bytes = DataHelper.getUTF8(path); buf=new StringBuilder(path.length()*2); @@ -132,12 +136,12 @@ class URIUtil case ' ': buf.append("%20"); continue; + case 0x7f: + buf.append("%7F"); + continue; default: - if (c<0) - { - buf.append('%'); + if (c <= 0x1f) // includes negative toHex(c,buf); - } else buf.append((char)c); continue; @@ -180,7 +184,10 @@ class URIUtil buf.append("%20"); continue; default: - buf.append(c); + if (c <= 0x1f || (c >= 0x7f && c <= 0x9f) || Character.isSpaceChar(c)) + toHex(c,buf); + else + buf.append(c); continue; } } @@ -195,11 +202,27 @@ class URIUtil */ private static void toHex(byte b, StringBuilder buf) { + buf.append('%'); int d=0xf&((0xF0&b)>>4); buf.append((char)((d>9?('A'-10):'0')+d)); d=0xf&b; buf.append((char)((d>9?('A'-10):'0')+d)); } + + /** + * UTF-8 + */ + private static void toHex(char c, StringBuilder buf) + { + if (c > 0x7f) { + byte[] b = DataHelper.getUTF8(Character.toString(c)); + for (int i = 0; i < b.length; i++) { + toHex(b[i], buf); + } + } else { + toHex((byte) c, buf); + } + } }