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