michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- michael@0: * vim: set ts=8 sts=4 et sw=4 tw=99: michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "vm/Xdr.h" michael@0: michael@0: #include michael@0: michael@0: #include "jsapi.h" michael@0: #include "jsscript.h" michael@0: michael@0: #include "vm/Debugger.h" michael@0: michael@0: using namespace js; michael@0: michael@0: void michael@0: XDRBuffer::freeBuffer() michael@0: { michael@0: js_free(base); michael@0: #ifdef DEBUG michael@0: memset(this, 0xe2, sizeof *this); michael@0: #endif michael@0: } michael@0: michael@0: bool michael@0: XDRBuffer::grow(size_t n) michael@0: { michael@0: JS_ASSERT(n > size_t(limit - cursor)); michael@0: michael@0: const size_t MEM_BLOCK = 8192; michael@0: size_t offset = cursor - base; michael@0: size_t newCapacity = JS_ROUNDUP(offset + n, MEM_BLOCK); michael@0: if (isUint32Overflow(newCapacity)) { michael@0: JS_ReportErrorNumber(cx(), js_GetErrorMessage, nullptr, JSMSG_TOO_BIG_TO_ENCODE); michael@0: return false; michael@0: } michael@0: michael@0: void *data = js_realloc(base, newCapacity); michael@0: if (!data) { michael@0: js_ReportOutOfMemory(cx()); michael@0: return false; michael@0: } michael@0: base = static_cast(data); michael@0: cursor = base + offset; michael@0: limit = base + newCapacity; michael@0: return true; michael@0: } michael@0: michael@0: template michael@0: bool michael@0: XDRState::codeChars(jschar *chars, size_t nchars) michael@0: { michael@0: size_t nbytes = nchars * sizeof(jschar); michael@0: if (mode == XDR_ENCODE) { michael@0: uint8_t *ptr = buf.write(nbytes); michael@0: if (!ptr) michael@0: return false; michael@0: mozilla::NativeEndian::copyAndSwapToLittleEndian(ptr, chars, nchars); michael@0: } else { michael@0: const uint8_t *ptr = buf.read(nbytes); michael@0: mozilla::NativeEndian::copyAndSwapFromLittleEndian(chars, ptr, nchars); michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: template michael@0: static bool michael@0: VersionCheck(XDRState *xdr) michael@0: { michael@0: uint32_t bytecodeVer; michael@0: if (mode == XDR_ENCODE) michael@0: bytecodeVer = XDR_BYTECODE_VERSION; michael@0: michael@0: if (!xdr->codeUint32(&bytecodeVer)) michael@0: return false; michael@0: michael@0: if (mode == XDR_DECODE && bytecodeVer != XDR_BYTECODE_VERSION) { michael@0: /* We do not provide binary compatibility with older scripts. */ michael@0: JS_ReportErrorNumber(xdr->cx(), js_GetErrorMessage, nullptr, JSMSG_BAD_SCRIPT_MAGIC); michael@0: return false; michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: template michael@0: bool michael@0: XDRState::codeFunction(MutableHandleObject objp) michael@0: { michael@0: if (mode == XDR_DECODE) michael@0: objp.set(nullptr); michael@0: michael@0: if (!VersionCheck(this)) michael@0: return false; michael@0: michael@0: return XDRInterpretedFunction(this, NullPtr(), NullPtr(), objp); michael@0: } michael@0: michael@0: template michael@0: bool michael@0: XDRState::codeScript(MutableHandleScript scriptp) michael@0: { michael@0: if (mode == XDR_DECODE) michael@0: scriptp.set(nullptr); michael@0: michael@0: if (!VersionCheck(this)) michael@0: return false; michael@0: michael@0: if (!XDRScript(this, NullPtr(), NullPtr(), NullPtr(), scriptp)) michael@0: return false; michael@0: michael@0: return true; michael@0: } michael@0: michael@0: template michael@0: bool michael@0: XDRState::codeConstValue(MutableHandleValue vp) michael@0: { michael@0: return XDRScriptConst(this, vp); michael@0: } michael@0: michael@0: XDRDecoder::XDRDecoder(JSContext *cx, const void *data, uint32_t length, michael@0: JSPrincipals *originPrincipals) michael@0: : XDRState(cx) michael@0: { michael@0: buf.setData(data, length); michael@0: this->originPrincipals_ = originPrincipals; michael@0: } michael@0: michael@0: template class js::XDRState; michael@0: template class js::XDRState;