aboutsummaryrefslogtreecommitdiff
path: root/src/org/gnunet/util/Strings.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/org/gnunet/util/Strings.java')
-rw-r--r--src/org/gnunet/util/Strings.java75
1 files changed, 55 insertions, 20 deletions
diff --git a/src/org/gnunet/util/Strings.java b/src/org/gnunet/util/Strings.java
index 3c369b1..a35568a 100644
--- a/src/org/gnunet/util/Strings.java
+++ b/src/org/gnunet/util/Strings.java
@@ -26,6 +26,21 @@ package org.gnunet.util;
26public class Strings { 26public class Strings {
27 private static final String encTable = "0123456789ABCDEFGHIJKLMNOPQRSTUV"; 27 private static final String encTable = "0123456789ABCDEFGHIJKLMNOPQRSTUV";
28 28
29
30 /**
31 * Convert binary data to ASCII encoding. The ASCII encoding is rather
32 * GNUnet specific. It was chosen such that it only uses characters
33 * in [0-9A-V], can be produced without complex arithmetics and uses a
34 * small number of characters.
35 * Does not append 0-terminator, but returns a pointer to the place where
36 * it should be placed, if needed.
37 *
38 * returned string has length ((size*8) + (((size*8) % 5) > 0 ? 5 - ((size*8) % 5) : 0)) / 5 bytes
39 *
40 * @param data data to encode
41 * @return pointer to the next byte in 'out' or NULL on error.
42 */
43
29 public static String dataToString(byte[] data) { 44 public static String dataToString(byte[] data) {
30 StringBuilder sb = new StringBuilder(); 45 StringBuilder sb = new StringBuilder();
31 46
@@ -54,15 +69,25 @@ public class Strings {
54 return sb.toString(); 69 return sb.toString();
55 } 70 }
56 71
57 public static byte[] stringToData(String string) { 72 /**
58 /* 73 * Convert ASCII encoding back to data
74 * out_size must match exactly the size of the data before it was encoded.
75 *
76 * @param string the string to decode
77 * @param outSize size of the output buffer
78 * @return GNUNET_OK on success, GNUNET_SYSERR if result has the wrong encoding
79 */
80
81 public static byte[] stringToData(String string, int outSize) {
59 long rpos; 82 long rpos;
60 long wpos; 83 long wpos;
61 long bits; 84 long bits;
62 long vbit; 85 long vbit;
63 int ret; 86 long ret;
64 int shift; 87 long shift;
65 int encoded_len = out_size * 8; 88 int enclen = string.length();
89 int encoded_len = outSize * 8;
90 byte[] out = new byte[outSize];
66 if (encoded_len % 5 > 0) { 91 if (encoded_len % 5 > 0) {
67 // padding! 92 // padding!
68 vbit = encoded_len % 5; 93 vbit = encoded_len % 5;
@@ -75,29 +100,39 @@ public class Strings {
75 throw new AssertionError(); 100 throw new AssertionError();
76 } 101 }
77 102
78 wpos = out_size; 103 wpos = outSize;
79 rpos = enclen; 104 rpos = enclen;
80 bits = (ret = getValue__(enc[--rpos])) >> (5 - encoded_len % 5); 105 bits = (ret = getValue__(string.charAt((int) (--rpos)))) >> (5 - encoded_len % 5);
81 if (-1 == ret) 106 if (-1 == ret) {
82 return GNUNET_SYSERR; 107 throw new AssertionError();
108 }
83 while (wpos > 0) { 109 while (wpos > 0) {
84 GNUNET_assert(rpos > 0); 110 assert rpos > 0;
85 bits = ((ret = getValue__(enc[--rpos])) << vbit) | bits; 111 bits = ((ret = getValue__(string.charAt((int) (--rpos)))) << vbit) | bits;
86 if (-1 == ret) 112 if (-1 == ret) {
87 return GNUNET_SYSERR; 113 throw new AssertionError();
114 }
88 vbit += 5; 115 vbit += 5;
89 if (vbit >= 8) { 116 if (vbit >= 8) {
90 out[--wpos] = (unsigned char)bits; 117 out[(int)--wpos] = (byte)((char) bits);
91 bits >>= 8; 118 bits >>= 8;
92 vbit -= 8; 119 vbit -= 8;
93 } 120 }
94 } 121 }
95 GNUNET_assert(rpos == 0); 122 assert(rpos == 0);
96 GNUNET_assert(vbit == 0); 123 assert(vbit == 0);
97 return GNUNET_OK; 124 return out;
98
99 } 125 }
100 */ 126
101 throw new UnsupportedOperationException("not yet implemented"); 127
128 private static int getValue__ (char a) {
129 if ((a >= '0') && (a <= '9')) {
130 return a - '0';
131 }
132 if ((a >= 'A') && (a <= 'V')) {
133 return (a - 'A' + 10);
134 }
135 return -1;
102 } 136 }
137
103} 138}