~hjd/ubuntu/wily/xmlgraphics-commons/debian-merged

« back to all changes in this revision

Viewing changes to src/java/org/apache/xmlgraphics/util/io/Base64DecodeStream.java

  • Committer: Hans Joachim Desserud
  • Date: 2015-11-11 18:22:53 UTC
  • mfrom: (9.1.5 sid)
  • Revision ID: hans_joachim_desserud-20151111182253-zwi0frfm97j0wddn
  * Merge from Debian unstable.  Remaining changes:
    - d/control: Drop dependencies required for unit testing as they
      include libmockito-java which would pull maven into main, disable unit
      test execution.

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
 * limitations under the License.
16
16
 */
17
17
 
18
 
/* $Id: Base64DecodeStream.java 1345683 2012-06-03 14:50:33Z gadams $ */
 
18
/* $Id: Base64DecodeStream.java 1610846 2014-07-15 20:44:18Z vhennebert $ */
19
19
 
20
20
package org.apache.xmlgraphics.util.io;
21
21
 
49
49
 *    "Base64DecodeStream: Bad Padding byte (1)."
50
50
 * </pre>
51
51
 *
52
 
 * @version $Id: Base64DecodeStream.java 1345683 2012-06-03 14:50:33Z gadams $
 
52
 * @version $Id: Base64DecodeStream.java 1610846 2014-07-15 20:44:18Z vhennebert $
53
53
 *
54
54
 * Originally authored by Thomas DeWeese, Vincent Hardy, and Chuck McManis.
55
55
 */
62
62
        this.src = src;
63
63
    }
64
64
 
65
 
    private static final byte[] pem_array = new byte[256];
 
65
    private static final byte[] PEM_ARRAY = new byte[256];
66
66
    static {
67
 
        for (int i=0; i<pem_array.length; i++)
68
 
            pem_array[i] = -1;
 
67
        for (int i = 0; i < PEM_ARRAY.length; i++) {
 
68
            PEM_ARRAY[i] = -1;
 
69
        }
69
70
 
70
71
        int idx = 0;
71
 
        for (char c='A'; c<='Z'; c++) {
72
 
            pem_array[c] = (byte)idx++;
73
 
        }
74
 
        for (char c='a'; c<='z'; c++) {
75
 
            pem_array[c] = (byte)idx++;
76
 
        }
77
 
 
78
 
        for (char c='0'; c<='9'; c++) {
79
 
            pem_array[c] = (byte)idx++;
80
 
        }
81
 
 
82
 
        pem_array['+'] = (byte)idx++;
83
 
        pem_array['/'] = (byte)idx++;
 
72
        for (char c = 'A'; c <= 'Z'; c++) {
 
73
            PEM_ARRAY[c] = (byte)idx++;
 
74
        }
 
75
        for (char c = 'a'; c <= 'z'; c++) {
 
76
            PEM_ARRAY[c] = (byte)idx++;
 
77
        }
 
78
 
 
79
        for (char c = '0'; c <= '9'; c++) {
 
80
            PEM_ARRAY[c] = (byte)idx++;
 
81
        }
 
82
 
 
83
        PEM_ARRAY['+'] = (byte)idx++;
 
84
        PEM_ARRAY['/'] = (byte)idx++;
84
85
    }
85
86
 
86
87
    public boolean markSupported() { return false; }
87
88
 
88
89
    public void close()
89
90
        throws IOException {
90
 
        EOF = true;
 
91
        eof = true;
91
92
    }
92
93
 
93
94
    public int available()
94
95
        throws IOException {
95
 
        return 3-out_offset;
 
96
        return 3 - outOffset;
96
97
    }
97
98
 
98
 
    byte[] decode_buffer = new byte[4];
99
 
    byte[] out_buffer = new byte[3];
100
 
    int  out_offset = 3;
101
 
    boolean EOF = false;
 
99
    byte[] decodeBuffer = new byte[4];
 
100
    byte[] outBuffer = new byte[3];
 
101
    int  outOffset = 3;
 
102
    boolean eof;
102
103
 
103
104
    public int read() throws IOException {
104
105
 
105
 
        if (out_offset == 3) {
106
 
            if (EOF || getNextAtom()) {
107
 
                EOF = true;
 
106
        if (outOffset == 3) {
 
107
            if (eof || getNextAtom()) {
 
108
                eof = true;
108
109
                return -1;
109
110
            }
110
111
        }
111
112
 
112
 
        return ((int)out_buffer[out_offset++])&0xFF;
 
113
        return ((int)outBuffer[outOffset++]) & 0xFF;
113
114
    }
114
115
 
115
116
    public int read(byte []out, int offset, int len)
117
118
 
118
119
        int idx = 0;
119
120
        while (idx < len) {
120
 
            if (out_offset == 3) {
121
 
                if (EOF || getNextAtom()) {
122
 
                    EOF = true;
123
 
                    if (idx == 0) return -1;
124
 
                    else          return idx;
 
121
            if (outOffset == 3) {
 
122
                if (eof || getNextAtom()) {
 
123
                    eof = true;
 
124
                    if (idx == 0) {
 
125
                        return -1;
 
126
                    } else {
 
127
                        return idx;
 
128
                    }
125
129
                }
126
130
            }
127
131
 
128
 
            out[offset+idx] = out_buffer[out_offset++];
 
132
            out[offset + idx] = outBuffer[outOffset++];
129
133
 
130
134
            idx++;
131
135
        }
133
137
    }
134
138
 
135
139
    final boolean getNextAtom() throws IOException {
136
 
        int count, a, b, c, d;
 
140
        int count;
 
141
        int a;
 
142
        int b;
 
143
        int c;
 
144
        int d;
137
145
 
138
146
        int off = 0;
139
 
        while(off != 4) {
140
 
            count = src.read(decode_buffer, off, 4-off);
141
 
            if (count == -1)
 
147
        while (off != 4) {
 
148
            count = src.read(decodeBuffer, off, 4 - off);
 
149
            if (count == -1) {
142
150
                return true;
 
151
            }
143
152
 
144
 
            int in=off, out=off;
145
 
            while(in < off+count) {
146
 
                if ((decode_buffer[in] != '\n') &&
147
 
                    (decode_buffer[in] != '\r') &&
148
 
                    (decode_buffer[in] != ' '))
149
 
                    decode_buffer[out++] = decode_buffer[in];
 
153
            int in = off;
 
154
            int out = off;
 
155
            while (in < off + count) {
 
156
                if ((decodeBuffer[in] != '\n')
 
157
                    && (decodeBuffer[in] != '\r')
 
158
                    && (decodeBuffer[in] != ' ')) {
 
159
                    decodeBuffer[out++] = decodeBuffer[in];
 
160
                }
150
161
                in++;
151
162
            }
152
163
 
153
164
            off = out;
154
165
        }
155
166
 
156
 
        a = pem_array[((int)decode_buffer[0])&0xFF];
157
 
        b = pem_array[((int)decode_buffer[1])&0xFF];
158
 
        c = pem_array[((int)decode_buffer[2])&0xFF];
159
 
        d = pem_array[((int)decode_buffer[3])&0xFF];
160
 
 
161
 
        out_buffer[0] = (byte)((a<<2) | (b>>>4));
162
 
        out_buffer[1] = (byte)((b<<4) | (c>>>2));
163
 
        out_buffer[2] = (byte)((c<<6) |  d     );
164
 
 
165
 
        if (decode_buffer[3] != '=') {
 
167
        a = PEM_ARRAY[((int)decodeBuffer[0]) & 0xFF];
 
168
        b = PEM_ARRAY[((int)decodeBuffer[1]) & 0xFF];
 
169
        c = PEM_ARRAY[((int)decodeBuffer[2]) & 0xFF];
 
170
        d = PEM_ARRAY[((int)decodeBuffer[3]) & 0xFF];
 
171
 
 
172
        outBuffer[0] = (byte)((a << 2) | (b >>> 4));
 
173
        outBuffer[1] = (byte)((b << 4) | (c >>> 2));
 
174
        outBuffer[2] = (byte)((c << 6) |  d);
 
175
 
 
176
        if (decodeBuffer[3] != '=') {
166
177
            // All three bytes are good.
167
 
            out_offset=0;
168
 
        } else if (decode_buffer[2] == '=') {
 
178
            outOffset = 0;
 
179
        } else if (decodeBuffer[2] == '=') {
169
180
            // Only one byte of output.
170
 
            out_buffer[2] = out_buffer[0];
171
 
            out_offset = 2;
172
 
            EOF=true;
 
181
            outBuffer[2] = outBuffer[0];
 
182
            outOffset = 2;
 
183
            eof = true;
173
184
        } else {
174
185
            // Only two bytes of output.
175
 
            out_buffer[2] = out_buffer[1];
176
 
            out_buffer[1] = out_buffer[0];
177
 
            out_offset = 1;
178
 
            EOF=true;
 
186
            outBuffer[2] = outBuffer[1];
 
187
            outBuffer[1] = outBuffer[0];
 
188
            outOffset = 1;
 
189
            eof = true;
179
190
        }
180
191
 
181
192
        return false;