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