]> WPIA git - gigi.git/blob - lib/jetty/org/eclipse/jetty/util/Utf8Appendable.java
Merge "Update notes about password security"
[gigi.git] / lib / jetty / org / eclipse / jetty / util / Utf8Appendable.java
1 //
2 //  ========================================================================
3 //  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4 //  ------------------------------------------------------------------------
5 //  All rights reserved. This program and the accompanying materials
6 //  are made available under the terms of the Eclipse Public License v1.0
7 //  and Apache License v2.0 which accompanies this distribution.
8 //
9 //      The Eclipse Public License is available at
10 //      http://www.eclipse.org/legal/epl-v10.html
11 //
12 //      The Apache License v2.0 is available at
13 //      http://www.opensource.org/licenses/apache2.0.php
14 //
15 //  You may elect to redistribute this code under either of these licenses.
16 //  ========================================================================
17 //
18
19 package org.eclipse.jetty.util;
20
21 import java.io.IOException;
22 import java.nio.ByteBuffer;
23
24 import org.eclipse.jetty.util.log.Log;
25 import org.eclipse.jetty.util.log.Logger;
26
27 /* ------------------------------------------------------------ */
28 /**
29  * Utf8 Appendable abstract base class
30  *
31  * This abstract class wraps a standard {@link java.lang.Appendable} and provides methods to append UTF-8 encoded bytes, that are converted into characters.
32  *
33  * This class is stateful and up to 4 calls to {@link #append(byte)} may be needed before state a character is appended to the string buffer.
34  *
35  * The UTF-8 decoding is done by this class and no additional buffers or Readers are used. The UTF-8 code was inspired by
36  * http://bjoern.hoehrmann.de/utf-8/decoder/dfa/
37  *
38  * License information for Bjoern Hoehrmann's code:
39  *
40  * Copyright (c) 2008-2009 Bjoern Hoehrmann <bjoern@hoehrmann.de>
41  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal
42  * in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
43  * copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
44  *
45  * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
46  *
47  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
48  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
49  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
50  **/
51 public abstract class Utf8Appendable
52 {
53     protected static final Logger LOG = Log.getLogger(Utf8Appendable.class);
54     public static final char REPLACEMENT = '\ufffd';
55     public static final byte[] REPLACEMENT_UTF8 = new byte[] {(byte)0xEF,(byte)0xBF,(byte)0xBD };
56     private static final int UTF8_ACCEPT = 0;
57     private static final int UTF8_REJECT = 12;
58
59     protected final Appendable _appendable;
60     protected int _state = UTF8_ACCEPT;
61
62     private static final byte[] BYTE_TABLE =
63     {
64         // The first part of the table maps bytes to character classes that
65         // to reduce the size of the transition table and create bitmasks.
66          0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
67          0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
68          0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
69          0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
70          1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,  9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,
71          7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,  7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
72          8,8,2,2,2,2,2,2,2,2,2,2,2,2,2,2,  2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
73         10,3,3,3,3,3,3,3,3,3,3,3,3,4,3,3, 11,6,6,6,5,8,8,8,8,8,8,8,8,8,8,8
74     };
75
76     private static final byte[] TRANS_TABLE =
77     {
78         // The second part is a transition table that maps a combination
79         // of a state of the automaton and a character class to a state.
80          0,12,24,36,60,96,84,12,12,12,48,72, 12,12,12,12,12,12,12,12,12,12,12,12,
81         12, 0,12,12,12,12,12, 0,12, 0,12,12, 12,24,12,12,12,12,12,24,12,24,12,12,
82         12,12,12,12,12,12,12,24,12,12,12,12, 12,24,12,12,12,12,12,12,12,24,12,12,
83         12,12,12,12,12,12,12,36,12,36,12,12, 12,36,12,12,12,12,12,36,12,36,12,12,
84         12,36,12,12,12,12,12,12,12,12,12,12
85     };
86
87     private int _codep;
88
89     public Utf8Appendable(Appendable appendable)
90     {
91         _appendable = appendable;
92     }
93
94     public abstract int length();
95
96     protected void reset()
97     {
98         _state = UTF8_ACCEPT;
99     }
100
101     public void append(byte b)
102     {
103         try
104         {
105             appendByte(b);
106         }
107         catch (IOException e)
108         {
109             throw new RuntimeException(e);
110         }
111     }
112     
113     public void append(ByteBuffer buf)
114     {
115         try
116         {
117             while (buf.remaining() > 0)
118             {
119                 appendByte(buf.get());
120             }
121         }
122         catch (IOException e)
123         {
124             throw new RuntimeException(e);
125         }
126     }
127
128     public void append(byte[] b, int offset, int length)
129     {
130         try
131         {
132             int end = offset + length;
133             for (int i = offset; i < end; i++)
134                 appendByte(b[i]);
135         }
136         catch (IOException e)
137         {
138             throw new RuntimeException(e);
139         }
140     }
141
142     public boolean append(byte[] b, int offset, int length, int maxChars)
143     {
144         try
145         {
146             int end = offset + length;
147             for (int i = offset; i < end; i++)
148             {
149                 if (length() > maxChars)
150                     return false;
151                 appendByte(b[i]);
152             }
153             return true;
154         }
155         catch (IOException e)
156         {
157             throw new RuntimeException(e);
158         }
159     }
160
161     protected void appendByte(byte b) throws IOException
162     {
163
164         if (b > 0 && _state == UTF8_ACCEPT)
165         {
166             _appendable.append((char)(b & 0xFF));
167         }
168         else
169         {
170             int i = b & 0xFF;
171             int type = BYTE_TABLE[i];
172             _codep = _state == UTF8_ACCEPT ? (0xFF >> type) & i : (i & 0x3F) | (_codep << 6);
173             int next = TRANS_TABLE[_state + type];
174
175             switch(next)
176             {
177                 case UTF8_ACCEPT:
178                     _state=next;
179                     if (_codep < Character.MIN_HIGH_SURROGATE)
180                     {
181                         _appendable.append((char)_codep);
182                     }
183                     else
184                     {
185                         for (char c : Character.toChars(_codep))
186                             _appendable.append(c);
187                     }
188                     break;
189                     
190                 case UTF8_REJECT:
191                     String reason = "byte "+TypeUtil.toHexString(b)+" in state "+(_state/12);
192                     _codep=0;
193                     _state = UTF8_ACCEPT;
194                     _appendable.append(REPLACEMENT);
195                     throw new NotUtf8Exception(reason);
196                     
197                 default:
198                     _state=next;
199                     
200             }
201         }
202     }
203
204     public boolean isUtf8SequenceComplete()
205     {
206         return _state == UTF8_ACCEPT;
207     }
208
209     @SuppressWarnings("serial")
210     public static class NotUtf8Exception extends IllegalArgumentException
211     {
212         public NotUtf8Exception(String reason)
213         {
214             super("Not valid UTF8! "+reason);
215         }
216     }
217
218     protected void checkState()
219     {
220         if (!isUtf8SequenceComplete())
221         {
222             _codep=0;
223             _state = UTF8_ACCEPT;
224             try
225             {
226                 _appendable.append(REPLACEMENT);
227             }
228             catch(IOException e)
229             {
230                 throw new RuntimeException(e);
231             }
232             throw new NotUtf8Exception("incomplete UTF8 sequence");
233         }
234     }
235     
236     public String toReplacedString()
237     {
238         if (!isUtf8SequenceComplete())
239         {
240             _codep=0;
241             _state = UTF8_ACCEPT;
242             try
243             {
244                 _appendable.append(REPLACEMENT);
245             }
246             catch(IOException e)
247             {
248                 throw new RuntimeException(e);
249             }
250             Throwable th= new NotUtf8Exception("incomplete UTF8 sequence");
251             LOG.warn(th.toString());
252             LOG.debug(th);
253         }
254         return _appendable.toString();
255     }
256 }