1 /*
2  * Copyright (c) 2012, 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.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 /**
25  * @test
26  * @bug 6732154
27  * @summary REG: Printing an Image using image/gif doc flavor crashes the VM, Solsparc
28  *
29  * @run main/othervm -Xcomp
30  *      -XX:CompileCommand=compileonly,compiler.c2.Test6732154::ascii85Encode
31  *      compiler.c2.Test6732154
32  */
33 
34 package compiler.c2;
35 
36 public class Test6732154 {
37 
38     // Exact copy of sun.print.PSPrinterJob.ascii85Encode([b)[b
ascii85Encode(byte[] inArr)39     private byte[] ascii85Encode(byte[] inArr) {
40         byte[]  outArr = new byte[((inArr.length+4) * 5 / 4) + 2];
41         long p1 = 85;
42         long p2 = p1*p1;
43         long p3 = p1*p2;
44         long p4 = p1*p3;
45         byte pling = '!';
46 
47         int i = 0;
48         int olen = 0;
49         long val, rem;
50 
51         while (i+3 < inArr.length) {
52             val = ((long)((inArr[i++]&0xff))<<24) +
53                   ((long)((inArr[i++]&0xff))<<16) +
54                   ((long)((inArr[i++]&0xff))<< 8) +
55                   ((long)(inArr[i++]&0xff));
56             if (val == 0) {
57                 outArr[olen++] = 'z';
58             } else {
59                 rem = val;
60                 outArr[olen++] = (byte)(rem / p4 + pling); rem = rem % p4;
61                 outArr[olen++] = (byte)(rem / p3 + pling); rem = rem % p3;
62                 outArr[olen++] = (byte)(rem / p2 + pling); rem = rem % p2;
63                 outArr[olen++] = (byte)(rem / p1 + pling); rem = rem % p1;
64                 outArr[olen++] = (byte)(rem + pling);
65             }
66         }
67         // input not a multiple of 4 bytes, write partial output.
68         if (i < inArr.length) {
69             int n = inArr.length - i; // n bytes remain to be written
70 
71             val = 0;
72             while (i < inArr.length) {
73                 val = (val << 8) + (inArr[i++]&0xff);
74             }
75 
76             int append = 4 - n;
77             while (append-- > 0) {
78                 val = val << 8;
79             }
80             byte []c = new byte[5];
81             rem = val;
82             c[0] = (byte)(rem / p4 + pling); rem = rem % p4;
83             c[1] = (byte)(rem / p3 + pling); rem = rem % p3;
84             c[2] = (byte)(rem / p2 + pling); rem = rem % p2;
85             c[3] = (byte)(rem / p1 + pling); rem = rem % p1;
86             c[4] = (byte)(rem + pling);
87 
88             for (int b = 0; b < n+1 ; b++) {
89                 outArr[olen++] = c[b];
90             }
91         }
92 
93         // write EOD marker.
94         outArr[olen++]='~'; outArr[olen++]='>';
95 
96         /* The original intention was to insert a newline after every 78 bytes.
97          * This was mainly intended for legibility but I decided against this
98          * partially because of the (small) amount of extra space, and
99          * partially because for line breaks either would have to hardwire
100          * ascii 10 (newline) or calculate space in bytes to allocate for
101          * the platform's newline byte sequence. Also need to be careful
102          * about where its inserted:
103          * Ascii 85 decoder ignores white space except for one special case:
104          * you must ensure you do not split the EOD marker across lines.
105          */
106         byte[] retArr = new byte[olen];
107         System.arraycopy(outArr, 0, retArr, 0, olen);
108         return retArr;
109     }
110 
main(String[] args)111     public static void main(String[] args) {
112         new Test6732154().ascii85Encode(new byte[0]);
113         System.out.println("Test passed.");
114     }
115 }
116