/Users/lyon/j4p/src/security/ManifestDigester.java

1    // Decompiled by Jad v1.5.8c. Copyright 2001 Pavel Kouznetsov. 
2    // Jad home page: http://www.geocities.com/kpdus/jad.html 
3    // Decompiler options: packimports(3)  
4    // Source File Name:   ManifestDigester.java 
5     
6    package security; 
7     
8    import java.io.ByteArrayOutputStream; 
9    import java.security.MessageDigest; 
10   import java.util.HashMap; 
11    
12   public class ManifestDigester { 
13       public static class Entry { 
14    
15           public byte[] digest(MessageDigest md) { 
16               md.reset(); 
17               if (oldStyle) 
18                   doOldStyle(md, 
19                              rawBytes, 
20                              offset, 
21                              lengthWithBlankLine); 
22               else 
23                   md.update(rawBytes, 
24                             offset, 
25                             lengthWithBlankLine); 
26               return md.digest(); 
27           } 
28    
29           private void doOldStyle(MessageDigest md, 
30                                   byte bytes[], 
31                                   int offset, 
32                                   int length) { 
33               int i = offset; 
34               int start = offset; 
35               int max = offset + length; 
36               int prev = -1; 
37               for (; i < max; i++) { 
38                   if (bytes[i] == 13 && prev == 32) { 
39                       md.update(bytes, 
40                                 start, 
41                                 i - start - 1); 
42                       start = i; 
43                   } 
44                   prev = bytes[i]; 
45               } 
46    
47               md.update(bytes, start, i - start); 
48           } 
49    
50           public byte[] digestWorkaround( 
51                   MessageDigest md) { 
52               md.reset(); 
53               md.update(rawBytes, offset, length); 
54               return md.digest(); 
55           } 
56    
57           int offset; 
58           int length; 
59           int lengthWithBlankLine; 
60           byte rawBytes[]; 
61           boolean oldStyle; 
62    
63           public Entry(int offset, 
64                        int length, 
65                        int lengthWithBlankLine, 
66                        byte rawBytes[]) { 
67               this.offset = offset; 
68               this.length = length; 
69               this.lengthWithBlankLine = 
70               lengthWithBlankLine; 
71               this.rawBytes = rawBytes; 
72           } 
73       } 
74    
75       static class Position { 
76    
77           int endOfFirstLine; 
78           int endOfSection; 
79           int startOfNext; 
80    
81           Position() { 
82           } 
83       } 
84    
85    
86       private boolean findSection(int offset, 
87                                   Position pos) { 
88           int i = offset; 
89           int len = rawBytes.length; 
90           int last = offset; 
91           boolean allBlank = true; 
92           pos.endOfFirstLine = -1; 
93           for (; i < len; i++) { 
94               byte b = rawBytes[i]; 
95               switch (b) { 
96                   case 13: // '\r' 
97                       if (pos.endOfFirstLine == -1) 
98                           pos.endOfFirstLine = i - 
99                                                1; 
100                      if (i < len && 
101                          rawBytes[i + 1] == 10) 
102                          i++; 
103                      // fall through 
104   
105                  case 10: // '\n' 
106                      if (pos.endOfFirstLine == -1) 
107                          pos.endOfFirstLine = i - 
108                                               1; 
109                      if (allBlank || i == len - 1) { 
110                          if (i == len - 1) 
111                              pos.endOfSection = i; 
112                          else 
113                              pos.endOfSection = 
114                              last; 
115                          pos.startOfNext = i + 1; 
116                          return true; 
117                      } 
118                      last = i; 
119                      allBlank = true; 
120                      break; 
121   
122                  default: 
123                      allBlank = false; 
124                      break; 
125              } 
126          } 
127   
128          return false; 
129      } 
130   
131      public ManifestDigester(byte bytes[]) { 
132          rawBytes = bytes; 
133          entries = new HashMap(); 
134          ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
135          Position pos = new Position(); 
136          if (!findSection(0, pos)) 
137              return; 
138          for (int start = pos.startOfNext; findSection( 
139                  start, pos); start = 
140                               pos.startOfNext) { 
141              int len = (pos.endOfFirstLine - 
142                         start) + 
143                        1; 
144              int sectionLen = (pos.endOfSection - 
145                                start) + 
146                               1; 
147              int sectionLenWithBlank = pos.startOfNext - 
148                                        start; 
149              if (len <= 6 || 
150                  !isNameAttr(bytes, start)) 
151                  continue; 
152              StringBuffer nameBuf = new StringBuffer(); 
153              nameBuf.append( 
154                      new String(bytes, 
155                                 start + 6, 
156                                 len - 6)); 
157              int i = start + len; 
158              if (i - start < sectionLen) 
159                  if (bytes[i] == 13) 
160                      i += 2; 
161                  else 
162                      i++; 
163              while (i - start < sectionLen && 
164                     bytes[i++] == 32) { 
165                  int wrapStart = i; 
166                  while (i - start < sectionLen && 
167                         bytes[i++] != 10) ; 
168                  if (bytes[i - 1] != 10) 
169                      return; 
170                  int wrapLen; 
171                  if (bytes[i - 2] == 13) 
172                      wrapLen = i - wrapStart - 2; 
173                  else 
174                      wrapLen = i - wrapStart - 1; 
175                  nameBuf.append( 
176                          new String(bytes, 
177                                     wrapStart, 
178                                     wrapLen)); 
179              } 
180              String name = nameBuf.toString(); 
181              entries.put(name, 
182                          new Entry(start, 
183                                    sectionLen, 
184                                    sectionLenWithBlank, 
185                                    rawBytes)); 
186          } 
187   
188      } 
189   
190      private boolean isNameAttr(byte bytes[], 
191                                 int start) { 
192          return (bytes[start] == 78 || 
193                  bytes[start] == 110) && 
194                 (bytes[start + 1] == 97 || 
195                  bytes[start + 1] == 65) && 
196                 (bytes[start + 2] == 109 || 
197                  bytes[start + 2] == 77) && 
198                 (bytes[start + 3] == 101 || 
199                  bytes[start + 3] == 69) && 
200                 bytes[start + 4] == 58 && 
201                 bytes[start + 5] == 32; 
202      } 
203   
204      public Entry get(String name, 
205                       boolean oldStyle) { 
206          Entry e = (Entry) entries.get(name); 
207          if (e != null) 
208              e.oldStyle = oldStyle; 
209          return e; 
210      } 
211   
212      public byte[] manifestDigest( 
213              MessageDigest md) { 
214          md.reset(); 
215          md.update(rawBytes, 0, rawBytes.length); 
216          return md.digest(); 
217      } 
218   
219      byte rawBytes[]; 
220      HashMap entries; 
221  } 
222