ijar.cc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. // Copyright 2015 The Bazel Authors. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. // ijar.cpp -- .jar -> _interface.jar tool.
  16. //
  17. #include <errno.h>
  18. #include <limits.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <memory>
  23. #include "third_party/ijar/zip.h"
  24. namespace devtools_ijar {
  25. bool verbose = false;
  26. // Reads a JVM class from classdata_in (of the specified length), and
  27. // writes out a simplified class to classdata_out, advancing the
  28. // pointer. Returns true if the class should be kept.
  29. bool StripClass(u1 *&classdata_out, const u1 *classdata_in, size_t in_length);
  30. const char *CLASS_EXTENSION = ".class";
  31. const size_t CLASS_EXTENSION_LENGTH = strlen(CLASS_EXTENSION);
  32. const char *KOTLIN_MODULE_EXTENSION = ".kotlin_module";
  33. const size_t KOTLIN_MODULE_EXTENSION_LENGTH = strlen(KOTLIN_MODULE_EXTENSION);
  34. const char *SCALA_TASTY_EXTENSION = ".tasty";
  35. const size_t SCALA_TASTY_EXTENSION_LENGTH = strlen(SCALA_TASTY_EXTENSION);
  36. const char *MANIFEST_DIR_PATH = "META-INF/";
  37. const size_t MANIFEST_DIR_PATH_LENGTH = strlen(MANIFEST_DIR_PATH);
  38. const char *MANIFEST_PATH = "META-INF/MANIFEST.MF";
  39. const size_t MANIFEST_PATH_LENGTH = strlen(MANIFEST_PATH);
  40. const char *MANIFEST_HEADER =
  41. "Manifest-Version: 1.0\r\n"
  42. "Created-By: bazel\r\n";
  43. const size_t MANIFEST_HEADER_LENGTH = strlen(MANIFEST_HEADER);
  44. // These attributes are used by JavaBuilder, Turbine, and ijar.
  45. // They must all be kept in sync.
  46. const char *TARGET_LABEL_KEY = "Target-Label: ";
  47. const size_t TARGET_LABEL_KEY_LENGTH = strlen(TARGET_LABEL_KEY);
  48. const char *INJECTING_RULE_KIND_KEY = "Injecting-Rule-Kind: ";
  49. const size_t INJECTING_RULE_KIND_KEY_LENGTH = strlen(INJECTING_RULE_KIND_KEY);
  50. const char *DUMMY_FILE = "dummy";
  51. const size_t DUMMY_PATH_LENGTH = strlen(DUMMY_FILE);
  52. // The size of an output jar containing only an empty dummy file:
  53. const size_t JAR_WITH_DUMMY_FILE_SIZE = 98ull + 2 * DUMMY_PATH_LENGTH;
  54. class JarExtractorProcessor : public ZipExtractorProcessor {
  55. public:
  56. // Set the ZipBuilder to add the ijar class to the output zip file.
  57. // This pointer should not be deleted while this class is still in use and
  58. // it should be set before any call to the Process() method.
  59. void SetZipBuilder(ZipBuilder *builder) { this->builder_ = builder; }
  60. virtual void WriteManifest(const char *target_label,
  61. const char *injecting_rule_kind) = 0;
  62. protected:
  63. // Not owned by JarStripperProcessor, see SetZipBuilder().
  64. ZipBuilder *builder_;
  65. };
  66. // ZipExtractorProcessor that select only .class file and use
  67. // StripClass to generate an interface class, storing as a new file
  68. // in the specified ZipBuilder.
  69. class JarStripperProcessor : public JarExtractorProcessor {
  70. public:
  71. JarStripperProcessor() {}
  72. virtual ~JarStripperProcessor() {}
  73. virtual void Process(const char *filename, const u4 attr, const u1 *data,
  74. const size_t size);
  75. virtual bool Accept(const char *filename, const u4 attr);
  76. virtual void WriteManifest(const char *target_label,
  77. const char *injecting_rule_kind);
  78. };
  79. static bool StartsWith(const char *str, const size_t str_len,
  80. const char *prefix, const size_t prefix_len) {
  81. return str_len >= prefix_len && strncmp(str, prefix, prefix_len) == 0;
  82. }
  83. static bool EndsWith(const char *str, const size_t str_len, const char *suffix,
  84. const size_t suffix_len) {
  85. return str_len >= suffix_len &&
  86. strcmp(str + str_len - suffix_len, suffix) == 0;
  87. }
  88. static bool IsKotlinModule(const char *filename, const size_t filename_len) {
  89. return StartsWith(filename, filename_len, MANIFEST_DIR_PATH,
  90. MANIFEST_DIR_PATH_LENGTH) &&
  91. EndsWith(filename, filename_len, KOTLIN_MODULE_EXTENSION,
  92. KOTLIN_MODULE_EXTENSION_LENGTH);
  93. }
  94. static bool IsScalaTasty(const char *filename, const size_t filename_len) {
  95. return EndsWith(filename, filename_len, SCALA_TASTY_EXTENSION,
  96. SCALA_TASTY_EXTENSION_LENGTH);
  97. }
  98. bool JarStripperProcessor::Accept(const char *filename, const u4 /*attr*/) {
  99. const size_t filename_len = strlen(filename);
  100. if (IsKotlinModule(filename, filename_len) ||
  101. IsScalaTasty(filename, filename_len)) {
  102. return true;
  103. }
  104. if (filename_len < CLASS_EXTENSION_LENGTH ||
  105. strcmp(filename + filename_len - CLASS_EXTENSION_LENGTH,
  106. CLASS_EXTENSION) != 0) {
  107. return false;
  108. }
  109. return true;
  110. }
  111. static bool IsModuleInfo(const char *filename) {
  112. const char *slash = strrchr(filename, '/');
  113. if (slash == NULL) {
  114. slash = filename;
  115. } else {
  116. slash++;
  117. }
  118. return strcmp(slash, "module-info.class") == 0;
  119. }
  120. void JarStripperProcessor::Process(const char *filename, const u4 /*attr*/,
  121. const u1 *data, const size_t size) {
  122. if (verbose) {
  123. fprintf(stderr, "INFO: StripClass: %s\n", filename);
  124. }
  125. if (IsModuleInfo(filename) || IsKotlinModule(filename, strlen(filename)) ||
  126. IsScalaTasty(filename, strlen(filename))) {
  127. u1 *q = builder_->NewFile(filename, 0);
  128. memcpy(q, data, size);
  129. builder_->FinishFile(size, /* compress: */ false, /* compute_crc: */ true);
  130. } else {
  131. u1 *buf = reinterpret_cast<u1 *>(malloc(size));
  132. u1 *classdata_out = buf;
  133. if (!StripClass(buf, data, size)) {
  134. free(classdata_out);
  135. return;
  136. }
  137. u1 *q = builder_->NewFile(filename, 0);
  138. size_t out_length = buf - classdata_out;
  139. memcpy(q, classdata_out, out_length);
  140. builder_->FinishFile(out_length, /* compress: */ false,
  141. /* compute_crc: */ true);
  142. free(classdata_out);
  143. }
  144. }
  145. // Copies the string into the buffer without the null terminator, returns
  146. // updated buffer pointer
  147. static u1 *WriteStr(u1 *buf, const char *str) {
  148. size_t len = strlen(str);
  149. memcpy(buf, str, len);
  150. return buf + len;
  151. }
  152. // Writes a manifest attribute including a "\r\n" line break, returns updated
  153. // buffer pointer.
  154. static u1 *WriteManifestAttr(u1 *buf, const char *key, const char *val) {
  155. buf = WriteStr(buf, key);
  156. buf = WriteStr(buf, val);
  157. *buf++ = '\r';
  158. *buf++ = '\n';
  159. return buf;
  160. }
  161. void JarStripperProcessor::WriteManifest(const char *target_label,
  162. const char *injecting_rule_kind) {
  163. if (target_label == nullptr) {
  164. return;
  165. }
  166. builder_->WriteEmptyFile(MANIFEST_DIR_PATH);
  167. u1 *start = builder_->NewFile(MANIFEST_PATH, 0);
  168. u1 *buf = start;
  169. buf = WriteStr(buf, MANIFEST_HEADER);
  170. buf = WriteManifestAttr(buf, TARGET_LABEL_KEY, target_label);
  171. if (injecting_rule_kind) {
  172. buf = WriteManifestAttr(buf, INJECTING_RULE_KIND_KEY, injecting_rule_kind);
  173. }
  174. size_t total_len = buf - start;
  175. builder_->FinishFile(total_len, /* compress: */ false,
  176. /* compute_crc: */ true);
  177. }
  178. class JarCopierProcessor : public JarExtractorProcessor {
  179. public:
  180. JarCopierProcessor(const char *jar) : jar_(jar) {}
  181. virtual ~JarCopierProcessor() {}
  182. virtual void Process(const char *filename, const u4 /*attr*/, const u1 *data,
  183. const size_t size);
  184. virtual bool Accept(const char *filename, const u4 /*attr*/);
  185. virtual void WriteManifest(const char *target_label,
  186. const char *injecting_rule_kind);
  187. private:
  188. class ManifestLocator : public ZipExtractorProcessor {
  189. public:
  190. ManifestLocator() : manifest_buf_(nullptr), manifest_size_(0) {}
  191. virtual ~ManifestLocator() { free(manifest_buf_); }
  192. u1 *manifest_buf_;
  193. size_t manifest_size_;
  194. virtual bool Accept(const char *filename, const u4 /*attr*/) {
  195. return strcmp(filename, MANIFEST_PATH) == 0;
  196. }
  197. virtual void Process(const char * /*filename*/, const u4 /*attr*/,
  198. const u1 *data, const size_t size) {
  199. manifest_buf_ = (u1 *)malloc(size);
  200. memmove(manifest_buf_, data, size);
  201. manifest_size_ = size;
  202. }
  203. };
  204. const char *jar_;
  205. u1 *AppendTargetLabelToManifest(u1 *buf, const u1 *manifest_data,
  206. const size_t size, const char *target_label,
  207. const char *injecting_rule_kind);
  208. };
  209. void JarCopierProcessor::Process(const char *filename, const u4 /*attr*/,
  210. const u1 *data, const size_t size) {
  211. if (verbose) {
  212. fprintf(stderr, "INFO: CopyFile: %s\n", filename);
  213. }
  214. // We already handled the manifest in WriteManifest
  215. if (strcmp(filename, MANIFEST_DIR_PATH) == 0 ||
  216. strcmp(filename, MANIFEST_PATH) == 0) {
  217. return;
  218. }
  219. u1 *q = builder_->NewFile(filename, 0);
  220. memcpy(q, data, size);
  221. builder_->FinishFile(size, /* compress: */ false, /* compute_crc: */ true);
  222. }
  223. bool JarCopierProcessor::Accept(const char * /*filename*/, const u4 /*attr*/) {
  224. return true;
  225. }
  226. void JarCopierProcessor::WriteManifest(const char *target_label,
  227. const char *injecting_rule_kind) {
  228. ManifestLocator manifest_locator;
  229. std::unique_ptr<ZipExtractor> in(
  230. ZipExtractor::Create(jar_, &manifest_locator));
  231. in->ProcessAll();
  232. bool wants_manifest =
  233. manifest_locator.manifest_buf_ != nullptr || target_label != nullptr;
  234. if (wants_manifest) {
  235. builder_->WriteEmptyFile(MANIFEST_DIR_PATH);
  236. u1 *start = builder_->NewFile(MANIFEST_PATH, 0);
  237. u1 *buf = start;
  238. // Three cases:
  239. // 1. We need to merge the target label into a pre-existing manifest
  240. // 2. Write a manifest from scratch with a target label
  241. // 3. Copy existing manifest without adding target label
  242. if (manifest_locator.manifest_buf_ != nullptr && target_label != nullptr) {
  243. buf = AppendTargetLabelToManifest(buf, manifest_locator.manifest_buf_,
  244. manifest_locator.manifest_size_,
  245. target_label, injecting_rule_kind);
  246. } else if (target_label != nullptr) {
  247. buf = WriteStr(buf, MANIFEST_HEADER);
  248. buf = WriteManifestAttr(buf, TARGET_LABEL_KEY, target_label);
  249. if (injecting_rule_kind) {
  250. buf = WriteManifestAttr(buf, INJECTING_RULE_KIND_KEY,
  251. injecting_rule_kind);
  252. }
  253. } else {
  254. memcpy(buf, manifest_locator.manifest_buf_,
  255. manifest_locator.manifest_size_);
  256. buf += manifest_locator.manifest_size_;
  257. }
  258. size_t total_len = buf - start;
  259. builder_->FinishFile(total_len, /* compress: */ false,
  260. /* compute_crc: */ true);
  261. }
  262. }
  263. u1 *JarCopierProcessor::AppendTargetLabelToManifest(
  264. u1 *buf, const u1 *manifest_data, const size_t size,
  265. const char *target_label, const char *injecting_rule_kind) {
  266. const char *line_start = (const char *)manifest_data;
  267. const char *data_end = (const char *)manifest_data + size;
  268. // Write main attributes part
  269. while (line_start < data_end && line_start[0] != '\r' &&
  270. line_start[0] != '\n') {
  271. const char *line_end = strchr(line_start, '\n');
  272. // Go past return char to point to next line, or to end of data buffer
  273. line_end = line_end != nullptr ? line_end + 1 : data_end;
  274. // Copy line unless it's Target-Label/Injecting-Rule-Kind and we're writing
  275. // that ourselves
  276. if (strncmp(line_start, TARGET_LABEL_KEY, TARGET_LABEL_KEY_LENGTH) != 0 &&
  277. strncmp(line_start, INJECTING_RULE_KIND_KEY,
  278. INJECTING_RULE_KIND_KEY_LENGTH) != 0) {
  279. size_t len = line_end - line_start;
  280. memcpy(buf, line_start, len);
  281. buf += len;
  282. }
  283. line_start = line_end;
  284. }
  285. // Append target label and, if given, rule kind
  286. buf = WriteManifestAttr(buf, TARGET_LABEL_KEY, target_label);
  287. if (injecting_rule_kind != nullptr) {
  288. buf = WriteManifestAttr(buf, INJECTING_RULE_KIND_KEY, injecting_rule_kind);
  289. }
  290. // Write the rest of the manifest file
  291. size_t sections_len = data_end - line_start;
  292. if (sections_len > 0) {
  293. memcpy(buf, line_start, sections_len);
  294. buf += sections_len;
  295. }
  296. return buf;
  297. }
  298. // WriteManifest, including zip file format overhead.
  299. static size_t EstimateManifestOutputSize(const char *target_label,
  300. const char *injecting_rule_kind) {
  301. if (target_label == nullptr) {
  302. return 0;
  303. }
  304. // local headers
  305. size_t length = 30 * 2 + MANIFEST_DIR_PATH_LENGTH + MANIFEST_PATH_LENGTH;
  306. // central directory
  307. length += 46 * 2 + MANIFEST_DIR_PATH_LENGTH + MANIFEST_PATH_LENGTH;
  308. // zip64 EOCD entries
  309. length += 56 * 2;
  310. // manifest content
  311. length += MANIFEST_HEADER_LENGTH;
  312. // target label manifest entry, including newline
  313. length += TARGET_LABEL_KEY_LENGTH + strlen(target_label) + 2;
  314. if (injecting_rule_kind) {
  315. // injecting rule kind manifest entry, including newline
  316. length += INJECTING_RULE_KIND_KEY_LENGTH + strlen(injecting_rule_kind) + 2;
  317. }
  318. return length;
  319. }
  320. // Opens "file_in" (a .jar file) for reading, and writes an interface
  321. // .jar to "file_out".
  322. static void OpenFilesAndProcessJar(const char *file_out, const char *file_in,
  323. bool strip_jar, const char *target_label,
  324. const char *injecting_rule_kind) {
  325. std::unique_ptr<JarExtractorProcessor> processor;
  326. if (strip_jar) {
  327. processor =
  328. std::unique_ptr<JarExtractorProcessor>(new JarStripperProcessor());
  329. } else {
  330. processor =
  331. std::unique_ptr<JarExtractorProcessor>(new JarCopierProcessor(file_in));
  332. }
  333. std::unique_ptr<ZipExtractor> in(
  334. ZipExtractor::Create(file_in, processor.get()));
  335. if (in == NULL) {
  336. fprintf(stderr, "Unable to open Zip file %s: %s\n", file_in,
  337. strerror(errno));
  338. abort();
  339. }
  340. u8 output_length = in->CalculateOutputLength();
  341. if (output_length < JAR_WITH_DUMMY_FILE_SIZE) {
  342. output_length = JAR_WITH_DUMMY_FILE_SIZE;
  343. }
  344. output_length +=
  345. EstimateManifestOutputSize(target_label, injecting_rule_kind);
  346. std::unique_ptr<ZipBuilder> out(ZipBuilder::Create(file_out, output_length));
  347. if (out == NULL) {
  348. fprintf(stderr, "Unable to open output file %s: %s\n", file_out,
  349. strerror(errno));
  350. abort();
  351. }
  352. processor->SetZipBuilder(out.get());
  353. processor->WriteManifest(target_label, injecting_rule_kind);
  354. // Process all files in the zip
  355. if (in->ProcessAll() < 0) {
  356. fprintf(stderr, "%s\n", in->GetError());
  357. abort();
  358. }
  359. // Add dummy file, since javac doesn't like truly empty jars.
  360. if (out->GetNumberFiles() == 0) {
  361. out->WriteEmptyFile(DUMMY_FILE);
  362. }
  363. // Finish writing the output file
  364. if (out->Finish() < 0) {
  365. fprintf(stderr, "%s\n", out->GetError());
  366. abort();
  367. }
  368. // Get all file size
  369. size_t in_length = in->GetSize();
  370. size_t out_length = out->GetSize();
  371. if (verbose) {
  372. fprintf(stderr, "INFO: produced interface jar: %s -> %s (%d%%).\n", file_in,
  373. file_out, static_cast<int>(100.0 * out_length / in_length));
  374. }
  375. }
  376. } // namespace devtools_ijar
  377. //
  378. // main method
  379. //
  380. static void usage() {
  381. fprintf(stderr,
  382. "Usage: ijar "
  383. "[-v] [--[no]strip_jar] "
  384. "[--target label label] [--injecting_rule_kind kind] "
  385. "x.jar [x_interface.jar>]\n");
  386. fprintf(stderr, "Creates an interface jar from the specified jar file.\n");
  387. exit(1);
  388. }
  389. int main(int argc, char **argv) {
  390. bool strip_jar = true;
  391. const char *target_label = NULL;
  392. const char *injecting_rule_kind = NULL;
  393. const char *filename_in = NULL;
  394. const char *filename_out = NULL;
  395. for (int ii = 1; ii < argc; ++ii) {
  396. if (strcmp(argv[ii], "-v") == 0) {
  397. devtools_ijar::verbose = true;
  398. } else if (strcmp(argv[ii], "--strip_jar") == 0) {
  399. strip_jar = true;
  400. } else if (strcmp(argv[ii], "--nostrip_jar") == 0) {
  401. strip_jar = false;
  402. } else if (strcmp(argv[ii], "--target_label") == 0) {
  403. if (++ii >= argc) {
  404. usage();
  405. }
  406. target_label = argv[ii];
  407. } else if (strcmp(argv[ii], "--injecting_rule_kind") == 0) {
  408. if (++ii >= argc) {
  409. usage();
  410. }
  411. injecting_rule_kind = argv[ii];
  412. } else if (filename_in == NULL) {
  413. filename_in = argv[ii];
  414. } else if (filename_out == NULL) {
  415. filename_out = argv[ii];
  416. } else {
  417. usage();
  418. }
  419. }
  420. if (filename_in == NULL) {
  421. usage();
  422. }
  423. // Guess output filename from input:
  424. char filename_out_buf[PATH_MAX];
  425. if (filename_out == NULL) {
  426. size_t len = strlen(filename_in);
  427. if (len > 4 && strncmp(filename_in + len - 4, ".jar", 4) == 0) {
  428. strcpy(filename_out_buf, filename_in);
  429. strcpy(filename_out_buf + len - 4, "-interface.jar");
  430. filename_out = filename_out_buf;
  431. } else {
  432. fprintf(stderr,
  433. "Can't determine output filename since input filename "
  434. "doesn't end with '.jar'.\n");
  435. return 1;
  436. }
  437. }
  438. if (devtools_ijar::verbose) {
  439. fprintf(stderr, "INFO: writing to '%s'.\n", filename_out);
  440. }
  441. devtools_ijar::OpenFilesAndProcessJar(filename_out, filename_in, strip_jar,
  442. target_label, injecting_rule_kind);
  443. return 0;
  444. }