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