1/*
2 * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package com.sun.xml.internal.messaging.saaj.util;
27
28
29// Cut&paste from sun.net.www.ParseUtil: decode, unescape
30
31public class ParseUtil {
32    /**
33     * Un-escape and return the character at position i in string s.
34     */
35    private static char unescape(String s, int i) {
36        return (char) Integer.parseInt(s.substring(i+1,i+3),16);
37    }
38
39    /**
40     * Returns a new String constructed from the specified String by replacing
41     * the URL escape sequences and UTF8 encoding with the characters they
42     * represent.
43     *
44     * @param s string
45     *
46     * @return decoded string
47     */
48    public static String decode(String s) {
49        StringBuilder sb = new StringBuilder();
50
51        int i=0;
52        while (i<s.length()) {
53            char c = s.charAt(i);
54            char c2, c3;
55
56            if (c != '%') {
57                i++;
58            } else {
59                try {
60                    c = unescape(s, i);
61                    i += 3;
62
63                    if ((c & 0x80) != 0) {
64                        switch (c >> 4) {
65                            case 0xC: case 0xD:
66                                c2 = unescape(s, i);
67                                i += 3;
68                                c = (char)(((c & 0x1f) << 6) | (c2 & 0x3f));
69                                break;
70
71                            case 0xE:
72                                c2 = unescape(s, i);
73                                i += 3;
74                                c3 = unescape(s, i);
75                                i += 3;
76                                c = (char)(((c & 0x0f) << 12) |
77                                           ((c2 & 0x3f) << 6) |
78                                            (c3 & 0x3f));
79                                break;
80
81                            default:
82                                throw new IllegalArgumentException();
83                        }
84                    }
85                } catch (NumberFormatException e) {
86                    throw new IllegalArgumentException();
87                }
88            }
89
90            sb.append(c);
91        }
92
93        return sb.toString();
94    }
95
96
97}
98