001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.commons.codec.net;
019
020import java.io.UnsupportedEncodingException;
021import java.nio.charset.Charset;
022import java.nio.charset.StandardCharsets;
023import java.nio.charset.UnsupportedCharsetException;
024import java.util.BitSet;
025
026import org.apache.commons.codec.DecoderException;
027import org.apache.commons.codec.EncoderException;
028import org.apache.commons.codec.StringDecoder;
029import org.apache.commons.codec.StringEncoder;
030
031/**
032 * Similar to the Quoted-Printable content-transfer-encoding defined in
033 * <a href="http://www.ietf.org/rfc/rfc1521.txt">RFC 1521</a> and designed to allow text containing mostly ASCII
034 * characters to be decipherable on an ASCII terminal without decoding.
035 * <p>
036 * <a href="http://www.ietf.org/rfc/rfc1522.txt">RFC 1522</a> describes techniques to allow the encoding of non-ASCII
037 * text in various portions of a RFC 822 [2] message header, in a manner which is unlikely to confuse existing message
038 * handling software.
039 * </p>
040 * <p>
041 * This class is conditionally thread-safe.
042 * The instance field for encoding blanks is mutable {@link #setEncodeBlanks(boolean)}
043 * but is not volatile, and accesses are not synchronized.
044 * If an instance of the class is shared between threads, the caller needs to ensure that suitable synchronization
045 * is used to ensure safe publication of the value between threads, and must not invoke
046 * {@link #setEncodeBlanks(boolean)} after initial setup.
047 * </p>
048 *
049 * @see <a href="http://www.ietf.org/rfc/rfc1522.txt">MIME (Multipurpose Internet Mail Extensions) Part Two: Message
050 *          Header Extensions for Non-ASCII Text</a>
051 *
052 * @since 1.3
053 */
054public class QCodec extends RFC1522Codec implements StringEncoder, StringDecoder {
055    /**
056     * BitSet of printable characters as defined in RFC 1522.
057     */
058    private static final BitSet PRINTABLE_CHARS = new BitSet(256);
059
060    // Static initializer for printable chars collection
061    static {
062        // alpha characters
063        PRINTABLE_CHARS.set(' ');
064        PRINTABLE_CHARS.set('!');
065        PRINTABLE_CHARS.set('"');
066        PRINTABLE_CHARS.set('#');
067        PRINTABLE_CHARS.set('$');
068        PRINTABLE_CHARS.set('%');
069        PRINTABLE_CHARS.set('&');
070        PRINTABLE_CHARS.set('\'');
071        PRINTABLE_CHARS.set('(');
072        PRINTABLE_CHARS.set(')');
073        PRINTABLE_CHARS.set('*');
074        PRINTABLE_CHARS.set('+');
075        PRINTABLE_CHARS.set(',');
076        PRINTABLE_CHARS.set('-');
077        PRINTABLE_CHARS.set('.');
078        PRINTABLE_CHARS.set('/');
079        for (int i = '0'; i <= '9'; i++) {
080            PRINTABLE_CHARS.set(i);
081        }
082        PRINTABLE_CHARS.set(':');
083        PRINTABLE_CHARS.set(';');
084        PRINTABLE_CHARS.set('<');
085        PRINTABLE_CHARS.set('>');
086        PRINTABLE_CHARS.set('@');
087        for (int i = 'A'; i <= 'Z'; i++) {
088            PRINTABLE_CHARS.set(i);
089        }
090        PRINTABLE_CHARS.set('[');
091        PRINTABLE_CHARS.set('\\');
092        PRINTABLE_CHARS.set(']');
093        PRINTABLE_CHARS.set('^');
094        PRINTABLE_CHARS.set('`');
095        for (int i = 'a'; i <= 'z'; i++) {
096            PRINTABLE_CHARS.set(i);
097        }
098        PRINTABLE_CHARS.set('{');
099        PRINTABLE_CHARS.set('|');
100        PRINTABLE_CHARS.set('}');
101        PRINTABLE_CHARS.set('~');
102    }
103    private static final byte SPACE = 32;
104
105    private static final byte UNDERSCORE = 95;
106
107    private boolean encodeBlanks;
108
109    /**
110     * Default constructor.
111     */
112    public QCodec() {
113        this(StandardCharsets.UTF_8);
114    }
115
116    /**
117     * Constructor which allows for the selection of a default Charset.
118     *
119     * @param charset
120     *            the default string Charset to use.
121     *
122     * @see Charset
123     * @since 1.7
124     */
125    public QCodec(final Charset charset) {
126        super(charset);
127    }
128
129    /**
130     * Constructor which allows for the selection of a default Charset.
131     *
132     * @param charsetName
133     *            the Charset to use.
134     * @throws java.nio.charset.UnsupportedCharsetException
135     *             If the named Charset is unavailable
136     * @since 1.7 throws UnsupportedCharsetException if the named Charset is unavailable
137     * @see Charset
138     */
139    public QCodec(final String charsetName) {
140        this(Charset.forName(charsetName));
141    }
142
143    /**
144     * Decodes a quoted-printable object into its original form. Escaped characters are converted back to their original
145     * representation.
146     *
147     * @param obj
148     *            quoted-printable object to convert into its original form
149     * @return original object
150     * @throws DecoderException
151     *             Thrown if the argument is not a {@code String}. Thrown if a failure condition is encountered
152     *             during the decode process.
153     */
154    @Override
155    public Object decode(final Object obj) throws DecoderException {
156        if (obj == null) {
157            return null;
158        }
159        if (obj instanceof String) {
160            return decode((String) obj);
161        }
162        throw new DecoderException("Objects of type " + obj.getClass().getName() + " cannot be decoded using Q codec");
163    }
164
165    /**
166     * Decodes a quoted-printable string into its original form. Escaped characters are converted back to their original
167     * representation.
168     *
169     * @param str
170     *            quoted-printable string to convert into its original form
171     * @return original string
172     * @throws DecoderException
173     *             A decoder exception is thrown if a failure condition is encountered during the decode process.
174     */
175    @Override
176    public String decode(final String str) throws DecoderException {
177        try {
178            return decodeText(str);
179        } catch (final UnsupportedEncodingException e) {
180            throw new DecoderException(e.getMessage(), e);
181        }
182    }
183
184    @Override
185    protected byte[] doDecoding(final byte[] bytes) throws DecoderException {
186        if (bytes == null) {
187            return null;
188        }
189        boolean hasUnderscores = false;
190        for (final byte b : bytes) {
191            if (b == UNDERSCORE) {
192                hasUnderscores = true;
193                break;
194            }
195        }
196        if (hasUnderscores) {
197            final byte[] tmp = new byte[bytes.length];
198            for (int i = 0; i < bytes.length; i++) {
199                final byte b = bytes[i];
200                if (b != UNDERSCORE) {
201                    tmp[i] = b;
202                } else {
203                    tmp[i] = SPACE;
204                }
205            }
206            return QuotedPrintableCodec.decodeQuotedPrintable(tmp);
207        }
208        return QuotedPrintableCodec.decodeQuotedPrintable(bytes);
209    }
210
211    @Override
212    protected byte[] doEncoding(final byte[] bytes) {
213        if (bytes == null) {
214            return null;
215        }
216        final byte[] data = QuotedPrintableCodec.encodeQuotedPrintable(PRINTABLE_CHARS, bytes);
217        if (this.encodeBlanks) {
218            for (int i = 0; i < data.length; i++) {
219                if (data[i] == SPACE) {
220                    data[i] = UNDERSCORE;
221                }
222            }
223        }
224        return data;
225    }
226
227    /**
228     * Encodes an object into its quoted-printable form using the default Charset. Unsafe characters are escaped.
229     *
230     * @param obj
231     *            object to convert to quoted-printable form
232     * @return quoted-printable object
233     * @throws EncoderException
234     *             thrown if a failure condition is encountered during the encoding process.
235     */
236    @Override
237    public Object encode(final Object obj) throws EncoderException {
238        if (obj == null) {
239            return null;
240        }
241        if (obj instanceof String) {
242            return encode((String) obj);
243        }
244        throw new EncoderException("Objects of type " + obj.getClass().getName() + " cannot be encoded using Q codec");
245    }
246
247    /**
248     * Encodes a string into its quoted-printable form using the default Charset. Unsafe characters are escaped.
249     *
250     * @param sourceStr
251     *            string to convert to quoted-printable form
252     * @return quoted-printable string
253     * @throws EncoderException
254     *             thrown if a failure condition is encountered during the encoding process.
255     */
256    @Override
257    public String encode(final String sourceStr) throws EncoderException {
258        return encode(sourceStr, getCharset());
259    }
260
261    /**
262     * Encodes a string into its quoted-printable form using the specified Charset. Unsafe characters are escaped.
263     *
264     * @param sourceStr
265     *            string to convert to quoted-printable form
266     * @param sourceCharset
267     *            the Charset for sourceStr
268     * @return quoted-printable string
269     * @throws EncoderException
270     *             thrown if a failure condition is encountered during the encoding process.
271     * @since 1.7
272     */
273    public String encode(final String sourceStr, final Charset sourceCharset) throws EncoderException {
274        return encodeText(sourceStr, sourceCharset);
275    }
276
277    /**
278     * Encodes a string into its quoted-printable form using the specified Charset. Unsafe characters are escaped.
279     *
280     * @param sourceStr
281     *            string to convert to quoted-printable form
282     * @param sourceCharset
283     *            the Charset for sourceStr
284     * @return quoted-printable string
285     * @throws EncoderException
286     *             thrown if a failure condition is encountered during the encoding process.
287     */
288    public String encode(final String sourceStr, final String sourceCharset) throws EncoderException {
289        try {
290            return encodeText(sourceStr, sourceCharset);
291        } catch (final UnsupportedCharsetException e) {
292            throw new EncoderException(e.getMessage(), e);
293        }
294    }
295
296    @Override
297    protected String getEncoding() {
298        return "Q";
299    }
300
301    /**
302     * Tests if optional transformation of SPACE characters is to be used
303     *
304     * @return {@code true} if SPACE characters are to be transformed, {@code false} otherwise
305     */
306    public boolean isEncodeBlanks() {
307        return this.encodeBlanks;
308    }
309
310    /**
311     * Defines whether optional transformation of SPACE characters is to be used
312     *
313     * @param b
314     *            {@code true} if SPACE characters are to be transformed, {@code false} otherwise
315     */
316    public void setEncodeBlanks(final boolean b) {
317        this.encodeBlanks = b;
318    }
319}