aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/gnunet/util/Strings.java
blob: b97eb0d161a81708e5e4894ba82bf33710990aba (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
 This file is part of GNUnet.
 Copyright (C) 2011, 2012 Christian Grothoff (and other contributing authors)

 GNUnet is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published
 by the Free Software Foundation; either version 3, or (at your
 option) any later version.

 GNUnet is distributed in the hope that it will be useful, but
 WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with GNUnet; see the file COPYING.  If not, write to the
 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 Boston, MA 02111-1307, USA.
 */

package org.gnunet.util;

/**
 * Common functions on Strings, specific to gnunet-java
 */
public class Strings {
    private static final String encTable = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";

    /**
     * Convert binary data to ASCII encoding.  The ASCII encoding is rather
     * GNUnet specific.  It was chosen such that it only uses characters
     * in [0-9A-V], can be produced without complex arithmetic and uses a
     * small number of characters.
     *
     * The returned string has length (data.length * 8 + 4) / 5
     *
     * @param data data to encode
     * @return pointer to the next byte in 'out' or NULL on error.
     */
    public static String dataToString(byte[] data) {
        StringBuilder sb = new StringBuilder();

        long rpos = 0;
        long bits = 0;
        long vbit = 0;
        long size = data.length;

        while ((rpos < size) || (vbit > 0)) {
            if ((rpos < size) && (vbit < 5)) {
                byte b = data[(int) rpos++];
                // convert (possibly negative) byte to int without signRaw extension
                int s = b >= 0 ? b : (256 + b);
                // eat 8 more bits
                bits = (bits << 8) | s;
                vbit += 8;
            }
            if (vbit < 5) {
                // zero-padding
                bits <<= (5 - vbit);
                vbit = 5;
            }
            sb.append(encTable.charAt((int) (bits >>> (vbit - 5)) & 31));
            vbit -= 5;
        }
        if (sb.length() != getEncodedStringLength(data.length))
            throw new AssertionError();
        return sb.toString();
    }

    /**
     * Convert ASCII encoding back to data. The size of the output array
     * must exactly match the size of the encoded string.
     *
     * @param string the string to decode
     * @param outData size of the output buffer
     * @return was the encoding successful?
     */
    public static boolean stringToData(String string, byte[] outData) {
        long rpos; // read position
        long wpos; // write position
        long bits; // bits to write next
        long vbit;
        long ret;
        long shift;
        int enclen = string.length();
        int encodedLen = outData.length * 8;

        if (0 == enclen) {
            if (0 == outData.length)
                return true;
            return false;
        }

        wpos = outData.length;
        rpos = enclen;

        if (encodedLen % 5 > 0) {
            // padding!
            vbit = encodedLen % 5;
            shift = 5 - vbit;
            bits = (ret = getValue(string.charAt((int) (--rpos)))) >>> (5 - (encodedLen % 5));
        } else {
            vbit = 5;
            shift = 0;
            bits = (ret = getValue(string.charAt((int) (--rpos))));
        }
        if ((encodedLen + shift) / 5 != enclen) {
            return false;
        }
        if (-1 == ret) {
            return false;
        }
        while (wpos > 0) {
            if (0 == rpos) {
                throw new AssertionError("rpos=0, but wpos " + wpos);
            }
            bits = ((ret = getValue(string.charAt((int) (--rpos)))) << vbit) | bits;
            if (-1 == ret) {
                return false;
            }
            vbit += 5;
            if (vbit >= 8) {
                outData[(int)--wpos] = (byte)((char) bits);
                bits >>>= 8;
                vbit -= 8;
            }
        }
        if (rpos != 0 || vbit != 0) {
            return false;
        }
        return true;
    }

    /**
     * Convert ASCII encoding back to data.
     * The outSize must match exactly the size of the data before it was encoded.
     *
     * @param string the string to decode
     * @param outSize size of the output buffer
     * @return the decoded data on success, null if result has the wrong encoding
     */
    public static byte[] stringToData(String string, int outSize) {
        byte[] outData = new byte[outSize];
        if (stringToData(string, outData)) {
            return outData;
        }
        return null;
    }

    /**
     * Compute the length of the resulting string when encoding data of the given size
     * in bytes.
     *
     * @param dataSize size of the data to encode in bytes
     * @return size of the string that would result from encoding
     */
    public static int getEncodedStringLength(int dataSize) {
        return (dataSize * 8 + 4) / 5;
    }

    /**
     * Compute the length of the resulting data in bytes when decoding a (valid) string of the
     * given size.
     *
     * @param stringSize size of the string to decode
     * @return size of the resulting data in bytes
     */
    public static int getDecodedDataLength(int stringSize) {
        return (stringSize * 5) / 8;
    }

    private static int getValue(char a) {
        int dec;

        switch (a)
        {
            case 'O':
            case 'o':
                a = '0';
                break;
            case 'i':
            case 'I':
            case 'l':
            case 'L':
                a = '1';
                break;
                /* also consider U to be V */
            case 'u':
            case 'U':
                a = 'V';
                break;
            default:
                break;
            }
            if ((a >= '0') && (a <= '9'))
                return a - '0';
            if ((a >= 'a') && (a <= 'z'))
                a = Character.toUpperCase((char) a);
                /* return (a - 'a' + 10); */
            dec = 0;
            if ((a >= 'A') && (a <= 'Z'))
            {
              if ('I' < a)
                  dec++;
              if ('L' < a)
                  dec++;
              if ('O' < a)
                  dec++;
              if ('U' < a)
                  dec++;
              return (a - 'A' + 10 - dec);
        }
        return -1;
    }

    public static byte[] stringToData(String s) {
        return stringToData(s, getDecodedDataLength(s.length()));
    }
}