1917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul/*
2917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * Copyright (C) 2007 The Android Open Source Project
3917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul *
4917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * Licensed under the Apache License, Version 2.0 (the "License");
5917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * you may not use this file except in compliance with the License.
6917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * You may obtain a copy of the License at
7917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul *
8917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul *      http://www.apache.org/licenses/LICENSE-2.0
9917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul *
10917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * Unless required by applicable law or agreed to in writing, software
11917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * distributed under the License is distributed on an "AS IS" BASIS,
12917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * See the License for the specific language governing permissions and
14917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * limitations under the License.
15917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul */
16917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul
17917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgulpackage com.android.dexgen.util;
18917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul
19917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul/**
20917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul * Utilities for parsing hexadecimal text.
21917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul */
22917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgulpublic final class HexParser {
23917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul    /**
24917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul     * This class is uninstantiable.
25917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul     */
26917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul    private HexParser() {
27917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul        // This space intentionally left blank.
28917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul    }
29917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul
30917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul    /**
31917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul     * Parses the given text as hex, returning a {@code byte[]}
32917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul     * corresponding to the text. The format is simple: Each line may
33917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul     * start with a hex offset followed by a colon (which is verified
34917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul     * and presumably used just as a comment), and then consists of
35917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul     * hex digits freely interspersed with whitespace. If a pound sign
36917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul     * is encountered, it and the rest of the line are ignored as a
37917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul     * comment. If a double quote is encountered, then the ASCII value
38917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul     * of the subsequent characters is used, until the next double
39917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul     * quote. Quoted strings may not span multiple lines.
40917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul     *
41917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul     * @param src {@code non-null;} the source string
42917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul     * @return {@code non-null;} the parsed form
43917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul     */
44917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul    public static byte[] parse(String src) {
45917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul        int len = src.length();
46917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul        byte[] result = new byte[len / 2];
47917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul        int at = 0;
48917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul        int outAt = 0;
49917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul
50917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul        while (at < len) {
51917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul            int nlAt = src.indexOf('\n', at);
52917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul            if (nlAt < 0) {
53917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul                nlAt = len;
54917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul            }
55917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul            int poundAt = src.indexOf('#', at);
56917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul
57917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul            String line;
58917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul            if ((poundAt >= 0) && (poundAt < nlAt)) {
59917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul                line = src.substring(at, poundAt);
60917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul            } else {
61917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul                line = src.substring(at, nlAt);
62917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul            }
63917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul            at = nlAt + 1;
64917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul
65917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul            int colonAt = line.indexOf(':');
66917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul
67917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul            atCheck:
68917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul            if (colonAt != -1) {
69917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul                int quoteAt = line.indexOf('\"');
70917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul                if ((quoteAt != -1) && (quoteAt < colonAt)) {
71917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul                    break atCheck;
72917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul                }
73917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul
74917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul                String atStr = line.substring(0, colonAt).trim();
75917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul                line = line.substring(colonAt + 1);
76917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul                int alleged = Integer.parseInt(atStr, 16);
77917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul                if (alleged != outAt) {
78917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul                    throw new RuntimeException("bogus offset marker: " +
79917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul                                               atStr);
80917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul                }
81917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul            }
82917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul
83917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul            int lineLen = line.length();
84917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul            int value = -1;
85917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul            boolean quoteMode = false;
86917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul
87917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul            for (int i = 0; i < lineLen; i++) {
88917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul                char c = line.charAt(i);
89917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul
90917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul                if (quoteMode) {
91917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul                    if (c == '\"') {
92917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul                        quoteMode = false;
93917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul                    } else {
94917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul                        result[outAt] = (byte) c;
95917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul                        outAt++;
96917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul                    }
97917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul                    continue;
98917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul                }
99917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul
100917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul                if (c <= ' ') {
101917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul                    continue;
102917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul                }
103917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul                if (c == '\"') {
104917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul                    if (value != -1) {
105917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul                        throw new RuntimeException("spare digit around " +
106917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul                                                   "offset " + Hex.u4(outAt));
107917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul                    }
108917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul                    quoteMode = true;
109917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul                    continue;
110917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul                }
111917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul
112917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul                int digVal = Character.digit(c, 16);
113917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul                if (digVal == -1) {
114917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul                    throw new RuntimeException("bogus digit character: \"" +
115917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul                                               c + "\"");
116917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul                }
117917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul                if (value == -1) {
118917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul                    value = digVal;
119917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul                } else {
120917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul                    result[outAt] = (byte) ((value << 4) | digVal);
121917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul                    outAt++;
122917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul                    value = -1;
123917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul                }
124917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul            }
125917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul
126917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul            if (value != -1) {
127917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul                throw new RuntimeException("spare digit around offset " +
128917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul                                           Hex.u4(outAt));
129917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul            }
130917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul
131917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul            if (quoteMode) {
132917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul                throw new RuntimeException("unterminated quote around " +
133917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul                                           "offset " + Hex.u4(outAt));
134917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul            }
135917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul        }
136917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul
137917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul        if (outAt < result.length) {
138917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul            byte[] newr = new byte[outAt];
139917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul            System.arraycopy(result, 0, newr, 0, outAt);
140917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul            result = newr;
141917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul        }
142917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul
143917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul        return result;
144917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul    }
145917cb222329ee8c035c3ffaf947e4265761b9367Piotr Gurgul}
146