0N/A/*
2362N/A * Copyright (c) 2001, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A#ifdef WIN32_LEAN_AND_MEAN
0N/Atypedef signed char byte ;
0N/A#endif
0N/A
0N/Astruct bytes {
0N/A byte* ptr;
0N/A size_t len;
0N/A byte* limit() { return ptr+len; }
0N/A
0N/A void set(byte* ptr_, size_t len_) { ptr = ptr_; len = len_; }
0N/A void set(const char* str) { ptr = (byte*)str; len = strlen(str); }
0N/A bool inBounds(const void* p); // p in [ptr, limit)
0N/A void malloc(size_t len_);
0N/A void realloc(size_t len_);
0N/A void free();
0N/A void copyFrom(const void* ptr_, size_t len_, size_t offset = 0);
0N/A void saveFrom(const void* ptr_, size_t len_);
0N/A void saveFrom(const char* str) { saveFrom(str, strlen(str)); }
0N/A void copyFrom(bytes& other, size_t offset = 0) {
0N/A copyFrom(other.ptr, other.len, offset);
0N/A }
0N/A void saveFrom(bytes& other) {
0N/A saveFrom(other.ptr, other.len);
0N/A }
0N/A void clear(int fill_byte = 0) { memset(ptr, fill_byte, len); }
0N/A byte* writeTo(byte* bp);
0N/A bool equals(bytes& other) { return 0 == compareTo(other); }
0N/A int compareTo(bytes& other);
0N/A bool contains(byte c) { return indexOf(c) >= 0; }
0N/A int indexOf(byte c);
0N/A // substrings:
0N/A static bytes of(byte* ptr, size_t len) {
0N/A bytes res;
0N/A res.set(ptr, len);
0N/A return res;
0N/A }
0N/A bytes slice(size_t beg, size_t end) {
0N/A bytes res;
0N/A res.ptr = ptr + beg;
0N/A res.len = end - beg;
0N/A assert(res.len == 0 || inBounds(res.ptr) && inBounds(res.limit()-1));
0N/A return res;
0N/A }
0N/A // building C strings inside byte buffers:
0N/A bytes& strcat(const char* str) { ::strcat((char*)ptr, str); return *this; }
0N/A bytes& strcat(bytes& other) { ::strncat((char*)ptr, (char*)other.ptr, other.len); return *this; }
0N/A char* strval() { assert(strlen((char*)ptr) == len); return (char*) ptr; }
0N/A#ifdef PRODUCT
0N/A const char* string() { return 0; }
0N/A#else
0N/A const char* string();
0N/A#endif
0N/A};
0N/A#define BYTES_OF(var) (bytes::of((byte*)&(var), sizeof(var)))
0N/A
0N/Astruct fillbytes {
0N/A bytes b;
0N/A size_t allocated;
0N/A
0N/A byte* base() { return b.ptr; }
0N/A size_t size() { return b.len; }
0N/A byte* limit() { return b.limit(); } // logical limit
0N/A void setLimit(byte* lp) { assert(isAllocated(lp)); b.len = lp - b.ptr; }
0N/A byte* end() { return b.ptr + allocated; } // physical limit
0N/A byte* loc(size_t o) { assert(o < b.len); return b.ptr + o; }
0N/A void init() { allocated = 0; b.set(null, 0); }
0N/A void init(size_t s) { init(); ensureSize(s); }
0N/A void free() { if (allocated != 0) b.free(); allocated = 0; }
0N/A void empty() { b.len = 0; }
0N/A byte* grow(size_t s); // grow so that limit() += s
0N/A int getByte(uint i) { return *loc(i) & 0xFF; }
0N/A void addByte(byte x) { *grow(1) = x; }
0N/A void ensureSize(size_t s); // make sure allocated >= s
0N/A void trimToSize() { if (allocated > size()) b.realloc(allocated = size()); }
0N/A bool canAppend(size_t s) { return allocated > b.len+s; }
0N/A bool isAllocated(byte* p) { return p >= base() && p <= end(); } //asserts
0N/A void set(bytes& src) { set(src.ptr, src.len); }
0N/A
0N/A void set(byte* ptr, size_t len) {
0N/A b.set(ptr, len);
0N/A allocated = 0; // mark as not reallocatable
0N/A }
0N/A
0N/A // block operations on resizing byte buffer:
0N/A fillbytes& append(const void* ptr_, size_t len_)
0N/A { memcpy(grow(len_), ptr_, len_); return (*this); }
0N/A fillbytes& append(bytes& other)
0N/A { return append(other.ptr, other.len); }
0N/A fillbytes& append(const char* str)
0N/A { return append(str, strlen(str)); }
0N/A};
0N/A
0N/Astruct ptrlist : fillbytes {
0N/A typedef const void* cvptr;
494N/A int length() { return (int)(size() / sizeof(cvptr)); }
0N/A cvptr* base() { return (cvptr*) fillbytes::base(); }
0N/A cvptr& get(int i) { return *(cvptr*)loc(i * sizeof(cvptr)); }
0N/A cvptr* limit() { return (cvptr*) fillbytes::limit(); }
0N/A void add(cvptr x) { *(cvptr*)grow(sizeof(x)) = x; }
0N/A void popTo(int l) { assert(l <= length()); b.len = l * sizeof(cvptr); }
0N/A int indexOf(cvptr x);
0N/A bool contains(cvptr x) { return indexOf(x) >= 0; }
0N/A void freeAll(); // frees every ptr on the list, plus the list itself
0N/A};
0N/A// Use a macro rather than mess with subtle mismatches
0N/A// between member and non-member function pointers.
0N/A#define PTRLIST_QSORT(ptrls, fn) \
0N/A ::qsort((ptrls).base(), (ptrls).length(), sizeof(void*), fn)
0N/A
0N/Astruct intlist : fillbytes {
494N/A int length() { return (int)(size() / sizeof(int)); }
0N/A int* base() { return (int*) fillbytes::base(); }
0N/A int& get(int i) { return *(int*)loc(i * sizeof(int)); }
0N/A int* limit() { return (int*) fillbytes::limit(); }
0N/A void add(int x) { *(int*)grow(sizeof(x)) = x; }
0N/A void popTo(int l) { assert(l <= length()); b.len = l * sizeof(int); }
0N/A int indexOf(int x);
0N/A bool contains(int x) { return indexOf(x) >= 0; }
0N/A};