OLD | NEW |
1 // Copyright 2001,2007 Alan Donovan. All rights reserved. | 1 // Copyright 2015 The Bazel Authors. All rights reserved. |
2 // | 2 // |
3 // Author: Alan Donovan <adonovan@google.com> | |
4 // | |
5 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
6 // you may not use this file except in compliance with the License. | 4 // you may not use this file except in compliance with the License. |
7 // You may obtain a copy of the License at | 5 // You may obtain a copy of the License at |
8 // | 6 // |
9 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
10 // | 8 // |
11 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
12 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14 // See the License for the specific language governing permissions and | 12 // See the License for the specific language governing permissions and |
(...skipping 10 matching lines...) Expand all Loading... |
25 #include <memory> | 23 #include <memory> |
26 | 24 |
27 #include "third_party/ijar/zip.h" | 25 #include "third_party/ijar/zip.h" |
28 | 26 |
29 namespace devtools_ijar { | 27 namespace devtools_ijar { |
30 | 28 |
31 bool verbose = false; | 29 bool verbose = false; |
32 | 30 |
33 // Reads a JVM class from classdata_in (of the specified length), and | 31 // Reads a JVM class from classdata_in (of the specified length), and |
34 // writes out a simplified class to classdata_out, advancing the | 32 // writes out a simplified class to classdata_out, advancing the |
35 // pointer. | 33 // pointer. Returns true if the class should be kept. |
36 void StripClass(u1 *&classdata_out, const u1 *classdata_in, size_t in_length); | 34 bool StripClass(u1*& classdata_out, const u1* classdata_in, size_t in_length); |
37 | 35 |
38 const char* CLASS_EXTENSION = ".class"; | 36 const char* CLASS_EXTENSION = ".class"; |
39 const size_t CLASS_EXTENSION_LENGTH = strlen(CLASS_EXTENSION); | 37 const size_t CLASS_EXTENSION_LENGTH = strlen(CLASS_EXTENSION); |
40 | 38 |
41 // ZipExtractorProcessor that select only .class file and use | 39 // ZipExtractorProcessor that select only .class file and use |
42 // StripClass to generate an interface class, storing as a new file | 40 // StripClass to generate an interface class, storing as a new file |
43 // in the specified ZipBuilder. | 41 // in the specified ZipBuilder. |
44 class JarStripperProcessor : public ZipExtractorProcessor { | 42 class JarStripperProcessor : public ZipExtractorProcessor { |
45 public: | 43 public: |
46 JarStripperProcessor() {} | 44 JarStripperProcessor() {} |
(...skipping 22 matching lines...) Expand all Loading... |
69 return strcmp(filename + offset, CLASS_EXTENSION) == 0; | 67 return strcmp(filename + offset, CLASS_EXTENSION) == 0; |
70 } | 68 } |
71 return false; | 69 return false; |
72 } | 70 } |
73 | 71 |
74 void JarStripperProcessor::Process(const char* filename, const u4 attr, | 72 void JarStripperProcessor::Process(const char* filename, const u4 attr, |
75 const u1* data, const size_t size) { | 73 const u1* data, const size_t size) { |
76 if (verbose) { | 74 if (verbose) { |
77 fprintf(stderr, "INFO: StripClass: %s\n", filename); | 75 fprintf(stderr, "INFO: StripClass: %s\n", filename); |
78 } | 76 } |
79 u1 *q = builder->NewFile(filename, 0); | 77 u1* buf = reinterpret_cast<u1*>(malloc(size)); |
80 u1 *classdata_out = q; | 78 u1* classdata_out = buf; |
81 StripClass(q, data, size); // actually process it | 79 if (!StripClass(buf, data, size)) { |
82 size_t out_length = q - classdata_out; | 80 free(classdata_out); |
| 81 return; |
| 82 } |
| 83 u1* q = builder->NewFile(filename, 0); |
| 84 size_t out_length = buf - classdata_out; |
| 85 memcpy(q, classdata_out, out_length); |
83 builder->FinishFile(out_length); | 86 builder->FinishFile(out_length); |
| 87 free(classdata_out); |
84 } | 88 } |
85 | 89 |
86 // Opens "file_in" (a .jar file) for reading, and writes an interface | 90 // Opens "file_in" (a .jar file) for reading, and writes an interface |
87 // .jar to "file_out". | 91 // .jar to "file_out". |
88 void OpenFilesAndProcessJar(const char *file_out, const char *file_in) { | 92 void OpenFilesAndProcessJar(const char *file_out, const char *file_in) { |
89 JarStripperProcessor processor; | 93 JarStripperProcessor processor; |
90 std::unique_ptr<ZipExtractor> in(ZipExtractor::Create(file_in, &processor)); | 94 std::unique_ptr<ZipExtractor> in(ZipExtractor::Create(file_in, &processor)); |
91 if (in.get() == NULL) { | 95 if (in.get() == NULL) { |
92 fprintf(stderr, "Unable to open Zip file %s: %s\n", file_in, | 96 fprintf(stderr, "Unable to open Zip file %s: %s\n", file_in, |
93 strerror(errno)); | 97 strerror(errno)); |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
173 } | 177 } |
174 } | 178 } |
175 | 179 |
176 if (devtools_ijar::verbose) { | 180 if (devtools_ijar::verbose) { |
177 fprintf(stderr, "INFO: writing to '%s'.\n", filename_out); | 181 fprintf(stderr, "INFO: writing to '%s'.\n", filename_out); |
178 } | 182 } |
179 | 183 |
180 devtools_ijar::OpenFilesAndProcessJar(filename_out, filename_in); | 184 devtools_ijar::OpenFilesAndProcessJar(filename_out, filename_in); |
181 return 0; | 185 return 0; |
182 } | 186 } |
OLD | NEW |