aboutsummaryrefslogtreecommitdiff
path: root/src/org/gnunet/util/Strings.java
blob: 3c369b18ff364be46f17ca086b5fb16f8ebdad59 (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
/*
 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";

    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();
    }

    public static byte[] stringToData(String string) {
        /*
        long rpos;
        long wpos;
        long bits;
        long vbit;
        int ret;
        int shift;
        int encoded_len = out_size * 8;
        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 = out_size;
        rpos = enclen;
        bits = (ret = getValue__(enc[--rpos])) >> (5 - encoded_len % 5);
        if (-1 == ret)
            return GNUNET_SYSERR;
        while (wpos > 0) {
            GNUNET_assert(rpos > 0);
            bits = ((ret = getValue__(enc[--rpos])) << vbit) | bits;
            if (-1 == ret)
                return GNUNET_SYSERR;
            vbit += 5;
            if (vbit >= 8) {
                out[--wpos] = (unsigned char)bits;
                bits >>= 8;
                vbit -= 8;
            }
        }
        GNUNET_assert(rpos == 0);
        GNUNET_assert(vbit == 0);
        return GNUNET_OK;

    }
    */
    throw new UnsupportedOperationException("not yet implemented");
    }
}