• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.bouncycastle.asn1;
2 
3 import java.io.ByteArrayInputStream;
4 import java.io.FileInputStream;
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.nio.channels.FileChannel;
8 
9 class StreamUtil
10 {
11     // BEGIN android-removed
12     // private static final long  MAX_MEMORY = Runtime.getRuntime().maxMemory();
13     // END android-removed
14 
15     /**
16      * Find out possible longest length...
17      *
18      * @param in input stream of interest
19      * @return length calculation or MAX_VALUE.
20      */
findLimit(InputStream in)21     static int findLimit(InputStream in)
22     {
23         if (in instanceof LimitedInputStream)
24         {
25             return ((LimitedInputStream)in).getRemaining();
26         }
27         else if (in instanceof ASN1InputStream)
28         {
29             return ((ASN1InputStream)in).getLimit();
30         }
31         else if (in instanceof ByteArrayInputStream)
32         {
33             return ((ByteArrayInputStream)in).available();
34         }
35         else if (in instanceof FileInputStream)
36         {
37             try
38             {
39                 FileChannel channel = ((FileInputStream)in).getChannel();
40                 long  size = (channel != null) ? channel.size() : Integer.MAX_VALUE;
41 
42                 if (size < Integer.MAX_VALUE)
43                 {
44                     return (int)size;
45                 }
46             }
47             catch (IOException e)
48             {
49                 // ignore - they'll find out soon enough!
50             }
51         }
52 
53         // BEGIN android-changed
54         long maxMemory = Runtime.getRuntime().maxMemory();
55         if (maxMemory > Integer.MAX_VALUE)
56         {
57             return Integer.MAX_VALUE;
58         }
59 
60         return (int) maxMemory;
61         // END android-changed
62     }
63 
calculateBodyLength( int length)64     static int calculateBodyLength(
65         int length)
66     {
67         int count = 1;
68 
69         if (length > 127)
70         {
71             int size = 1;
72             int val = length;
73 
74             while ((val >>>= 8) != 0)
75             {
76                 size++;
77             }
78 
79             for (int i = (size - 1) * 8; i >= 0; i -= 8)
80             {
81                 count++;
82             }
83         }
84 
85         return count;
86     }
87 
calculateTagLength(int tagNo)88     static int calculateTagLength(int tagNo)
89         throws IOException
90     {
91         int length = 1;
92 
93         if (tagNo >= 31)
94         {
95             if (tagNo < 128)
96             {
97                 length++;
98             }
99             else
100             {
101                 byte[] stack = new byte[5];
102                 int pos = stack.length;
103 
104                 stack[--pos] = (byte)(tagNo & 0x7F);
105 
106                 do
107                 {
108                     tagNo >>= 7;
109                     stack[--pos] = (byte)(tagNo & 0x7F | 0x80);
110                 }
111                 while (tagNo > 127);
112 
113                 length += stack.length - pos;
114             }
115         }
116 
117         return length;
118     }
119 }
120