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