aboutsummaryrefslogtreecommitdiff
path: root/src/org/gnunet/util/Strings.java
blob: a35568a3b2eff6a76a70b731eaae22eb6f38696b (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
/*
 This file is part of GNUnet.
 (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 = "0123456789ABCDEFGHIJKLMNOPQRSTUV";


    /**
     * 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 arithmetics and uses a
     * small number of characters.
     * Does not append 0-terminator, but returns a pointer to the place where
     * it should be placed, if needed.
     *
     * returned string has length ((size*8) + (((size*8) % 5) > 0 ? 5 - ((size*8) % 5) : 0)) / 5 bytes
     *
     * @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 double to int without sign 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;
        }
        return sb.toString();
    }

    /**
     * Convert ASCII encoding back to data
     * out_size 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 GNUNET_OK on success, GNUNET_SYSERR if result has the wrong encoding
     */

    public static byte[] stringToData(String string, int outSize) {
        long rpos;
        long wpos;
        long bits;
        long vbit;
        long ret;
        long shift;
        int enclen = string.length();
        int encoded_len = outSize * 8;
        byte[] out = new byte[outSize];
        if (encoded_len % 5 > 0) {
            // padding!
            vbit = encoded_len % 5;
            shift = 5 - vbit;
        } else {
            vbit = 0;
            shift = 0;
        }
        if ((encoded_len + shift) / 5 != enclen) {
            throw new AssertionError();
        }

        wpos = outSize;
        rpos = enclen;
        bits = (ret = getValue__(string.charAt((int) (--rpos)))) >> (5 - encoded_len % 5);
        if (-1 == ret) {
            throw new AssertionError();
        }
        while (wpos > 0) {
            assert rpos > 0;
            bits = ((ret = getValue__(string.charAt((int) (--rpos)))) << vbit) | bits;
            if (-1 == ret) {
                throw new AssertionError();
            }
            vbit += 5;
            if (vbit >= 8) {
                out[(int)--wpos] = (byte)((char) bits);
                bits >>= 8;
                vbit -= 8;
            }
        }
        assert(rpos == 0);
        assert(vbit == 0);
        return out;
    }


    private static int getValue__ (char a) {
        if ((a >= '0') && (a <= '9')) {
            return a - '0';
        }
        if ((a >= 'A') && (a <= 'V')) {
            return (a - 'A' + 10);
        }
        return -1;
    }

}