J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2001-2005 Sun Microsystems, Inc. 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. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun in the LICENSE file that accompanied this code. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | * accompanied this code). |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License version |
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | // -*- C++ -*- |
| 27 | // Program for unpacking specially compressed Java packages. |
| 28 | // John R. Rose |
| 29 | |
| 30 | #include <sys/types.h> |
| 31 | |
| 32 | #include <stdio.h> |
| 33 | #include <string.h> |
| 34 | #include <stdlib.h> |
| 35 | #include <stdarg.h> |
| 36 | |
| 37 | #include <limits.h> |
| 38 | #include <time.h> |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | #include "defines.h" |
| 44 | #include "bytes.h" |
| 45 | #include "utils.h" |
| 46 | #include "coding.h" |
| 47 | #include "bands.h" |
| 48 | |
| 49 | #include "constants.h" |
| 50 | |
| 51 | #include "zip.h" |
| 52 | |
| 53 | #include "unpack.h" |
| 54 | |
| 55 | |
| 56 | // tags, in canonical order: |
| 57 | static const byte TAGS_IN_ORDER[] = { |
| 58 | CONSTANT_Utf8, |
| 59 | CONSTANT_Integer, |
| 60 | CONSTANT_Float, |
| 61 | CONSTANT_Long, |
| 62 | CONSTANT_Double, |
| 63 | CONSTANT_String, |
| 64 | CONSTANT_Class, |
| 65 | CONSTANT_Signature, |
| 66 | CONSTANT_NameandType, |
| 67 | CONSTANT_Fieldref, |
| 68 | CONSTANT_Methodref, |
| 69 | CONSTANT_InterfaceMethodref |
| 70 | }; |
| 71 | #define N_TAGS_IN_ORDER (sizeof TAGS_IN_ORDER) |
| 72 | |
| 73 | #ifndef PRODUCT |
| 74 | static const char* TAG_NAME[] = { |
| 75 | "*None", |
| 76 | "Utf8", |
| 77 | "*Unicode", |
| 78 | "Integer", |
| 79 | "Float", |
| 80 | "Long", |
| 81 | "Double", |
| 82 | "Class", |
| 83 | "String", |
| 84 | "Fieldref", |
| 85 | "Methodref", |
| 86 | "InterfaceMethodref", |
| 87 | "NameandType", |
| 88 | "*Signature", |
| 89 | 0 |
| 90 | }; |
| 91 | |
| 92 | static const char* ATTR_CONTEXT_NAME[] = { // match ATTR_CONTEXT_NAME, etc. |
| 93 | "class", "field", "method", "code" |
| 94 | }; |
| 95 | |
| 96 | #else |
| 97 | |
| 98 | #define ATTR_CONTEXT_NAME ((const char**)null) |
| 99 | |
| 100 | #endif |
| 101 | |
| 102 | |
| 103 | // REQUESTED must be -2 for u2 and REQUESTED_LDC must be -1 for u1 |
| 104 | enum { NOT_REQUESTED = 0, REQUESTED = -2, REQUESTED_LDC = -1 }; |
| 105 | |
| 106 | #define NO_INORD ((uint)-1) |
| 107 | |
| 108 | struct entry { |
| 109 | byte tag; |
| 110 | |
| 111 | #if 0 |
| 112 | byte bits; |
| 113 | enum { |
| 114 | //EB_EXTRA = 1, |
| 115 | EB_SUPER = 2 |
| 116 | }; |
| 117 | #endif |
| 118 | unsigned short nrefs; // pack w/ tag |
| 119 | |
| 120 | int outputIndex; |
| 121 | uint inord; // &cp.entries[cp.tag_base[this->tag]+this->inord] == this |
| 122 | |
| 123 | entry* *refs; |
| 124 | |
| 125 | // put last to pack best |
| 126 | union { |
| 127 | bytes b; |
| 128 | int i; |
| 129 | jlong l; |
| 130 | } value; |
| 131 | |
| 132 | void requestOutputIndex(cpool& cp, int req = REQUESTED); |
| 133 | int getOutputIndex() { |
| 134 | assert(outputIndex > NOT_REQUESTED); |
| 135 | return outputIndex; |
| 136 | } |
| 137 | |
| 138 | entry* ref(int refnum) { |
| 139 | assert((uint)refnum < nrefs); |
| 140 | return refs[refnum]; |
| 141 | } |
| 142 | |
| 143 | const char* utf8String() { |
| 144 | assert(tagMatches(CONSTANT_Utf8)); |
| 145 | assert(value.b.len == strlen((const char*)value.b.ptr)); |
| 146 | return (const char*)value.b.ptr; |
| 147 | } |
| 148 | |
| 149 | entry* className() { |
| 150 | assert(tagMatches(CONSTANT_Class)); |
| 151 | return ref(0); |
| 152 | } |
| 153 | |
| 154 | entry* memberClass() { |
| 155 | assert(tagMatches(CONSTANT_Member)); |
| 156 | return ref(0); |
| 157 | } |
| 158 | |
| 159 | entry* memberDescr() { |
| 160 | assert(tagMatches(CONSTANT_Member)); |
| 161 | return ref(1); |
| 162 | } |
| 163 | |
| 164 | entry* descrName() { |
| 165 | assert(tagMatches(CONSTANT_NameandType)); |
| 166 | return ref(0); |
| 167 | } |
| 168 | |
| 169 | entry* descrType() { |
| 170 | assert(tagMatches(CONSTANT_NameandType)); |
| 171 | return ref(1); |
| 172 | } |
| 173 | |
| 174 | int typeSize(); |
| 175 | |
| 176 | bytes& asUtf8(); |
| 177 | int asInteger() { assert(tag == CONSTANT_Integer); return value.i; } |
| 178 | |
| 179 | bool isUtf8(bytes& b) { return tagMatches(CONSTANT_Utf8) && value.b.equals(b); } |
| 180 | |
| 181 | bool isDoubleWord() { return tag == CONSTANT_Double || tag == CONSTANT_Long; } |
| 182 | |
| 183 | bool tagMatches(byte tag2) { |
| 184 | return (tag2 == tag) |
| 185 | || (tag2 == CONSTANT_Utf8 && tag == CONSTANT_Signature) |
| 186 | #ifndef PRODUCT |
| 187 | || (tag2 == CONSTANT_Literal |
| 188 | && tag >= CONSTANT_Integer && tag <= CONSTANT_String && tag != CONSTANT_Class) |
| 189 | || (tag2 == CONSTANT_Member |
| 190 | && tag >= CONSTANT_Fieldref && tag <= CONSTANT_InterfaceMethodref) |
| 191 | #endif |
| 192 | ; |
| 193 | } |
| 194 | |
| 195 | #ifdef PRODUCT |
| 196 | char* string() { return 0; } |
| 197 | #else |
| 198 | char* string(); // see far below |
| 199 | #endif |
| 200 | }; |
| 201 | |
| 202 | entry* cpindex::get(uint i) { |
| 203 | if (i >= len) |
| 204 | return null; |
| 205 | else if (base1 != null) |
| 206 | // primary index |
| 207 | return &base1[i]; |
| 208 | else |
| 209 | // secondary index |
| 210 | return base2[i]; |
| 211 | } |
| 212 | |
| 213 | inline bytes& entry::asUtf8() { |
| 214 | assert(tagMatches(CONSTANT_Utf8)); |
| 215 | return value.b; |
| 216 | } |
| 217 | |
| 218 | int entry::typeSize() { |
| 219 | assert(tagMatches(CONSTANT_Utf8)); |
| 220 | const char* sigp = (char*) value.b.ptr; |
| 221 | switch (*sigp) { |
| 222 | case '(': sigp++; break; // skip opening '(' |
| 223 | case 'D': |
| 224 | case 'J': return 2; // double field |
| 225 | default: return 1; // field |
| 226 | } |
| 227 | int siglen = 0; |
| 228 | for (;;) { |
| 229 | int ch = *sigp++; |
| 230 | switch (ch) { |
| 231 | case 'D': case 'J': |
| 232 | siglen += 1; |
| 233 | break; |
| 234 | case '[': |
| 235 | // Skip rest of array info. |
| 236 | while (ch == '[') { ch = *sigp++; } |
| 237 | if (ch != 'L') break; |
| 238 | // else fall through |
| 239 | case 'L': |
| 240 | sigp = strchr(sigp, ';'); |
| 241 | if (sigp == null) { |
| 242 | unpack_abort("bad data"); |
| 243 | return 0; |
| 244 | } |
| 245 | sigp += 1; |
| 246 | break; |
| 247 | case ')': // closing ')' |
| 248 | return siglen; |
| 249 | } |
| 250 | siglen += 1; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | inline cpindex* cpool::getFieldIndex(entry* classRef) { |
| 255 | assert(classRef->tagMatches(CONSTANT_Class)); |
| 256 | assert((uint)classRef->inord < tag_count[CONSTANT_Class]); |
| 257 | return &member_indexes[classRef->inord*2+0]; |
| 258 | } |
| 259 | inline cpindex* cpool::getMethodIndex(entry* classRef) { |
| 260 | assert(classRef->tagMatches(CONSTANT_Class)); |
| 261 | assert((uint)classRef->inord < tag_count[CONSTANT_Class]); |
| 262 | return &member_indexes[classRef->inord*2+1]; |
| 263 | } |
| 264 | |
| 265 | struct inner_class { |
| 266 | entry* inner; |
| 267 | entry* outer; |
| 268 | entry* name; |
| 269 | int flags; |
| 270 | inner_class* next_sibling; |
| 271 | bool requested; |
| 272 | }; |
| 273 | |
| 274 | // Here is where everything gets deallocated: |
| 275 | void unpacker::free() { |
| 276 | int i; |
| 277 | assert(jniobj == null); // caller resp. |
| 278 | assert(infileptr == null); // caller resp. |
| 279 | if (jarout != null) jarout->reset(); |
| 280 | if (gzin != null) { gzin->free(); gzin = null; } |
| 281 | if (free_input) input.free(); |
| 282 | // free everybody ever allocated with U_NEW or (recently) with T_NEW |
| 283 | assert(smallbuf.base() == null || mallocs.contains(smallbuf.base())); |
| 284 | assert(tsmallbuf.base() == null || tmallocs.contains(tsmallbuf.base())); |
| 285 | mallocs.freeAll(); |
| 286 | tmallocs.freeAll(); |
| 287 | smallbuf.init(); |
| 288 | tsmallbuf.init(); |
| 289 | bcimap.free(); |
| 290 | class_fixup_type.free(); |
| 291 | class_fixup_offset.free(); |
| 292 | class_fixup_ref.free(); |
| 293 | code_fixup_type.free(); |
| 294 | code_fixup_offset.free(); |
| 295 | code_fixup_source.free(); |
| 296 | requested_ics.free(); |
| 297 | cur_classfile_head.free(); |
| 298 | cur_classfile_tail.free(); |
| 299 | for (i = 0; i < ATTR_CONTEXT_LIMIT; i++) |
| 300 | attr_defs[i].free(); |
| 301 | |
| 302 | // free CP state |
| 303 | cp.outputEntries.free(); |
| 304 | for (i = 0; i < CONSTANT_Limit; i++) |
| 305 | cp.tag_extras[i].free(); |
| 306 | } |
| 307 | |
| 308 | // input handling |
| 309 | // Attempts to advance rplimit so that (rplimit-rp) is at least 'more'. |
| 310 | // Will eagerly read ahead by larger chunks, if possible. |
| 311 | // Returns false if (rplimit-rp) is not at least 'more', |
| 312 | // unless rplimit hits input.limit(). |
| 313 | bool unpacker::ensure_input(jlong more) { |
| 314 | julong want = more - input_remaining(); |
| 315 | if ((jlong)want <= 0) return true; // it's already in the buffer |
| 316 | if (rplimit == input.limit()) return true; // not expecting any more |
| 317 | |
| 318 | if (read_input_fn == null) { |
| 319 | // assume it is already all there |
| 320 | bytes_read += input.limit() - rplimit; |
| 321 | rplimit = input.limit(); |
| 322 | return true; |
| 323 | } |
| 324 | CHECK_0; |
| 325 | |
| 326 | julong remaining = (input.limit() - rplimit); // how much left to read? |
| 327 | byte* rpgoal = (want >= remaining)? input.limit(): rplimit + (size_t)want; |
| 328 | enum { CHUNK_SIZE = (1<<14) }; |
| 329 | julong fetch = want; |
| 330 | if (fetch < CHUNK_SIZE) |
| 331 | fetch = CHUNK_SIZE; |
| 332 | if (fetch > remaining*3/4) |
| 333 | fetch = remaining; |
| 334 | // Try to fetch at least "more" bytes. |
| 335 | while ((jlong)fetch > 0) { |
| 336 | jlong nr = (*read_input_fn)(this, rplimit, fetch, remaining); |
| 337 | if (nr <= 0) { |
| 338 | return (rplimit >= rpgoal); |
| 339 | } |
| 340 | remaining -= nr; |
| 341 | rplimit += nr; |
| 342 | fetch -= nr; |
| 343 | bytes_read += nr; |
| 344 | assert(remaining == (input.limit() - rplimit)); |
| 345 | } |
| 346 | return true; |
| 347 | } |
| 348 | |
| 349 | // output handling |
| 350 | |
| 351 | fillbytes* unpacker::close_output(fillbytes* which) { |
| 352 | assert(wp != null); |
| 353 | if (which == null) { |
| 354 | if (wpbase == cur_classfile_head.base()) { |
| 355 | which = &cur_classfile_head; |
| 356 | } else { |
| 357 | which = &cur_classfile_tail; |
| 358 | } |
| 359 | } |
| 360 | assert(wpbase == which->base()); |
| 361 | assert(wplimit == which->end()); |
| 362 | which->setLimit(wp); |
| 363 | wp = null; |
| 364 | wplimit = null; |
| 365 | //wpbase = null; |
| 366 | return which; |
| 367 | } |
| 368 | |
| 369 | //maybe_inline |
| 370 | void unpacker::ensure_put_space(size_t size) { |
| 371 | if (wp + size <= wplimit) return; |
| 372 | // Determine which segment needs expanding. |
| 373 | fillbytes* which = close_output(); |
| 374 | byte* wp0 = which->grow(size); |
| 375 | wpbase = which->base(); |
| 376 | wplimit = which->end(); |
| 377 | wp = wp0; |
| 378 | } |
| 379 | |
| 380 | maybe_inline |
| 381 | byte* unpacker::put_space(size_t size) { |
| 382 | byte* wp0 = wp; |
| 383 | byte* wp1 = wp0 + size; |
| 384 | if (wp1 > wplimit) { |
| 385 | ensure_put_space(size); |
| 386 | wp0 = wp; |
| 387 | wp1 = wp0 + size; |
| 388 | } |
| 389 | wp = wp1; |
| 390 | return wp0; |
| 391 | } |
| 392 | |
| 393 | maybe_inline |
| 394 | void unpacker::putu2_at(byte* wp, int n) { |
| 395 | if (n != (unsigned short)n) { |
| 396 | unpack_abort(ERROR_OVERFLOW); |
| 397 | return; |
| 398 | } |
| 399 | wp[0] = (n) >> 8; |
| 400 | wp[1] = (n) >> 0; |
| 401 | } |
| 402 | |
| 403 | maybe_inline |
| 404 | void unpacker::putu4_at(byte* wp, int n) { |
| 405 | wp[0] = (n) >> 24; |
| 406 | wp[1] = (n) >> 16; |
| 407 | wp[2] = (n) >> 8; |
| 408 | wp[3] = (n) >> 0; |
| 409 | } |
| 410 | |
| 411 | maybe_inline |
| 412 | void unpacker::putu8_at(byte* wp, jlong n) { |
| 413 | putu4_at(wp+0, (int)((julong)n >> 32)); |
| 414 | putu4_at(wp+4, (int)((julong)n >> 0)); |
| 415 | } |
| 416 | |
| 417 | maybe_inline |
| 418 | void unpacker::putu2(int n) { |
| 419 | putu2_at(put_space(2), n); |
| 420 | } |
| 421 | |
| 422 | maybe_inline |
| 423 | void unpacker::putu4(int n) { |
| 424 | putu4_at(put_space(4), n); |
| 425 | } |
| 426 | |
| 427 | maybe_inline |
| 428 | void unpacker::putu8(jlong n) { |
| 429 | putu8_at(put_space(8), n); |
| 430 | } |
| 431 | |
| 432 | maybe_inline |
| 433 | int unpacker::putref_index(entry* e, int size) { |
| 434 | if (e == null) |
| 435 | return 0; |
| 436 | else if (e->outputIndex > NOT_REQUESTED) |
| 437 | return e->outputIndex; |
| 438 | else if (e->tag == CONSTANT_Signature) |
| 439 | return putref_index(e->ref(0), size); |
| 440 | else { |
| 441 | e->requestOutputIndex(cp, -size); |
| 442 | // Later on we'll fix the bits. |
| 443 | class_fixup_type.addByte(size); |
| 444 | class_fixup_offset.add(wpoffset()); |
| 445 | class_fixup_ref.add(e); |
| 446 | return !assert(1) ? 0 : 0x20+size; // 0x22 is easy to eyeball |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | maybe_inline |
| 451 | void unpacker::putref(entry* e) { |
| 452 | int oidx = putref_index(e, 2); |
| 453 | putu2_at(put_space(2), oidx); |
| 454 | } |
| 455 | |
| 456 | maybe_inline |
| 457 | void unpacker::putu1ref(entry* e) { |
| 458 | int oidx = putref_index(e, 1); |
| 459 | putu1_at(put_space(1), oidx); |
| 460 | } |
| 461 | |
| 462 | |
| 463 | static int total_cp_size[] = {0, 0}; |
| 464 | static int largest_cp_ref[] = {0, 0}; |
| 465 | static int hash_probes[] = {0, 0}; |
| 466 | |
| 467 | // Allocation of small and large blocks. |
| 468 | |
| 469 | enum { CHUNK = (1 << 14), SMALL = (1 << 9) }; |
| 470 | |
| 471 | // Call malloc. Try to combine small blocks and free much later. |
| 472 | void* unpacker::alloc_heap(size_t size, bool smallOK, bool temp) { |
| 473 | CHECK_0; |
| 474 | if (!smallOK || size > SMALL) { |
| 475 | void* res = must_malloc(size); |
| 476 | (temp ? &tmallocs : &mallocs)->add(res); |
| 477 | return res; |
| 478 | } |
| 479 | fillbytes& xsmallbuf = *(temp ? &tsmallbuf : &smallbuf); |
| 480 | if (!xsmallbuf.canAppend(size+1)) { |
| 481 | xsmallbuf.init(CHUNK); |
| 482 | (temp ? &tmallocs : &mallocs)->add(xsmallbuf.base()); |
| 483 | } |
| 484 | int growBy = size; |
| 485 | growBy += -growBy & 7; // round up mod 8 |
| 486 | return xsmallbuf.grow(growBy); |
| 487 | } |
| 488 | |
| 489 | maybe_inline |
| 490 | void unpacker::saveTo(bytes& b, byte* ptr, size_t len) { |
| 491 | b.ptr = U_NEW(byte, len+1); |
| 492 | if (aborting()) { |
| 493 | b.len = 0; |
| 494 | return; |
| 495 | } |
| 496 | b.len = len; |
| 497 | b.copyFrom(ptr, len); |
| 498 | } |
| 499 | |
| 500 | // Read up through band_headers. |
| 501 | // Do the archive_size dance to set the size of the input mega-buffer. |
| 502 | void unpacker::read_file_header() { |
| 503 | // Read file header to determine file type and total size. |
| 504 | enum { |
| 505 | MAGIC_BYTES = 4, |
| 506 | AH_LENGTH_0 = 3, //minver, majver, options are outside of archive_size |
| 507 | AH_LENGTH = 26, //maximum archive header length (w/ all fields) |
| 508 | // Length contributions from optional header fields: |
| 509 | AH_FILE_HEADER_LEN = 5, // sizehi/lo/next/modtime/files |
| 510 | AH_CP_NUMBER_LEN = 4, // int/float/long/double |
| 511 | AH_SPECIAL_FORMAT_LEN = 2, // layouts/band-headers |
| 512 | AH_LENGTH_MIN = AH_LENGTH |
| 513 | -(AH_FILE_HEADER_LEN+AH_SPECIAL_FORMAT_LEN+AH_CP_NUMBER_LEN), |
| 514 | FIRST_READ = MAGIC_BYTES + AH_LENGTH_MIN |
| 515 | }; |
| 516 | bool foreign_buf = (read_input_fn == null); |
| 517 | byte initbuf[FIRST_READ + C_SLOP + 200]; // 200 is for JAR I/O |
| 518 | if (foreign_buf) { |
| 519 | // inbytes is all there is |
| 520 | input.set(inbytes); |
| 521 | rp = input.base(); |
| 522 | rplimit = input.limit(); |
| 523 | } else { |
| 524 | // inbytes, if not empty, contains some read-ahead we must use first |
| 525 | // ensure_input will take care of copying it into initbuf, |
| 526 | // then querying read_input_fn for any additional data needed. |
| 527 | // However, the caller must assume that we use up all of inbytes. |
| 528 | // There is no way to tell the caller that we used only part of them. |
| 529 | // Therefore, the caller must use only a bare minimum of read-ahead. |
| 530 | if (inbytes.len > FIRST_READ) { |
| 531 | abort("too much pushback"); |
| 532 | return; |
| 533 | } |
| 534 | input.set(initbuf, sizeof(initbuf)); |
| 535 | input.b.clear(); |
| 536 | input.b.copyFrom(inbytes); |
| 537 | rplimit = rp = input.base(); |
| 538 | rplimit += inbytes.len; |
| 539 | bytes_read += inbytes.len; |
| 540 | } |
| 541 | // Read only 19 bytes, which is certain to contain #archive_size fields, |
| 542 | // but is certain not to overflow past the archive_header. |
| 543 | input.b.len = FIRST_READ; |
| 544 | if (!ensure_input(FIRST_READ)) |
| 545 | abort("EOF reading archive magic number"); |
| 546 | |
| 547 | if (rp[0] == 'P' && rp[1] == 'K') { |
| 548 | #ifdef UNPACK_JNI |
| 549 | // Java driver must handle this case before we get this far. |
| 550 | abort("encountered a JAR header in unpacker"); |
| 551 | #else |
| 552 | // In the Unix-style program, we simply simulate a copy command. |
| 553 | // Copy until EOF; assume the JAR file is the last segment. |
| 554 | fprintf(errstrm, "Copy-mode.\n"); |
| 555 | for (;;) { |
| 556 | jarout->write_data(rp, input_remaining()); |
| 557 | if (foreign_buf) |
| 558 | break; // one-time use of a passed in buffer |
| 559 | if (input.size() < CHUNK) { |
| 560 | // Get some breathing room. |
| 561 | input.set(U_NEW(byte, (size_t) CHUNK + C_SLOP), (size_t) CHUNK); |
| 562 | CHECK; |
| 563 | } |
| 564 | rp = rplimit = input.base(); |
| 565 | if (!ensure_input(1)) |
| 566 | break; |
| 567 | } |
| 568 | jarout->closeJarFile(false); |
| 569 | #endif |
| 570 | return; |
| 571 | } |
| 572 | |
| 573 | // Read the magic number. |
| 574 | magic = 0; |
| 575 | for (int i1 = 0; i1 < sizeof(magic); i1++) { |
| 576 | magic <<= 8; |
| 577 | magic += (*rp++ & 0xFF); |
| 578 | } |
| 579 | |
| 580 | // Read the first 3 values from the header. |
| 581 | value_stream hdr; |
| 582 | int hdrVals = 0; |
| 583 | int hdrValsSkipped = 0; // debug only |
| 584 | hdr.init(rp, rplimit, UNSIGNED5_spec); |
| 585 | minver = hdr.getInt(); |
| 586 | majver = hdr.getInt(); |
| 587 | hdrVals += 2; |
| 588 | |
| 589 | if (magic != JAVA_PACKAGE_MAGIC || |
| 590 | (majver != JAVA5_PACKAGE_MAJOR_VERSION && |
| 591 | majver != JAVA6_PACKAGE_MAJOR_VERSION) || |
| 592 | (minver != JAVA5_PACKAGE_MINOR_VERSION && |
| 593 | minver != JAVA6_PACKAGE_MINOR_VERSION)) { |
| 594 | char message[200]; |
| 595 | sprintf(message, "@" ERROR_FORMAT ": magic/ver = " |
| 596 | "%08X/%d.%d should be %08X/%d.%d OR %08X/%d.%d\n", |
| 597 | magic, majver, minver, |
| 598 | JAVA_PACKAGE_MAGIC, JAVA5_PACKAGE_MAJOR_VERSION, JAVA5_PACKAGE_MINOR_VERSION, |
| 599 | JAVA_PACKAGE_MAGIC, JAVA6_PACKAGE_MAJOR_VERSION, JAVA6_PACKAGE_MINOR_VERSION); |
| 600 | abort(message); |
| 601 | } |
| 602 | CHECK; |
| 603 | |
| 604 | archive_options = hdr.getInt(); |
| 605 | hdrVals += 1; |
| 606 | assert(hdrVals == AH_LENGTH_0); // first three fields only |
| 607 | |
| 608 | #define ORBIT(bit) |(bit) |
| 609 | int OPTION_LIMIT = (0 ARCHIVE_BIT_DO(ORBIT)); |
| 610 | #undef ORBIT |
| 611 | if ((archive_options & ~OPTION_LIMIT) != 0) { |
| 612 | fprintf(errstrm, "Warning: Illegal archive options 0x%x\n", |
| 613 | archive_options); |
| 614 | // Do not abort. If the format really changes, version numbers will bump. |
| 615 | //abort("illegal archive options"); |
| 616 | } |
| 617 | |
| 618 | if ((archive_options & AO_HAVE_FILE_HEADERS) != 0) { |
| 619 | uint hi = hdr.getInt(); |
| 620 | uint lo = hdr.getInt(); |
| 621 | archive_size = band::makeLong(hi, lo); |
| 622 | hdrVals += 2; |
| 623 | } else { |
| 624 | hdrValsSkipped += 2; |
| 625 | } |
| 626 | |
| 627 | if (archive_size != (size_t)archive_size) { |
| 628 | // Silly size specified. |
| 629 | abort("archive too large"); |
| 630 | return; |
| 631 | } |
| 632 | |
| 633 | // Now we can size the whole archive. |
| 634 | // Read everything else into a mega-buffer. |
| 635 | rp = hdr.rp; |
| 636 | int header_size_0 = (rp - input.base()); // used-up header (4byte + 3int) |
| 637 | int header_size_1 = (rplimit - rp); // buffered unused initial fragment |
| 638 | int header_size = header_size_0+header_size_1; |
| 639 | unsized_bytes_read = header_size_0; |
| 640 | CHECK; |
| 641 | if (foreign_buf) { |
| 642 | if (archive_size > header_size_1) { |
| 643 | abort("EOF reading fixed input buffer"); |
| 644 | return; |
| 645 | } |
| 646 | } else if (archive_size > 0) { |
| 647 | input.set(U_NEW(byte, (size_t) header_size_0 + archive_size + C_SLOP), |
| 648 | (size_t) header_size_0 + archive_size); |
| 649 | assert(input.limit()[0] == 0); |
| 650 | // Move all the bytes we read initially into the real buffer. |
| 651 | input.b.copyFrom(initbuf, header_size); |
| 652 | rp = input.b.ptr + header_size_0; |
| 653 | rplimit = input.b.ptr + header_size; |
| 654 | } else { |
| 655 | // It's more complicated and painful. |
| 656 | // A zero archive_size means that we must read until EOF. |
| 657 | assert(archive_size == 0); |
| 658 | input.init(CHUNK*2); |
| 659 | CHECK; |
| 660 | input.b.len = input.allocated; |
| 661 | rp = rplimit = input.base(); |
| 662 | // Set up input buffer as if we already read the header: |
| 663 | input.b.copyFrom(initbuf, header_size); |
| 664 | rplimit += header_size; |
| 665 | while (ensure_input(input.limit() - rp)) { |
| 666 | size_t dataSoFar = input_remaining(); |
| 667 | size_t nextSize = dataSoFar + CHUNK; |
| 668 | input.ensureSize(nextSize); |
| 669 | CHECK; |
| 670 | input.b.len = input.allocated; |
| 671 | rp = rplimit = input.base(); |
| 672 | rplimit += dataSoFar; |
| 673 | } |
| 674 | size_t dataSize = (rplimit - input.base()); |
| 675 | input.b.len = dataSize; |
| 676 | input.grow(C_SLOP); |
| 677 | CHECK; |
| 678 | free_input = true; // free it later |
| 679 | input.b.len = dataSize; |
| 680 | assert(input.limit()[0] == 0); |
| 681 | rp = rplimit = input.base(); |
| 682 | rplimit += dataSize; |
| 683 | rp += header_size_0; // already scanned these bytes... |
| 684 | } |
| 685 | live_input = true; // mark as "do not reuse" |
| 686 | if (aborting()) { |
| 687 | abort("cannot allocate large input buffer for package file"); |
| 688 | return; |
| 689 | } |
| 690 | |
| 691 | // read the rest of the header fields |
| 692 | ensure_input((AH_LENGTH-AH_LENGTH_0) * B_MAX); |
| 693 | CHECK; |
| 694 | hdr.rp = rp; |
| 695 | hdr.rplimit = rplimit; |
| 696 | |
| 697 | if ((archive_options & AO_HAVE_FILE_HEADERS) != 0) { |
| 698 | archive_next_count = hdr.getInt(); |
| 699 | archive_modtime = hdr.getInt(); |
| 700 | file_count = hdr.getInt(); |
| 701 | hdrVals += 3; |
| 702 | } else { |
| 703 | hdrValsSkipped += 3; |
| 704 | } |
| 705 | |
| 706 | if ((archive_options & AO_HAVE_SPECIAL_FORMATS) != 0) { |
| 707 | band_headers_size = hdr.getInt(); |
| 708 | attr_definition_count = hdr.getInt(); |
| 709 | hdrVals += 2; |
| 710 | } else { |
| 711 | hdrValsSkipped += 2; |
| 712 | } |
| 713 | |
| 714 | int cp_counts[N_TAGS_IN_ORDER]; |
| 715 | for (int k = 0; k < N_TAGS_IN_ORDER; k++) { |
| 716 | if (!(archive_options & AO_HAVE_CP_NUMBERS)) { |
| 717 | switch (TAGS_IN_ORDER[k]) { |
| 718 | case CONSTANT_Integer: |
| 719 | case CONSTANT_Float: |
| 720 | case CONSTANT_Long: |
| 721 | case CONSTANT_Double: |
| 722 | cp_counts[k] = 0; |
| 723 | hdrValsSkipped += 1; |
| 724 | continue; |
| 725 | } |
| 726 | } |
| 727 | cp_counts[k] = hdr.getInt(); |
| 728 | hdrVals += 1; |
| 729 | } |
| 730 | |
| 731 | ic_count = hdr.getInt(); |
| 732 | default_class_minver = hdr.getInt(); |
| 733 | default_class_majver = hdr.getInt(); |
| 734 | class_count = hdr.getInt(); |
| 735 | hdrVals += 4; |
| 736 | |
| 737 | // done with archive_header |
| 738 | hdrVals += hdrValsSkipped; |
| 739 | assert(hdrVals == AH_LENGTH); |
| 740 | #ifndef PRODUCT |
| 741 | int assertSkipped = AH_LENGTH - AH_LENGTH_MIN; |
| 742 | if ((archive_options & AO_HAVE_FILE_HEADERS) != 0) |
| 743 | assertSkipped -= AH_FILE_HEADER_LEN; |
| 744 | if ((archive_options & AO_HAVE_SPECIAL_FORMATS) != 0) |
| 745 | assertSkipped -= AH_SPECIAL_FORMAT_LEN; |
| 746 | if ((archive_options & AO_HAVE_CP_NUMBERS) != 0) |
| 747 | assertSkipped -= AH_CP_NUMBER_LEN; |
| 748 | assert(hdrValsSkipped == assertSkipped); |
| 749 | #endif //PRODUCT |
| 750 | |
| 751 | rp = hdr.rp; |
| 752 | if (rp > rplimit) |
| 753 | abort("EOF reading archive header"); |
| 754 | |
| 755 | // Now size the CP. |
| 756 | assert(N_TAGS_IN_ORDER == cpool::NUM_COUNTS); |
| 757 | cp.init(this, cp_counts); |
| 758 | CHECK; |
| 759 | |
| 760 | default_file_modtime = archive_modtime; |
| 761 | if (default_file_modtime == 0 && !(archive_options & AO_HAVE_FILE_MODTIME)) |
| 762 | default_file_modtime = DEFAULT_ARCHIVE_MODTIME; // taken from driver |
| 763 | if ((archive_options & AO_DEFLATE_HINT) != 0) |
| 764 | default_file_options |= FO_DEFLATE_HINT; |
| 765 | |
| 766 | // meta-bytes, if any, immediately follow archive header |
| 767 | //band_headers.readData(band_headers_size); |
| 768 | ensure_input(band_headers_size); |
| 769 | if (input_remaining() < band_headers_size) { |
| 770 | abort("EOF reading band headers"); |
| 771 | return; |
| 772 | } |
| 773 | bytes band_headers; |
| 774 | // The "1+" allows an initial byte to be pushed on the front. |
| 775 | band_headers.set(1+U_NEW(byte, 1+band_headers_size+C_SLOP), |
| 776 | band_headers_size); |
| 777 | CHECK; |
| 778 | // Start scanning band headers here: |
| 779 | band_headers.copyFrom(rp, band_headers.len); |
| 780 | rp += band_headers.len; |
| 781 | assert(rp <= rplimit); |
| 782 | meta_rp = band_headers.ptr; |
| 783 | // Put evil meta-codes at the end of the band headers, |
| 784 | // so we are sure to throw an error if we run off the end. |
| 785 | bytes::of(band_headers.limit(), C_SLOP).clear(_meta_error); |
| 786 | } |
| 787 | |
| 788 | |
| 789 | void unpacker::finish() { |
| 790 | if (verbose >= 1) { |
| 791 | fprintf(errstrm, |
| 792 | "A total of %lld bytes were read in %d segment(s).\n", |
| 793 | bytes_read_before_reset+bytes_read, |
| 794 | segments_read_before_reset+1); |
| 795 | fprintf(errstrm, |
| 796 | "A total of %lld file content bytes were written.\n", |
| 797 | bytes_written_before_reset+bytes_written); |
| 798 | fprintf(errstrm, |
| 799 | "A total of %d files (of which %d are classes) were written to output.\n", |
| 800 | files_written_before_reset+files_written, |
| 801 | classes_written_before_reset+classes_written); |
| 802 | } |
| 803 | if (jarout != null) |
| 804 | jarout->closeJarFile(true); |
| 805 | if (errstrm != null) { |
| 806 | if (errstrm == stdout || errstrm == stderr) { |
| 807 | fflush(errstrm); |
| 808 | } else { |
| 809 | fclose(errstrm); |
| 810 | } |
| 811 | errstrm = null; |
| 812 | errstrm_name = null; |
| 813 | } |
| 814 | } |
| 815 | |
| 816 | |
| 817 | // Cf. PackageReader.readConstantPoolCounts |
| 818 | void cpool::init(unpacker* u_, int counts[NUM_COUNTS]) { |
| 819 | this->u = u_; |
| 820 | |
| 821 | // Fill-pointer for CP. |
| 822 | int next_entry = 0; |
| 823 | |
| 824 | // Size the constant pool: |
| 825 | for (int k = 0; k < N_TAGS_IN_ORDER; k++) { |
| 826 | byte tag = TAGS_IN_ORDER[k]; |
| 827 | int len = counts[k]; |
| 828 | tag_count[tag] = len; |
| 829 | tag_base[tag] = next_entry; |
| 830 | next_entry += len; |
| 831 | // Detect and defend against constant pool size overflow. |
| 832 | // (Pack200 forbids the sum of CP counts to exceed 2^29-1.) |
| 833 | enum { |
| 834 | CP_SIZE_LIMIT = (1<<29), |
| 835 | IMPLICIT_ENTRY_COUNT = 1 // empty Utf8 string |
| 836 | }; |
| 837 | if (len >= (1<<29) || len < 0 |
| 838 | || next_entry >= CP_SIZE_LIMIT+IMPLICIT_ENTRY_COUNT) { |
| 839 | abort("archive too large: constant pool limit exceeded"); |
| 840 | return; |
| 841 | } |
| 842 | } |
| 843 | |
| 844 | // Close off the end of the CP: |
| 845 | nentries = next_entry; |
| 846 | |
| 847 | // place a limit on future CP growth: |
| 848 | int generous = 0; |
| 849 | generous += u->ic_count*3; // implicit name, outer, outer.utf8 |
| 850 | generous += 40; // WKUs, misc |
| 851 | generous += u->class_count; // implicit SourceFile strings |
| 852 | maxentries = nentries + generous; |
| 853 | |
| 854 | // Note that this CP does not include "empty" entries |
| 855 | // for longs and doubles. Those are introduced when |
| 856 | // the entries are renumbered for classfile output. |
| 857 | |
| 858 | entries = U_NEW(entry, maxentries); |
| 859 | CHECK; |
| 860 | |
| 861 | first_extra_entry = &entries[nentries]; |
| 862 | |
| 863 | // Initialize the standard indexes. |
| 864 | tag_count[CONSTANT_All] = nentries; |
| 865 | tag_base[ CONSTANT_All] = 0; |
| 866 | for (int tag = 0; tag < CONSTANT_Limit; tag++) { |
| 867 | entry* cpMap = &entries[tag_base[tag]]; |
| 868 | tag_index[tag].init(tag_count[tag], cpMap, tag); |
| 869 | } |
| 870 | |
| 871 | // Initialize hashTab to a generous power-of-two size. |
| 872 | uint pow2 = 1; |
| 873 | uint target = maxentries + maxentries/2; // 60% full |
| 874 | while (pow2 < target) pow2 <<= 1; |
| 875 | hashTab = U_NEW(entry*, hashTabLength = pow2); |
| 876 | } |
| 877 | |
| 878 | static byte* store_Utf8_char(byte* cp, unsigned short ch) { |
| 879 | if (ch >= 0x001 && ch <= 0x007F) { |
| 880 | *cp++ = (byte) ch; |
| 881 | } else if (ch <= 0x07FF) { |
| 882 | *cp++ = (byte) (0xC0 | ((ch >> 6) & 0x1F)); |
| 883 | *cp++ = (byte) (0x80 | ((ch >> 0) & 0x3F)); |
| 884 | } else { |
| 885 | *cp++ = (byte) (0xE0 | ((ch >> 12) & 0x0F)); |
| 886 | *cp++ = (byte) (0x80 | ((ch >> 6) & 0x3F)); |
| 887 | *cp++ = (byte) (0x80 | ((ch >> 0) & 0x3F)); |
| 888 | } |
| 889 | return cp; |
| 890 | } |
| 891 | |
| 892 | static byte* skip_Utf8_chars(byte* cp, int len) { |
| 893 | for (;; cp++) { |
| 894 | int ch = *cp & 0xFF; |
| 895 | if ((ch & 0xC0) != 0x80) { |
| 896 | if (len-- == 0) |
| 897 | return cp; |
| 898 | if (ch < 0x80 && len == 0) |
| 899 | return cp+1; |
| 900 | } |
| 901 | } |
| 902 | } |
| 903 | |
| 904 | static int compare_Utf8_chars(bytes& b1, bytes& b2) { |
| 905 | int l1 = b1.len; |
| 906 | int l2 = b2.len; |
| 907 | int l0 = (l1 < l2) ? l1 : l2; |
| 908 | byte* p1 = b1.ptr; |
| 909 | byte* p2 = b2.ptr; |
| 910 | int c0 = 0; |
| 911 | for (int i = 0; i < l0; i++) { |
| 912 | int c1 = p1[i] & 0xFF; |
| 913 | int c2 = p2[i] & 0xFF; |
| 914 | if (c1 != c2) { |
| 915 | // Before returning the obvious answer, |
| 916 | // check to see if c1 or c2 is part of a 0x0000, |
| 917 | // which encodes as {0xC0,0x80}. The 0x0000 is the |
| 918 | // lowest-sorting Java char value, and yet it encodes |
| 919 | // as if it were the first char after 0x7F, which causes |
| 920 | // strings containing nulls to sort too high. All other |
| 921 | // comparisons are consistent between Utf8 and Java chars. |
| 922 | if (c1 == 0xC0 && (p1[i+1] & 0xFF) == 0x80) c1 = 0; |
| 923 | if (c2 == 0xC0 && (p2[i+1] & 0xFF) == 0x80) c2 = 0; |
| 924 | if (c0 == 0xC0) { |
| 925 | assert(((c1|c2) & 0xC0) == 0x80); // c1 & c2 are extension chars |
| 926 | if (c1 == 0x80) c1 = 0; // will sort below c2 |
| 927 | if (c2 == 0x80) c2 = 0; // will sort below c1 |
| 928 | } |
| 929 | return c1 - c2; |
| 930 | } |
| 931 | c0 = c1; // save away previous char |
| 932 | } |
| 933 | // common prefix is identical; return length difference if any |
| 934 | return l1 - l2; |
| 935 | } |
| 936 | |
| 937 | // Cf. PackageReader.readUtf8Bands |
| 938 | local_inline |
| 939 | void unpacker::read_Utf8_values(entry* cpMap, int len) { |
| 940 | // Implicit first Utf8 string is the empty string. |
| 941 | enum { |
| 942 | // certain bands begin with implicit zeroes |
| 943 | PREFIX_SKIP_2 = 2, |
| 944 | SUFFIX_SKIP_1 = 1 |
| 945 | }; |
| 946 | |
| 947 | int i; |
| 948 | |
| 949 | // First band: Read lengths of shared prefixes. |
| 950 | if (len > PREFIX_SKIP_2) |
| 951 | cp_Utf8_prefix.readData(len - PREFIX_SKIP_2); |
| 952 | |
| 953 | // Second band: Read lengths of unshared suffixes: |
| 954 | if (len > SUFFIX_SKIP_1) |
| 955 | cp_Utf8_suffix.readData(len - SUFFIX_SKIP_1); |
| 956 | |
| 957 | bytes* allsuffixes = T_NEW(bytes, len); |
| 958 | CHECK; |
| 959 | |
| 960 | int nbigsuf = 0; |
| 961 | fillbytes charbuf; // buffer to allocate small strings |
| 962 | charbuf.init(); |
| 963 | |
| 964 | // Third band: Read the char values in the unshared suffixes: |
| 965 | cp_Utf8_chars.readData(cp_Utf8_suffix.getIntTotal()); |
| 966 | for (i = 0; i < len; i++) { |
| 967 | int suffix = (i < SUFFIX_SKIP_1)? 0: cp_Utf8_suffix.getInt(); |
| 968 | if (suffix < 0) { |
| 969 | abort("bad utf8 suffix"); |
| 970 | return; |
| 971 | } |
| 972 | if (suffix == 0 && i >= SUFFIX_SKIP_1) { |
| 973 | // chars are packed in cp_Utf8_big_chars |
| 974 | nbigsuf += 1; |
| 975 | continue; |
| 976 | } |
| 977 | bytes& chars = allsuffixes[i]; |
| 978 | uint size3 = suffix * 3; // max Utf8 length |
| 979 | bool isMalloc = (suffix > SMALL); |
| 980 | if (isMalloc) { |
| 981 | chars.malloc(size3); |
| 982 | } else { |
| 983 | if (!charbuf.canAppend(size3+1)) { |
| 984 | assert(charbuf.allocated == 0 || tmallocs.contains(charbuf.base())); |
| 985 | charbuf.init(CHUNK); // Reset to new buffer. |
| 986 | tmallocs.add(charbuf.base()); |
| 987 | } |
| 988 | chars.set(charbuf.grow(size3+1), size3); |
| 989 | } |
| 990 | CHECK; |
| 991 | byte* chp = chars.ptr; |
| 992 | for (int j = 0; j < suffix; j++) { |
| 993 | unsigned short ch = cp_Utf8_chars.getInt(); |
| 994 | chp = store_Utf8_char(chp, ch); |
| 995 | } |
| 996 | // shrink to fit: |
| 997 | if (isMalloc) { |
| 998 | chars.realloc(chp - chars.ptr); |
| 999 | CHECK; |
| 1000 | tmallocs.add(chars.ptr); // free it later |
| 1001 | } else { |
| 1002 | int shrink = chars.limit() - chp; |
| 1003 | chars.len -= shrink; |
| 1004 | charbuf.b.len -= shrink; // ungrow to reclaim buffer space |
| 1005 | // Note that we did not reclaim the final '\0'. |
| 1006 | assert(chars.limit() == charbuf.limit()-1); |
| 1007 | assert(strlen((char*)chars.ptr) == chars.len); |
| 1008 | } |
| 1009 | } |
| 1010 | //cp_Utf8_chars.done(); |
| 1011 | if (assert(1)) charbuf.b.set(null, 0); // tidy |
| 1012 | |
| 1013 | // Fourth band: Go back and size the specially packed strings. |
| 1014 | int maxlen = 0; |
| 1015 | cp_Utf8_big_suffix.readData(nbigsuf); |
| 1016 | cp_Utf8_suffix.rewind(); |
| 1017 | for (i = 0; i < len; i++) { |
| 1018 | int suffix = (i < SUFFIX_SKIP_1)? 0: cp_Utf8_suffix.getInt(); |
| 1019 | int prefix = (i < PREFIX_SKIP_2)? 0: cp_Utf8_prefix.getInt(); |
| 1020 | if (prefix < 0 || prefix+suffix < 0) { |
| 1021 | abort("bad utf8 prefix"); |
| 1022 | return; |
| 1023 | } |
| 1024 | bytes& chars = allsuffixes[i]; |
| 1025 | if (suffix == 0 && i >= SUFFIX_SKIP_1) { |
| 1026 | suffix = cp_Utf8_big_suffix.getInt(); |
| 1027 | assert(chars.ptr == null); |
| 1028 | chars.len = suffix; // just a momentary hack |
| 1029 | } else { |
| 1030 | assert(chars.ptr != null); |
| 1031 | } |
| 1032 | if (maxlen < prefix + suffix) { |
| 1033 | maxlen = prefix + suffix; |
| 1034 | } |
| 1035 | } |
| 1036 | //cp_Utf8_suffix.done(); // will use allsuffixes[i].len (ptr!=null) |
| 1037 | //cp_Utf8_big_suffix.done(); // will use allsuffixes[i].len |
| 1038 | |
| 1039 | // Fifth band(s): Get the specially packed characters. |
| 1040 | cp_Utf8_big_suffix.rewind(); |
| 1041 | for (i = 0; i < len; i++) { |
| 1042 | bytes& chars = allsuffixes[i]; |
| 1043 | if (chars.ptr != null) continue; // already input |
| 1044 | int suffix = chars.len; // pick up the hack |
| 1045 | uint size3 = suffix * 3; |
| 1046 | if (suffix == 0) continue; // done with empty string |
| 1047 | chars.malloc(size3); |
| 1048 | byte* chp = chars.ptr; |
| 1049 | band saved_band = cp_Utf8_big_chars; |
| 1050 | cp_Utf8_big_chars.readData(suffix); |
| 1051 | for (int j = 0; j < suffix; j++) { |
| 1052 | unsigned short ch = cp_Utf8_big_chars.getInt(); |
| 1053 | chp = store_Utf8_char(chp, ch); |
| 1054 | } |
| 1055 | chars.realloc(chp - chars.ptr); |
| 1056 | CHECK; |
| 1057 | tmallocs.add(chars.ptr); // free it later |
| 1058 | //cp_Utf8_big_chars.done(); |
| 1059 | cp_Utf8_big_chars = saved_band; // reset the band for the next string |
| 1060 | } |
| 1061 | cp_Utf8_big_chars.readData(0); // zero chars |
| 1062 | //cp_Utf8_big_chars.done(); |
| 1063 | |
| 1064 | // Finally, sew together all the prefixes and suffixes. |
| 1065 | bytes bigbuf; |
| 1066 | bigbuf.malloc(maxlen * 3 + 1); // max Utf8 length, plus slop for null |
| 1067 | CHECK; |
| 1068 | int prevlen = 0; // previous string length (in chars) |
| 1069 | tmallocs.add(bigbuf.ptr); // free after this block |
| 1070 | cp_Utf8_prefix.rewind(); |
| 1071 | for (i = 0; i < len; i++) { |
| 1072 | bytes& chars = allsuffixes[i]; |
| 1073 | int prefix = (i < PREFIX_SKIP_2)? 0: cp_Utf8_prefix.getInt(); |
| 1074 | int suffix = chars.len; |
| 1075 | byte* fillp; |
| 1076 | // by induction, the buffer is already filled with the prefix |
| 1077 | // make sure the prefix value is not corrupted, though: |
| 1078 | if (prefix > prevlen) { |
| 1079 | abort("utf8 prefix overflow"); |
| 1080 | return; |
| 1081 | } |
| 1082 | fillp = skip_Utf8_chars(bigbuf.ptr, prefix); |
| 1083 | // copy the suffix into the same buffer: |
| 1084 | fillp = chars.writeTo(fillp); |
| 1085 | assert(bigbuf.inBounds(fillp)); |
| 1086 | *fillp = 0; // bigbuf must contain a well-formed Utf8 string |
| 1087 | int length = fillp - bigbuf.ptr; |
| 1088 | bytes& value = cpMap[i].value.b; |
| 1089 | value.set(U_NEW(byte, length+1), length); |
| 1090 | value.copyFrom(bigbuf.ptr, length); |
| 1091 | CHECK; |
| 1092 | // Index all Utf8 strings |
| 1093 | entry* &htref = cp.hashTabRef(CONSTANT_Utf8, value); |
| 1094 | if (htref == null) { |
| 1095 | // Note that if two identical strings are transmitted, |
| 1096 | // the first is taken to be the canonical one. |
| 1097 | htref = &cpMap[i]; |
| 1098 | } |
| 1099 | prevlen = prefix + suffix; |
| 1100 | } |
| 1101 | //cp_Utf8_prefix.done(); |
| 1102 | |
| 1103 | // Free intermediate buffers. |
| 1104 | free_temps(); |
| 1105 | } |
| 1106 | |
| 1107 | local_inline |
| 1108 | void unpacker::read_single_words(band& cp_band, entry* cpMap, int len) { |
| 1109 | cp_band.readData(len); |
| 1110 | for (int i = 0; i < len; i++) { |
| 1111 | cpMap[i].value.i = cp_band.getInt(); // coding handles signs OK |
| 1112 | } |
| 1113 | } |
| 1114 | |
| 1115 | maybe_inline |
| 1116 | void unpacker::read_double_words(band& cp_bands, entry* cpMap, int len) { |
| 1117 | band& cp_band_hi = cp_bands; |
| 1118 | band& cp_band_lo = cp_bands.nextBand(); |
| 1119 | cp_band_hi.readData(len); |
| 1120 | cp_band_lo.readData(len); |
| 1121 | for (int i = 0; i < len; i++) { |
| 1122 | cpMap[i].value.l = cp_band_hi.getLong(cp_band_lo, true); |
| 1123 | } |
| 1124 | //cp_band_hi.done(); |
| 1125 | //cp_band_lo.done(); |
| 1126 | } |
| 1127 | |
| 1128 | maybe_inline |
| 1129 | void unpacker::read_single_refs(band& cp_band, byte refTag, entry* cpMap, int len) { |
| 1130 | assert(refTag == CONSTANT_Utf8); |
| 1131 | cp_band.setIndexByTag(refTag); |
| 1132 | cp_band.readData(len); |
| 1133 | CHECK; |
| 1134 | int indexTag = (cp_band.bn == e_cp_Class) ? CONSTANT_Class : 0; |
| 1135 | for (int i = 0; i < len; i++) { |
| 1136 | entry& e = cpMap[i]; |
| 1137 | e.refs = U_NEW(entry*, e.nrefs = 1); |
| 1138 | entry* utf = cp_band.getRef(); |
| 1139 | CHECK; |
| 1140 | e.refs[0] = utf; |
| 1141 | e.value.b = utf->value.b; // copy value of Utf8 string to self |
| 1142 | if (indexTag != 0) { |
| 1143 | // Maintain cross-reference: |
| 1144 | entry* &htref = cp.hashTabRef(indexTag, e.value.b); |
| 1145 | if (htref == null) { |
| 1146 | // Note that if two identical classes are transmitted, |
| 1147 | // the first is taken to be the canonical one. |
| 1148 | htref = &e; |
| 1149 | } |
| 1150 | } |
| 1151 | } |
| 1152 | //cp_band.done(); |
| 1153 | } |
| 1154 | |
| 1155 | maybe_inline |
| 1156 | void unpacker::read_double_refs(band& cp_band, byte ref1Tag, byte ref2Tag, |
| 1157 | entry* cpMap, int len) { |
| 1158 | band& cp_band1 = cp_band; |
| 1159 | band& cp_band2 = cp_band.nextBand(); |
| 1160 | cp_band1.setIndexByTag(ref1Tag); |
| 1161 | cp_band2.setIndexByTag(ref2Tag); |
| 1162 | cp_band1.readData(len); |
| 1163 | cp_band2.readData(len); |
| 1164 | CHECK; |
| 1165 | for (int i = 0; i < len; i++) { |
| 1166 | entry& e = cpMap[i]; |
| 1167 | e.refs = U_NEW(entry*, e.nrefs = 2); |
| 1168 | e.refs[0] = cp_band1.getRef(); |
| 1169 | e.refs[1] = cp_band2.getRef(); |
| 1170 | CHECK; |
| 1171 | } |
| 1172 | //cp_band1.done(); |
| 1173 | //cp_band2.done(); |
| 1174 | } |
| 1175 | |
| 1176 | // Cf. PackageReader.readSignatureBands |
| 1177 | maybe_inline |
| 1178 | void unpacker::read_signature_values(entry* cpMap, int len) { |
| 1179 | cp_Signature_form.setIndexByTag(CONSTANT_Utf8); |
| 1180 | cp_Signature_form.readData(len); |
| 1181 | CHECK; |
| 1182 | int ncTotal = 0; |
| 1183 | int i; |
| 1184 | for (i = 0; i < len; i++) { |
| 1185 | entry& e = cpMap[i]; |
| 1186 | entry& form = *cp_Signature_form.getRef(); |
| 1187 | CHECK; |
| 1188 | int nc = 0; |
| 1189 | |
| 1190 | for ( const char* ncp = form.utf8String() ; *ncp; ncp++) { |
| 1191 | if (*ncp == 'L') nc++; |
| 1192 | } |
| 1193 | |
| 1194 | ncTotal += nc; |
| 1195 | e.refs = U_NEW(entry*, cpMap[i].nrefs = 1 + nc); |
| 1196 | CHECK; |
| 1197 | e.refs[0] = &form; |
| 1198 | } |
| 1199 | //cp_Signature_form.done(); |
| 1200 | cp_Signature_classes.setIndexByTag(CONSTANT_Class); |
| 1201 | cp_Signature_classes.readData(ncTotal); |
| 1202 | for (i = 0; i < len; i++) { |
| 1203 | entry& e = cpMap[i]; |
| 1204 | for (int j = 1; j < e.nrefs; j++) { |
| 1205 | e.refs[j] = cp_Signature_classes.getRef(); |
| 1206 | CHECK; |
| 1207 | } |
| 1208 | } |
| 1209 | //cp_Signature_classes.done(); |
| 1210 | } |
| 1211 | |
| 1212 | // Cf. PackageReader.readConstantPool |
| 1213 | void unpacker::read_cp() { |
| 1214 | byte* rp0 = rp; |
| 1215 | |
| 1216 | int i; |
| 1217 | |
| 1218 | for (int k = 0; k < N_TAGS_IN_ORDER; k++) { |
| 1219 | byte tag = TAGS_IN_ORDER[k]; |
| 1220 | int len = cp.tag_count[tag]; |
| 1221 | int base = cp.tag_base[tag]; |
| 1222 | |
| 1223 | printcr(1,"Reading %d %s entries...", len, NOT_PRODUCT(TAG_NAME[tag])+0); |
| 1224 | entry* cpMap = &cp.entries[base]; |
| 1225 | for (i = 0; i < len; i++) { |
| 1226 | cpMap[i].tag = tag; |
| 1227 | cpMap[i].inord = i; |
| 1228 | } |
| 1229 | |
| 1230 | switch (tag) { |
| 1231 | case CONSTANT_Utf8: |
| 1232 | read_Utf8_values(cpMap, len); |
| 1233 | break; |
| 1234 | case CONSTANT_Integer: |
| 1235 | read_single_words(cp_Int, cpMap, len); |
| 1236 | break; |
| 1237 | case CONSTANT_Float: |
| 1238 | read_single_words(cp_Float, cpMap, len); |
| 1239 | break; |
| 1240 | case CONSTANT_Long: |
| 1241 | read_double_words(cp_Long_hi /*& cp_Long_lo*/, cpMap, len); |
| 1242 | break; |
| 1243 | case CONSTANT_Double: |
| 1244 | read_double_words(cp_Double_hi /*& cp_Double_lo*/, cpMap, len); |
| 1245 | break; |
| 1246 | case CONSTANT_String: |
| 1247 | read_single_refs(cp_String, CONSTANT_Utf8, cpMap, len); |
| 1248 | break; |
| 1249 | case CONSTANT_Class: |
| 1250 | read_single_refs(cp_Class, CONSTANT_Utf8, cpMap, len); |
| 1251 | break; |
| 1252 | case CONSTANT_Signature: |
| 1253 | read_signature_values(cpMap, len); |
| 1254 | break; |
| 1255 | case CONSTANT_NameandType: |
| 1256 | read_double_refs(cp_Descr_name /*& cp_Descr_type*/, |
| 1257 | CONSTANT_Utf8, CONSTANT_Signature, |
| 1258 | cpMap, len); |
| 1259 | break; |
| 1260 | case CONSTANT_Fieldref: |
| 1261 | read_double_refs(cp_Field_class /*& cp_Field_desc*/, |
| 1262 | CONSTANT_Class, CONSTANT_NameandType, |
| 1263 | cpMap, len); |
| 1264 | break; |
| 1265 | case CONSTANT_Methodref: |
| 1266 | read_double_refs(cp_Method_class /*& cp_Method_desc*/, |
| 1267 | CONSTANT_Class, CONSTANT_NameandType, |
| 1268 | cpMap, len); |
| 1269 | break; |
| 1270 | case CONSTANT_InterfaceMethodref: |
| 1271 | read_double_refs(cp_Imethod_class /*& cp_Imethod_desc*/, |
| 1272 | CONSTANT_Class, CONSTANT_NameandType, |
| 1273 | cpMap, len); |
| 1274 | break; |
| 1275 | default: |
| 1276 | assert(false); |
| 1277 | break; |
| 1278 | } |
| 1279 | |
| 1280 | // Initialize the tag's CP index right away, since it might be needed |
| 1281 | // in the next pass to initialize the CP for another tag. |
| 1282 | #ifndef PRODUCT |
| 1283 | cpindex* ix = &cp.tag_index[tag]; |
| 1284 | assert(ix->ixTag == tag); |
| 1285 | assert(ix->len == len); |
| 1286 | assert(ix->base1 == cpMap); |
| 1287 | #endif |
| 1288 | CHECK; |
| 1289 | } |
| 1290 | |
| 1291 | cp.expandSignatures(); |
| 1292 | CHECK; |
| 1293 | cp.initMemberIndexes(); |
| 1294 | CHECK; |
| 1295 | |
| 1296 | printcr(1,"parsed %d constant pool entries in %d bytes", cp.nentries, (rp - rp0)); |
| 1297 | |
| 1298 | #define SNAME(n,s) #s "\0" |
| 1299 | const char* symNames = ( |
| 1300 | ALL_ATTR_DO(SNAME) |
| 1301 | "<init>" |
| 1302 | ); |
| 1303 | #undef SNAME |
| 1304 | |
| 1305 | for (int sn = 0; sn < cpool::s_LIMIT; sn++) { |
| 1306 | assert(symNames[0] >= '0' && symNames[0] <= 'Z'); // sanity |
| 1307 | bytes name; name.set(symNames); |
| 1308 | if (name.len > 0 && name.ptr[0] != '0') { |
| 1309 | cp.sym[sn] = cp.ensureUtf8(name); |
| 1310 | printcr(4, "well-known sym %d=%s", sn, cp.sym[sn]->string()); |
| 1311 | } |
| 1312 | symNames += name.len + 1; // skip trailing null to next name |
| 1313 | } |
| 1314 | |
| 1315 | band::initIndexes(this); |
| 1316 | } |
| 1317 | |
| 1318 | static band* no_bands[] = { null }; // shared empty body |
| 1319 | |
| 1320 | inline |
| 1321 | band& unpacker::attr_definitions::fixed_band(int e_class_xxx) { |
| 1322 | return u->all_bands[xxx_flags_hi_bn + (e_class_xxx-e_class_flags_hi)]; |
| 1323 | } |
| 1324 | inline band& unpacker::attr_definitions::xxx_flags_hi() |
| 1325 | { return fixed_band(e_class_flags_hi); } |
| 1326 | inline band& unpacker::attr_definitions::xxx_flags_lo() |
| 1327 | { return fixed_band(e_class_flags_lo); } |
| 1328 | inline band& unpacker::attr_definitions::xxx_attr_count() |
| 1329 | { return fixed_band(e_class_attr_count); } |
| 1330 | inline band& unpacker::attr_definitions::xxx_attr_indexes() |
| 1331 | { return fixed_band(e_class_attr_indexes); } |
| 1332 | inline band& unpacker::attr_definitions::xxx_attr_calls() |
| 1333 | { return fixed_band(e_class_attr_calls); } |
| 1334 | |
| 1335 | |
| 1336 | inline |
| 1337 | unpacker::layout_definition* |
| 1338 | unpacker::attr_definitions::defineLayout(int idx, |
| 1339 | entry* nameEntry, |
| 1340 | const char* layout) { |
| 1341 | const char* name = nameEntry->value.b.strval(); |
| 1342 | layout_definition* lo = defineLayout(idx, name, layout); |
| 1343 | CHECK_0; |
| 1344 | lo->nameEntry = nameEntry; |
| 1345 | return lo; |
| 1346 | } |
| 1347 | |
| 1348 | unpacker::layout_definition* |
| 1349 | unpacker::attr_definitions::defineLayout(int idx, |
| 1350 | const char* name, |
| 1351 | const char* layout) { |
| 1352 | assert(flag_limit != 0); // must be set up already |
| 1353 | if (idx >= 0) { |
| 1354 | // Fixed attr. |
| 1355 | if (idx >= flag_limit) |
| 1356 | abort("attribute index too large"); |
| 1357 | if (isRedefined(idx)) |
| 1358 | abort("redefined attribute index"); |
| 1359 | redef |= ((julong)1<<idx); |
| 1360 | } else { |
| 1361 | idx = flag_limit + overflow_count.length(); |
| 1362 | overflow_count.add(0); // make a new counter |
| 1363 | } |
| 1364 | layout_definition* lo = U_NEW(layout_definition, 1); |
| 1365 | CHECK_0; |
| 1366 | lo->idx = idx; |
| 1367 | lo->name = name; |
| 1368 | lo->layout = layout; |
| 1369 | for (int adds = (idx+1) - layouts.length(); adds > 0; adds--) { |
| 1370 | layouts.add(null); |
| 1371 | } |
| 1372 | CHECK_0; |
| 1373 | layouts.get(idx) = lo; |
| 1374 | return lo; |
| 1375 | } |
| 1376 | |
| 1377 | band** |
| 1378 | unpacker::attr_definitions::buildBands(unpacker::layout_definition* lo) { |
| 1379 | int i; |
| 1380 | if (lo->elems != null) |
| 1381 | return lo->bands(); |
| 1382 | if (lo->layout[0] == '\0') { |
| 1383 | lo->elems = no_bands; |
| 1384 | } else { |
| 1385 | // Create bands for this attribute by parsing the layout. |
| 1386 | bool hasCallables = lo->hasCallables(); |
| 1387 | bands_made = 0x10000; // base number for bands made |
| 1388 | const char* lp = lo->layout; |
| 1389 | lp = parseLayout(lp, lo->elems, -1); |
| 1390 | CHECK_0; |
| 1391 | if (lp[0] != '\0' || band_stack.length() > 0) { |
| 1392 | abort("garbage at end of layout"); |
| 1393 | } |
| 1394 | band_stack.popTo(0); |
| 1395 | CHECK_0; |
| 1396 | |
| 1397 | // Fix up callables to point at their callees. |
| 1398 | band** bands = lo->elems; |
| 1399 | assert(bands == lo->bands()); |
| 1400 | int num_callables = 0; |
| 1401 | if (hasCallables) { |
| 1402 | while (bands[num_callables] != null) { |
| 1403 | if (bands[num_callables]->le_kind != EK_CBLE) { |
| 1404 | abort("garbage mixed with callables"); |
| 1405 | break; |
| 1406 | } |
| 1407 | num_callables += 1; |
| 1408 | } |
| 1409 | } |
| 1410 | for (i = 0; i < calls_to_link.length(); i++) { |
| 1411 | band& call = *(band*) calls_to_link.get(i); |
| 1412 | assert(call.le_kind == EK_CALL); |
| 1413 | // Determine the callee. |
| 1414 | int call_num = call.le_len; |
| 1415 | if (call_num < 0 || call_num >= num_callables) { |
| 1416 | abort("bad call in layout"); |
| 1417 | break; |
| 1418 | } |
| 1419 | band& cble = *bands[call_num]; |
| 1420 | // Link the call to it. |
| 1421 | call.le_body[0] = &cble; |
| 1422 | // Distinguish backward calls and callables: |
| 1423 | assert(cble.le_kind == EK_CBLE); |
| 1424 | assert(cble.le_len == call_num); |
| 1425 | cble.le_back |= call.le_back; |
| 1426 | } |
| 1427 | calls_to_link.popTo(0); |
| 1428 | } |
| 1429 | return lo->elems; |
| 1430 | } |
| 1431 | |
| 1432 | /* attribute layout language parser |
| 1433 | |
| 1434 | attribute_layout: |
| 1435 | ( layout_element )* | ( callable )+ |
| 1436 | layout_element: |
| 1437 | ( integral | replication | union | call | reference ) |
| 1438 | |
| 1439 | callable: |
| 1440 | '[' body ']' |
| 1441 | body: |
| 1442 | ( layout_element )+ |
| 1443 | |
| 1444 | integral: |
| 1445 | ( unsigned_int | signed_int | bc_index | bc_offset | flag ) |
| 1446 | unsigned_int: |
| 1447 | uint_type |
| 1448 | signed_int: |
| 1449 | 'S' uint_type |
| 1450 | any_int: |
| 1451 | ( unsigned_int | signed_int ) |
| 1452 | bc_index: |
| 1453 | ( 'P' uint_type | 'PO' uint_type ) |
| 1454 | bc_offset: |
| 1455 | 'O' any_int |
| 1456 | flag: |
| 1457 | 'F' uint_type |
| 1458 | uint_type: |
| 1459 | ( 'B' | 'H' | 'I' | 'V' ) |
| 1460 | |
| 1461 | replication: |
| 1462 | 'N' uint_type '[' body ']' |
| 1463 | |
| 1464 | union: |
| 1465 | 'T' any_int (union_case)* '(' ')' '[' (body)? ']' |
| 1466 | union_case: |
| 1467 | '(' union_case_tag (',' union_case_tag)* ')' '[' (body)? ']' |
| 1468 | union_case_tag: |
| 1469 | ( numeral | numeral '-' numeral ) |
| 1470 | call: |
| 1471 | '(' numeral ')' |
| 1472 | |
| 1473 | reference: |
| 1474 | reference_type ( 'N' )? uint_type |
| 1475 | reference_type: |
| 1476 | ( constant_ref | schema_ref | utf8_ref | untyped_ref ) |
| 1477 | constant_ref: |
| 1478 | ( 'KI' | 'KJ' | 'KF' | 'KD' | 'KS' | 'KQ' ) |
| 1479 | schema_ref: |
| 1480 | ( 'RC' | 'RS' | 'RD' | 'RF' | 'RM' | 'RI' ) |
| 1481 | utf8_ref: |
| 1482 | 'RU' |
| 1483 | untyped_ref: |
| 1484 | 'RQ' |
| 1485 | |
| 1486 | numeral: |
| 1487 | '(' ('-')? (digit)+ ')' |
| 1488 | digit: |
| 1489 | ( '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' ) |
| 1490 | |
| 1491 | */ |
| 1492 | |
| 1493 | const char* |
| 1494 | unpacker::attr_definitions::parseIntLayout(const char* lp, band* &res, |
| 1495 | byte le_kind, bool can_be_signed) { |
| 1496 | const char* lp0 = lp; |
| 1497 | band* b = U_NEW(band, 1); |
| 1498 | CHECK_(lp); |
| 1499 | char le = *lp++; |
| 1500 | int spec = UNSIGNED5_spec; |
| 1501 | if (le == 'S' && can_be_signed) { |
| 1502 | // Note: This is the last use of sign. There is no 'EF_SIGN'. |
| 1503 | spec = SIGNED5_spec; |
| 1504 | le = *lp++; |
| 1505 | } else if (le == 'B') { |
| 1506 | spec = BYTE1_spec; // unsigned byte |
| 1507 | } |
| 1508 | b->init(u, bands_made++, spec); |
| 1509 | b->le_kind = le_kind; |
| 1510 | int le_len = 0; |
| 1511 | switch (le) { |
| 1512 | case 'B': le_len = 1; break; |
| 1513 | case 'H': le_len = 2; break; |
| 1514 | case 'I': le_len = 4; break; |
| 1515 | case 'V': le_len = 0; break; |
| 1516 | default: abort("bad layout element"); |
| 1517 | } |
| 1518 | b->le_len = le_len; |
| 1519 | band_stack.add(b); |
| 1520 | res = b; |
| 1521 | return lp; |
| 1522 | } |
| 1523 | |
| 1524 | const char* |
| 1525 | unpacker::attr_definitions::parseNumeral(const char* lp, int &res) { |
| 1526 | const char* lp0 = lp; |
| 1527 | bool sgn = false; |
| 1528 | if (*lp == '0') { res = 0; return lp+1; } // special case '0' |
| 1529 | if (*lp == '-') { sgn = true; lp++; } |
| 1530 | const char* dp = lp; |
| 1531 | int con = 0; |
| 1532 | while (*dp >= '0' && *dp <= '9') { |
| 1533 | int con0 = con; |
| 1534 | con *= 10; |
| 1535 | con += (*dp++) - '0'; |
| 1536 | if (con <= con0) { con = -1; break; } // numeral overflow |
| 1537 | } |
| 1538 | if (lp == dp) { |
| 1539 | abort("missing numeral in layout"); |
| 1540 | return ""; |
| 1541 | } |
| 1542 | lp = dp; |
| 1543 | if (con < 0 && !(sgn && con == -con)) { |
| 1544 | // (Portability note: Misses the error if int is not 32 bits.) |
| 1545 | abort("numeral overflow"); |
| 1546 | return "" ; |
| 1547 | } |
| 1548 | if (sgn) con = -con; |
| 1549 | res = con; |
| 1550 | return lp; |
| 1551 | } |
| 1552 | |
| 1553 | band** |
| 1554 | unpacker::attr_definitions::popBody(int bs_base) { |
| 1555 | // Return everything that was pushed, as a null-terminated pointer array. |
| 1556 | int bs_limit = band_stack.length(); |
| 1557 | if (bs_base == bs_limit) { |
| 1558 | return no_bands; |
| 1559 | } else { |
| 1560 | int nb = bs_limit - bs_base; |
| 1561 | band** res = U_NEW(band*, nb+1); |
| 1562 | CHECK_(no_bands); |
| 1563 | for (int i = 0; i < nb; i++) { |
| 1564 | band* b = (band*) band_stack.get(bs_base + i); |
| 1565 | res[i] = b; |
| 1566 | } |
| 1567 | band_stack.popTo(bs_base); |
| 1568 | return res; |
| 1569 | } |
| 1570 | } |
| 1571 | |
| 1572 | const char* |
| 1573 | unpacker::attr_definitions::parseLayout(const char* lp, band** &res, |
| 1574 | int curCble) { |
| 1575 | const char* lp0 = lp; |
| 1576 | int bs_base = band_stack.length(); |
| 1577 | bool top_level = (bs_base == 0); |
| 1578 | band* b; |
| 1579 | enum { can_be_signed = true }; // optional arg to parseIntLayout |
| 1580 | |
| 1581 | for (bool done = false; !done; ) { |
| 1582 | switch (*lp++) { |
| 1583 | case 'B': case 'H': case 'I': case 'V': // unsigned_int |
| 1584 | case 'S': // signed_int |
| 1585 | --lp; // reparse |
| 1586 | case 'F': |
| 1587 | lp = parseIntLayout(lp, b, EK_INT); |
| 1588 | break; |
| 1589 | case 'P': |
| 1590 | { |
| 1591 | int le_bci = EK_BCI; |
| 1592 | if (*lp == 'O') { |
| 1593 | ++lp; |
| 1594 | le_bci = EK_BCID; |
| 1595 | } |
| 1596 | assert(*lp != 'S'); // no PSH, etc. |
| 1597 | lp = parseIntLayout(lp, b, EK_INT); |
| 1598 | b->le_bci = le_bci; |
| 1599 | if (le_bci == EK_BCI) |
| 1600 | b->defc = coding::findBySpec(BCI5_spec); |
| 1601 | else |
| 1602 | b->defc = coding::findBySpec(BRANCH5_spec); |
| 1603 | } |
| 1604 | break; |
| 1605 | case 'O': |
| 1606 | lp = parseIntLayout(lp, b, EK_INT, can_be_signed); |
| 1607 | b->le_bci = EK_BCO; |
| 1608 | b->defc = coding::findBySpec(BRANCH5_spec); |
| 1609 | break; |
| 1610 | case 'N': // replication: 'N' uint '[' elem ... ']' |
| 1611 | lp = parseIntLayout(lp, b, EK_REPL); |
| 1612 | assert(*lp == '['); |
| 1613 | ++lp; |
| 1614 | lp = parseLayout(lp, b->le_body, curCble); |
| 1615 | CHECK_(lp); |
| 1616 | break; |
| 1617 | case 'T': // union: 'T' any_int union_case* '(' ')' '[' body ']' |
| 1618 | lp = parseIntLayout(lp, b, EK_UN, can_be_signed); |
| 1619 | { |
| 1620 | int union_base = band_stack.length(); |
| 1621 | for (;;) { // for each case |
| 1622 | band& k_case = *U_NEW(band, 1); |
| 1623 | CHECK_(lp); |
| 1624 | band_stack.add(&k_case); |
| 1625 | k_case.le_kind = EK_CASE; |
| 1626 | k_case.bn = bands_made++; |
| 1627 | if (*lp++ != '(') { |
| 1628 | abort("bad union case"); |
| 1629 | return ""; |
| 1630 | } |
| 1631 | if (*lp++ != ')') { |
| 1632 | --lp; // reparse |
| 1633 | // Read some case values. (Use band_stack for temp. storage.) |
| 1634 | int case_base = band_stack.length(); |
| 1635 | for (;;) { |
| 1636 | int caseval = 0; |
| 1637 | lp = parseNumeral(lp, caseval); |
| 1638 | band_stack.add((void*)caseval); |
| 1639 | if (*lp == '-') { |
| 1640 | // new in version 160, allow (1-5) for (1,2,3,4,5) |
| 1641 | if (u->majver < JAVA6_PACKAGE_MAJOR_VERSION) { |
| 1642 | abort("bad range in union case label (old archive format)"); |
| 1643 | return ""; |
| 1644 | } |
| 1645 | int caselimit = caseval; |
| 1646 | lp++; |
| 1647 | lp = parseNumeral(lp, caselimit); |
| 1648 | if (caseval >= caselimit |
| 1649 | || (uint)(caselimit - caseval) > 0x10000) { |
| 1650 | // Note: 0x10000 is arbitrary implementation restriction. |
| 1651 | // We can remove it later if it's important to. |
| 1652 | abort("bad range in union case label"); |
| 1653 | return ""; |
| 1654 | } |
| 1655 | for (;;) { |
| 1656 | ++caseval; |
| 1657 | band_stack.add((void*)caseval); |
| 1658 | if (caseval == caselimit) break; |
| 1659 | } |
| 1660 | } |
| 1661 | if (*lp != ',') break; |
| 1662 | lp++; |
| 1663 | } |
| 1664 | if (*lp++ != ')') { |
| 1665 | abort("bad case label"); |
| 1666 | return ""; |
| 1667 | } |
| 1668 | // save away the case labels |
| 1669 | int ntags = band_stack.length() - case_base; |
| 1670 | int* tags = U_NEW(int, 1+ntags); |
| 1671 | CHECK_(lp); |
| 1672 | k_case.le_casetags = tags; |
| 1673 | *tags++ = ntags; |
| 1674 | for (int i = 0; i < ntags; i++) { |
| 1675 | *tags++ = ptrlowbits(band_stack.get(case_base+i)); |
| 1676 | } |
| 1677 | band_stack.popTo(case_base); |
| 1678 | CHECK_(lp); |
| 1679 | } |
| 1680 | // Got le_casetags. Now grab the body. |
| 1681 | assert(*lp == '['); |
| 1682 | ++lp; |
| 1683 | lp = parseLayout(lp, k_case.le_body, curCble); |
| 1684 | CHECK_(lp); |
| 1685 | if (k_case.le_casetags == null) break; // done |
| 1686 | } |
| 1687 | b->le_body = popBody(union_base); |
| 1688 | } |
| 1689 | break; |
| 1690 | case '(': // call: '(' -?NN* ')' |
| 1691 | { |
| 1692 | band& call = *U_NEW(band, 1); |
| 1693 | CHECK_(lp); |
| 1694 | band_stack.add(&call); |
| 1695 | call.le_kind = EK_CALL; |
| 1696 | call.bn = bands_made++; |
| 1697 | call.le_body = U_NEW(band*, 2); // fill in later |
| 1698 | int call_num = 0; |
| 1699 | lp = parseNumeral(lp, call_num); |
| 1700 | call.le_back = (call_num <= 0); |
| 1701 | call_num += curCble; // numeral is self-relative offset |
| 1702 | call.le_len = call_num; //use le_len as scratch |
| 1703 | calls_to_link.add(&call); |
| 1704 | CHECK_(lp); |
| 1705 | if (*lp++ != ')') { |
| 1706 | abort("bad call label"); |
| 1707 | return ""; |
| 1708 | } |
| 1709 | } |
| 1710 | break; |
| 1711 | case 'K': // reference_type: constant_ref |
| 1712 | case 'R': // reference_type: schema_ref |
| 1713 | { |
| 1714 | int ixTag = CONSTANT_None; |
| 1715 | if (lp[-1] == 'K') { |
| 1716 | switch (*lp++) { |
| 1717 | case 'I': ixTag = CONSTANT_Integer; break; |
| 1718 | case 'J': ixTag = CONSTANT_Long; break; |
| 1719 | case 'F': ixTag = CONSTANT_Float; break; |
| 1720 | case 'D': ixTag = CONSTANT_Double; break; |
| 1721 | case 'S': ixTag = CONSTANT_String; break; |
| 1722 | case 'Q': ixTag = CONSTANT_Literal; break; |
| 1723 | } |
| 1724 | } else { |
| 1725 | switch (*lp++) { |
| 1726 | case 'C': ixTag = CONSTANT_Class; break; |
| 1727 | case 'S': ixTag = CONSTANT_Signature; break; |
| 1728 | case 'D': ixTag = CONSTANT_NameandType; break; |
| 1729 | case 'F': ixTag = CONSTANT_Fieldref; break; |
| 1730 | case 'M': ixTag = CONSTANT_Methodref; break; |
| 1731 | case 'I': ixTag = CONSTANT_InterfaceMethodref; break; |
| 1732 | case 'U': ixTag = CONSTANT_Utf8; break; //utf8_ref |
| 1733 | case 'Q': ixTag = CONSTANT_All; break; //untyped_ref |
| 1734 | } |
| 1735 | } |
| 1736 | if (ixTag == CONSTANT_None) { |
| 1737 | abort("bad reference layout"); |
| 1738 | break; |
| 1739 | } |
| 1740 | bool nullOK = false; |
| 1741 | if (*lp == 'N') { |
| 1742 | nullOK = true; |
| 1743 | lp++; |
| 1744 | } |
| 1745 | lp = parseIntLayout(lp, b, EK_REF); |
| 1746 | b->defc = coding::findBySpec(UNSIGNED5_spec); |
| 1747 | b->initRef(ixTag, nullOK); |
| 1748 | } |
| 1749 | break; |
| 1750 | case '[': |
| 1751 | { |
| 1752 | // [callable1][callable2]... |
| 1753 | if (!top_level) { |
| 1754 | abort("bad nested callable"); |
| 1755 | break; |
| 1756 | } |
| 1757 | curCble += 1; |
| 1758 | NOT_PRODUCT(int call_num = band_stack.length() - bs_base); |
| 1759 | band& cble = *U_NEW(band, 1); |
| 1760 | CHECK_(lp); |
| 1761 | band_stack.add(&cble); |
| 1762 | cble.le_kind = EK_CBLE; |
| 1763 | NOT_PRODUCT(cble.le_len = call_num); |
| 1764 | cble.bn = bands_made++; |
| 1765 | lp = parseLayout(lp, cble.le_body, curCble); |
| 1766 | } |
| 1767 | break; |
| 1768 | case ']': |
| 1769 | // Hit a closing brace. This ends whatever body we were in. |
| 1770 | done = true; |
| 1771 | break; |
| 1772 | case '\0': |
| 1773 | // Hit a null. Also ends the (top-level) body. |
| 1774 | --lp; // back up, so caller can see the null also |
| 1775 | done = true; |
| 1776 | break; |
| 1777 | default: |
| 1778 | abort("bad layout"); |
| 1779 | break; |
| 1780 | } |
| 1781 | CHECK_(lp); |
| 1782 | } |
| 1783 | |
| 1784 | // Return the accumulated bands: |
| 1785 | res = popBody(bs_base); |
| 1786 | return lp; |
| 1787 | } |
| 1788 | |
| 1789 | void unpacker::read_attr_defs() { |
| 1790 | int i; |
| 1791 | |
| 1792 | // Tell each AD which attrc it is and where its fixed flags are: |
| 1793 | attr_defs[ATTR_CONTEXT_CLASS].attrc = ATTR_CONTEXT_CLASS; |
| 1794 | attr_defs[ATTR_CONTEXT_CLASS].xxx_flags_hi_bn = e_class_flags_hi; |
| 1795 | attr_defs[ATTR_CONTEXT_FIELD].attrc = ATTR_CONTEXT_FIELD; |
| 1796 | attr_defs[ATTR_CONTEXT_FIELD].xxx_flags_hi_bn = e_field_flags_hi; |
| 1797 | attr_defs[ATTR_CONTEXT_METHOD].attrc = ATTR_CONTEXT_METHOD; |
| 1798 | attr_defs[ATTR_CONTEXT_METHOD].xxx_flags_hi_bn = e_method_flags_hi; |
| 1799 | attr_defs[ATTR_CONTEXT_CODE].attrc = ATTR_CONTEXT_CODE; |
| 1800 | attr_defs[ATTR_CONTEXT_CODE].xxx_flags_hi_bn = e_code_flags_hi; |
| 1801 | |
| 1802 | // Decide whether bands for the optional high flag words are present. |
| 1803 | attr_defs[ATTR_CONTEXT_CLASS] |
| 1804 | .setHaveLongFlags((archive_options & AO_HAVE_CLASS_FLAGS_HI) != 0); |
| 1805 | attr_defs[ATTR_CONTEXT_FIELD] |
| 1806 | .setHaveLongFlags((archive_options & AO_HAVE_FIELD_FLAGS_HI) != 0); |
| 1807 | attr_defs[ATTR_CONTEXT_METHOD] |
| 1808 | .setHaveLongFlags((archive_options & AO_HAVE_METHOD_FLAGS_HI) != 0); |
| 1809 | attr_defs[ATTR_CONTEXT_CODE] |
| 1810 | .setHaveLongFlags((archive_options & AO_HAVE_CODE_FLAGS_HI) != 0); |
| 1811 | |
| 1812 | // Set up built-in attrs. |
| 1813 | // (The simple ones are hard-coded. The metadata layouts are not.) |
| 1814 | const char* md_layout = ( |
| 1815 | // parameter annotations: |
| 1816 | #define MDL0 \ |
| 1817 | "[NB[(1)]]" |
| 1818 | MDL0 |
| 1819 | // annotations: |
| 1820 | #define MDL1 \ |
| 1821 | "[NH[(1)]]" \ |
| 1822 | "[RSHNH[RUH(1)]]" |
| 1823 | MDL1 |
| 1824 | // member_value: |
| 1825 | "[TB" |
| 1826 | "(66,67,73,83,90)[KIH]" |
| 1827 | "(68)[KDH]" |
| 1828 | "(70)[KFH]" |
| 1829 | "(74)[KJH]" |
| 1830 | "(99)[RSH]" |
| 1831 | "(101)[RSHRUH]" |
| 1832 | "(115)[RUH]" |
| 1833 | "(91)[NH[(0)]]" |
| 1834 | "(64)[" |
| 1835 | // nested annotation: |
| 1836 | "RSH" |
| 1837 | "NH[RUH(0)]" |
| 1838 | "]" |
| 1839 | "()[]" |
| 1840 | "]" |
| 1841 | ); |
| 1842 | |
| 1843 | const char* md_layout_P = md_layout; |
| 1844 | const char* md_layout_A = md_layout+strlen(MDL0); |
| 1845 | const char* md_layout_V = md_layout+strlen(MDL0 MDL1); |
| 1846 | assert(0 == strncmp(&md_layout_A[-3], ")]][", 4)); |
| 1847 | assert(0 == strncmp(&md_layout_V[-3], ")]][", 4)); |
| 1848 | |
| 1849 | for (i = 0; i < ATTR_CONTEXT_LIMIT; i++) { |
| 1850 | attr_definitions& ad = attr_defs[i]; |
| 1851 | ad.defineLayout(X_ATTR_RuntimeVisibleAnnotations, |
| 1852 | "RuntimeVisibleAnnotations", md_layout_A); |
| 1853 | ad.defineLayout(X_ATTR_RuntimeInvisibleAnnotations, |
| 1854 | "RuntimeInvisibleAnnotations", md_layout_A); |
| 1855 | if (i != ATTR_CONTEXT_METHOD) continue; |
| 1856 | ad.defineLayout(METHOD_ATTR_RuntimeVisibleParameterAnnotations, |
| 1857 | "RuntimeVisibleParameterAnnotations", md_layout_P); |
| 1858 | ad.defineLayout(METHOD_ATTR_RuntimeInvisibleParameterAnnotations, |
| 1859 | "RuntimeInvisibleParameterAnnotations", md_layout_P); |
| 1860 | ad.defineLayout(METHOD_ATTR_AnnotationDefault, |
| 1861 | "AnnotationDefault", md_layout_V); |
| 1862 | } |
| 1863 | |
| 1864 | attr_definition_headers.readData(attr_definition_count); |
| 1865 | attr_definition_name.readData(attr_definition_count); |
| 1866 | attr_definition_layout.readData(attr_definition_count); |
| 1867 | |
| 1868 | CHECK; |
| 1869 | |
| 1870 | // Initialize correct predef bits, to distinguish predefs from new defs. |
| 1871 | #define ORBIT(n,s) |((julong)1<<n) |
| 1872 | attr_defs[ATTR_CONTEXT_CLASS].predef |
| 1873 | = (0 X_ATTR_DO(ORBIT) CLASS_ATTR_DO(ORBIT)); |
| 1874 | attr_defs[ATTR_CONTEXT_FIELD].predef |
| 1875 | = (0 X_ATTR_DO(ORBIT) FIELD_ATTR_DO(ORBIT)); |
| 1876 | attr_defs[ATTR_CONTEXT_METHOD].predef |
| 1877 | = (0 X_ATTR_DO(ORBIT) METHOD_ATTR_DO(ORBIT)); |
| 1878 | attr_defs[ATTR_CONTEXT_CODE].predef |
| 1879 | = (0 O_ATTR_DO(ORBIT) CODE_ATTR_DO(ORBIT)); |
| 1880 | #undef ORBIT |
| 1881 | // Clear out the redef bits, folding them back into predef. |
| 1882 | for (i = 0; i < ATTR_CONTEXT_LIMIT; i++) { |
| 1883 | attr_defs[i].predef |= attr_defs[i].redef; |
| 1884 | attr_defs[i].redef = 0; |
| 1885 | } |
| 1886 | |
| 1887 | // Now read the transmitted locally defined attrs. |
| 1888 | // This will set redef bits again. |
| 1889 | for (i = 0; i < attr_definition_count; i++) { |
| 1890 | int header = attr_definition_headers.getByte(); |
| 1891 | int attrc = ADH_BYTE_CONTEXT(header); |
| 1892 | int idx = ADH_BYTE_INDEX(header); |
| 1893 | entry* name = attr_definition_name.getRef(); |
| 1894 | entry* layout = attr_definition_layout.getRef(); |
| 1895 | CHECK; |
| 1896 | attr_defs[attrc].defineLayout(idx, name, layout->value.b.strval()); |
| 1897 | } |
| 1898 | } |
| 1899 | |
| 1900 | #define NO_ENTRY_YET ((entry*)-1) |
| 1901 | |
| 1902 | static bool isDigitString(bytes& x, int beg, int end) { |
| 1903 | if (beg == end) return false; // null string |
| 1904 | byte* xptr = x.ptr; |
| 1905 | for (int i = beg; i < end; i++) { |
| 1906 | char ch = xptr[i]; |
| 1907 | if (!(ch >= '0' && ch <= '9')) return false; |
| 1908 | } |
| 1909 | return true; |
| 1910 | } |
| 1911 | |
| 1912 | enum { // constants for parsing class names |
| 1913 | SLASH_MIN = '.', |
| 1914 | SLASH_MAX = '/', |
| 1915 | DOLLAR_MIN = 0, |
| 1916 | DOLLAR_MAX = '-' |
| 1917 | }; |
| 1918 | |
| 1919 | static int lastIndexOf(int chmin, int chmax, bytes& x, int pos) { |
| 1920 | byte* ptr = x.ptr; |
| 1921 | for (byte* cp = ptr + pos; --cp >= ptr; ) { |
| 1922 | assert(x.inBounds(cp)); |
| 1923 | if (*cp >= chmin && *cp <= chmax) |
| 1924 | return cp - ptr; |
| 1925 | } |
| 1926 | return -1; |
| 1927 | } |
| 1928 | |
| 1929 | maybe_inline |
| 1930 | inner_class* cpool::getIC(entry* inner) { |
| 1931 | if (inner == null) return null; |
| 1932 | assert(inner->tag == CONSTANT_Class); |
| 1933 | if (inner->inord == NO_INORD) return null; |
| 1934 | inner_class* ic = ic_index[inner->inord]; |
| 1935 | assert(ic == null || ic->inner == inner); |
| 1936 | return ic; |
| 1937 | } |
| 1938 | |
| 1939 | maybe_inline |
| 1940 | inner_class* cpool::getFirstChildIC(entry* outer) { |
| 1941 | if (outer == null) return null; |
| 1942 | assert(outer->tag == CONSTANT_Class); |
| 1943 | if (outer->inord == NO_INORD) return null; |
| 1944 | inner_class* ic = ic_child_index[outer->inord]; |
| 1945 | assert(ic == null || ic->outer == outer); |
| 1946 | return ic; |
| 1947 | } |
| 1948 | |
| 1949 | maybe_inline |
| 1950 | inner_class* cpool::getNextChildIC(inner_class* child) { |
| 1951 | inner_class* ic = child->next_sibling; |
| 1952 | assert(ic == null || ic->outer == child->outer); |
| 1953 | return ic; |
| 1954 | } |
| 1955 | |
| 1956 | void unpacker::read_ics() { |
| 1957 | int i; |
| 1958 | int index_size = cp.tag_count[CONSTANT_Class]; |
| 1959 | inner_class** ic_index = U_NEW(inner_class*, index_size); |
| 1960 | inner_class** ic_child_index = U_NEW(inner_class*, index_size); |
| 1961 | cp.ic_index = ic_index; |
| 1962 | cp.ic_child_index = ic_child_index; |
| 1963 | ics = U_NEW(inner_class, ic_count); |
| 1964 | ic_this_class.readData(ic_count); |
| 1965 | ic_flags.readData(ic_count); |
| 1966 | CHECK; |
| 1967 | // Scan flags to get count of long-form bands. |
| 1968 | int long_forms = 0; |
| 1969 | for (i = 0; i < ic_count; i++) { |
| 1970 | int flags = ic_flags.getInt(); // may be long form! |
| 1971 | if ((flags & ACC_IC_LONG_FORM) != 0) { |
| 1972 | long_forms += 1; |
| 1973 | ics[i].name = NO_ENTRY_YET; |
| 1974 | } |
| 1975 | flags &= ~ACC_IC_LONG_FORM; |
| 1976 | entry* inner = ic_this_class.getRef(); |
| 1977 | CHECK; |
| 1978 | uint inord = inner->inord; |
| 1979 | assert(inord < cp.tag_count[CONSTANT_Class]); |
| 1980 | if (ic_index[inord] != null) { |
| 1981 | abort("identical inner class"); |
| 1982 | break; |
| 1983 | } |
| 1984 | ic_index[inord] = &ics[i]; |
| 1985 | ics[i].inner = inner; |
| 1986 | ics[i].flags = flags; |
| 1987 | assert(cp.getIC(inner) == &ics[i]); |
| 1988 | } |
| 1989 | CHECK; |
| 1990 | //ic_this_class.done(); |
| 1991 | //ic_flags.done(); |
| 1992 | ic_outer_class.readData(long_forms); |
| 1993 | ic_name.readData(long_forms); |
| 1994 | for (i = 0; i < ic_count; i++) { |
| 1995 | if (ics[i].name == NO_ENTRY_YET) { |
| 1996 | // Long form. |
| 1997 | ics[i].outer = ic_outer_class.getRefN(); |
| 1998 | ics[i].name = ic_name.getRefN(); |
| 1999 | } else { |
| 2000 | // Fill in outer and name based on inner. |
| 2001 | bytes& n = ics[i].inner->value.b; |
| 2002 | bytes pkgOuter; |
| 2003 | bytes number; |
| 2004 | bytes name; |
| 2005 | // Parse n into pkgOuter and name (and number). |
| 2006 | printcr(5, "parse short IC name %s", n.ptr); |
| 2007 | int dollar1, dollar2; // pointers to $ in the pattern |
| 2008 | // parse n = (<pkg>/)*<outer>($<number>)?($<name>)? |
| 2009 | int nlen = n.len; |
| 2010 | int pkglen = lastIndexOf(SLASH_MIN, SLASH_MAX, n, nlen) + 1; |
| 2011 | dollar2 = lastIndexOf(DOLLAR_MIN, DOLLAR_MAX, n, nlen); |
| 2012 | if (dollar2 < 0) { |
| 2013 | abort(); |
| 2014 | return; |
| 2015 | } |
| 2016 | assert(dollar2 >= pkglen); |
| 2017 | if (isDigitString(n, dollar2+1, nlen)) { |
| 2018 | // n = (<pkg>/)*<outer>$<number> |
| 2019 | number = n.slice(dollar2+1, nlen); |
| 2020 | name.set(null,0); |
| 2021 | dollar1 = dollar2; |
| 2022 | } else if (pkglen < (dollar1 |
| 2023 | = lastIndexOf(DOLLAR_MIN, DOLLAR_MAX, n, dollar2-1)) |
| 2024 | && isDigitString(n, dollar1+1, dollar2)) { |
| 2025 | // n = (<pkg>/)*<outer>$<number>$<name> |
| 2026 | number = n.slice(dollar1+1, dollar2); |
| 2027 | name = n.slice(dollar2+1, nlen); |
| 2028 | } else { |
| 2029 | // n = (<pkg>/)*<outer>$<name> |
| 2030 | dollar1 = dollar2; |
| 2031 | number.set(null,0); |
| 2032 | name = n.slice(dollar2+1, nlen); |
| 2033 | } |
| 2034 | if (number.ptr == null) |
| 2035 | pkgOuter = n.slice(0, dollar1); |
| 2036 | else |
| 2037 | pkgOuter.set(null,0); |
| 2038 | printcr(5,"=> %s$ 0%s $%s", |
| 2039 | pkgOuter.string(), number.string(), name.string()); |
| 2040 | |
| 2041 | if (pkgOuter.ptr != null) |
| 2042 | ics[i].outer = cp.ensureClass(pkgOuter); |
| 2043 | |
| 2044 | if (name.ptr != null) |
| 2045 | ics[i].name = cp.ensureUtf8(name); |
| 2046 | } |
| 2047 | |
| 2048 | // update child/sibling list |
| 2049 | if (ics[i].outer != null) { |
| 2050 | uint outord = ics[i].outer->inord; |
| 2051 | if (outord != NO_INORD) { |
| 2052 | assert(outord < cp.tag_count[CONSTANT_Class]); |
| 2053 | ics[i].next_sibling = ic_child_index[outord]; |
| 2054 | ic_child_index[outord] = &ics[i]; |
| 2055 | } |
| 2056 | } |
| 2057 | } |
| 2058 | //ic_outer_class.done(); |
| 2059 | //ic_name.done(); |
| 2060 | } |
| 2061 | |
| 2062 | void unpacker::read_classes() { |
| 2063 | int i; |
| 2064 | printcr(1," ...scanning %d classes...", class_count); |
| 2065 | class_this.readData(class_count); |
| 2066 | class_super.readData(class_count); |
| 2067 | class_interface_count.readData(class_count); |
| 2068 | class_interface.readData(class_interface_count.getIntTotal()); |
| 2069 | |
| 2070 | CHECK; |
| 2071 | |
| 2072 | #if 0 |
| 2073 | // Make a little mark on super-classes. |
| 2074 | for (i = 0; i < class_count; i++) { |
| 2075 | entry* e = class_super.getRefN(); |
| 2076 | if (e != null) e->bits |= entry::EB_SUPER; |
| 2077 | } |
| 2078 | class_super.rewind(); |
| 2079 | #endif |
| 2080 | |
| 2081 | // Members. |
| 2082 | class_field_count.readData(class_count); |
| 2083 | class_method_count.readData(class_count); |
| 2084 | |
| 2085 | CHECK; |
| 2086 | |
| 2087 | int field_count = class_field_count.getIntTotal(); |
| 2088 | int method_count = class_method_count.getIntTotal(); |
| 2089 | |
| 2090 | field_descr.readData(field_count); |
| 2091 | read_attrs(ATTR_CONTEXT_FIELD, field_count); |
| 2092 | |
| 2093 | method_descr.readData(method_count); |
| 2094 | read_attrs(ATTR_CONTEXT_METHOD, method_count); |
| 2095 | |
| 2096 | CHECK; |
| 2097 | |
| 2098 | read_attrs(ATTR_CONTEXT_CLASS, class_count); |
| 2099 | |
| 2100 | read_code_headers(); |
| 2101 | |
| 2102 | printcr(1,"scanned %d classes, %d fields, %d methods, %d code headers", |
| 2103 | class_count, field_count, method_count, code_count); |
| 2104 | } |
| 2105 | |
| 2106 | maybe_inline |
| 2107 | int unpacker::attr_definitions::predefCount(uint idx) { |
| 2108 | return isPredefined(idx) ? flag_count[idx] : 0; |
| 2109 | } |
| 2110 | |
| 2111 | void unpacker::read_attrs(int attrc, int obj_count) { |
| 2112 | attr_definitions& ad = attr_defs[attrc]; |
| 2113 | assert(ad.attrc == attrc); |
| 2114 | |
| 2115 | int i, idx, count; |
| 2116 | |
| 2117 | CHECK; |
| 2118 | |
| 2119 | bool haveLongFlags = ad.haveLongFlags(); |
| 2120 | |
| 2121 | band& xxx_flags_hi = ad.xxx_flags_hi(); |
| 2122 | assert(endsWith(xxx_flags_hi.name, "_flags_hi")); |
| 2123 | if (haveLongFlags) |
| 2124 | xxx_flags_hi.readData(obj_count); |
| 2125 | |
| 2126 | band& xxx_flags_lo = ad.xxx_flags_lo(); |
| 2127 | assert(endsWith(xxx_flags_lo.name, "_flags_lo")); |
| 2128 | xxx_flags_lo.readData(obj_count); |
| 2129 | |
| 2130 | // pre-scan flags, counting occurrences of each index bit |
| 2131 | julong indexMask = ad.flagIndexMask(); // which flag bits are index bits? |
| 2132 | for (i = 0; i < obj_count; i++) { |
| 2133 | julong indexBits = xxx_flags_hi.getLong(xxx_flags_lo, haveLongFlags); |
| 2134 | if ((indexBits & ~indexMask) > (ushort)-1) { |
| 2135 | abort("undefined attribute flag bit"); |
| 2136 | return; |
| 2137 | } |
| 2138 | indexBits &= indexMask; // ignore classfile flag bits |
| 2139 | for (idx = 0; indexBits != 0; idx++, indexBits >>= 1) { |
| 2140 | ad.flag_count[idx] += (indexBits & 1); |
| 2141 | } |
| 2142 | } |
| 2143 | // we'll scan these again later for output: |
| 2144 | xxx_flags_lo.rewind(); |
| 2145 | xxx_flags_hi.rewind(); |
| 2146 | |
| 2147 | band& xxx_attr_count = ad.xxx_attr_count(); |
| 2148 | assert(endsWith(xxx_attr_count.name, "_attr_count")); |
| 2149 | // There is one count element for each 1<<16 bit set in flags: |
| 2150 | xxx_attr_count.readData(ad.predefCount(X_ATTR_OVERFLOW)); |
| 2151 | |
| 2152 | band& xxx_attr_indexes = ad.xxx_attr_indexes(); |
| 2153 | assert(endsWith(xxx_attr_indexes.name, "_attr_indexes")); |
| 2154 | int overflowIndexCount = xxx_attr_count.getIntTotal(); |
| 2155 | xxx_attr_indexes.readData(overflowIndexCount); |
| 2156 | // pre-scan attr indexes, counting occurrences of each value |
| 2157 | for (i = 0; i < overflowIndexCount; i++) { |
| 2158 | idx = xxx_attr_indexes.getInt(); |
| 2159 | if (!ad.isIndex(idx)) { |
| 2160 | abort("attribute index out of bounds"); |
| 2161 | return; |
| 2162 | } |
| 2163 | ad.getCount(idx) += 1; |
| 2164 | } |
| 2165 | xxx_attr_indexes.rewind(); // we'll scan it again later for output |
| 2166 | |
| 2167 | // We will need a backward call count for each used backward callable. |
| 2168 | int backwardCounts = 0; |
| 2169 | for (idx = 0; idx < ad.layouts.length(); idx++) { |
| 2170 | layout_definition* lo = ad.getLayout(idx); |
| 2171 | if (lo != null && ad.getCount(idx) != 0) { |
| 2172 | // Build the bands lazily, only when they are used. |
| 2173 | band** bands = ad.buildBands(lo); |
| 2174 | CHECK; |
| 2175 | if (lo->hasCallables()) { |
| 2176 | for (i = 0; bands[i] != null; i++) { |
| 2177 | if (bands[i]->le_back) { |
| 2178 | assert(bands[i]->le_kind == EK_CBLE); |
| 2179 | backwardCounts += 1; |
| 2180 | } |
| 2181 | } |
| 2182 | } |
| 2183 | } |
| 2184 | } |
| 2185 | ad.xxx_attr_calls().readData(backwardCounts); |
| 2186 | |
| 2187 | // Read built-in bands. |
| 2188 | // Mostly, these are hand-coded equivalents to readBandData(). |
| 2189 | switch (attrc) { |
| 2190 | case ATTR_CONTEXT_CLASS: |
| 2191 | |
| 2192 | count = ad.predefCount(CLASS_ATTR_SourceFile); |
| 2193 | class_SourceFile_RUN.readData(count); |
| 2194 | |
| 2195 | count = ad.predefCount(CLASS_ATTR_EnclosingMethod); |
| 2196 | class_EnclosingMethod_RC.readData(count); |
| 2197 | class_EnclosingMethod_RDN.readData(count); |
| 2198 | |
| 2199 | count = ad.predefCount(X_ATTR_Signature); |
| 2200 | class_Signature_RS.readData(count); |
| 2201 | |
| 2202 | ad.readBandData(X_ATTR_RuntimeVisibleAnnotations); |
| 2203 | ad.readBandData(X_ATTR_RuntimeInvisibleAnnotations); |
| 2204 | |
| 2205 | count = ad.predefCount(CLASS_ATTR_InnerClasses); |
| 2206 | class_InnerClasses_N.readData(count); |
| 2207 | count = class_InnerClasses_N.getIntTotal(); |
| 2208 | class_InnerClasses_RC.readData(count); |
| 2209 | class_InnerClasses_F.readData(count); |
| 2210 | // Drop remaining columns wherever flags are zero: |
| 2211 | count -= class_InnerClasses_F.getIntCount(0); |
| 2212 | class_InnerClasses_outer_RCN.readData(count); |
| 2213 | class_InnerClasses_name_RUN.readData(count); |
| 2214 | |
| 2215 | count = ad.predefCount(CLASS_ATTR_ClassFile_version); |
| 2216 | class_ClassFile_version_minor_H.readData(count); |
| 2217 | class_ClassFile_version_major_H.readData(count); |
| 2218 | break; |
| 2219 | |
| 2220 | case ATTR_CONTEXT_FIELD: |
| 2221 | |
| 2222 | count = ad.predefCount(FIELD_ATTR_ConstantValue); |
| 2223 | field_ConstantValue_KQ.readData(count); |
| 2224 | |
| 2225 | count = ad.predefCount(X_ATTR_Signature); |
| 2226 | field_Signature_RS.readData(count); |
| 2227 | |
| 2228 | ad.readBandData(X_ATTR_RuntimeVisibleAnnotations); |
| 2229 | ad.readBandData(X_ATTR_RuntimeInvisibleAnnotations); |
| 2230 | break; |
| 2231 | |
| 2232 | case ATTR_CONTEXT_METHOD: |
| 2233 | |
| 2234 | code_count = ad.predefCount(METHOD_ATTR_Code); |
| 2235 | // Code attrs are handled very specially below... |
| 2236 | |
| 2237 | count = ad.predefCount(METHOD_ATTR_Exceptions); |
| 2238 | method_Exceptions_N.readData(count); |
| 2239 | count = method_Exceptions_N.getIntTotal(); |
| 2240 | method_Exceptions_RC.readData(count); |
| 2241 | |
| 2242 | count = ad.predefCount(X_ATTR_Signature); |
| 2243 | method_Signature_RS.readData(count); |
| 2244 | |
| 2245 | ad.readBandData(X_ATTR_RuntimeVisibleAnnotations); |
| 2246 | ad.readBandData(X_ATTR_RuntimeInvisibleAnnotations); |
| 2247 | ad.readBandData(METHOD_ATTR_RuntimeVisibleParameterAnnotations); |
| 2248 | ad.readBandData(METHOD_ATTR_RuntimeInvisibleParameterAnnotations); |
| 2249 | ad.readBandData(METHOD_ATTR_AnnotationDefault); |
| 2250 | break; |
| 2251 | |
| 2252 | case ATTR_CONTEXT_CODE: |
| 2253 | // (keep this code aligned with its brother in unpacker::write_attrs) |
| 2254 | count = ad.predefCount(CODE_ATTR_StackMapTable); |
| 2255 | // disable this feature in old archives! |
| 2256 | if (count != 0 && majver < JAVA6_PACKAGE_MAJOR_VERSION) { |
| 2257 | abort("undefined StackMapTable attribute (old archive format)"); |
| 2258 | return; |
| 2259 | } |
| 2260 | code_StackMapTable_N.readData(count); |
| 2261 | count = code_StackMapTable_N.getIntTotal(); |
| 2262 | code_StackMapTable_frame_T.readData(count); |
| 2263 | // the rest of it depends in a complicated way on frame tags |
| 2264 | { |
| 2265 | int fat_frame_count = 0; |
| 2266 | int offset_count = 0; |
| 2267 | int type_count = 0; |
| 2268 | for (int k = 0; k < count; k++) { |
| 2269 | int tag = code_StackMapTable_frame_T.getByte(); |
| 2270 | if (tag <= 127) { |
| 2271 | // (64-127) [(2)] |
| 2272 | if (tag >= 64) type_count++; |
| 2273 | } else if (tag <= 251) { |
| 2274 | // (247) [(1)(2)] |
| 2275 | // (248-251) [(1)] |
| 2276 | if (tag >= 247) offset_count++; |
| 2277 | if (tag == 247) type_count++; |
| 2278 | } else if (tag <= 254) { |
| 2279 | // (252) [(1)(2)] |
| 2280 | // (253) [(1)(2)(2)] |
| 2281 | // (254) [(1)(2)(2)(2)] |
| 2282 | offset_count++; |
| 2283 | type_count += (tag - 251); |
| 2284 | } else { |
| 2285 | // (255) [(1)NH[(2)]NH[(2)]] |
| 2286 | fat_frame_count++; |
| 2287 | } |
| 2288 | } |
| 2289 | |
| 2290 | // done pre-scanning frame tags: |
| 2291 | code_StackMapTable_frame_T.rewind(); |
| 2292 | |
| 2293 | // deal completely with fat frames: |
| 2294 | offset_count += fat_frame_count; |
| 2295 | code_StackMapTable_local_N.readData(fat_frame_count); |
| 2296 | type_count += code_StackMapTable_local_N.getIntTotal(); |
| 2297 | code_StackMapTable_stack_N.readData(fat_frame_count); |
| 2298 | type_count += code_StackMapTable_stack_N.getIntTotal(); |
| 2299 | // read the rest: |
| 2300 | code_StackMapTable_offset.readData(offset_count); |
| 2301 | code_StackMapTable_T.readData(type_count); |
| 2302 | // (7) [RCH] |
| 2303 | count = code_StackMapTable_T.getIntCount(7); |
| 2304 | code_StackMapTable_RC.readData(count); |
| 2305 | // (8) [PH] |
| 2306 | count = code_StackMapTable_T.getIntCount(8); |
| 2307 | code_StackMapTable_P.readData(count); |
| 2308 | } |
| 2309 | |
| 2310 | count = ad.predefCount(CODE_ATTR_LineNumberTable); |
| 2311 | code_LineNumberTable_N.readData(count); |
| 2312 | count = code_LineNumberTable_N.getIntTotal(); |
| 2313 | code_LineNumberTable_bci_P.readData(count); |
| 2314 | code_LineNumberTable_line.readData(count); |
| 2315 | |
| 2316 | count = ad.predefCount(CODE_ATTR_LocalVariableTable); |
| 2317 | code_LocalVariableTable_N.readData(count); |
| 2318 | count = code_LocalVariableTable_N.getIntTotal(); |
| 2319 | code_LocalVariableTable_bci_P.readData(count); |
| 2320 | code_LocalVariableTable_span_O.readData(count); |
| 2321 | code_LocalVariableTable_name_RU.readData(count); |
| 2322 | code_LocalVariableTable_type_RS.readData(count); |
| 2323 | code_LocalVariableTable_slot.readData(count); |
| 2324 | |
| 2325 | count = ad.predefCount(CODE_ATTR_LocalVariableTypeTable); |
| 2326 | code_LocalVariableTypeTable_N.readData(count); |
| 2327 | count = code_LocalVariableTypeTable_N.getIntTotal(); |
| 2328 | code_LocalVariableTypeTable_bci_P.readData(count); |
| 2329 | code_LocalVariableTypeTable_span_O.readData(count); |
| 2330 | code_LocalVariableTypeTable_name_RU.readData(count); |
| 2331 | code_LocalVariableTypeTable_type_RS.readData(count); |
| 2332 | code_LocalVariableTypeTable_slot.readData(count); |
| 2333 | break; |
| 2334 | } |
| 2335 | |
| 2336 | // Read compressor-defined bands. |
| 2337 | for (idx = 0; idx < ad.layouts.length(); idx++) { |
| 2338 | if (ad.getLayout(idx) == null) |
| 2339 | continue; // none at this fixed index <32 |
| 2340 | if (idx < ad.flag_limit && ad.isPredefined(idx)) |
| 2341 | continue; // already handled |
| 2342 | if (ad.getCount(idx) == 0) |
| 2343 | continue; // no attributes of this type (then why transmit layouts?) |
| 2344 | ad.readBandData(idx); |
| 2345 | } |
| 2346 | } |
| 2347 | |
| 2348 | void unpacker::attr_definitions::readBandData(int idx) { |
| 2349 | int j; |
| 2350 | uint count = getCount(idx); |
| 2351 | if (count == 0) return; |
| 2352 | layout_definition* lo = getLayout(idx); |
| 2353 | if (lo != null) { |
| 2354 | printcr(1, "counted %d [redefined = %d predefined = %d] attributes of type %s.%s", |
| 2355 | count, isRedefined(idx), isPredefined(idx), |
| 2356 | ATTR_CONTEXT_NAME[attrc], lo->name); |
| 2357 | } |
| 2358 | bool hasCallables = lo->hasCallables(); |
| 2359 | band** bands = lo->bands(); |
| 2360 | if (!hasCallables) { |
| 2361 | // Read through the rest of the bands in a regular way. |
| 2362 | readBandData(bands, count); |
| 2363 | } else { |
| 2364 | // Deal with the callables. |
| 2365 | // First set up the forward entry count for each callable. |
| 2366 | // This is stored on band::length of the callable. |
| 2367 | bands[0]->expectMoreLength(count); |
| 2368 | for (j = 0; bands[j] != null; j++) { |
| 2369 | band& j_cble = *bands[j]; |
| 2370 | assert(j_cble.le_kind == EK_CBLE); |
| 2371 | if (j_cble.le_back) { |
| 2372 | // Add in the predicted effects of backward calls, too. |
| 2373 | int back_calls = xxx_attr_calls().getInt(); |
| 2374 | j_cble.expectMoreLength(back_calls); |
| 2375 | // In a moment, more forward calls may increment j_cble.length. |
| 2376 | } |
| 2377 | } |
| 2378 | // Now consult whichever callables have non-zero entry counts. |
| 2379 | readBandData(bands, -1); |
| 2380 | } |
| 2381 | } |
| 2382 | |
| 2383 | // Recursive helper to the previous function: |
| 2384 | void unpacker::attr_definitions::readBandData(band** body, uint count) { |
| 2385 | int i, j, k; |
| 2386 | for (j = 0; body[j] != null; j++) { |
| 2387 | band& b = *body[j]; |
| 2388 | if (b.defc != null) { |
| 2389 | // It has data, so read it. |
| 2390 | b.readData(count); |
| 2391 | } |
| 2392 | switch (b.le_kind) { |
| 2393 | case EK_REPL: |
| 2394 | { |
| 2395 | int reps = b.getIntTotal(); |
| 2396 | readBandData(b.le_body, reps); |
| 2397 | } |
| 2398 | break; |
| 2399 | case EK_UN: |
| 2400 | { |
| 2401 | int remaining = count; |
| 2402 | for (k = 0; b.le_body[k] != null; k++) { |
| 2403 | band& k_case = *b.le_body[k]; |
| 2404 | int k_count = 0; |
| 2405 | if (k_case.le_casetags == null) { |
| 2406 | k_count = remaining; // last (empty) case |
| 2407 | } else { |
| 2408 | int* tags = k_case.le_casetags; |
| 2409 | int ntags = *tags++; // 1st element is length (why not?) |
| 2410 | while (ntags-- > 0) { |
| 2411 | int tag = *tags++; |
| 2412 | k_count += b.getIntCount(tag); |
| 2413 | } |
| 2414 | } |
| 2415 | readBandData(k_case.le_body, k_count); |
| 2416 | remaining -= k_count; |
| 2417 | } |
| 2418 | assert(remaining == 0); |
| 2419 | } |
| 2420 | break; |
| 2421 | case EK_CALL: |
| 2422 | // Push the count forward, if it is not a backward call. |
| 2423 | if (!b.le_back) { |
| 2424 | band& cble = *b.le_body[0]; |
| 2425 | assert(cble.le_kind == EK_CBLE); |
| 2426 | cble.expectMoreLength(count); |
| 2427 | } |
| 2428 | break; |
| 2429 | case EK_CBLE: |
| 2430 | assert(count == -1); // incoming count is meaningless |
| 2431 | k = b.length; |
| 2432 | assert(k >= 0); |
| 2433 | // This is intended and required for non production mode. |
| 2434 | assert((b.length = -1)); // make it unable to accept more calls now. |
| 2435 | readBandData(b.le_body, k); |
| 2436 | break; |
| 2437 | } |
| 2438 | } |
| 2439 | } |
| 2440 | |
| 2441 | static inline |
| 2442 | band** findMatchingCase(int matchTag, band** cases) { |
| 2443 | for (int k = 0; cases[k] != null; k++) { |
| 2444 | band& k_case = *cases[k]; |
| 2445 | if (k_case.le_casetags != null) { |
| 2446 | // If it has tags, it must match a tag. |
| 2447 | int* tags = k_case.le_casetags; |
| 2448 | int ntags = *tags++; // 1st element is length |
| 2449 | for (; ntags > 0; ntags--) { |
| 2450 | int tag = *tags++; |
| 2451 | if (tag == matchTag) |
| 2452 | break; |
| 2453 | } |
| 2454 | if (ntags == 0) |
| 2455 | continue; // does not match |
| 2456 | } |
| 2457 | return k_case.le_body; |
| 2458 | } |
| 2459 | return null; |
| 2460 | } |
| 2461 | |
| 2462 | // write attribute band data: |
| 2463 | void unpacker::putlayout(band** body) { |
| 2464 | int i; |
| 2465 | int prevBII = -1; |
| 2466 | int prevBCI = -1; |
| 2467 | for (i = 0; body[i] != null; i++) { |
| 2468 | band& b = *body[i]; |
| 2469 | byte le_kind = b.le_kind; |
| 2470 | |
| 2471 | // Handle scalar part, if any. |
| 2472 | int x = 0; |
| 2473 | entry* e = null; |
| 2474 | if (b.defc != null) { |
| 2475 | // It has data, so unparse an element. |
| 2476 | if (b.ixTag != CONSTANT_None) { |
| 2477 | assert(le_kind == EK_REF); |
| 2478 | if (b.ixTag == CONSTANT_Literal) |
| 2479 | e = b.getRefUsing(cp.getKQIndex()); |
| 2480 | else |
| 2481 | e = b.getRefN(); |
| 2482 | switch (b.le_len) { |
| 2483 | case 0: break; |
| 2484 | case 1: putu1ref(e); break; |
| 2485 | case 2: putref(e); break; |
| 2486 | case 4: putu2(0); putref(e); break; |
| 2487 | default: assert(false); |
| 2488 | } |
| 2489 | } else { |
| 2490 | assert(le_kind == EK_INT || le_kind == EK_REPL || le_kind == EK_UN); |
| 2491 | x = b.getInt(); |
| 2492 | |
| 2493 | assert(!b.le_bci || prevBCI == to_bci(prevBII)); |
| 2494 | switch (b.le_bci) { |
| 2495 | case EK_BCI: // PH: transmit R(bci), store bci |
| 2496 | x = to_bci(prevBII = x); |
| 2497 | prevBCI = x; |
| 2498 | break; |
| 2499 | case EK_BCID: // POH: transmit D(R(bci)), store bci |
| 2500 | x = to_bci(prevBII += x); |
| 2501 | prevBCI = x; |
| 2502 | break; |
| 2503 | case EK_BCO: // OH: transmit D(R(bci)), store D(bci) |
| 2504 | x = to_bci(prevBII += x) - prevBCI; |
| 2505 | prevBCI += x; |
| 2506 | break; |
| 2507 | } |
| 2508 | assert(!b.le_bci || prevBCI == to_bci(prevBII)); |
| 2509 | |
| 2510 | switch (b.le_len) { |
| 2511 | case 0: break; |
| 2512 | case 1: putu1(x); break; |
| 2513 | case 2: putu2(x); break; |
| 2514 | case 4: putu4(x); break; |
| 2515 | default: assert(false); |
| 2516 | } |
| 2517 | } |
| 2518 | } |
| 2519 | |
| 2520 | // Handle subparts, if any. |
| 2521 | switch (le_kind) { |
| 2522 | case EK_REPL: |
| 2523 | // x is the repeat count |
| 2524 | while (x-- > 0) { |
| 2525 | putlayout(b.le_body); |
| 2526 | } |
| 2527 | break; |
| 2528 | case EK_UN: |
| 2529 | // x is the tag |
| 2530 | putlayout(findMatchingCase(x, b.le_body)); |
| 2531 | break; |
| 2532 | case EK_CALL: |
| 2533 | { |
| 2534 | band& cble = *b.le_body[0]; |
| 2535 | assert(cble.le_kind == EK_CBLE); |
| 2536 | assert(cble.le_len == b.le_len); |
| 2537 | putlayout(cble.le_body); |
| 2538 | } |
| 2539 | break; |
| 2540 | |
| 2541 | #ifndef PRODUCT |
| 2542 | case EK_CBLE: |
| 2543 | case EK_CASE: |
| 2544 | assert(false); // should not reach here |
| 2545 | #endif |
| 2546 | } |
| 2547 | } |
| 2548 | } |
| 2549 | |
| 2550 | void unpacker::read_files() { |
| 2551 | file_name.readData(file_count); |
| 2552 | if ((archive_options & AO_HAVE_FILE_SIZE_HI) != 0) |
| 2553 | file_size_hi.readData(file_count); |
| 2554 | file_size_lo.readData(file_count); |
| 2555 | if ((archive_options & AO_HAVE_FILE_MODTIME) != 0) |
| 2556 | file_modtime.readData(file_count); |
| 2557 | int allFiles = file_count + class_count; |
| 2558 | if ((archive_options & AO_HAVE_FILE_OPTIONS) != 0) { |
| 2559 | file_options.readData(file_count); |
| 2560 | // FO_IS_CLASS_STUB might be set, causing overlap between classes and files |
| 2561 | for (int i = 0; i < file_count; i++) { |
| 2562 | if ((file_options.getInt() & FO_IS_CLASS_STUB) != 0) { |
| 2563 | allFiles -= 1; // this one counts as both class and file |
| 2564 | } |
| 2565 | } |
| 2566 | file_options.rewind(); |
| 2567 | } |
| 2568 | assert((default_file_options & FO_IS_CLASS_STUB) == 0); |
| 2569 | files_remaining = allFiles; |
| 2570 | } |
| 2571 | |
| 2572 | maybe_inline |
| 2573 | void unpacker::get_code_header(int& max_stack, |
| 2574 | int& max_na_locals, |
| 2575 | int& handler_count, |
| 2576 | int& cflags) { |
| 2577 | int sc = code_headers.getByte(); |
| 2578 | if (sc == 0) { |
| 2579 | max_stack = max_na_locals = handler_count = cflags = -1; |
| 2580 | return; |
| 2581 | } |
| 2582 | // Short code header is the usual case: |
| 2583 | int nh; |
| 2584 | int mod; |
| 2585 | if (sc < 1 + 12*12) { |
| 2586 | sc -= 1; |
| 2587 | nh = 0; |
| 2588 | mod = 12; |
| 2589 | } else if (sc < 1 + 12*12 + 8*8) { |
| 2590 | sc -= 1 + 12*12; |
| 2591 | nh = 1; |
| 2592 | mod = 8; |
| 2593 | } else { |
| 2594 | assert(sc < 1 + 12*12 + 8*8 + 7*7); |
| 2595 | sc -= 1 + 12*12 + 8*8; |
| 2596 | nh = 2; |
| 2597 | mod = 7; |
| 2598 | } |
| 2599 | max_stack = sc % mod; |
| 2600 | max_na_locals = sc / mod; // caller must add static, siglen |
| 2601 | handler_count = nh; |
| 2602 | if ((archive_options & AO_HAVE_ALL_CODE_FLAGS) != 0) |
| 2603 | cflags = -1; |
| 2604 | else |
| 2605 | cflags = 0; // this one has no attributes |
| 2606 | } |
| 2607 | |
| 2608 | // Cf. PackageReader.readCodeHeaders |
| 2609 | void unpacker::read_code_headers() { |
| 2610 | code_headers.readData(code_count); |
| 2611 | CHECK; |
| 2612 | int totalHandlerCount = 0; |
| 2613 | int totalFlagsCount = 0; |
| 2614 | for (int i = 0; i < code_count; i++) { |
| 2615 | int max_stack, max_locals, handler_count, cflags; |
| 2616 | get_code_header(max_stack, max_locals, handler_count, cflags); |
| 2617 | if (max_stack < 0) code_max_stack.expectMoreLength(1); |
| 2618 | if (max_locals < 0) code_max_na_locals.expectMoreLength(1); |
| 2619 | if (handler_count < 0) code_handler_count.expectMoreLength(1); |
| 2620 | else totalHandlerCount += handler_count; |
| 2621 | if (cflags < 0) totalFlagsCount += 1; |
| 2622 | } |
| 2623 | code_headers.rewind(); // replay later during writing |
| 2624 | |
| 2625 | code_max_stack.readData(); |
| 2626 | code_max_na_locals.readData(); |
| 2627 | code_handler_count.readData(); |
| 2628 | totalHandlerCount += code_handler_count.getIntTotal(); |
| 2629 | |
| 2630 | // Read handler specifications. |
| 2631 | // Cf. PackageReader.readCodeHandlers. |
| 2632 | code_handler_start_P.readData(totalHandlerCount); |
| 2633 | code_handler_end_PO.readData(totalHandlerCount); |
| 2634 | code_handler_catch_PO.readData(totalHandlerCount); |
| 2635 | code_handler_class_RCN.readData(totalHandlerCount); |
| 2636 | |
| 2637 | read_attrs(ATTR_CONTEXT_CODE, totalFlagsCount); |
| 2638 | } |
| 2639 | |
| 2640 | static inline bool is_in_range(uint n, uint min, uint max) { |
| 2641 | return n - min <= max - min; // unsigned arithmetic! |
| 2642 | } |
| 2643 | static inline bool is_field_op(int bc) { |
| 2644 | return is_in_range(bc, bc_getstatic, bc_putfield); |
| 2645 | } |
| 2646 | static inline bool is_invoke_init_op(int bc) { |
| 2647 | return is_in_range(bc, _invokeinit_op, _invokeinit_limit-1); |
| 2648 | } |
| 2649 | static inline bool is_self_linker_op(int bc) { |
| 2650 | return is_in_range(bc, _self_linker_op, _self_linker_limit-1); |
| 2651 | } |
| 2652 | static bool is_branch_op(int bc) { |
| 2653 | return is_in_range(bc, bc_ifeq, bc_jsr) |
| 2654 | || is_in_range(bc, bc_ifnull, bc_jsr_w); |
| 2655 | } |
| 2656 | static bool is_local_slot_op(int bc) { |
| 2657 | return is_in_range(bc, bc_iload, bc_aload) |
| 2658 | || is_in_range(bc, bc_istore, bc_astore) |
| 2659 | || bc == bc_iinc || bc == bc_ret; |
| 2660 | } |
| 2661 | band* unpacker::ref_band_for_op(int bc) { |
| 2662 | switch (bc) { |
| 2663 | case bc_ildc: |
| 2664 | case bc_ildc_w: |
| 2665 | return &bc_intref; |
| 2666 | case bc_fldc: |
| 2667 | case bc_fldc_w: |
| 2668 | return &bc_floatref; |
| 2669 | case bc_lldc2_w: |
| 2670 | return &bc_longref; |
| 2671 | case bc_dldc2_w: |
| 2672 | return &bc_doubleref; |
| 2673 | case bc_aldc: |
| 2674 | case bc_aldc_w: |
| 2675 | return &bc_stringref; |
| 2676 | case bc_cldc: |
| 2677 | case bc_cldc_w: |
| 2678 | return &bc_classref; |
| 2679 | |
| 2680 | case bc_getstatic: |
| 2681 | case bc_putstatic: |
| 2682 | case bc_getfield: |
| 2683 | case bc_putfield: |
| 2684 | return &bc_fieldref; |
| 2685 | |
| 2686 | case bc_invokevirtual: |
| 2687 | case bc_invokespecial: |
| 2688 | case bc_invokestatic: |
| 2689 | return &bc_methodref; |
| 2690 | case bc_invokeinterface: |
| 2691 | return &bc_imethodref; |
| 2692 | |
| 2693 | case bc_new: |
| 2694 | case bc_anewarray: |
| 2695 | case bc_checkcast: |
| 2696 | case bc_instanceof: |
| 2697 | case bc_multianewarray: |
| 2698 | return &bc_classref; |
| 2699 | } |
| 2700 | return null; |
| 2701 | } |
| 2702 | |
| 2703 | maybe_inline |
| 2704 | band* unpacker::ref_band_for_self_op(int bc, bool& isAloadVar, int& origBCVar) { |
| 2705 | if (!is_self_linker_op(bc)) return null; |
| 2706 | int idx = (bc - _self_linker_op); |
| 2707 | bool isSuper = (idx >= _self_linker_super_flag); |
| 2708 | if (isSuper) idx -= _self_linker_super_flag; |
| 2709 | bool isAload = (idx >= _self_linker_aload_flag); |
| 2710 | if (isAload) idx -= _self_linker_aload_flag; |
| 2711 | int origBC = _first_linker_op + idx; |
| 2712 | bool isField = is_field_op(origBC); |
| 2713 | isAloadVar = isAload; |
| 2714 | origBCVar = _first_linker_op + idx; |
| 2715 | if (!isSuper) |
| 2716 | return isField? &bc_thisfield: &bc_thismethod; |
| 2717 | else |
| 2718 | return isField? &bc_superfield: &bc_supermethod; |
| 2719 | } |
| 2720 | |
| 2721 | // Cf. PackageReader.readByteCodes |
| 2722 | inline // called exactly once => inline |
| 2723 | void unpacker::read_bcs() { |
| 2724 | printcr(3, "reading compressed bytecodes and operands for %d codes...", |
| 2725 | code_count); |
| 2726 | |
| 2727 | // read from bc_codes and bc_case_count |
| 2728 | fillbytes all_switch_ops; |
| 2729 | all_switch_ops.init(); |
| 2730 | CHECK; |
| 2731 | |
| 2732 | // Read directly from rp/rplimit. |
| 2733 | //Do this later: bc_codes.readData(...) |
| 2734 | byte* rp0 = rp; |
| 2735 | |
| 2736 | band* bc_which; |
| 2737 | byte* opptr = rp; |
| 2738 | byte* oplimit = rplimit; |
| 2739 | |
| 2740 | bool isAload; // passed by ref and then ignored |
| 2741 | int junkBC; // passed by ref and then ignored |
| 2742 | for (int k = 0; k < code_count; k++) { |
| 2743 | // Scan one method: |
| 2744 | for (;;) { |
| 2745 | if (opptr+2 > oplimit) { |
| 2746 | rp = opptr; |
| 2747 | ensure_input(2); |
| 2748 | oplimit = rplimit; |
| 2749 | rp = rp0; // back up |
| 2750 | } |
| 2751 | if (opptr == oplimit) { abort(); break; } |
| 2752 | int bc = *opptr++ & 0xFF; |
| 2753 | bool isWide = false; |
| 2754 | if (bc == bc_wide) { |
| 2755 | if (opptr == oplimit) { abort(); break; } |
| 2756 | bc = *opptr++ & 0xFF; |
| 2757 | isWide = true; |
| 2758 | } |
| 2759 | // Adjust expectations of various band sizes. |
| 2760 | switch (bc) { |
| 2761 | case bc_tableswitch: |
| 2762 | case bc_lookupswitch: |
| 2763 | all_switch_ops.addByte(bc); |
| 2764 | break; |
| 2765 | case bc_iinc: |
| 2766 | bc_local.expectMoreLength(1); |
| 2767 | bc_which = isWide ? &bc_short : &bc_byte; |
| 2768 | bc_which->expectMoreLength(1); |
| 2769 | break; |
| 2770 | case bc_sipush: |
| 2771 | bc_short.expectMoreLength(1); |
| 2772 | break; |
| 2773 | case bc_bipush: |
| 2774 | bc_byte.expectMoreLength(1); |
| 2775 | break; |
| 2776 | case bc_newarray: |
| 2777 | bc_byte.expectMoreLength(1); |
| 2778 | break; |
| 2779 | case bc_multianewarray: |
| 2780 | assert(ref_band_for_op(bc) == &bc_classref); |
| 2781 | bc_classref.expectMoreLength(1); |
| 2782 | bc_byte.expectMoreLength(1); |
| 2783 | break; |
| 2784 | case bc_ref_escape: |
| 2785 | bc_escrefsize.expectMoreLength(1); |
| 2786 | bc_escref.expectMoreLength(1); |
| 2787 | break; |
| 2788 | case bc_byte_escape: |
| 2789 | bc_escsize.expectMoreLength(1); |
| 2790 | // bc_escbyte will have to be counted too |
| 2791 | break; |
| 2792 | default: |
| 2793 | if (is_invoke_init_op(bc)) { |
| 2794 | bc_initref.expectMoreLength(1); |
| 2795 | break; |
| 2796 | } |
| 2797 | bc_which = ref_band_for_self_op(bc, isAload, junkBC); |
| 2798 | if (bc_which != null) { |
| 2799 | bc_which->expectMoreLength(1); |
| 2800 | break; |
| 2801 | } |
| 2802 | if (is_branch_op(bc)) { |
| 2803 | bc_label.expectMoreLength(1); |
| 2804 | break; |
| 2805 | } |
| 2806 | bc_which = ref_band_for_op(bc); |
| 2807 | if (bc_which != null) { |
| 2808 | bc_which->expectMoreLength(1); |
| 2809 | assert(bc != bc_multianewarray); // handled elsewhere |
| 2810 | break; |
| 2811 | } |
| 2812 | if (is_local_slot_op(bc)) { |
| 2813 | bc_local.expectMoreLength(1); |
| 2814 | break; |
| 2815 | } |
| 2816 | break; |
| 2817 | case bc_end_marker: |
| 2818 | // Increment k and test against code_count. |
| 2819 | goto doneScanningMethod; |
| 2820 | } |
| 2821 | } |
| 2822 | doneScanningMethod:{} |
| 2823 | if (aborting()) break; |
| 2824 | } |
| 2825 | |
| 2826 | // Go through the formality, so we can use it in a regular fashion later: |
| 2827 | assert(rp == rp0); |
| 2828 | bc_codes.readData(opptr - rp); |
| 2829 | |
| 2830 | int i = 0; |
| 2831 | |
| 2832 | // To size instruction bands correctly, we need info on switches: |
| 2833 | bc_case_count.readData(all_switch_ops.size()); |
| 2834 | for (i = 0; i < all_switch_ops.size(); i++) { |
| 2835 | int caseCount = bc_case_count.getInt(); |
| 2836 | int bc = all_switch_ops.getByte(i); |
| 2837 | bc_label.expectMoreLength(1+caseCount); // default label + cases |
| 2838 | bc_case_value.expectMoreLength(bc == bc_tableswitch ? 1 : caseCount); |
| 2839 | printcr(2, "switch bc=%d caseCount=%d", bc, caseCount); |
| 2840 | } |
| 2841 | bc_case_count.rewind(); // uses again for output |
| 2842 | |
| 2843 | all_switch_ops.free(); |
| 2844 | |
| 2845 | for (i = e_bc_case_value; i <= e_bc_escsize; i++) { |
| 2846 | all_bands[i].readData(); |
| 2847 | } |
| 2848 | |
| 2849 | // The bc_escbyte band is counted by the immediately previous band. |
| 2850 | bc_escbyte.readData(bc_escsize.getIntTotal()); |
| 2851 | |
| 2852 | printcr(3, "scanned %d opcode and %d operand bytes for %d codes...", |
| 2853 | (int)(bc_codes.size()), |
| 2854 | (int)(bc_escsize.maxRP() - bc_case_value.minRP()), |
| 2855 | code_count); |
| 2856 | } |
| 2857 | |
| 2858 | void unpacker::read_bands() { |
| 2859 | byte* rp0 = rp; |
| 2860 | int i; |
| 2861 | |
| 2862 | read_file_header(); |
| 2863 | CHECK; |
| 2864 | |
| 2865 | if (cp.nentries == 0) { |
| 2866 | // read_file_header failed to read a CP, because it copied a JAR. |
| 2867 | return; |
| 2868 | } |
| 2869 | |
| 2870 | // Do this after the file header has been read: |
| 2871 | check_options(); |
| 2872 | |
| 2873 | read_cp(); |
| 2874 | CHECK; |
| 2875 | read_attr_defs(); |
| 2876 | CHECK; |
| 2877 | read_ics(); |
| 2878 | CHECK; |
| 2879 | read_classes(); |
| 2880 | CHECK; |
| 2881 | read_bcs(); |
| 2882 | CHECK; |
| 2883 | read_files(); |
| 2884 | } |
| 2885 | |
| 2886 | /// CP routines |
| 2887 | |
| 2888 | entry*& cpool::hashTabRef(byte tag, bytes& b) { |
| 2889 | printcr(5, "hashTabRef tag=%d %s[%d]", tag, b.string(), b.len); |
| 2890 | uint hash = tag + b.len; |
| 2891 | for (int i = 0; i < b.len; i++) { |
| 2892 | hash = hash * 31 + (0xFF & b.ptr[i]); |
| 2893 | } |
| 2894 | entry** ht = hashTab; |
| 2895 | int hlen = hashTabLength; |
| 2896 | assert((hlen & (hlen-1)) == 0); // must be power of 2 |
| 2897 | uint hash1 = hash & (hlen-1); // == hash % hlen |
| 2898 | uint hash2 = 0; // lazily computed (requires mod op.) |
| 2899 | int probes = 0; |
| 2900 | while (ht[hash1] != null) { |
| 2901 | entry& e = *ht[hash1]; |
| 2902 | if (e.value.b.equals(b) && e.tag == tag) |
| 2903 | break; |
| 2904 | if (hash2 == 0) |
| 2905 | // Note: hash2 must be relatively prime to hlen, hence the "|1". |
| 2906 | hash2 = (((hash % 499) & (hlen-1)) | 1); |
| 2907 | hash1 += hash2; |
| 2908 | if (hash1 >= hlen) hash1 -= hlen; |
| 2909 | assert(hash1 < hlen); |
| 2910 | assert(++probes < hlen); |
| 2911 | } |
| 2912 | #ifndef PRODUCT |
| 2913 | hash_probes[0] += 1; |
| 2914 | hash_probes[1] += probes; |
| 2915 | #endif |
| 2916 | printcr(5, " => @%d %p", hash1, ht[hash1]); |
| 2917 | return ht[hash1]; |
| 2918 | } |
| 2919 | |
| 2920 | maybe_inline |
| 2921 | static void insert_extra(entry* e, ptrlist& extras) { |
| 2922 | // This ordering helps implement the Pack200 requirement |
| 2923 | // of a predictable CP order in the class files produced. |
| 2924 | e->inord = NO_INORD; // mark as an "extra" |
| 2925 | extras.add(e); |
| 2926 | // Note: We will sort the list (by string-name) later. |
| 2927 | } |
| 2928 | |
| 2929 | entry* cpool::ensureUtf8(bytes& b) { |
| 2930 | entry*& ix = hashTabRef(CONSTANT_Utf8, b); |
| 2931 | if (ix != null) return ix; |
| 2932 | // Make one. |
| 2933 | if (nentries == maxentries) { |
| 2934 | abort("cp utf8 overflow"); |
| 2935 | return &entries[tag_base[CONSTANT_Utf8]]; // return something |
| 2936 | } |
| 2937 | entry& e = entries[nentries++]; |
| 2938 | e.tag = CONSTANT_Utf8; |
| 2939 | u->saveTo(e.value.b, b); |
| 2940 | assert(&e >= first_extra_entry); |
| 2941 | insert_extra(&e, tag_extras[CONSTANT_Utf8]); |
| 2942 | printcr(4,"ensureUtf8 miss %s", e.string()); |
| 2943 | return ix = &e; |
| 2944 | } |
| 2945 | |
| 2946 | entry* cpool::ensureClass(bytes& b) { |
| 2947 | entry*& ix = hashTabRef(CONSTANT_Class, b); |
| 2948 | if (ix != null) return ix; |
| 2949 | // Make one. |
| 2950 | if (nentries == maxentries) { |
| 2951 | abort("cp class overflow"); |
| 2952 | return &entries[tag_base[CONSTANT_Class]]; // return something |
| 2953 | } |
| 2954 | entry& e = entries[nentries++]; |
| 2955 | e.tag = CONSTANT_Class; |
| 2956 | e.nrefs = 1; |
| 2957 | e.refs = U_NEW(entry*, 1); |
| 2958 | ix = &e; // hold my spot in the index |
| 2959 | entry* utf = ensureUtf8(b); |
| 2960 | e.refs[0] = utf; |
| 2961 | e.value.b = utf->value.b; |
| 2962 | assert(&e >= first_extra_entry); |
| 2963 | insert_extra(&e, tag_extras[CONSTANT_Class]); |
| 2964 | printcr(4,"ensureClass miss %s", e.string()); |
| 2965 | return &e; |
| 2966 | } |
| 2967 | |
| 2968 | void cpool::expandSignatures() { |
| 2969 | int i; |
| 2970 | int nsigs = 0; |
| 2971 | int nreused = 0; |
| 2972 | int first_sig = tag_base[CONSTANT_Signature]; |
| 2973 | int sig_limit = tag_count[CONSTANT_Signature] + first_sig; |
| 2974 | fillbytes buf; |
| 2975 | buf.init(1<<10); |
| 2976 | CHECK; |
| 2977 | for (i = first_sig; i < sig_limit; i++) { |
| 2978 | entry& e = entries[i]; |
| 2979 | assert(e.tag == CONSTANT_Signature); |
| 2980 | int refnum = 0; |
| 2981 | bytes form = e.refs[refnum++]->asUtf8(); |
| 2982 | buf.empty(); |
| 2983 | for (int j = 0; j < form.len; j++) { |
| 2984 | int c = form.ptr[j]; |
| 2985 | buf.addByte(c); |
| 2986 | if (c == 'L') { |
| 2987 | entry* cls = e.refs[refnum++]; |
| 2988 | buf.append(cls->className()->asUtf8()); |
| 2989 | } |
| 2990 | } |
| 2991 | assert(refnum == e.nrefs); |
| 2992 | bytes& sig = buf.b; |
| 2993 | printcr(5,"signature %d %s -> %s", i, form.ptr, sig.ptr); |
| 2994 | |
| 2995 | // try to find a pre-existing Utf8: |
| 2996 | entry* &e2 = hashTabRef(CONSTANT_Utf8, sig); |
| 2997 | if (e2 != null) { |
| 2998 | assert(e2->isUtf8(sig)); |
| 2999 | e.value.b = e2->value.b; |
| 3000 | e.refs[0] = e2; |
| 3001 | e.nrefs = 1; |
| 3002 | printcr(5,"signature replaced %d => %s", i, e.string()); |
| 3003 | nreused++; |
| 3004 | } else { |
| 3005 | // there is no other replacement; reuse this CP entry as a Utf8 |
| 3006 | u->saveTo(e.value.b, sig); |
| 3007 | e.tag = CONSTANT_Utf8; |
| 3008 | e.nrefs = 0; |
| 3009 | e2 = &e; |
| 3010 | printcr(5,"signature changed %d => %s", e.inord, e.string()); |
| 3011 | } |
| 3012 | nsigs++; |
| 3013 | } |
| 3014 | printcr(1,"expanded %d signatures (reused %d utfs)", nsigs, nreused); |
| 3015 | buf.free(); |
| 3016 | |
| 3017 | // go expunge all references to remaining signatures: |
| 3018 | for (i = 0; i < nentries; i++) { |
| 3019 | entry& e = entries[i]; |
| 3020 | for (int j = 0; j < e.nrefs; j++) { |
| 3021 | entry*& e2 = e.refs[j]; |
| 3022 | if (e2 != null && e2->tag == CONSTANT_Signature) |
| 3023 | e2 = e2->refs[0]; |
| 3024 | } |
| 3025 | } |
| 3026 | } |
| 3027 | |
| 3028 | void cpool::initMemberIndexes() { |
| 3029 | // This function does NOT refer to any class schema. |
| 3030 | // It is totally internal to the cpool. |
| 3031 | int i, j, len; |
| 3032 | |
| 3033 | // Get the pre-existing indexes: |
| 3034 | int nclasses = tag_count[CONSTANT_Class]; |
| 3035 | entry* classes = tag_base[CONSTANT_Class] + entries; |
| 3036 | int nfields = tag_count[CONSTANT_Fieldref]; |
| 3037 | entry* fields = tag_base[CONSTANT_Fieldref] + entries; |
| 3038 | int nmethods = tag_count[CONSTANT_Methodref]; |
| 3039 | entry* methods = tag_base[CONSTANT_Methodref] + entries; |
| 3040 | |
| 3041 | int* field_counts = T_NEW(int, nclasses); |
| 3042 | int* method_counts = T_NEW(int, nclasses); |
| 3043 | cpindex* all_indexes = U_NEW(cpindex, nclasses*2); |
| 3044 | entry** field_ix = U_NEW(entry*, nfields+nclasses); |
| 3045 | entry** method_ix = U_NEW(entry*, nmethods+nclasses); |
| 3046 | |
| 3047 | for (j = 0; j < nfields; j++) { |
| 3048 | entry& f = fields[j]; |
| 3049 | i = f.memberClass()->inord; |
| 3050 | assert((uint)i < nclasses); |
| 3051 | field_counts[i]++; |
| 3052 | } |
| 3053 | for (j = 0; j < nmethods; j++) { |
| 3054 | entry& m = methods[j]; |
| 3055 | i = m.memberClass()->inord; |
| 3056 | assert((uint)i < nclasses); |
| 3057 | method_counts[i]++; |
| 3058 | } |
| 3059 | |
| 3060 | int fbase = 0, mbase = 0; |
| 3061 | for (i = 0; i < nclasses; i++) { |
| 3062 | int fc = field_counts[i]; |
| 3063 | int mc = method_counts[i]; |
| 3064 | all_indexes[i*2+0].init(fc, field_ix+fbase, |
| 3065 | CONSTANT_Fieldref + SUBINDEX_BIT); |
| 3066 | all_indexes[i*2+1].init(mc, method_ix+mbase, |
| 3067 | CONSTANT_Methodref + SUBINDEX_BIT); |
| 3068 | // reuse field_counts and member_counts as fill pointers: |
| 3069 | field_counts[i] = fbase; |
| 3070 | method_counts[i] = mbase; |
| 3071 | printcr(3, "class %d fields @%d[%d] methods @%d[%d]", |
| 3072 | i, fbase, fc, mbase, mc); |
| 3073 | fbase += fc+1; |
| 3074 | mbase += mc+1; |
| 3075 | // (the +1 leaves a space between every subarray) |
| 3076 | } |
| 3077 | assert(fbase == nfields+nclasses); |
| 3078 | assert(mbase == nmethods+nclasses); |
| 3079 | |
| 3080 | for (j = 0; j < nfields; j++) { |
| 3081 | entry& f = fields[j]; |
| 3082 | i = f.memberClass()->inord; |
| 3083 | field_ix[field_counts[i]++] = &f; |
| 3084 | } |
| 3085 | for (j = 0; j < nmethods; j++) { |
| 3086 | entry& m = methods[j]; |
| 3087 | i = m.memberClass()->inord; |
| 3088 | method_ix[method_counts[i]++] = &m; |
| 3089 | } |
| 3090 | |
| 3091 | member_indexes = all_indexes; |
| 3092 | |
| 3093 | #ifndef PRODUCT |
| 3094 | // Test the result immediately on every class and field. |
| 3095 | int fvisited = 0, mvisited = 0; |
| 3096 | int prevord; |
| 3097 | for (i = 0; i < nclasses; i++) { |
| 3098 | entry* cls = &classes[i]; |
| 3099 | cpindex* fix = getFieldIndex(cls); |
| 3100 | cpindex* mix = getMethodIndex(cls); |
| 3101 | printcr(2, "field and method index for %s [%d] [%d]", |
| 3102 | cls->string(), mix->len, fix->len); |
| 3103 | prevord = -1; |
| 3104 | for (j = 0, len = fix->len; j < len; j++) { |
| 3105 | entry* f = fix->get(j); |
| 3106 | assert(f != null); |
| 3107 | printcr(3, "- field %s", f->string()); |
| 3108 | assert(f->memberClass() == cls); |
| 3109 | assert(prevord < (int)f->inord); |
| 3110 | prevord = f->inord; |
| 3111 | fvisited++; |
| 3112 | } |
| 3113 | assert(fix->base2[j] == null); |
| 3114 | prevord = -1; |
| 3115 | for (j = 0, len = mix->len; j < len; j++) { |
| 3116 | entry* m = mix->get(j); |
| 3117 | assert(m != null); |
| 3118 | printcr(3, "- method %s", m->string()); |
| 3119 | assert(m->memberClass() == cls); |
| 3120 | assert(prevord < (int)m->inord); |
| 3121 | prevord = m->inord; |
| 3122 | mvisited++; |
| 3123 | } |
| 3124 | assert(mix->base2[j] == null); |
| 3125 | } |
| 3126 | assert(fvisited == nfields); |
| 3127 | assert(mvisited == nmethods); |
| 3128 | #endif |
| 3129 | |
| 3130 | // Free intermediate buffers. |
| 3131 | u->free_temps(); |
| 3132 | } |
| 3133 | |
| 3134 | void entry::requestOutputIndex(cpool& cp, int req) { |
| 3135 | assert(outputIndex <= NOT_REQUESTED); // must not have assigned indexes yet |
| 3136 | if (tag == CONSTANT_Signature) { |
| 3137 | ref(0)->requestOutputIndex(cp, req); |
| 3138 | return; |
| 3139 | } |
| 3140 | assert(req == REQUESTED || req == REQUESTED_LDC); |
| 3141 | if (outputIndex != NOT_REQUESTED) { |
| 3142 | if (req == REQUESTED_LDC) |
| 3143 | outputIndex = req; // this kind has precedence |
| 3144 | return; |
| 3145 | } |
| 3146 | outputIndex = req; |
| 3147 | //assert(!cp.outputEntries.contains(this)); |
| 3148 | assert(tag != CONSTANT_Signature); |
| 3149 | cp.outputEntries.add(this); |
| 3150 | for (int j = 0; j < nrefs; j++) { |
| 3151 | ref(j)->requestOutputIndex(cp); |
| 3152 | } |
| 3153 | } |
| 3154 | |
| 3155 | void cpool::resetOutputIndexes() { |
| 3156 | int i; |
| 3157 | int noes = outputEntries.length(); |
| 3158 | entry** oes = (entry**) outputEntries.base(); |
| 3159 | for (i = 0; i < noes; i++) { |
| 3160 | entry& e = *oes[i]; |
| 3161 | e.outputIndex = NOT_REQUESTED; |
| 3162 | } |
| 3163 | outputIndexLimit = 0; |
| 3164 | outputEntries.empty(); |
| 3165 | #ifndef PRODUCT |
| 3166 | // they must all be clear now |
| 3167 | for (i = 0; i < nentries; i++) |
| 3168 | assert(entries[i].outputIndex == NOT_REQUESTED); |
| 3169 | #endif |
| 3170 | } |
| 3171 | |
| 3172 | static const byte TAG_ORDER[CONSTANT_Limit] = { |
| 3173 | 0, 1, 0, 2, 3, 4, 5, 7, 6, 10, 11, 12, 9, 8 |
| 3174 | }; |
| 3175 | |
| 3176 | extern "C" |
| 3177 | int outputEntry_cmp(const void* e1p, const void* e2p) { |
| 3178 | // Sort entries according to the Pack200 rules for deterministic |
| 3179 | // constant pool ordering. |
| 3180 | // |
| 3181 | // The four sort keys as follows, in order of decreasing importance: |
| 3182 | // 1. ldc first, then non-ldc guys |
| 3183 | // 2. normal cp_All entries by input order (i.e., address order) |
| 3184 | // 3. after that, extra entries by lexical order (as in tag_extras[*]) |
| 3185 | entry& e1 = *(entry*) *(void**) e1p; |
| 3186 | entry& e2 = *(entry*) *(void**) e2p; |
| 3187 | int oi1 = e1.outputIndex; |
| 3188 | int oi2 = e2.outputIndex; |
| 3189 | assert(oi1 == REQUESTED || oi1 == REQUESTED_LDC); |
| 3190 | assert(oi2 == REQUESTED || oi2 == REQUESTED_LDC); |
| 3191 | if (oi1 != oi2) { |
| 3192 | if (oi1 == REQUESTED_LDC) return 0-1; |
| 3193 | if (oi2 == REQUESTED_LDC) return 1-0; |
| 3194 | // Else fall through; neither is an ldc request. |
| 3195 | } |
| 3196 | if (e1.inord != NO_INORD || e2.inord != NO_INORD) { |
| 3197 | // One or both is normal. Use input order. |
| 3198 | if (&e1 > &e2) return 1-0; |
| 3199 | if (&e1 < &e2) return 0-1; |
| 3200 | return 0; // equal pointers |
| 3201 | } |
| 3202 | // Both are extras. Sort by tag and then by value. |
| 3203 | if (e1.tag != e2.tag) { |
| 3204 | return TAG_ORDER[e1.tag] - TAG_ORDER[e2.tag]; |
| 3205 | } |
| 3206 | // If the tags are the same, use string comparison. |
| 3207 | return compare_Utf8_chars(e1.value.b, e2.value.b); |
| 3208 | } |
| 3209 | |
| 3210 | void cpool::computeOutputIndexes() { |
| 3211 | int i; |
| 3212 | |
| 3213 | #ifndef PRODUCT |
| 3214 | // outputEntries must be a complete list of those requested: |
| 3215 | static uint checkStart = 0; |
| 3216 | int checkStep = 1; |
| 3217 | if (nentries > 100) checkStep = nentries / 100; |
| 3218 | for (i = (checkStart++ % checkStep); i < nentries; i += checkStep) { |
| 3219 | entry& e = entries[i]; |
| 3220 | if (e.outputIndex != NOT_REQUESTED) { |
| 3221 | assert(outputEntries.contains(&e)); |
| 3222 | } else { |
| 3223 | assert(!outputEntries.contains(&e)); |
| 3224 | } |
| 3225 | } |
| 3226 | |
| 3227 | // check hand-initialization of TAG_ORDER |
| 3228 | for (i = 0; i < N_TAGS_IN_ORDER; i++) { |
| 3229 | byte tag = TAGS_IN_ORDER[i]; |
| 3230 | assert(TAG_ORDER[tag] == i+1); |
| 3231 | } |
| 3232 | #endif |
| 3233 | |
| 3234 | int noes = outputEntries.length(); |
| 3235 | entry** oes = (entry**) outputEntries.base(); |
| 3236 | |
| 3237 | // Sort the output constant pool into the order required by Pack200. |
| 3238 | PTRLIST_QSORT(outputEntries, outputEntry_cmp); |
| 3239 | |
| 3240 | // Allocate a new index for each entry that needs one. |
| 3241 | // We do this in two passes, one for LDC entries and one for the rest. |
| 3242 | int nextIndex = 1; // always skip index #0 in output cpool |
| 3243 | for (i = 0; i < noes; i++) { |
| 3244 | entry& e = *oes[i]; |
| 3245 | assert(e.outputIndex == REQUESTED || e.outputIndex == REQUESTED_LDC); |
| 3246 | e.outputIndex = nextIndex++; |
| 3247 | if (e.isDoubleWord()) nextIndex++; // do not use the next index |
| 3248 | } |
| 3249 | outputIndexLimit = nextIndex; |
| 3250 | printcr(3,"renumbering CP to %d entries", outputIndexLimit); |
| 3251 | } |
| 3252 | |
| 3253 | #ifndef PRODUCT |
| 3254 | // debugging goo |
| 3255 | |
| 3256 | unpacker* debug_u; |
| 3257 | |
| 3258 | static bytes& getbuf(int len) { // for debugging only! |
| 3259 | static int bn = 0; |
| 3260 | static bytes bufs[8] = { 0 }; |
| 3261 | bytes& buf = bufs[bn++ & 7]; |
| 3262 | while (buf.len < len+10) |
| 3263 | buf.realloc(buf.len ? buf.len * 2 : 1000); |
| 3264 | buf.ptr[0] = 0; // for the sake of strcat |
| 3265 | return buf; |
| 3266 | } |
| 3267 | |
| 3268 | char* entry::string() { |
| 3269 | bytes buf; |
| 3270 | switch (tag) { |
| 3271 | case CONSTANT_None: |
| 3272 | return (char*)"<empty>"; |
| 3273 | case CONSTANT_Signature: |
| 3274 | if (value.b.ptr == null) |
| 3275 | return ref(0)->string(); |
| 3276 | // else fall through: |
| 3277 | case CONSTANT_Utf8: |
| 3278 | buf = value.b; |
| 3279 | break; |
| 3280 | case CONSTANT_Integer: |
| 3281 | case CONSTANT_Float: |
| 3282 | buf = getbuf(12); |
| 3283 | sprintf((char*)buf.ptr, "0x%08x", value.i); |
| 3284 | break; |
| 3285 | case CONSTANT_Long: |
| 3286 | case CONSTANT_Double: |
| 3287 | buf = getbuf(24); |
| 3288 | sprintf((char*)buf.ptr, "0x%016llx", value.l); |
| 3289 | break; |
| 3290 | default: |
| 3291 | if (nrefs == 0) { |
| 3292 | buf = getbuf(20); |
| 3293 | sprintf((char*)buf.ptr, "<tag=%d>", tag); |
| 3294 | } else if (nrefs == 1) { |
| 3295 | return refs[0]->string(); |
| 3296 | } else { |
| 3297 | char* s1 = refs[0]->string(); |
| 3298 | char* s2 = refs[1]->string(); |
| 3299 | buf = getbuf(strlen(s1) + 1 + strlen(s2) + 4 + 1); |
| 3300 | buf.strcat(s1).strcat(" ").strcat(s2); |
| 3301 | if (nrefs > 2) buf.strcat(" ..."); |
| 3302 | } |
| 3303 | } |
| 3304 | return (char*)buf.ptr; |
| 3305 | } |
| 3306 | |
| 3307 | void print_cp_entry(int i) { |
| 3308 | entry& e = debug_u->cp.entries[i]; |
| 3309 | char buf[30]; |
| 3310 | sprintf(buf, ((uint)e.tag < CONSTANT_Limit)? TAG_NAME[e.tag]: "%d", e.tag); |
| 3311 | printf(" %d\t%s %s\n", i, buf, e.string()); |
| 3312 | } |
| 3313 | |
| 3314 | void print_cp_entries(int beg, int end) { |
| 3315 | for (int i = beg; i < end; i++) |
| 3316 | print_cp_entry(i); |
| 3317 | } |
| 3318 | |
| 3319 | void print_cp() { |
| 3320 | print_cp_entries(0, debug_u->cp.nentries); |
| 3321 | } |
| 3322 | |
| 3323 | #endif |
| 3324 | |
| 3325 | // Unpacker Start |
| 3326 | |
| 3327 | const char str_tf[] = "true\0false"; |
| 3328 | #undef STR_TRUE |
| 3329 | #undef STR_FALSE |
| 3330 | #define STR_TRUE (&str_tf[0]) |
| 3331 | #define STR_FALSE (&str_tf[5]) |
| 3332 | |
| 3333 | const char* unpacker::get_option(const char* prop) { |
| 3334 | if (prop == null ) return null; |
| 3335 | if (strcmp(prop, UNPACK_DEFLATE_HINT) == 0) { |
| 3336 | return deflate_hint_or_zero == 0? null : STR_TF(deflate_hint_or_zero > 0); |
| 3337 | #ifdef HAVE_STRIP |
| 3338 | } else if (strcmp(prop, UNPACK_STRIP_COMPILE) == 0) { |
| 3339 | return STR_TF(strip_compile); |
| 3340 | } else if (strcmp(prop, UNPACK_STRIP_DEBUG) == 0) { |
| 3341 | return STR_TF(strip_debug); |
| 3342 | } else if (strcmp(prop, UNPACK_STRIP_JCOV) == 0) { |
| 3343 | return STR_TF(strip_jcov); |
| 3344 | #endif /*HAVE_STRIP*/ |
| 3345 | } else if (strcmp(prop, UNPACK_REMOVE_PACKFILE) == 0) { |
| 3346 | return STR_TF(remove_packfile); |
| 3347 | } else if (strcmp(prop, DEBUG_VERBOSE) == 0) { |
| 3348 | return saveIntStr(verbose); |
| 3349 | } else if (strcmp(prop, UNPACK_MODIFICATION_TIME) == 0) { |
| 3350 | return (modification_time_or_zero == 0)? null: |
| 3351 | saveIntStr(modification_time_or_zero); |
| 3352 | } else if (strcmp(prop, UNPACK_LOG_FILE) == 0) { |
| 3353 | return log_file; |
| 3354 | } else { |
| 3355 | return NULL; // unknown option ignore |
| 3356 | } |
| 3357 | } |
| 3358 | |
| 3359 | bool unpacker::set_option(const char* prop, const char* value) { |
| 3360 | if (prop == NULL) return false; |
| 3361 | if (strcmp(prop, UNPACK_DEFLATE_HINT) == 0) { |
| 3362 | deflate_hint_or_zero = ( (value == null || strcmp(value, "keep") == 0) |
| 3363 | ? 0: BOOL_TF(value) ? +1: -1); |
| 3364 | #ifdef HAVE_STRIP |
| 3365 | } else if (strcmp(prop, UNPACK_STRIP_COMPILE) == 0) { |
| 3366 | strip_compile = STR_TF(value); |
| 3367 | } else if (strcmp(prop, UNPACK_STRIP_DEBUG) == 0) { |
| 3368 | strip_debug = STR_TF(value); |
| 3369 | } else if (strcmp(prop, UNPACK_STRIP_JCOV) == 0) { |
| 3370 | strip_jcov = STR_TF(value); |
| 3371 | #endif /*HAVE_STRIP*/ |
| 3372 | } else if (strcmp(prop, UNPACK_REMOVE_PACKFILE) == 0) { |
| 3373 | remove_packfile = STR_TF(value); |
| 3374 | } else if (strcmp(prop, DEBUG_VERBOSE) == 0) { |
| 3375 | verbose = (value == null)? 0: atoi(value); |
| 3376 | } else if (strcmp(prop, DEBUG_VERBOSE ".bands") == 0) { |
| 3377 | #ifndef PRODUCT |
| 3378 | verbose_bands = (value == null)? 0: atoi(value); |
| 3379 | #endif |
| 3380 | } else if (strcmp(prop, UNPACK_MODIFICATION_TIME) == 0) { |
| 3381 | if (value == null || (strcmp(value, "keep") == 0)) { |
| 3382 | modification_time_or_zero = 0; |
| 3383 | } else if (strcmp(value, "now") == 0) { |
| 3384 | time_t now; |
| 3385 | time(&now); |
| 3386 | modification_time_or_zero = (int) now; |
| 3387 | } else { |
| 3388 | modification_time_or_zero = atoi(value); |
| 3389 | if (modification_time_or_zero == 0) |
| 3390 | modification_time_or_zero = 1; // make non-zero |
| 3391 | } |
| 3392 | } else if (strcmp(prop, UNPACK_LOG_FILE) == 0) { |
| 3393 | log_file = (value == null)? value: saveStr(value); |
| 3394 | } else { |
| 3395 | return false; // unknown option ignore |
| 3396 | } |
| 3397 | return true; |
| 3398 | } |
| 3399 | |
| 3400 | // Deallocate all internal storage and reset to a clean state. |
| 3401 | // Do not disturb any input or output connections, including |
| 3402 | // infileptr, infileno, inbytes, read_input_fn, jarout, or errstrm. |
| 3403 | // Do not reset any unpack options. |
| 3404 | void unpacker::reset() { |
| 3405 | bytes_read_before_reset += bytes_read; |
| 3406 | bytes_written_before_reset += bytes_written; |
| 3407 | files_written_before_reset += files_written; |
| 3408 | classes_written_before_reset += classes_written; |
| 3409 | segments_read_before_reset += 1; |
| 3410 | if (verbose >= 2) { |
| 3411 | fprintf(errstrm, |
| 3412 | "After segment %d, %lld bytes read and %lld bytes written.\n", |
| 3413 | segments_read_before_reset-1, |
| 3414 | bytes_read_before_reset, bytes_written_before_reset); |
| 3415 | fprintf(errstrm, |
| 3416 | "After segment %d, %d files (of which %d are classes) written to output.\n", |
| 3417 | segments_read_before_reset-1, |
| 3418 | files_written_before_reset, classes_written_before_reset); |
| 3419 | if (archive_next_count != 0) { |
| 3420 | fprintf(errstrm, |
| 3421 | "After segment %d, %d segment%s remaining (estimated).\n", |
| 3422 | segments_read_before_reset-1, |
| 3423 | archive_next_count, archive_next_count==1?"":"s"); |
| 3424 | } |
| 3425 | } |
| 3426 | |
| 3427 | unpacker save_u = (*this); // save bytewise image |
| 3428 | infileptr = null; // make asserts happy |
| 3429 | jniobj = null; // make asserts happy |
| 3430 | jarout = null; // do not close the output jar |
| 3431 | gzin = null; // do not close the input gzip stream |
| 3432 | bytes esn; |
| 3433 | if (errstrm_name != null) { |
| 3434 | esn.saveFrom(errstrm_name); |
| 3435 | } else { |
| 3436 | esn.set(null, 0); |
| 3437 | } |
| 3438 | this->free(); |
| 3439 | mtrace('s', 0, 0); // note the boundary between segments |
| 3440 | this->init(read_input_fn); |
| 3441 | |
| 3442 | // restore selected interface state: |
| 3443 | #define SAVE(x) this->x = save_u.x |
| 3444 | SAVE(jniobj); |
| 3445 | SAVE(jnienv); |
| 3446 | SAVE(infileptr); // buffered |
| 3447 | SAVE(infileno); // unbuffered |
| 3448 | SAVE(inbytes); // direct |
| 3449 | SAVE(jarout); |
| 3450 | SAVE(gzin); |
| 3451 | //SAVE(read_input_fn); |
| 3452 | SAVE(errstrm); |
| 3453 | SAVE(verbose); // verbose level, 0 means no output |
| 3454 | SAVE(strip_compile); |
| 3455 | SAVE(strip_debug); |
| 3456 | SAVE(strip_jcov); |
| 3457 | SAVE(remove_packfile); |
| 3458 | SAVE(deflate_hint_or_zero); // ==0 means not set, otherwise -1 or 1 |
| 3459 | SAVE(modification_time_or_zero); |
| 3460 | SAVE(bytes_read_before_reset); |
| 3461 | SAVE(bytes_written_before_reset); |
| 3462 | SAVE(files_written_before_reset); |
| 3463 | SAVE(classes_written_before_reset); |
| 3464 | SAVE(segments_read_before_reset); |
| 3465 | #undef SAVE |
| 3466 | if (esn.len > 0) { |
| 3467 | errstrm_name = saveStr(esn.strval()); |
| 3468 | esn.free(); |
| 3469 | } |
| 3470 | log_file = errstrm_name; |
| 3471 | // Note: If we use strip_names, watch out: They get nuked here. |
| 3472 | } |
| 3473 | |
| 3474 | void unpacker::init(read_input_fn_t input_fn) { |
| 3475 | int i; |
| 3476 | NOT_PRODUCT(debug_u = this); |
| 3477 | BYTES_OF(*this).clear(); |
| 3478 | if (assert(1)) free(); // just to make sure freeing is idempotent |
| 3479 | this->u = this; // self-reference for U_NEW macro |
| 3480 | errstrm = stdout; // default error-output |
| 3481 | log_file = LOGFILE_STDOUT; |
| 3482 | read_input_fn = input_fn; |
| 3483 | all_bands = band::makeBands(this); |
| 3484 | // Make a default jar buffer; caller may safely overwrite it. |
| 3485 | jarout = U_NEW(jar, 1); |
| 3486 | jarout->init(this); |
| 3487 | for (i = 0; i < ATTR_CONTEXT_LIMIT; i++) |
| 3488 | attr_defs[i].u = u; // set up outer ptr |
| 3489 | } |
| 3490 | |
| 3491 | const char* unpacker::get_abort_message() { |
| 3492 | return abort_message; |
| 3493 | } |
| 3494 | |
| 3495 | void unpacker::dump_options() { |
| 3496 | static const char* opts[] = { |
| 3497 | UNPACK_LOG_FILE, |
| 3498 | UNPACK_DEFLATE_HINT, |
| 3499 | #ifdef HAVE_STRIP |
| 3500 | UNPACK_STRIP_COMPILE, |
| 3501 | UNPACK_STRIP_DEBUG, |
| 3502 | UNPACK_STRIP_JCOV, |
| 3503 | #endif /*HAVE_STRIP*/ |
| 3504 | UNPACK_REMOVE_PACKFILE, |
| 3505 | DEBUG_VERBOSE, |
| 3506 | UNPACK_MODIFICATION_TIME, |
| 3507 | null |
| 3508 | }; |
| 3509 | for (int i = 0; opts[i] != null; i++) { |
| 3510 | const char* str = get_option(opts[i]); |
| 3511 | if (str == null) { |
| 3512 | if (verbose == 0) continue; |
| 3513 | str = "(not set)"; |
| 3514 | } |
| 3515 | fprintf(errstrm, "%s=%s\n", opts[i], str); |
| 3516 | } |
| 3517 | } |
| 3518 | |
| 3519 | |
| 3520 | // Usage: unpack a byte buffer |
| 3521 | // packptr is a reference to byte buffer containing a |
| 3522 | // packed file and len is the length of the buffer. |
| 3523 | // If null, the callback is used to fill an internal buffer. |
| 3524 | void unpacker::start(void* packptr, size_t len) { |
| 3525 | NOT_PRODUCT(debug_u = this); |
| 3526 | if (packptr != null && len != 0) { |
| 3527 | inbytes.set((byte*) packptr, len); |
| 3528 | } |
| 3529 | read_bands(); |
| 3530 | } |
| 3531 | |
| 3532 | void unpacker::check_options() { |
| 3533 | const char* strue = "true"; |
| 3534 | const char* sfalse = "false"; |
| 3535 | if (deflate_hint_or_zero != 0) { |
| 3536 | bool force_deflate_hint = (deflate_hint_or_zero > 0); |
| 3537 | if (force_deflate_hint) |
| 3538 | default_file_options |= FO_DEFLATE_HINT; |
| 3539 | else |
| 3540 | default_file_options &= ~FO_DEFLATE_HINT; |
| 3541 | // Turn off per-file deflate hint by force. |
| 3542 | suppress_file_options |= FO_DEFLATE_HINT; |
| 3543 | } |
| 3544 | if (modification_time_or_zero != 0) { |
| 3545 | default_file_modtime = modification_time_or_zero; |
| 3546 | // Turn off per-file modtime by force. |
| 3547 | archive_options &= ~AO_HAVE_FILE_MODTIME; |
| 3548 | } |
| 3549 | // %%% strip_compile, etc... |
| 3550 | } |
| 3551 | |
| 3552 | // classfile writing |
| 3553 | |
| 3554 | void unpacker::reset_cur_classfile() { |
| 3555 | // set defaults |
| 3556 | cur_class_minver = default_class_minver; |
| 3557 | cur_class_majver = default_class_majver; |
| 3558 | |
| 3559 | // reset constant pool state |
| 3560 | cp.resetOutputIndexes(); |
| 3561 | |
| 3562 | // reset fixups |
| 3563 | class_fixup_type.empty(); |
| 3564 | class_fixup_offset.empty(); |
| 3565 | class_fixup_ref.empty(); |
| 3566 | requested_ics.empty(); |
| 3567 | } |
| 3568 | |
| 3569 | cpindex* cpool::getKQIndex() { |
| 3570 | char ch = '?'; |
| 3571 | if (u->cur_descr != null) { |
| 3572 | entry* type = u->cur_descr->descrType(); |
| 3573 | ch = type->value.b.ptr[0]; |
| 3574 | } |
| 3575 | byte tag = CONSTANT_Integer; |
| 3576 | switch (ch) { |
| 3577 | case 'L': tag = CONSTANT_String; break; |
| 3578 | case 'I': tag = CONSTANT_Integer; break; |
| 3579 | case 'J': tag = CONSTANT_Long; break; |
| 3580 | case 'F': tag = CONSTANT_Float; break; |
| 3581 | case 'D': tag = CONSTANT_Double; break; |
| 3582 | case 'B': case 'S': case 'C': |
| 3583 | case 'Z': tag = CONSTANT_Integer; break; |
| 3584 | default: abort("bad KQ reference"); break; |
| 3585 | } |
| 3586 | return getIndex(tag); |
| 3587 | } |
| 3588 | |
| 3589 | uint unpacker::to_bci(uint bii) { |
| 3590 | uint len = bcimap.length(); |
| 3591 | uint* map = (uint*) bcimap.base(); |
| 3592 | assert(len > 0); // must be initialized before using to_bci |
| 3593 | if (bii < len) |
| 3594 | return map[bii]; |
| 3595 | // Else it's a fractional or out-of-range BCI. |
| 3596 | uint key = bii-len; |
| 3597 | for (int i = len; ; i--) { |
| 3598 | if (map[i-1]-(i-1) <= key) |
| 3599 | break; |
| 3600 | else |
| 3601 | --bii; |
| 3602 | } |
| 3603 | return bii; |
| 3604 | } |
| 3605 | |
| 3606 | void unpacker::put_stackmap_type() { |
| 3607 | int tag = code_StackMapTable_T.getByte(); |
| 3608 | putu1(tag); |
| 3609 | switch (tag) { |
| 3610 | case 7: // (7) [RCH] |
| 3611 | putref(code_StackMapTable_RC.getRef()); |
| 3612 | break; |
| 3613 | case 8: // (8) [PH] |
| 3614 | putu2(to_bci(code_StackMapTable_P.getInt())); |
| 3615 | break; |
| 3616 | } |
| 3617 | } |
| 3618 | |
| 3619 | // Functions for writing code. |
| 3620 | |
| 3621 | maybe_inline |
| 3622 | void unpacker::put_label(int curIP, int size) { |
| 3623 | code_fixup_type.addByte(size); |
| 3624 | code_fixup_offset.add(put_empty(size)); |
| 3625 | code_fixup_source.add(curIP); |
| 3626 | } |
| 3627 | |
| 3628 | inline // called exactly once => inline |
| 3629 | void unpacker::write_bc_ops() { |
| 3630 | bcimap.empty(); |
| 3631 | code_fixup_type.empty(); |
| 3632 | code_fixup_offset.empty(); |
| 3633 | code_fixup_source.empty(); |
| 3634 | |
| 3635 | band* bc_which; |
| 3636 | |
| 3637 | byte* opptr = bc_codes.curRP(); |
| 3638 | // No need for oplimit, since the codes are pre-counted. |
| 3639 | |
| 3640 | size_t codeBase = wpoffset(); |
| 3641 | |
| 3642 | bool isAload; // copy-out result |
| 3643 | int origBC; |
| 3644 | |
| 3645 | entry* thisClass = cur_class; |
| 3646 | entry* superClass = cur_super; |
| 3647 | entry* newClass = null; // class of last _new opcode |
| 3648 | |
| 3649 | // overwrite any prior index on these bands; it changes w/ current class: |
| 3650 | bc_thisfield.setIndex( cp.getFieldIndex( thisClass)); |
| 3651 | bc_thismethod.setIndex( cp.getMethodIndex(thisClass)); |
| 3652 | if (superClass != null) { |
| 3653 | bc_superfield.setIndex( cp.getFieldIndex( superClass)); |
| 3654 | bc_supermethod.setIndex(cp.getMethodIndex(superClass)); |
| 3655 | } else { |
| 3656 | NOT_PRODUCT(bc_superfield.setIndex(null)); |
| 3657 | NOT_PRODUCT(bc_supermethod.setIndex(null)); |
| 3658 | } |
| 3659 | |
| 3660 | for (int curIP = 0; ; curIP++) { |
| 3661 | int curPC = wpoffset() - codeBase; |
| 3662 | bcimap.add(curPC); |
| 3663 | ensure_put_space(10); // covers most instrs w/o further bounds check |
| 3664 | int bc = *opptr++ & 0xFF; |
| 3665 | |
| 3666 | putu1_fast(bc); |
| 3667 | // Note: See '--wp' below for pseudo-bytecodes like bc_end_marker. |
| 3668 | |
| 3669 | bool isWide = false; |
| 3670 | if (bc == bc_wide) { |
| 3671 | bc = *opptr++ & 0xFF; |
| 3672 | putu1_fast(bc); |
| 3673 | isWide = true; |
| 3674 | } |
| 3675 | switch (bc) { |
| 3676 | case bc_end_marker: |
| 3677 | --wp; // not really part of the code |
| 3678 | assert(opptr <= bc_codes.maxRP()); |
| 3679 | bc_codes.curRP() = opptr; // advance over this in bc_codes |
| 3680 | goto doneScanningMethod; |
| 3681 | case bc_tableswitch: // apc: (df, lo, hi, (hi-lo+1)*(label)) |
| 3682 | case bc_lookupswitch: // apc: (df, nc, nc*(case, label)) |
| 3683 | { |
| 3684 | int caseCount = bc_case_count.getInt(); |
| 3685 | while (((wpoffset() - codeBase) % 4) != 0) putu1_fast(0); |
| 3686 | ensure_put_space(30 + caseCount*8); |
| 3687 | put_label(curIP, 4); //int df = bc_label.getInt(); |
| 3688 | if (bc == bc_tableswitch) { |
| 3689 | int lo = bc_case_value.getInt(); |
| 3690 | int hi = lo + caseCount-1; |
| 3691 | putu4(lo); |
| 3692 | putu4(hi); |
| 3693 | for (int j = 0; j < caseCount; j++) { |
| 3694 | put_label(curIP, 4); //int lVal = bc_label.getInt(); |
| 3695 | //int cVal = lo + j; |
| 3696 | } |
| 3697 | } else { |
| 3698 | putu4(caseCount); |
| 3699 | for (int j = 0; j < caseCount; j++) { |
| 3700 | int cVal = bc_case_value.getInt(); |
| 3701 | putu4(cVal); |
| 3702 | put_label(curIP, 4); //int lVal = bc_label.getInt(); |
| 3703 | } |
| 3704 | } |
| 3705 | assert(to_bci(curIP) == curPC); |
| 3706 | continue; |
| 3707 | } |
| 3708 | case bc_iinc: |
| 3709 | { |
| 3710 | int local = bc_local.getInt(); |
| 3711 | int delta = (isWide ? bc_short : bc_byte).getInt(); |
| 3712 | if (isWide) { |
| 3713 | putu2(local); |
| 3714 | putu2(delta); |
| 3715 | } else { |
| 3716 | putu1_fast(local); |
| 3717 | putu1_fast(delta); |
| 3718 | } |
| 3719 | continue; |
| 3720 | } |
| 3721 | case bc_sipush: |
| 3722 | { |
| 3723 | int val = bc_short.getInt(); |
| 3724 | putu2(val); |
| 3725 | continue; |
| 3726 | } |
| 3727 | case bc_bipush: |
| 3728 | case bc_newarray: |
| 3729 | { |
| 3730 | int val = bc_byte.getByte(); |
| 3731 | putu1_fast(val); |
| 3732 | continue; |
| 3733 | } |
| 3734 | case bc_ref_escape: |
| 3735 | { |
| 3736 | // Note that insnMap has one entry for this. |
| 3737 | --wp; // not really part of the code |
| 3738 | int size = bc_escrefsize.getInt(); |
| 3739 | entry* ref = bc_escref.getRefN(); |
| 3740 | CHECK; |
| 3741 | switch (size) { |
| 3742 | case 1: putu1ref(ref); break; |
| 3743 | case 2: putref(ref); break; |
| 3744 | default: assert(false); |
| 3745 | } |
| 3746 | continue; |
| 3747 | } |
| 3748 | case bc_byte_escape: |
| 3749 | { |
| 3750 | // Note that insnMap has one entry for all these bytes. |
| 3751 | --wp; // not really part of the code |
| 3752 | int size = bc_escsize.getInt(); |
| 3753 | ensure_put_space(size); |
| 3754 | for (int j = 0; j < size; j++) |
| 3755 | putu1_fast(bc_escbyte.getByte()); |
| 3756 | continue; |
| 3757 | } |
| 3758 | default: |
| 3759 | if (is_invoke_init_op(bc)) { |
| 3760 | origBC = bc_invokespecial; |
| 3761 | entry* classRef; |
| 3762 | switch (bc - _invokeinit_op) { |
| 3763 | case _invokeinit_self_option: classRef = thisClass; break; |
| 3764 | case _invokeinit_super_option: classRef = superClass; break; |
| 3765 | default: assert(bc == _invokeinit_op+_invokeinit_new_option); |
| 3766 | case _invokeinit_new_option: classRef = newClass; break; |
| 3767 | } |
| 3768 | wp[-1] = origBC; // overwrite with origBC |
| 3769 | int coding = bc_initref.getInt(); |
| 3770 | // Find the nth overloading of <init> in classRef. |
| 3771 | entry* ref = null; |
| 3772 | cpindex* ix = (classRef == null)? null: cp.getMethodIndex(classRef); |
| 3773 | for (int j = 0, which_init = 0; ; j++) { |
| 3774 | ref = (ix == null)? null: ix->get(j); |
| 3775 | if (ref == null) break; // oops, bad input |
| 3776 | assert(ref->tag == CONSTANT_Methodref); |
| 3777 | if (ref->memberDescr()->descrName() == cp.sym[cpool::s_lt_init_gt]) { |
| 3778 | if (which_init++ == coding) break; |
| 3779 | } |
| 3780 | } |
| 3781 | putref(ref); |
| 3782 | continue; |
| 3783 | } |
| 3784 | bc_which = ref_band_for_self_op(bc, isAload, origBC); |
| 3785 | if (bc_which != null) { |
| 3786 | if (!isAload) { |
| 3787 | wp[-1] = origBC; // overwrite with origBC |
| 3788 | } else { |
| 3789 | wp[-1] = bc_aload_0; // overwrite with _aload_0 |
| 3790 | // Note: insnMap keeps the _aload_0 separate. |
| 3791 | bcimap.add(++curPC); |
| 3792 | ++curIP; |
| 3793 | putu1_fast(origBC); |
| 3794 | } |
| 3795 | entry* ref = bc_which->getRef(); |
| 3796 | CHECK; |
| 3797 | putref(ref); |
| 3798 | continue; |
| 3799 | } |
| 3800 | if (is_branch_op(bc)) { |
| 3801 | //int lVal = bc_label.getInt(); |
| 3802 | if (bc < bc_goto_w) { |
| 3803 | put_label(curIP, 2); //putu2(lVal & 0xFFFF); |
| 3804 | } else { |
| 3805 | assert(bc <= bc_jsr_w); |
| 3806 | put_label(curIP, 4); //putu4(lVal); |
| 3807 | } |
| 3808 | assert(to_bci(curIP) == curPC); |
| 3809 | continue; |
| 3810 | } |
| 3811 | bc_which = ref_band_for_op(bc); |
| 3812 | if (bc_which != null) { |
| 3813 | entry* ref = bc_which->getRefCommon(bc_which->ix, bc_which->nullOK); |
| 3814 | CHECK; |
| 3815 | if (ref == null && bc_which == &bc_classref) { |
| 3816 | // Shorthand for class self-references. |
| 3817 | ref = thisClass; |
| 3818 | } |
| 3819 | origBC = bc; |
| 3820 | switch (bc) { |
| 3821 | case bc_ildc: |
| 3822 | case bc_cldc: |
| 3823 | case bc_fldc: |
| 3824 | case bc_aldc: |
| 3825 | origBC = bc_ldc; |
| 3826 | break; |
| 3827 | case bc_ildc_w: |
| 3828 | case bc_cldc_w: |
| 3829 | case bc_fldc_w: |
| 3830 | case bc_aldc_w: |
| 3831 | origBC = bc_ldc_w; |
| 3832 | break; |
| 3833 | case bc_lldc2_w: |
| 3834 | case bc_dldc2_w: |
| 3835 | origBC = bc_ldc2_w; |
| 3836 | break; |
| 3837 | case bc_new: |
| 3838 | newClass = ref; |
| 3839 | break; |
| 3840 | } |
| 3841 | wp[-1] = origBC; // overwrite with origBC |
| 3842 | if (origBC == bc_ldc) { |
| 3843 | putu1ref(ref); |
| 3844 | } else { |
| 3845 | putref(ref); |
| 3846 | } |
| 3847 | if (origBC == bc_multianewarray) { |
| 3848 | // Copy the trailing byte also. |
| 3849 | int val = bc_byte.getByte(); |
| 3850 | putu1_fast(val); |
| 3851 | } else if (origBC == bc_invokeinterface) { |
| 3852 | int argSize = ref->memberDescr()->descrType()->typeSize(); |
| 3853 | putu1_fast(1 + argSize); |
| 3854 | putu1_fast(0); |
| 3855 | } |
| 3856 | continue; |
| 3857 | } |
| 3858 | if (is_local_slot_op(bc)) { |
| 3859 | int local = bc_local.getInt(); |
| 3860 | if (isWide) { |
| 3861 | putu2(local); |
| 3862 | if (bc == bc_iinc) { |
| 3863 | int iVal = bc_short.getInt(); |
| 3864 | putu2(iVal); |
| 3865 | } |
| 3866 | } else { |
| 3867 | putu1_fast(local); |
| 3868 | if (bc == bc_iinc) { |
| 3869 | int iVal = bc_byte.getByte(); |
| 3870 | putu1_fast(iVal); |
| 3871 | } |
| 3872 | } |
| 3873 | continue; |
| 3874 | } |
| 3875 | // Random bytecode. Just copy it. |
| 3876 | assert(bc < bc_bytecode_limit); |
| 3877 | } |
| 3878 | } |
| 3879 | doneScanningMethod:{} |
| 3880 | //bcimap.add(curPC); // PC limit is already also in map, from bc_end_marker |
| 3881 | |
| 3882 | // Armed with a bcimap, we can now fix up all the labels. |
| 3883 | for (int i = 0; i < code_fixup_type.size(); i++) { |
| 3884 | int type = code_fixup_type.getByte(i); |
| 3885 | byte* bp = wp_at(code_fixup_offset.get(i)); |
| 3886 | int curIP = code_fixup_source.get(i); |
| 3887 | int destIP = curIP + bc_label.getInt(); |
| 3888 | int span = to_bci(destIP) - to_bci(curIP); |
| 3889 | switch (type) { |
| 3890 | case 2: putu2_at(bp, (ushort)span); break; |
| 3891 | case 4: putu4_at(bp, span); break; |
| 3892 | default: assert(false); |
| 3893 | } |
| 3894 | } |
| 3895 | } |
| 3896 | |
| 3897 | inline // called exactly once => inline |
| 3898 | void unpacker::write_code() { |
| 3899 | int i, j; |
| 3900 | |
| 3901 | int max_stack, max_locals, handler_count, cflags; |
| 3902 | get_code_header(max_stack, max_locals, handler_count, cflags); |
| 3903 | |
| 3904 | if (max_stack < 0) max_stack = code_max_stack.getInt(); |
| 3905 | if (max_locals < 0) max_locals = code_max_na_locals.getInt(); |
| 3906 | if (handler_count < 0) handler_count = code_handler_count.getInt(); |
| 3907 | |
| 3908 | int siglen = cur_descr->descrType()->typeSize(); |
| 3909 | CHECK; |
| 3910 | if ((cur_descr_flags & ACC_STATIC) == 0) siglen++; |
| 3911 | max_locals += siglen; |
| 3912 | |
| 3913 | putu2(max_stack); |
| 3914 | putu2(max_locals); |
| 3915 | size_t bcbase = put_empty(4); |
| 3916 | |
| 3917 | // Write the bytecodes themselves. |
| 3918 | write_bc_ops(); |
| 3919 | CHECK; |
| 3920 | |
| 3921 | byte* bcbasewp = wp_at(bcbase); |
| 3922 | putu4_at(bcbasewp, wp - (bcbasewp+4)); // size of code attr |
| 3923 | |
| 3924 | putu2(handler_count); |
| 3925 | for (j = 0; j < handler_count; j++) { |
| 3926 | int bii = code_handler_start_P.getInt(); |
| 3927 | putu2(to_bci(bii)); |
| 3928 | bii += code_handler_end_PO.getInt(); |
| 3929 | putu2(to_bci(bii)); |
| 3930 | bii += code_handler_catch_PO.getInt(); |
| 3931 | putu2(to_bci(bii)); |
| 3932 | putref(code_handler_class_RCN.getRefN()); |
| 3933 | CHECK; |
| 3934 | } |
| 3935 | |
| 3936 | julong indexBits = cflags; |
| 3937 | if (cflags < 0) { |
| 3938 | bool haveLongFlags = attr_defs[ATTR_CONTEXT_CODE].haveLongFlags(); |
| 3939 | indexBits = code_flags_hi.getLong(code_flags_lo, haveLongFlags); |
| 3940 | } |
| 3941 | write_attrs(ATTR_CONTEXT_CODE, indexBits); |
| 3942 | } |
| 3943 | |
| 3944 | int unpacker::write_attrs(int attrc, julong indexBits) { |
| 3945 | CHECK_0; |
| 3946 | if (indexBits == 0) { |
| 3947 | // Quick short-circuit. |
| 3948 | putu2(0); |
| 3949 | return 0; |
| 3950 | } |
| 3951 | |
| 3952 | attr_definitions& ad = attr_defs[attrc]; |
| 3953 | |
| 3954 | int i, j, j2, idx, count; |
| 3955 | |
| 3956 | int oiCount = 0; |
| 3957 | if (ad.isPredefined(X_ATTR_OVERFLOW) |
| 3958 | && (indexBits & ((julong)1<<X_ATTR_OVERFLOW)) != 0) { |
| 3959 | indexBits -= ((julong)1<<X_ATTR_OVERFLOW); |
| 3960 | oiCount = ad.xxx_attr_count().getInt(); |
| 3961 | } |
| 3962 | |
| 3963 | int bitIndexes[X_ATTR_LIMIT_FLAGS_HI]; |
| 3964 | int biCount = 0; |
| 3965 | |
| 3966 | // Fill bitIndexes with index bits, in order. |
| 3967 | for (idx = 0; indexBits != 0; idx++, indexBits >>= 1) { |
| 3968 | if ((indexBits & 1) != 0) |
| 3969 | bitIndexes[biCount++] = idx; |
| 3970 | } |
| 3971 | assert(biCount <= lengthof(bitIndexes)); |
| 3972 | |
| 3973 | // Write a provisional attribute count, perhaps to be corrected later. |
| 3974 | int naOffset = wpoffset(); |
| 3975 | int na0 = biCount + oiCount; |
| 3976 | putu2(na0); |
| 3977 | |
| 3978 | int na = 0; |
| 3979 | for (i = 0; i < na0; i++) { |
| 3980 | if (i < biCount) |
| 3981 | idx = bitIndexes[i]; |
| 3982 | else |
| 3983 | idx = ad.xxx_attr_indexes().getInt(); |
| 3984 | assert(ad.isIndex(idx)); |
| 3985 | entry* aname = null; |
| 3986 | entry* ref; // scratch |
| 3987 | size_t abase = put_empty(2+4); |
| 3988 | CHECK_0; |
| 3989 | if (idx < ad.flag_limit && ad.isPredefined(idx)) { |
| 3990 | // Switch on the attrc and idx simultaneously. |
| 3991 | switch (ADH_BYTE(attrc, idx)) { |
| 3992 | |
| 3993 | case ADH_BYTE(ATTR_CONTEXT_CLASS, X_ATTR_OVERFLOW): |
| 3994 | case ADH_BYTE(ATTR_CONTEXT_FIELD, X_ATTR_OVERFLOW): |
| 3995 | case ADH_BYTE(ATTR_CONTEXT_METHOD, X_ATTR_OVERFLOW): |
| 3996 | case ADH_BYTE(ATTR_CONTEXT_CODE, X_ATTR_OVERFLOW): |
| 3997 | // no attribute at all, so back up on this one |
| 3998 | wp = wp_at(abase); |
| 3999 | continue; |
| 4000 | |
| 4001 | case ADH_BYTE(ATTR_CONTEXT_CLASS, CLASS_ATTR_ClassFile_version): |
| 4002 | cur_class_minver = class_ClassFile_version_minor_H.getInt(); |
| 4003 | cur_class_majver = class_ClassFile_version_major_H.getInt(); |
| 4004 | // back up; not a real attribute |
| 4005 | wp = wp_at(abase); |
| 4006 | continue; |
| 4007 | |
| 4008 | case ADH_BYTE(ATTR_CONTEXT_CLASS, CLASS_ATTR_InnerClasses): |
| 4009 | // note the existence of this attr, but save for later |
| 4010 | if (cur_class_has_local_ics) |
| 4011 | abort("too many InnerClasses attrs"); |
| 4012 | cur_class_has_local_ics = true; |
| 4013 | wp = wp_at(abase); |
| 4014 | continue; |
| 4015 | |
| 4016 | case ADH_BYTE(ATTR_CONTEXT_CLASS, CLASS_ATTR_SourceFile): |
| 4017 | aname = cp.sym[cpool::s_SourceFile]; |
| 4018 | ref = class_SourceFile_RUN.getRefN(); |
| 4019 | CHECK_0; |
| 4020 | if (ref == null) { |
| 4021 | bytes& n = cur_class->ref(0)->value.b; |
| 4022 | // parse n = (<pkg>/)*<outer>?($<id>)* |
| 4023 | int pkglen = lastIndexOf(SLASH_MIN, SLASH_MAX, n, n.len)+1; |
| 4024 | bytes prefix = n.slice(pkglen, n.len); |
| 4025 | for (;;) { |
| 4026 | // Work backwards, finding all '$', '#', etc. |
| 4027 | int dollar = lastIndexOf(DOLLAR_MIN, DOLLAR_MAX, prefix, prefix.len); |
| 4028 | if (dollar < 0) break; |
| 4029 | prefix = prefix.slice(0, dollar); |
| 4030 | } |
| 4031 | const char* suffix = ".java"; |
| 4032 | int len = prefix.len + strlen(suffix); |
| 4033 | bytes name; name.set(T_NEW(byte, len + 1), len); |
| 4034 | name.strcat(prefix).strcat(suffix); |
| 4035 | ref = cp.ensureUtf8(name); |
| 4036 | } |
| 4037 | putref(ref); |
| 4038 | break; |
| 4039 | |
| 4040 | case ADH_BYTE(ATTR_CONTEXT_CLASS, CLASS_ATTR_EnclosingMethod): |
| 4041 | aname = cp.sym[cpool::s_EnclosingMethod]; |
| 4042 | putref(class_EnclosingMethod_RC.getRefN()); |
| 4043 | putref(class_EnclosingMethod_RDN.getRefN()); |
| 4044 | break; |
| 4045 | |
| 4046 | case ADH_BYTE(ATTR_CONTEXT_FIELD, FIELD_ATTR_ConstantValue): |
| 4047 | aname = cp.sym[cpool::s_ConstantValue]; |
| 4048 | putref(field_ConstantValue_KQ.getRefUsing(cp.getKQIndex())); |
| 4049 | break; |
| 4050 | |
| 4051 | case ADH_BYTE(ATTR_CONTEXT_METHOD, METHOD_ATTR_Code): |
| 4052 | aname = cp.sym[cpool::s_Code]; |
| 4053 | write_code(); |
| 4054 | break; |
| 4055 | |
| 4056 | case ADH_BYTE(ATTR_CONTEXT_METHOD, METHOD_ATTR_Exceptions): |
| 4057 | aname = cp.sym[cpool::s_Exceptions]; |
| 4058 | putu2(count = method_Exceptions_N.getInt()); |
| 4059 | for (j = 0; j < count; j++) { |
| 4060 | putref(method_Exceptions_RC.getRefN()); |
| 4061 | } |
| 4062 | break; |
| 4063 | |
| 4064 | case ADH_BYTE(ATTR_CONTEXT_CODE, CODE_ATTR_StackMapTable): |
| 4065 | aname = cp.sym[cpool::s_StackMapTable]; |
| 4066 | // (keep this code aligned with its brother in unpacker::read_attrs) |
| 4067 | putu2(count = code_StackMapTable_N.getInt()); |
| 4068 | for (j = 0; j < count; j++) { |
| 4069 | int tag = code_StackMapTable_frame_T.getByte(); |
| 4070 | putu1(tag); |
| 4071 | if (tag <= 127) { |
| 4072 | // (64-127) [(2)] |
| 4073 | if (tag >= 64) put_stackmap_type(); |
| 4074 | } else if (tag <= 251) { |
| 4075 | // (247) [(1)(2)] |
| 4076 | // (248-251) [(1)] |
| 4077 | if (tag >= 247) putu2(code_StackMapTable_offset.getInt()); |
| 4078 | if (tag == 247) put_stackmap_type(); |
| 4079 | } else if (tag <= 254) { |
| 4080 | // (252) [(1)(2)] |
| 4081 | // (253) [(1)(2)(2)] |
| 4082 | // (254) [(1)(2)(2)(2)] |
| 4083 | putu2(code_StackMapTable_offset.getInt()); |
| 4084 | for (int j2 = (tag - 251); j2 > 0; j2--) { |
| 4085 | put_stackmap_type(); |
| 4086 | } |
| 4087 | } else { |
| 4088 | // (255) [(1)NH[(2)]NH[(2)]] |
| 4089 | putu2(code_StackMapTable_offset.getInt()); |
| 4090 | putu2(j2 = code_StackMapTable_local_N.getInt()); |
| 4091 | while (j2-- > 0) put_stackmap_type(); |
| 4092 | putu2(j2 = code_StackMapTable_stack_N.getInt()); |
| 4093 | while (j2-- > 0) put_stackmap_type(); |
| 4094 | } |
| 4095 | } |
| 4096 | break; |
| 4097 | |
| 4098 | case ADH_BYTE(ATTR_CONTEXT_CODE, CODE_ATTR_LineNumberTable): |
| 4099 | aname = cp.sym[cpool::s_LineNumberTable]; |
| 4100 | putu2(count = code_LineNumberTable_N.getInt()); |
| 4101 | for (j = 0; j < count; j++) { |
| 4102 | putu2(to_bci(code_LineNumberTable_bci_P.getInt())); |
| 4103 | putu2(code_LineNumberTable_line.getInt()); |
| 4104 | } |
| 4105 | break; |
| 4106 | |
| 4107 | case ADH_BYTE(ATTR_CONTEXT_CODE, CODE_ATTR_LocalVariableTable): |
| 4108 | aname = cp.sym[cpool::s_LocalVariableTable]; |
| 4109 | putu2(count = code_LocalVariableTable_N.getInt()); |
| 4110 | for (j = 0; j < count; j++) { |
| 4111 | int bii = code_LocalVariableTable_bci_P.getInt(); |
| 4112 | int bci = to_bci(bii); |
| 4113 | putu2(bci); |
| 4114 | bii += code_LocalVariableTable_span_O.getInt(); |
| 4115 | putu2(to_bci(bii) - bci); |
| 4116 | putref(code_LocalVariableTable_name_RU.getRefN()); |
| 4117 | putref(code_LocalVariableTable_type_RS.getRefN()); |
| 4118 | putu2(code_LocalVariableTable_slot.getInt()); |
| 4119 | } |
| 4120 | break; |
| 4121 | |
| 4122 | case ADH_BYTE(ATTR_CONTEXT_CODE, CODE_ATTR_LocalVariableTypeTable): |
| 4123 | aname = cp.sym[cpool::s_LocalVariableTypeTable]; |
| 4124 | putu2(count = code_LocalVariableTypeTable_N.getInt()); |
| 4125 | for (j = 0; j < count; j++) { |
| 4126 | int bii = code_LocalVariableTypeTable_bci_P.getInt(); |
| 4127 | int bci = to_bci(bii); |
| 4128 | putu2(bci); |
| 4129 | bii += code_LocalVariableTypeTable_span_O.getInt(); |
| 4130 | putu2(to_bci(bii) - bci); |
| 4131 | putref(code_LocalVariableTypeTable_name_RU.getRefN()); |
| 4132 | putref(code_LocalVariableTypeTable_type_RS.getRefN()); |
| 4133 | putu2(code_LocalVariableTypeTable_slot.getInt()); |
| 4134 | } |
| 4135 | break; |
| 4136 | |
| 4137 | case ADH_BYTE(ATTR_CONTEXT_CLASS, X_ATTR_Signature): |
| 4138 | aname = cp.sym[cpool::s_Signature]; |
| 4139 | putref(class_Signature_RS.getRefN()); |
| 4140 | break; |
| 4141 | |
| 4142 | case ADH_BYTE(ATTR_CONTEXT_FIELD, X_ATTR_Signature): |
| 4143 | aname = cp.sym[cpool::s_Signature]; |
| 4144 | putref(field_Signature_RS.getRefN()); |
| 4145 | break; |
| 4146 | |
| 4147 | case ADH_BYTE(ATTR_CONTEXT_METHOD, X_ATTR_Signature): |
| 4148 | aname = cp.sym[cpool::s_Signature]; |
| 4149 | putref(method_Signature_RS.getRefN()); |
| 4150 | break; |
| 4151 | |
| 4152 | case ADH_BYTE(ATTR_CONTEXT_CLASS, X_ATTR_Deprecated): |
| 4153 | case ADH_BYTE(ATTR_CONTEXT_FIELD, X_ATTR_Deprecated): |
| 4154 | case ADH_BYTE(ATTR_CONTEXT_METHOD, X_ATTR_Deprecated): |
| 4155 | aname = cp.sym[cpool::s_Deprecated]; |
| 4156 | // no data |
| 4157 | break; |
| 4158 | } |
| 4159 | } |
| 4160 | |
| 4161 | if (aname == null) { |
| 4162 | // Unparse a compressor-defined attribute. |
| 4163 | layout_definition* lo = ad.getLayout(idx); |
| 4164 | if (lo == null) { |
| 4165 | abort("bad layout index"); |
| 4166 | break; |
| 4167 | } |
| 4168 | assert(lo->idx == idx); |
| 4169 | aname = lo->nameEntry; |
| 4170 | if (aname == null) { |
| 4171 | bytes nameb; nameb.set(lo->name); |
| 4172 | aname = cp.ensureUtf8(nameb); |
| 4173 | // Cache the name entry for next time. |
| 4174 | lo->nameEntry = aname; |
| 4175 | } |
| 4176 | // Execute all the layout elements. |
| 4177 | band** bands = lo->bands(); |
| 4178 | if (lo->hasCallables()) { |
| 4179 | band& cble = *bands[0]; |
| 4180 | assert(cble.le_kind == EK_CBLE); |
| 4181 | bands = cble.le_body; |
| 4182 | } |
| 4183 | putlayout(bands); |
| 4184 | } |
| 4185 | |
| 4186 | if (aname == null) |
| 4187 | abort("bad attribute index"); |
| 4188 | CHECK_0; |
| 4189 | |
| 4190 | byte* wp1 = wp; |
| 4191 | wp = wp_at(abase); |
| 4192 | |
| 4193 | // DTRT if this attr is on the strip-list. |
| 4194 | // (Note that we emptied the data out of the band first.) |
| 4195 | if (ad.strip_names.contains(aname)) { |
| 4196 | continue; |
| 4197 | } |
| 4198 | |
| 4199 | // patch the name and length |
| 4200 | putref(aname); |
| 4201 | putu4(wp1 - (wp+4)); // put the attr size |
| 4202 | wp = wp1; |
| 4203 | na++; // count the attrs actually written |
| 4204 | } |
| 4205 | |
| 4206 | if (na != na0) |
| 4207 | // Refresh changed count. |
| 4208 | putu2_at(wp_at(naOffset), na); |
| 4209 | return na; |
| 4210 | } |
| 4211 | |
| 4212 | void unpacker::write_members(int num, int attrc) { |
| 4213 | CHECK; |
| 4214 | attr_definitions& ad = attr_defs[attrc]; |
| 4215 | band& member_flags_hi = ad.xxx_flags_hi(); |
| 4216 | band& member_flags_lo = ad.xxx_flags_lo(); |
| 4217 | band& member_descr = (&member_flags_hi)[e_field_descr-e_field_flags_hi]; |
| 4218 | assert(endsWith(member_descr.name, "_descr")); |
| 4219 | assert(endsWith(member_flags_lo.name, "_flags_lo")); |
| 4220 | assert(endsWith(member_flags_lo.name, "_flags_lo")); |
| 4221 | bool haveLongFlags = ad.haveLongFlags(); |
| 4222 | |
| 4223 | putu2(num); |
| 4224 | julong indexMask = attr_defs[attrc].flagIndexMask(); |
| 4225 | for (int i = 0; i < num; i++) { |
| 4226 | julong mflags = member_flags_hi.getLong(member_flags_lo, haveLongFlags); |
| 4227 | entry* mdescr = member_descr.getRef(); |
| 4228 | cur_descr = mdescr; |
| 4229 | putu2(cur_descr_flags = (ushort)(mflags & ~indexMask)); |
| 4230 | CHECK; |
| 4231 | putref(mdescr->descrName()); |
| 4232 | putref(mdescr->descrType()); |
| 4233 | write_attrs(attrc, (mflags & indexMask)); |
| 4234 | CHECK; |
| 4235 | } |
| 4236 | cur_descr = null; |
| 4237 | } |
| 4238 | |
| 4239 | extern "C" |
| 4240 | int raw_address_cmp(const void* p1p, const void* p2p) { |
| 4241 | void* p1 = *(void**) p1p; |
| 4242 | void* p2 = *(void**) p2p; |
| 4243 | return (p1 > p2)? 1: (p1 < p2)? -1: 0; |
| 4244 | } |
| 4245 | |
| 4246 | void unpacker::write_classfile_tail() { |
| 4247 | cur_classfile_tail.empty(); |
| 4248 | set_output(&cur_classfile_tail); |
| 4249 | |
| 4250 | int i, num; |
| 4251 | |
| 4252 | attr_definitions& ad = attr_defs[ATTR_CONTEXT_CLASS]; |
| 4253 | |
| 4254 | bool haveLongFlags = ad.haveLongFlags(); |
| 4255 | julong kflags = class_flags_hi.getLong(class_flags_lo, haveLongFlags); |
| 4256 | julong indexMask = ad.flagIndexMask(); |
| 4257 | |
| 4258 | cur_class = class_this.getRef(); |
| 4259 | cur_super = class_super.getRef(); |
| 4260 | |
| 4261 | CHECK; |
| 4262 | |
| 4263 | if (cur_super == cur_class) cur_super = null; |
| 4264 | // special representation for java/lang/Object |
| 4265 | |
| 4266 | putu2((ushort)(kflags & ~indexMask)); |
| 4267 | putref(cur_class); |
| 4268 | putref(cur_super); |
| 4269 | |
| 4270 | putu2(num = class_interface_count.getInt()); |
| 4271 | for (i = 0; i < num; i++) { |
| 4272 | putref(class_interface.getRef()); |
| 4273 | } |
| 4274 | |
| 4275 | write_members(class_field_count.getInt(), ATTR_CONTEXT_FIELD); |
| 4276 | write_members(class_method_count.getInt(), ATTR_CONTEXT_METHOD); |
| 4277 | CHECK; |
| 4278 | |
| 4279 | cur_class_has_local_ics = false; // may be set true by write_attrs |
| 4280 | |
| 4281 | |
| 4282 | int naOffset = wpoffset(); |
| 4283 | int na = write_attrs(ATTR_CONTEXT_CLASS, (kflags & indexMask)); |
| 4284 | |
| 4285 | |
| 4286 | // at the very last, choose which inner classes (if any) pertain to k: |
| 4287 | #ifdef ASSERT |
| 4288 | for (i = 0; i < ic_count; i++) { |
| 4289 | assert(!ics[i].requested); |
| 4290 | } |
| 4291 | #endif |
| 4292 | // First, consult the global table and the local constant pool, |
| 4293 | // and decide on the globally implied inner classes. |
| 4294 | // (Note that we read the cpool's outputIndex fields, but we |
| 4295 | // do not yet write them, since the local IC attribute might |
| 4296 | // reverse a global decision to declare an IC.) |
| 4297 | assert(requested_ics.length() == 0); // must start out empty |
| 4298 | // Always include all members of the current class. |
| 4299 | for (inner_class* child = cp.getFirstChildIC(cur_class); |
| 4300 | child != null; |
| 4301 | child = cp.getNextChildIC(child)) { |
| 4302 | child->requested = true; |
| 4303 | requested_ics.add(child); |
| 4304 | } |
| 4305 | // And, for each inner class mentioned in the constant pool, |
| 4306 | // include it and all its outers. |
| 4307 | int noes = cp.outputEntries.length(); |
| 4308 | entry** oes = (entry**) cp.outputEntries.base(); |
| 4309 | for (i = 0; i < noes; i++) { |
| 4310 | entry& e = *oes[i]; |
| 4311 | if (e.tag != CONSTANT_Class) continue; // wrong sort |
| 4312 | for (inner_class* ic = cp.getIC(&e); |
| 4313 | ic != null; |
| 4314 | ic = cp.getIC(ic->outer)) { |
| 4315 | if (ic->requested) break; // already processed |
| 4316 | ic->requested = true; |
| 4317 | requested_ics.add(ic); |
| 4318 | } |
| 4319 | } |
| 4320 | int local_ics = requested_ics.length(); |
| 4321 | // Second, consult a local attribute (if any) and adjust the global set. |
| 4322 | inner_class* extra_ics = null; |
| 4323 | int num_extra_ics = 0; |
| 4324 | if (cur_class_has_local_ics) { |
| 4325 | // adjust the set of ICs by symmetric set difference w/ the locals |
| 4326 | num_extra_ics = class_InnerClasses_N.getInt(); |
| 4327 | if (num_extra_ics == 0) { |
| 4328 | // Explicit zero count has an irregular meaning: It deletes the attr. |
| 4329 | local_ics = 0; // (short-circuit all tests of requested bits) |
| 4330 | } else { |
| 4331 | extra_ics = T_NEW(inner_class, num_extra_ics); |
| 4332 | // Note: extra_ics will be freed up by next call to get_next_file(). |
| 4333 | } |
| 4334 | } |
| 4335 | for (i = 0; i < num_extra_ics; i++) { |
| 4336 | inner_class& extra_ic = extra_ics[i]; |
| 4337 | extra_ic.inner = class_InnerClasses_RC.getRef(); |
| 4338 | CHECK; |
| 4339 | // Find the corresponding equivalent global IC: |
| 4340 | inner_class* global_ic = cp.getIC(extra_ic.inner); |
| 4341 | int flags = class_InnerClasses_F.getInt(); |
| 4342 | if (flags == 0) { |
| 4343 | // The extra IC is simply a copy of a global IC. |
| 4344 | if (global_ic == null) { |
| 4345 | abort("bad reference to inner class"); |
| 4346 | break; |
| 4347 | } |
| 4348 | extra_ic = (*global_ic); // fill in rest of fields |
| 4349 | } else { |
| 4350 | flags &= ~ACC_IC_LONG_FORM; // clear high bit if set to get clean zero |
| 4351 | extra_ic.flags = flags; |
| 4352 | extra_ic.outer = class_InnerClasses_outer_RCN.getRefN(); |
| 4353 | extra_ic.name = class_InnerClasses_name_RUN.getRefN(); |
| 4354 | // Detect if this is an exact copy of the global tuple. |
| 4355 | if (global_ic != null) { |
| 4356 | if (global_ic->flags != extra_ic.flags || |
| 4357 | global_ic->outer != extra_ic.outer || |
| 4358 | global_ic->name != extra_ic.name) { |
| 4359 | global_ic = null; // not really the same, so break the link |
| 4360 | } |
| 4361 | } |
| 4362 | } |
| 4363 | if (global_ic != null && global_ic->requested) { |
| 4364 | // This local repetition reverses the globally implied request. |
| 4365 | global_ic->requested = false; |
| 4366 | extra_ic.requested = false; |
| 4367 | local_ics -= 1; |
| 4368 | } else { |
| 4369 | // The global either does not exist, or is not yet requested. |
| 4370 | extra_ic.requested = true; |
| 4371 | local_ics += 1; |
| 4372 | } |
| 4373 | } |
| 4374 | // Finally, if there are any that survived, put them into an attribute. |
| 4375 | // (Note that a zero-count attribute is always deleted.) |
| 4376 | // The putref calls below will tell the constant pool to add any |
| 4377 | // necessary local CP references to support the InnerClasses attribute. |
| 4378 | // This step must be the last round of additions to the local CP. |
| 4379 | if (local_ics > 0) { |
| 4380 | // append the new attribute: |
| 4381 | putref(cp.sym[cpool::s_InnerClasses]); |
| 4382 | putu4(2 + 2*4*local_ics); |
| 4383 | putu2(local_ics); |
| 4384 | PTRLIST_QSORT(requested_ics, raw_address_cmp); |
| 4385 | int num_global_ics = requested_ics.length(); |
| 4386 | for (i = -num_global_ics; i < num_extra_ics; i++) { |
| 4387 | inner_class* ic; |
| 4388 | if (i < 0) |
| 4389 | ic = (inner_class*) requested_ics.get(num_global_ics+i); |
| 4390 | else |
| 4391 | ic = &extra_ics[i]; |
| 4392 | if (ic->requested) { |
| 4393 | putref(ic->inner); |
| 4394 | putref(ic->outer); |
| 4395 | putref(ic->name); |
| 4396 | putu2(ic->flags); |
| 4397 | NOT_PRODUCT(local_ics--); |
| 4398 | } |
| 4399 | } |
| 4400 | assert(local_ics == 0); // must balance |
| 4401 | putu2_at(wp_at(naOffset), ++na); // increment class attr count |
| 4402 | } |
| 4403 | |
| 4404 | // Tidy up global 'requested' bits: |
| 4405 | for (i = requested_ics.length(); --i >= 0; ) { |
| 4406 | inner_class* ic = (inner_class*) requested_ics.get(i); |
| 4407 | ic->requested = false; |
| 4408 | } |
| 4409 | requested_ics.empty(); |
| 4410 | |
| 4411 | CHECK; |
| 4412 | close_output(); |
| 4413 | |
| 4414 | // rewrite CP references in the tail |
| 4415 | cp.computeOutputIndexes(); |
| 4416 | int nextref = 0; |
| 4417 | for (i = 0; i < (int)class_fixup_type.size(); i++) { |
| 4418 | int type = class_fixup_type.getByte(i); |
| 4419 | byte* fixp = wp_at(class_fixup_offset.get(i)); |
| 4420 | entry* e = (entry*)class_fixup_ref.get(nextref++); |
| 4421 | int idx = e->getOutputIndex(); |
| 4422 | switch (type) { |
| 4423 | case 1: putu1_at(fixp, idx); break; |
| 4424 | case 2: putu2_at(fixp, idx); break; |
| 4425 | default: assert(false); // should not reach here |
| 4426 | } |
| 4427 | } |
| 4428 | CHECK; |
| 4429 | } |
| 4430 | |
| 4431 | void unpacker::write_classfile_head() { |
| 4432 | cur_classfile_head.empty(); |
| 4433 | set_output(&cur_classfile_head); |
| 4434 | |
| 4435 | putu4(JAVA_MAGIC); |
| 4436 | putu2(cur_class_minver); |
| 4437 | putu2(cur_class_majver); |
| 4438 | putu2(cp.outputIndexLimit); |
| 4439 | |
| 4440 | int checkIndex = 1; |
| 4441 | int noes = cp.outputEntries.length(); |
| 4442 | entry** oes = (entry**) cp.outputEntries.base(); |
| 4443 | for (int i = 0; i < noes; i++) { |
| 4444 | entry& e = *oes[i]; |
| 4445 | assert(e.getOutputIndex() == checkIndex++); |
| 4446 | byte tag = e.tag; |
| 4447 | assert(tag != CONSTANT_Signature); |
| 4448 | putu1(tag); |
| 4449 | switch (tag) { |
| 4450 | case CONSTANT_Utf8: |
| 4451 | putu2(e.value.b.len); |
| 4452 | put_bytes(e.value.b); |
| 4453 | break; |
| 4454 | case CONSTANT_Integer: |
| 4455 | case CONSTANT_Float: |
| 4456 | putu4(e.value.i); |
| 4457 | break; |
| 4458 | case CONSTANT_Long: |
| 4459 | case CONSTANT_Double: |
| 4460 | putu8(e.value.l); |
| 4461 | assert(checkIndex++); |
| 4462 | break; |
| 4463 | case CONSTANT_Class: |
| 4464 | case CONSTANT_String: |
| 4465 | // just write the ref |
| 4466 | putu2(e.refs[0]->getOutputIndex()); |
| 4467 | break; |
| 4468 | case CONSTANT_Fieldref: |
| 4469 | case CONSTANT_Methodref: |
| 4470 | case CONSTANT_InterfaceMethodref: |
| 4471 | case CONSTANT_NameandType: |
| 4472 | putu2(e.refs[0]->getOutputIndex()); |
| 4473 | putu2(e.refs[1]->getOutputIndex()); |
| 4474 | break; |
| 4475 | default: |
| 4476 | abort(ERROR_INTERNAL); |
| 4477 | } |
| 4478 | } |
| 4479 | |
| 4480 | #ifndef PRODUCT |
| 4481 | total_cp_size[0] += cp.outputIndexLimit; |
| 4482 | total_cp_size[1] += cur_classfile_head.size(); |
| 4483 | #endif |
| 4484 | close_output(); |
| 4485 | } |
| 4486 | |
| 4487 | unpacker::file* unpacker::get_next_file() { |
| 4488 | CHECK_0; |
| 4489 | free_temps(); |
| 4490 | if (files_remaining == 0) { |
| 4491 | // Leave a clue that we're exhausted. |
| 4492 | cur_file.name = null; |
| 4493 | cur_file.size = null; |
| 4494 | if (archive_size != 0) { |
| 4495 | julong predicted_size = unsized_bytes_read + archive_size; |
| 4496 | if (predicted_size != bytes_read) |
| 4497 | abort("archive header had incorrect size"); |
| 4498 | } |
| 4499 | return null; |
| 4500 | } |
| 4501 | files_remaining -= 1; |
| 4502 | assert(files_written < file_count || classes_written < class_count); |
| 4503 | cur_file.name = ""; |
| 4504 | cur_file.size = 0; |
| 4505 | cur_file.modtime = default_file_modtime; |
| 4506 | cur_file.options = default_file_options; |
| 4507 | cur_file.data[0].set(null, 0); |
| 4508 | cur_file.data[1].set(null, 0); |
| 4509 | if (files_written < file_count) { |
| 4510 | entry* e = file_name.getRef(); |
| 4511 | CHECK_0; |
| 4512 | cur_file.name = e->utf8String(); |
| 4513 | bool haveLongSize = ((archive_options & AO_HAVE_FILE_SIZE_HI) != 0); |
| 4514 | cur_file.size = file_size_hi.getLong(file_size_lo, haveLongSize); |
| 4515 | if ((archive_options & AO_HAVE_FILE_MODTIME) != 0) |
| 4516 | cur_file.modtime += file_modtime.getInt(); //relative to archive modtime |
| 4517 | if ((archive_options & AO_HAVE_FILE_OPTIONS) != 0) |
| 4518 | cur_file.options |= file_options.getInt() & ~suppress_file_options; |
| 4519 | } else if (classes_written < class_count) { |
| 4520 | // there is a class for a missing file record |
| 4521 | cur_file.options |= FO_IS_CLASS_STUB; |
| 4522 | } |
| 4523 | if ((cur_file.options & FO_IS_CLASS_STUB) != 0) { |
| 4524 | assert(classes_written < class_count); |
| 4525 | classes_written += 1; |
| 4526 | if (cur_file.size != 0) { |
| 4527 | abort("class file size transmitted"); |
| 4528 | return null; |
| 4529 | } |
| 4530 | reset_cur_classfile(); |
| 4531 | |
| 4532 | // write the meat of the classfile: |
| 4533 | write_classfile_tail(); |
| 4534 | cur_file.data[1] = cur_classfile_tail.b; |
| 4535 | CHECK_0; |
| 4536 | |
| 4537 | // write the CP of the classfile, second: |
| 4538 | write_classfile_head(); |
| 4539 | cur_file.data[0] = cur_classfile_head.b; |
| 4540 | CHECK_0; |
| 4541 | |
| 4542 | cur_file.size += cur_file.data[0].len; |
| 4543 | cur_file.size += cur_file.data[1].len; |
| 4544 | if (cur_file.name[0] == '\0') { |
| 4545 | bytes& prefix = cur_class->ref(0)->value.b; |
| 4546 | const char* suffix = ".class"; |
| 4547 | int len = prefix.len + strlen(suffix); |
| 4548 | bytes name; name.set(T_NEW(byte, len + 1), len); |
| 4549 | cur_file.name = name.strcat(prefix).strcat(suffix).strval(); |
| 4550 | } |
| 4551 | } else { |
| 4552 | // If there is buffered file data, produce a pointer to it. |
| 4553 | if (cur_file.size != (size_t) cur_file.size) { |
| 4554 | // Silly size specified. |
| 4555 | abort("resource file too large"); |
| 4556 | return null; |
| 4557 | } |
| 4558 | size_t rpleft = input_remaining(); |
| 4559 | if (rpleft > 0) { |
| 4560 | if (rpleft > cur_file.size) |
| 4561 | rpleft = (size_t) cur_file.size; |
| 4562 | cur_file.data[0].set(rp, rpleft); |
| 4563 | rp += rpleft; |
| 4564 | } |
| 4565 | if (rpleft < cur_file.size) { |
| 4566 | // Caller must read the rest. |
| 4567 | size_t fleft = cur_file.size - rpleft; |
| 4568 | bytes_read += fleft; // Credit it to the overall archive size. |
| 4569 | } |
| 4570 | } |
| 4571 | CHECK_0; |
| 4572 | bytes_written += cur_file.size; |
| 4573 | files_written += 1; |
| 4574 | return &cur_file; |
| 4575 | } |
| 4576 | |
| 4577 | // Write a file to jarout. |
| 4578 | void unpacker::write_file_to_jar(unpacker::file* f) { |
| 4579 | size_t htsize = f->data[0].len + f->data[1].len; |
| 4580 | julong fsize = f->size; |
| 4581 | #ifndef PRODUCT |
| 4582 | if (nowrite NOT_PRODUCT(|| skipfiles-- > 0)) { |
| 4583 | printcr(2,"would write %d bytes to %s", (int) fsize, f->name); |
| 4584 | return; |
| 4585 | } |
| 4586 | #endif |
| 4587 | if (htsize == fsize) { |
| 4588 | jarout->addJarEntry(f->name, f->deflate_hint(), f->modtime, |
| 4589 | f->data[0], f->data[1]); |
| 4590 | } else { |
| 4591 | assert(input_remaining() == 0); |
| 4592 | bytes part1, part2; |
| 4593 | part1.len = f->data[0].len; |
| 4594 | part1.set(T_NEW(byte, part1.len), part1.len); |
| 4595 | part1.copyFrom(f->data[0]); |
| 4596 | assert(f->data[1].len == 0); |
| 4597 | part2.set(null, 0); |
| 4598 | size_t fleft = (size_t) fsize - part1.len; |
| 4599 | assert(bytes_read > fleft); // part2 already credited by get_next_file |
| 4600 | bytes_read -= fleft; |
| 4601 | if (fleft > 0) { |
| 4602 | // Must read some more. |
| 4603 | if (live_input) { |
| 4604 | // Stop using the input buffer. Make a new one: |
| 4605 | if (free_input) input.free(); |
| 4606 | input.init(fleft > (1<<12) ? fleft : (1<<12)); |
| 4607 | free_input = true; |
| 4608 | live_input = false; |
| 4609 | } else { |
| 4610 | // Make it large enough. |
| 4611 | assert(free_input); // must be reallocable |
| 4612 | input.ensureSize(fleft); |
| 4613 | } |
| 4614 | rplimit = rp = input.base(); |
| 4615 | input.setLimit(rp + fleft); |
| 4616 | if (!ensure_input(fleft)) |
| 4617 | abort("EOF reading resource file"); |
| 4618 | part2.ptr = input_scan(); |
| 4619 | part2.len = input_remaining(); |
| 4620 | rplimit = rp = input.base(); |
| 4621 | } |
| 4622 | jarout->addJarEntry(f->name, f->deflate_hint(), f->modtime, |
| 4623 | part1, part2); |
| 4624 | } |
| 4625 | if (verbose >= 3) { |
| 4626 | fprintf(errstrm, "Wrote %lld bytes to: %s\n", fsize, f->name); |
| 4627 | } |
| 4628 | } |
| 4629 | |
| 4630 | // Redirect the stdio to the specified file in the unpack.log.file option |
| 4631 | void unpacker::redirect_stdio() { |
| 4632 | if (log_file == null) { |
| 4633 | log_file = LOGFILE_STDOUT; |
| 4634 | } |
| 4635 | if (log_file == errstrm_name) |
| 4636 | // Nothing more to be done. |
| 4637 | return; |
| 4638 | errstrm_name = log_file; |
| 4639 | if (strcmp(log_file, LOGFILE_STDERR) == 0) { |
| 4640 | errstrm = stderr; |
| 4641 | return; |
| 4642 | } else if (strcmp(log_file, LOGFILE_STDOUT) == 0) { |
| 4643 | errstrm = stdout; |
| 4644 | return; |
| 4645 | } else if (log_file[0] != '\0' && (errstrm = fopen(log_file,"a+")) != NULL) { |
| 4646 | return; |
| 4647 | } else { |
| 4648 | char log_file_name[PATH_MAX+100]; |
| 4649 | char tmpdir[PATH_MAX]; |
| 4650 | #ifdef WIN32 |
| 4651 | int n = GetTempPath(PATH_MAX,tmpdir); //API returns with trailing '\' |
| 4652 | if (n < 1 || n > PATH_MAX) { |
| 4653 | sprintf(tmpdir,"C:\\"); |
| 4654 | } |
| 4655 | sprintf(log_file_name, "%sunpack.log", tmpdir); |
| 4656 | #else |
| 4657 | sprintf(tmpdir,"/tmp"); |
| 4658 | sprintf(log_file_name, "/tmp/unpack.log"); |
| 4659 | #endif |
| 4660 | if ((errstrm = fopen(log_file_name, "a+")) != NULL) { |
| 4661 | log_file = errstrm_name = saveStr(log_file_name); |
| 4662 | return ; |
| 4663 | } |
| 4664 | |
| 4665 | char *tname = tempnam(tmpdir,"#upkg"); |
| 4666 | sprintf(log_file_name, "%s", tname); |
| 4667 | if ((errstrm = fopen(log_file_name, "a+")) != NULL) { |
| 4668 | log_file = errstrm_name = saveStr(log_file_name); |
| 4669 | return ; |
| 4670 | } |
| 4671 | #ifndef WIN32 |
| 4672 | sprintf(log_file_name, "/dev/null"); |
| 4673 | // On windows most likely it will fail. |
| 4674 | if ( (errstrm = fopen(log_file_name, "a+")) != NULL) { |
| 4675 | log_file = errstrm_name = saveStr(log_file_name); |
| 4676 | return ; |
| 4677 | } |
| 4678 | #endif |
| 4679 | // Last resort |
| 4680 | // (Do not use stdout, since it might be jarout->jarfp.) |
| 4681 | errstrm = stderr; |
| 4682 | log_file = errstrm_name = LOGFILE_STDERR; |
| 4683 | } |
| 4684 | } |
| 4685 | |
| 4686 | #ifndef PRODUCT |
| 4687 | int unpacker::printcr_if_verbose(int level, const char* fmt ...) { |
| 4688 | if (verbose < level+10) return 0; |
| 4689 | va_list vl; |
| 4690 | va_start(vl, fmt); |
| 4691 | char fmtbuf[300]; |
| 4692 | strcpy(fmtbuf+100, fmt); |
| 4693 | strcat(fmtbuf+100, "\n"); |
| 4694 | char* fmt2 = fmtbuf+100; |
| 4695 | while (level-- > 0) *--fmt2 = ' '; |
| 4696 | vfprintf(errstrm, fmt2, vl); |
| 4697 | return 1; // for ?: usage |
| 4698 | } |
| 4699 | #endif |
| 4700 | |
| 4701 | void unpacker::abort(const char* message) { |
| 4702 | if (message == null) message = "error unpacking archive"; |
| 4703 | #ifdef UNPACK_JNI |
| 4704 | if (message[0] == '@') { // secret convention for sprintf |
| 4705 | bytes saved; |
| 4706 | saved.saveFrom(message+1); |
| 4707 | mallocs.add(message = saved.strval()); |
| 4708 | } |
| 4709 | abort_message = message; |
| 4710 | return; |
| 4711 | #else |
| 4712 | if (message[0] == '@') ++message; |
| 4713 | fprintf(errstrm, "%s\n", message); |
| 4714 | #ifndef PRODUCT |
| 4715 | fflush(errstrm); |
| 4716 | ::abort(); |
| 4717 | #else |
| 4718 | exit(-1); |
| 4719 | #endif |
| 4720 | #endif // JNI |
| 4721 | } |