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