src/jdk.pack/share/native/common-unpack/bytes.cpp
author sdama
Tue, 12 Jun 2018 18:30:52 +0530
changeset 50522 99f4d3b6d487
parent 47216 71c04702a3d5
permissions -rw-r--r--
8204861: fix for 8196993 has broken the build on linux Summary: undo the changes done for 8196993 Reviewed-by: stefank, shade
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 5506
diff changeset
     2
 * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5191
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5191
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5191
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5191
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5191
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include <stdio.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
#include <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include <string.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include "defines.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include "bytes.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#include "utils.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
static byte dummy[1 << 10];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
bool bytes::inBounds(const void* p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
  return p >= ptr && p < limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
void bytes::malloc(size_t len_) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
  len = len_;
5191
79b41f733e33 6902299: Java JAR "unpack200" must verify input parameters
ksrini
parents: 2624
diff changeset
    43
  ptr = NEW(byte, add_size(len_, 1));  // add trailing zero byte always
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
  if (ptr == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    // set ptr to some victim memory, to ease escape
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    set(dummy, sizeof(dummy)-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    unpack_abort(ERROR_ENOMEM);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
void bytes::realloc(size_t len_) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
  if (len == len_)   return;  // nothing to do
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
  if (ptr == dummy)  return;  // escaping from an error
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
  if (ptr == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    malloc(len_);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
  byte* oldptr = ptr;
5191
79b41f733e33 6902299: Java JAR "unpack200" must verify input parameters
ksrini
parents: 2624
diff changeset
    59
  ptr = (len_ >= PSIZE_MAX) ? null : (byte*)::realloc(ptr, add_size(len_, 1));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
  if (ptr != null)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    mtrace('r', oldptr, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    mtrace('m', ptr, len_+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    if (len < len_)  memset(ptr+len, 0, len_-len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    ptr[len_] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    len = len_;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
  } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    ptr = oldptr;  // ease our escape
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    unpack_abort(ERROR_ENOMEM);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
void bytes::free() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
  if (ptr == dummy)  return;  // escaping from an error
1082
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
    74
  if (ptr != null) {
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
    75
    mtrace('f', ptr, 0);
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
    76
    ::free(ptr);
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
    77
  }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
  len = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
  ptr = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
int bytes::indexOf(byte c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
  byte* p = (byte*) memchr(ptr, c, len);
1082
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
    84
  return (p == 0) ? -1 : (int)(p - ptr);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
byte* bytes::writeTo(byte* bp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
  memcpy(bp, ptr, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
  return bp+len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
int bytes::compareTo(bytes& other) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
  size_t l1 = len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
  size_t l2 = other.len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
  int cmp = memcmp(ptr, other.ptr, (l1 < l2) ? l1 : l2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
  if (cmp != 0)  return cmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
  return (l1 < l2) ? -1 : (l1 > l2) ? 1 : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
void bytes::saveFrom(const void* ptr_, size_t len_) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
  malloc(len_);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
  // Save as much as possible.  (Helps unpacker::abort.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
  if (len_ > len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    assert(ptr == dummy);  // error recovery
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    len_ = len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
  copyFrom(ptr_, len_);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
//#TODO: Need to fix for exception handling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
void bytes::copyFrom(const void* ptr_, size_t len_, size_t offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
  assert(len_ == 0 || inBounds(ptr + offset));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
  assert(len_ == 0 || inBounds(ptr + offset+len_-1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
  memcpy(ptr+offset, ptr_, len_);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
#ifndef PRODUCT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
const char* bytes::string() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
  if (len == 0)  return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
  if (ptr[len] == 0 && strlen((char*)ptr) == len)  return (const char*) ptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
  bytes junk;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
  junk.saveFrom(*this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
  return (char*) junk.ptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
// Make sure there are 'o' bytes beyond the fill pointer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
// advance the fill pointer, and return the old fill pointer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
byte* fillbytes::grow(size_t s) {
2602
5b394a4b6ce1 6755943: Java JAR Pack200 Decompression should enforce stricter header checks
ksrini
parents: 2
diff changeset
   131
  size_t nlen = add_size(b.len, s);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
  if (nlen <= allocated) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    b.len = nlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    return limit()-s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
  size_t maxlen = nlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
  if (maxlen < 128)          maxlen = 128;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
  if (maxlen < allocated*2)  maxlen = allocated*2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
  if (allocated == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    // Initial buffer was not malloced.  Do not reallocate it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    bytes old = b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    b.malloc(maxlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    if (b.len == maxlen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
      old.writeTo(b.ptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
  } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    b.realloc(maxlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
  allocated = b.len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
  if (allocated != maxlen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    assert(unpack_aborting());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    b.len = nlen-s;  // back up
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    return dummy;    // scribble during error recov.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
  // after realloc, recompute pointers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
  b.len = nlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
  assert(b.len <= allocated);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
  return limit()-s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
void fillbytes::ensureSize(size_t s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
  if (allocated >= s)  return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
  size_t len0 = b.len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
  grow(s - size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
  b.len = len0;  // put it back
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
int ptrlist::indexOf(const void* x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
  int len = length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
  for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    if (get(i) == x)  return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
  return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
void ptrlist::freeAll() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
  int len = length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
  for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    void* p = (void*) get(i);
1082
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
   179
    if (p != null)  {
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
   180
      mtrace('f', p, 0);
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
   181
      ::free(p);
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
   182
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
  free();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
int intlist::indexOf(int x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
  int len = length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
  for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    if (get(i) == x)  return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
  return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
}