jruby (owner)

Revisions

gist: 209635 Download_button fork
public
Public Clone URL: git://gist.github.com/209635.git
Embed All Files: show embed
Java #
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
* Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved.
*
* Sun Microsystems, Inc. has intellectual property rights relating to technology embodied in the product
* that is described in this document. In particular, and without limitation, these intellectual property
* rights may include one or more of the U.S. patents listed at http://www.sun.com/patents and one or
* more additional patents or pending patent applications in the U.S. and in other countries.
*
* U.S. Government Rights - Commercial software. Government users are subject to the Sun
* Microsystems, Inc. standard license agreement and applicable provisions of the FAR and its
* supplements.
*
* Use is subject to license terms. Sun, Sun Microsystems, the Sun logo, Java and Solaris are trademarks or
* registered trademarks of Sun Microsystems, Inc. in the U.S. and other countries. All SPARC trademarks
* are used under license and are trademarks or registered trademarks of SPARC International, Inc. in the
* U.S. and other countries.
*
* UNIX is a registered trademark in the U.S. and other countries, exclusively licensed through X/Open
* Company, Ltd.
*/
 
package com.sun.max.unsafe;
 
import java.io.*;
 
import com.sun.max.memory.*;
import com.sun.max.util.*;
 
/**
* Utilities for converting between Java strings and C strings (encoded as UTF8 bytes).
*
* @author Bernd Mathiske
* @author Doug Simon
*/
public final class CString {
 
    private CString() {
    }
 
    /**
* Determines the length of a NULL terminated C string located in natively {@link Memory#allocate(int) allocated} memory.
* @param cString the string for which to get the length
* @return the length
*/
    public static Size length(Pointer cString) {
        Pointer p = cString;
        while (p.readByte(0) != (byte) 0) {
            p = p.plus(1);
        }
        return p.minus(cString).asSize();
    }
 
    /**
* Gets the byte at given index in C string with bounds check.
*
* @param cString the C string
* @param length length of C string (@see length)
* @param index index of byte to get
* @return -1 if the index is out of range or the byte at the index
*/
    public static int getByte(Pointer cString, Size length, Offset index) {
        if (index.lessThan(length.asOffset())) {
            return cString.readByte(index);
        }
        return -1;
    }
 
    /**
* Converts a NULL terminated C string located in natively allocated memory to a Java string.
*/
    public static String utf8ToJava(Pointer cString) throws Utf8Exception {
        final int n = length(cString).toInt();
        final byte[] bytes = new byte[n];
        Memory.readBytes(cString, n, bytes);
        return Utf8.utf8ToString(false, bytes);
    }
 
    /**
* Creates a NULL terminated C string (in natively allocated} memory) from a Java string.
* The returned C string must be deallocated by {@link Memory#deallocate(Address)} when finished with.
*/
    public static Pointer utf8FromJava(String string) {
        final byte[] utf8 = Utf8.stringToUtf8(string);
        final Pointer cString = Memory.mustAllocate(utf8.length + 1);
        Pointer p = cString;
        for (byte utf8Char : utf8) {
            p.writeByte(0, utf8Char);
            p = p.plus(1);
        }
        p.writeByte(0, (byte) 0);
        return cString;
    }
 
    public static byte[] read(InputStream stream) throws IOException {
        final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        while (true) {
            final int ch = stream.read();
            if (ch < 0) {
                throw new IOException();
            }
            buffer.write(ch);
            if (ch == 0) {
                return buffer.toByteArray();
            }
        }
    }
 
    /**
* Fills a given buffer with a zero-terminated sequence of bytes from a source buffer.
*
* @param source the byte array containing the source bytes
* @param start the start offset in {@code source} of the bytes to be written (inclusive)
* @param end the end offset of {@code source} of the bytes to be written (exclusive)
* @param buffer a pointer to the beginning of the buffer
* @param bufferSize the size of the buffer
* @return an index into the next byte to be written which is start <= result <= end
*/
    public static int writeBytes(byte[] source, int start, int end, Pointer buffer, int bufferSize) {
        final int n = Math.min(bufferSize - 1, end - start);
        for (int i = 0; i < n; i++) {
            buffer.writeByte(i, source[start + i]);
        }
        buffer.writeByte(n, (byte) 0);
        return start + n;
    }
 
    /**
* Fills a given buffer with the bytes in the UTF8 representation of a string following by a terminating zero. The
* maximum number of bytes written to the buffer is limited to the number of leading characters of {@code string}
* that can be completely encoded in {@code bufferSize - 2} bytes.
*
* @param string the String to write to the buffer
* @param buffer a pointer to the beginning of the buffer
* @param bufferSize the size of the buffer
* @return a pointer to the position in the buffer following the terminating zero character
*/
    public static Pointer writeUtf8(final String string, final Pointer buffer, final int bufferSize) {
        int position = 0;
        final int endPosition = bufferSize - 1;
        for (int i = 0; i < string.length(); i++) {
            final char ch = string.charAt(i);
            if ((ch >= 0x0001) && (ch <= 0x007F)) {
                if (position >= endPosition) {
                    break;
                }
                buffer.writeByte(position++, (byte) ch);
            } else if (ch > 0x07FF) {
                if (position + 2 >= endPosition) {
                    break;
                }
                buffer.writeByte(position++, (byte) (0xe0 | (byte) (ch >> 12)));
                buffer.writeByte(position++, (byte) (0x80 | ((ch & 0xfc0) >> 6)));
                buffer.writeByte(position++, (byte) (0x80 | (ch & 0x3f)));
            } else {
                if (position + 1 >= endPosition) {
                    break;
                }
                buffer.writeByte(position++, (byte) (0xc0 | (byte) (ch >> 6)));
                buffer.writeByte(position++, (byte) (0x80 | (ch & 0x3f)));
            }
        }
        buffer.writeByte(position, (byte) 0);
        return buffer.plus(position + 1);
    }
 
    /**
* Fills a given buffer with the bytes in the UTF8 representation of a string following by a terminating zero. The
* maximum number of bytes written to the buffer is limited to the number of leading characters of {@code string}
* that can be completely encoded in {@code bufferSize - 2} bytes.
*
* @param chars the characters to write to the buffer
* @param start the index of the character in {@code string} from which to start copying
* @param buffer a pointer to the beginning of the buffer
* @param bufferSize the size of the buffer
* @return the number of characters from {@code string} written to the buffer
*/
    public static int writePartialUtf8(final char[] chars, final int start, final Pointer buffer, final int bufferSize) {
        int position = 0;
        final int endPosition = bufferSize - 1;
        int i = start;
        while (i < chars.length) {
            final char ch = chars[i];
            if ((ch >= 0x0001) && (ch <= 0x007F)) {
                if (position >= endPosition) {
                    break;
                }
                buffer.writeByte(position++, (byte) ch);
            } else if (ch > 0x07FF) {
                if (position + 2 >= endPosition) {
                    break;
                }
                buffer.writeByte(position++, (byte) (0xe0 | (byte) (ch >> 12)));
                buffer.writeByte(position++, (byte) (0x80 | ((ch & 0xfc0) >> 6)));
                buffer.writeByte(position++, (byte) (0x80 | (ch & 0x3f)));
            } else {
                if (position + 1 >= endPosition) {
                    break;
                }
                buffer.writeByte(position++, (byte) (0xc0 | (byte) (ch >> 6)));
                buffer.writeByte(position++, (byte) (0x80 | (ch & 0x3f)));
            }
            i++;
        }
        buffer.writeByte(position, (byte) 0);
        return i;
    }
 
    /**
* Fills a given buffer with the bytes in the UTF8 representation of a string following by a terminating zero. The
* maximum number of bytes written to the buffer is limited to the number of leading characters of {@code string}
* that can be completely encoded in {@code bufferSize - 2} bytes.
*
* @param string the String to write to the buffer
* @param start the index of the character in {@code string} from which to start copying
* @param buffer a pointer to the beginning of the buffer
* @param bufferSize the size of the buffer
* @return the number of characters from {@code string} written to the buffer
*/
    public static int writePartialUtf8(final String string, final int start, final Pointer buffer, final int bufferSize) {
        int position = 0;
        final int endPosition = bufferSize - 1;
        int i = start;
        while (i < string.length()) {
            final char ch = string.charAt(i);
            if ((ch >= 0x0001) && (ch <= 0x007F)) {
                if (position >= endPosition) {
                    break;
                }
                buffer.writeByte(position++, (byte) ch);
            } else if (ch > 0x07FF) {
                if (position + 2 >= endPosition) {
                    break;
                }
                buffer.writeByte(position++, (byte) (0xe0 | (byte) (ch >> 12)));
                buffer.writeByte(position++, (byte) (0x80 | ((ch & 0xfc0) >> 6)));
                buffer.writeByte(position++, (byte) (0x80 | (ch & 0x3f)));
            } else {
                if (position + 1 >= endPosition) {
                    break;
                }
                buffer.writeByte(position++, (byte) (0xc0 | (byte) (ch >> 6)));
                buffer.writeByte(position++, (byte) (0x80 | (ch & 0x3f)));
            }
            i++;
        }
        buffer.writeByte(position, (byte) 0);
        return i;
    }
 
    public static byte[] toByteArray(Pointer start, int length) {
        final byte[] buffer = new byte[length];
        for (int i = 0; i < length; i++) {
            buffer[i] = start.getByte(i);
        }
        return buffer;
    }
 
    /**
* Copies an array of Java strings into a native array of C strings. The memory for the C string array and each
* element in the array is allocated in one memory chunk. The C string array is first in the chunk, followed by 0 if
* {@code appendNullDelimiter == true}, followed by {@code strings.length} null terminated C strings. De-allocating
* the memory for the buffer is the responsibility of the caller.
*
* @param strings an array of Java strings
* @param appendNullDelimiter {@code true} if a null delimiter character '\0' should be appended
* @return a native buffer than can be cast to the C type {@code char**} and used as the first argument to a C
* {@code main} function
*/
    public static Pointer utf8ArrayFromStringArray(String[] strings, boolean appendNullDelimiter) {
        final int nullDelimiter = appendNullDelimiter ? 1 : 0;
        final int pointerArraySize = Word.size() * (strings.length + nullDelimiter);
        int bufferSize = pointerArraySize;
        final int[] utf8Lengths = new int[strings.length];
        for (int i = 0; i < strings.length; ++i) {
            final String s = strings[i];
            final int utf8Length = Utf8.utf8Length(s);
            utf8Lengths[i] = utf8Length;
            bufferSize += utf8Length + 1;
        }
        final Pointer buffer = Memory.mustAllocate(bufferSize);
 
        Pointer stringPointer = buffer.plus(pointerArraySize);
        for (int i = 0; i < strings.length; ++i) {
            final String s = strings[i];
            buffer.setWord(i, stringPointer);
            stringPointer = CString.writeUtf8(s, stringPointer, utf8Lengths[i] + 1);
        }
        if (appendNullDelimiter) {
            buffer.setWord(strings.length, Word.zero());
        }
 
        return buffer;
    }
 
    public static int parseUnsignedInt(Pointer pointer) {
        int result = 0;
        Pointer ptr = pointer;
        while (true) {
            final char ch = (char) ptr.getByte();
            if (ch == 0) {
                break;
            }
            if (ch >= '0' && ch <= '9') {
                result *= 10;
                result += ch - '0';
            } else {
                return -1;
            }
            ptr = ptr.plus(1);
        }
        return result;
    }
 
    public static boolean equals(Pointer cstring, String string) {
        if (cstring.isZero()) {
            return false;
        }
        for (int i = 0; i < string.length(); i++) {
            final byte ch = cstring.getByte(i);
            if (ch == 0 || ch != string.charAt(i)) {
                return false;
            }
        }
        return cstring.getByte(string.length()) == 0;
    }
 
    /**
* Determines if a given C string starts with a given prefix.
*
* @param cstring the C string to test
* @param prefix the prefix to test against
* @return {@code true} if {@code cstring} starts with {@code prefix}
*/
    public static boolean startsWith(Pointer cstring, String prefix) {
        if (cstring.isZero()) {
            return false;
        }
        for (int i = 0; i < prefix.length(); i++) {
            final byte ch = cstring.getByte(i);
            if (ch == 0 || ch != prefix.charAt(i)) {
                return false;
            }
        }
        return true;
    }
}