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 /**
  26  * @test
  27  * @bug 6732154
  28  * @summary REG: Printing an Image using image/gif doc flavor crashes the VM, Solsparc
  29  *
  30  * @run main/othervm -Xcomp
  31  *      -XX:CompileCommand=compileonly,compiler.c2.Test6732154::ascii85Encode
  32  *      compiler.c2.Test6732154
  33  */
  34 
  35 package compiler.c2;
  36 
  37 public class Test6732154 {
  38 
  39     // Exact copy of sun.print.PSPrinterJob.ascii85Encode([b)[b
  40     private byte[] ascii85Encode(byte[] inArr) {
  41         byte[]  outArr = new byte[((inArr.length+4) * 5 / 4) + 2];
  42         long p1 = 85;
  43         long p2 = p1*p1;
  44         long p3 = p1*p2;
  45         long p4 = p1*p3;
  46         byte pling = '!';
  47 
  48         int i = 0;
  49         int olen = 0;
  50         long val, rem;
  51 
  52         while (i+3 < inArr.length) {
  53             val = ((long)((inArr[i++]&0xff))<<24) +
  54                   ((long)((inArr[i++]&0xff))<<16) +
  55                   ((long)((inArr[i++]&0xff))<< 8) +
  56                   ((long)(inArr[i++]&0xff));
  57             if (val == 0) {
  58                 outArr[olen++] = 'z';
  59             } else {
  60                 rem = val;
  61                 outArr[olen++] = (byte)(rem / p4 + pling); rem = rem % p4;
  62                 outArr[olen++] = (byte)(rem / p3 + pling); rem = rem % p3;
  63                 outArr[olen++] = (byte)(rem / p2 + pling); rem = rem % p2;
  64                 outArr[olen++] = (byte)(rem / p1 + pling); rem = rem % p1;
  65                 outArr[olen++] = (byte)(rem + pling);
  66             }
  67         }
  68         // input not a multiple of 4 bytes, write partial output.
  69         if (i < inArr.length) {
  70             int n = inArr.length - i; // n bytes remain to be written
  71 
  72             val = 0;
  73             while (i < inArr.length) {
  74                 val = (val << 8) + (inArr[i++]&0xff);
  75             }
  76 
  77             int append = 4 - n;
  78             while (append-- > 0) {
  79                 val = val << 8;
  80             }
  81             byte []c = new byte[5];
  82             rem = val;
  83             c[0] = (byte)(rem / p4 + pling); rem = rem % p4;
  84             c[1] = (byte)(rem / p3 + pling); rem = rem % p3;
  85             c[2] = (byte)(rem / p2 + pling); rem = rem % p2;
  86             c[3] = (byte)(rem / p1 + pling); rem = rem % p1;
  87             c[4] = (byte)(rem + pling);
  88 
  89             for (int b = 0; b < n+1 ; b++) {
  90                 outArr[olen++] = c[b];
  91             }
  92         }
  93 
  94         // write EOD marker.
  95         outArr[olen++]='~'; outArr[olen++]='>';
  96 
  97         /* The original intention was to insert a newline after every 78 bytes.
  98          * This was mainly intended for legibility but I decided against this
  99          * partially because of the (small) amount of extra space, and
 100          * partially because for line breaks either would have to hardwire
 101          * ascii 10 (newline) or calculate space in bytes to allocate for
 102          * the platform's newline byte sequence. Also need to be careful
 103          * about where its inserted:
 104          * Ascii 85 decoder ignores white space except for one special case:
 105          * you must ensure you do not split the EOD marker across lines.
 106          */
 107         byte[] retArr = new byte[olen];
 108         System.arraycopy(outArr, 0, retArr, 0, olen);
 109         return retArr;
 110     }
 111 
 112     public static void main(String[] args) {
 113         new Test6732154().ascii85Encode(new byte[0]);
 114         System.out.println("Test passed.");
 115     }
 116 }