1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
16 * The Original Code is Mozilla Communicator client code, released
19 * The Initial Developer of the Original Code is
20 * Netscape Communications Corporation.
21 * Portions created by the Initial Developer are Copyright (C) 1998
22 * the Initial Developer. All Rights Reserved.
26 * Alternatively, the contents of this file may be used under the terms of
27 * either of the GNU General Public License Version 2 or later (the "GPL"),
28 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
40 #include "jsversion.h"
46 #include "jsutil.h" /* Added by JSIFY */
52 #include "jsobj.h" /* js_XDRObject */
53 #include "jsscript.h" /* js_XDRScript */
60 #define DBG(x) ((void)0)
63 typedef struct JSXDRMemState
{
70 #define MEM_BLOCK 8192
71 #define MEM_PRIV(xdr) ((JSXDRMemState *)(xdr))
73 #define MEM_BASE(xdr) (MEM_PRIV(xdr)->base)
74 #define MEM_COUNT(xdr) (MEM_PRIV(xdr)->count)
75 #define MEM_LIMIT(xdr) (MEM_PRIV(xdr)->limit)
77 #define MEM_LEFT(xdr, bytes) \
79 if ((xdr)->mode == JSXDR_DECODE && \
80 MEM_COUNT(xdr) + bytes > MEM_LIMIT(xdr)) { \
81 JS_ReportErrorNumber((xdr)->cx, js_GetErrorMessage, NULL, \
87 #define MEM_NEED(xdr, bytes) \
89 if ((xdr)->mode == JSXDR_ENCODE) { \
90 if (MEM_LIMIT(xdr) && \
91 MEM_COUNT(xdr) + bytes > MEM_LIMIT(xdr)) { \
92 uint32 limit_ = JS_ROUNDUP(MEM_COUNT(xdr) + bytes, MEM_BLOCK);\
93 void *data_ = JS_realloc((xdr)->cx, MEM_BASE(xdr), limit_); \
96 MEM_BASE(xdr) = (char *) data_; \
97 MEM_LIMIT(xdr) = limit_; \
100 MEM_LEFT(xdr, bytes); \
104 #define MEM_DATA(xdr) ((void *)(MEM_BASE(xdr) + MEM_COUNT(xdr)))
105 #define MEM_INCR(xdr,bytes) (MEM_COUNT(xdr) += (bytes))
108 mem_get32(JSXDRState
*xdr
, uint32
*lp
)
111 *lp
= *(uint32
*)MEM_DATA(xdr
);
117 mem_set32(JSXDRState
*xdr
, uint32
*lp
)
120 *(uint32
*)MEM_DATA(xdr
) = *lp
;
126 mem_getbytes(JSXDRState
*xdr
, char *bytes
, uint32 len
)
129 memcpy(bytes
, MEM_DATA(xdr
), len
);
135 mem_setbytes(JSXDRState
*xdr
, char *bytes
, uint32 len
)
138 memcpy(MEM_DATA(xdr
), bytes
, len
);
144 mem_raw(JSXDRState
*xdr
, uint32 len
)
147 if (xdr
->mode
== JSXDR_ENCODE
) {
149 } else if (xdr
->mode
== JSXDR_DECODE
) {
152 data
= MEM_DATA(xdr
);
158 mem_seek(JSXDRState
*xdr
, int32 offset
, JSXDRWhence whence
)
162 if ((int32
)MEM_COUNT(xdr
) + offset
< 0) {
163 JS_ReportErrorNumber(xdr
->cx
, js_GetErrorMessage
, NULL
,
164 JSMSG_SEEK_BEYOND_START
);
168 MEM_NEED(xdr
, offset
);
169 MEM_COUNT(xdr
) += offset
;
173 JS_ReportErrorNumber(xdr
->cx
, js_GetErrorMessage
, NULL
,
174 JSMSG_SEEK_BEYOND_START
);
177 if (xdr
->mode
== JSXDR_ENCODE
) {
178 if ((uint32
)offset
> MEM_COUNT(xdr
))
179 MEM_NEED(xdr
, offset
- MEM_COUNT(xdr
));
180 MEM_COUNT(xdr
) = offset
;
182 if ((uint32
)offset
> MEM_LIMIT(xdr
)) {
183 JS_ReportErrorNumber(xdr
->cx
, js_GetErrorMessage
, NULL
,
184 JSMSG_SEEK_BEYOND_END
);
187 MEM_COUNT(xdr
) = offset
;
192 xdr
->mode
== JSXDR_ENCODE
||
193 (int32
)MEM_LIMIT(xdr
) + offset
< 0) {
194 JS_ReportErrorNumber(xdr
->cx
, js_GetErrorMessage
, NULL
,
198 MEM_COUNT(xdr
) = MEM_LIMIT(xdr
) + offset
;
202 JS_snprintf(numBuf
, sizeof numBuf
, "%d", whence
);
203 JS_ReportErrorNumber(xdr
->cx
, js_GetErrorMessage
, NULL
,
204 JSMSG_WHITHER_WHENCE
, numBuf
);
211 mem_tell(JSXDRState
*xdr
)
213 return MEM_COUNT(xdr
);
217 mem_finalize(JSXDRState
*xdr
)
219 JS_free(xdr
->cx
, MEM_BASE(xdr
));
222 static JSXDROps xdrmem_ops
= {
223 mem_get32
, mem_set32
, mem_getbytes
, mem_setbytes
,
224 mem_raw
, mem_seek
, mem_tell
, mem_finalize
228 JS_XDRInitBase(JSXDRState
*xdr
, JSXDRMode mode
, JSContext
*cx
)
232 xdr
->registry
= NULL
;
233 xdr
->numclasses
= xdr
->maxclasses
= 0;
235 xdr
->userdata
= NULL
;
239 JS_PUBLIC_API(JSXDRState
*)
240 JS_XDRNewMem(JSContext
*cx
, JSXDRMode mode
)
242 JSXDRState
*xdr
= (JSXDRState
*) JS_malloc(cx
, sizeof(JSXDRMemState
));
245 JS_XDRInitBase(xdr
, mode
, cx
);
246 if (mode
== JSXDR_ENCODE
) {
247 if (!(MEM_BASE(xdr
) = (char *) JS_malloc(cx
, MEM_BLOCK
))) {
252 /* XXXbe ok, so better not deref MEM_BASE(xdr) if not ENCODE */
253 MEM_BASE(xdr
) = NULL
;
255 xdr
->ops
= &xdrmem_ops
;
257 MEM_LIMIT(xdr
) = MEM_BLOCK
;
261 JS_PUBLIC_API(void *)
262 JS_XDRMemGetData(JSXDRState
*xdr
, uint32
*lp
)
264 if (xdr
->ops
!= &xdrmem_ops
)
266 *lp
= MEM_COUNT(xdr
);
267 return MEM_BASE(xdr
);
271 JS_XDRMemSetData(JSXDRState
*xdr
, void *data
, uint32 len
)
273 if (xdr
->ops
!= &xdrmem_ops
)
275 MEM_LIMIT(xdr
) = len
;
276 MEM_BASE(xdr
) = (char *) data
;
280 JS_PUBLIC_API(uint32
)
281 JS_XDRMemDataLeft(JSXDRState
*xdr
)
283 if (xdr
->ops
!= &xdrmem_ops
)
285 return MEM_LIMIT(xdr
) - MEM_COUNT(xdr
);
289 JS_XDRMemResetData(JSXDRState
*xdr
)
291 if (xdr
->ops
!= &xdrmem_ops
)
297 JS_XDRDestroy(JSXDRState
*xdr
)
299 JSContext
*cx
= xdr
->cx
;
300 xdr
->ops
->finalize(xdr
);
302 JS_free(cx
, xdr
->registry
);
304 JS_DHashTableDestroy((JSDHashTable
*) xdr
->reghash
);
309 JS_PUBLIC_API(JSBool
)
310 JS_XDRUint8(JSXDRState
*xdr
, uint8
*b
)
313 if (!JS_XDRUint32(xdr
, &l
))
319 JS_PUBLIC_API(JSBool
)
320 JS_XDRUint16(JSXDRState
*xdr
, uint16
*s
)
323 if (!JS_XDRUint32(xdr
, &l
))
329 JS_PUBLIC_API(JSBool
)
330 JS_XDRUint32(JSXDRState
*xdr
, uint32
*lp
)
333 if (xdr
->mode
== JSXDR_ENCODE
) {
334 uint32 xl
= JSXDR_SWAB32(*lp
);
335 ok
= xdr
->ops
->set32(xdr
, &xl
);
336 } else if (xdr
->mode
== JSXDR_DECODE
) {
337 ok
= xdr
->ops
->get32(xdr
, lp
);
338 *lp
= JSXDR_SWAB32(*lp
);
343 JS_PUBLIC_API(JSBool
)
344 JS_XDRBytes(JSXDRState
*xdr
, char *bytes
, uint32 len
)
347 static char padbuf
[JSXDR_ALIGN
-1];
349 if (xdr
->mode
== JSXDR_ENCODE
) {
350 if (!xdr
->ops
->setbytes(xdr
, bytes
, len
))
353 if (!xdr
->ops
->getbytes(xdr
, bytes
, len
))
356 len
= xdr
->ops
->tell(xdr
);
357 if (len
% JSXDR_ALIGN
) {
358 padlen
= JSXDR_ALIGN
- (len
% JSXDR_ALIGN
);
359 if (xdr
->mode
== JSXDR_ENCODE
) {
360 if (!xdr
->ops
->setbytes(xdr
, padbuf
, padlen
))
363 if (!xdr
->ops
->seek(xdr
, padlen
, JSXDR_SEEK_CUR
))
371 * Convert between a C string and the XDR representation:
372 * leading 32-bit count, then counted vector of chars,
373 * then possibly \0 padding to multiple of 4.
375 JS_PUBLIC_API(JSBool
)
376 JS_XDRCString(JSXDRState
*xdr
, char **sp
)
380 if (xdr
->mode
== JSXDR_ENCODE
)
382 JS_XDRUint32(xdr
, &len
);
383 if (xdr
->mode
== JSXDR_DECODE
) {
384 if (!(*sp
= (char *) JS_malloc(xdr
->cx
, len
+ 1)))
387 if (!JS_XDRBytes(xdr
, *sp
, len
)) {
388 if (xdr
->mode
== JSXDR_DECODE
)
389 JS_free(xdr
->cx
, *sp
);
392 if (xdr
->mode
== JSXDR_DECODE
) {
394 } else if (xdr
->mode
== JSXDR_FREE
) {
395 JS_free(xdr
->cx
, *sp
);
401 JS_PUBLIC_API(JSBool
)
402 JS_XDRCStringOrNull(JSXDRState
*xdr
, char **sp
)
404 uint32 null
= (*sp
== NULL
);
405 if (!JS_XDRUint32(xdr
, &null
))
411 return JS_XDRCString(xdr
, sp
);
415 XDRChars(JSXDRState
*xdr
, jschar
*chars
, uint32 nchars
)
417 uint32 i
, padlen
, nbytes
;
420 nbytes
= nchars
* sizeof(jschar
);
421 padlen
= nbytes
% JSXDR_ALIGN
;
423 padlen
= JSXDR_ALIGN
- padlen
;
426 if (!(raw
= (jschar
*) xdr
->ops
->raw(xdr
, nbytes
)))
428 if (xdr
->mode
== JSXDR_ENCODE
) {
429 for (i
= 0; i
!= nchars
; i
++)
430 raw
[i
] = JSXDR_SWAB16(chars
[i
]);
432 memset((char *)raw
+ nbytes
- padlen
, 0, padlen
);
433 } else if (xdr
->mode
== JSXDR_DECODE
) {
434 for (i
= 0; i
!= nchars
; i
++)
435 chars
[i
] = JSXDR_SWAB16(raw
[i
]);
441 * Convert between a JS (Unicode) string and the XDR representation.
443 JS_PUBLIC_API(JSBool
)
444 JS_XDRString(JSXDRState
*xdr
, JSString
**strp
)
449 if (xdr
->mode
== JSXDR_ENCODE
)
450 nchars
= JSSTRING_LENGTH(*strp
);
451 if (!JS_XDRUint32(xdr
, &nchars
))
454 if (xdr
->mode
== JSXDR_DECODE
) {
455 chars
= (jschar
*) JS_malloc(xdr
->cx
, (nchars
+ 1) * sizeof(jschar
));
459 chars
= JSSTRING_CHARS(*strp
);
462 if (!XDRChars(xdr
, chars
, nchars
))
464 if (xdr
->mode
== JSXDR_DECODE
) {
466 *strp
= JS_NewUCString(xdr
->cx
, chars
, nchars
);
473 if (xdr
->mode
== JSXDR_DECODE
)
474 JS_free(xdr
->cx
, chars
);
478 JS_PUBLIC_API(JSBool
)
479 JS_XDRStringOrNull(JSXDRState
*xdr
, JSString
**strp
)
481 uint32 null
= (*strp
== NULL
);
482 if (!JS_XDRUint32(xdr
, &null
))
488 return JS_XDRString(xdr
, strp
);
492 XDRDoubleValue(JSXDRState
*xdr
, jsdouble
*dp
)
496 if (xdr
->mode
== JSXDR_ENCODE
)
498 if (!JS_XDRUint32(xdr
, &u
.s
.lo
) || !JS_XDRUint32(xdr
, &u
.s
.hi
))
500 if (xdr
->mode
== JSXDR_DECODE
)
505 JS_PUBLIC_API(JSBool
)
506 JS_XDRDouble(JSXDRState
*xdr
, jsdouble
**dpp
)
510 if (xdr
->mode
== JSXDR_ENCODE
)
512 if (!XDRDoubleValue(xdr
, &d
))
514 if (xdr
->mode
== JSXDR_DECODE
) {
515 *dpp
= JS_NewDouble(xdr
->cx
, d
);
522 /* These are magic pseudo-tags: see jsapi.h, near the top, for real tags. */
523 #define JSVAL_XDRNULL 0x8
524 #define JSVAL_XDRVOID 0xA
527 XDRValueBody(JSXDRState
*xdr
, uint32 type
, jsval
*vp
)
538 if (xdr
->mode
== JSXDR_ENCODE
)
539 str
= JSVAL_TO_STRING(*vp
);
540 if (!JS_XDRString(xdr
, &str
))
542 if (xdr
->mode
== JSXDR_DECODE
)
543 *vp
= STRING_TO_JSVAL(str
);
548 if (xdr
->mode
== JSXDR_ENCODE
)
549 dp
= JSVAL_TO_DOUBLE(*vp
);
550 if (!JS_XDRDouble(xdr
, &dp
))
552 if (xdr
->mode
== JSXDR_DECODE
)
553 *vp
= DOUBLE_TO_JSVAL(dp
);
558 if (xdr
->mode
== JSXDR_ENCODE
)
559 obj
= JSVAL_TO_OBJECT(*vp
);
560 if (!js_XDRObject(xdr
, &obj
))
562 if (xdr
->mode
== JSXDR_DECODE
)
563 *vp
= OBJECT_TO_JSVAL(obj
);
566 case JSVAL_BOOLEAN
: {
568 if (xdr
->mode
== JSXDR_ENCODE
)
569 b
= (uint32
) JSVAL_TO_BOOLEAN(*vp
);
570 if (!JS_XDRUint32(xdr
, &b
))
572 if (xdr
->mode
== JSXDR_DECODE
)
573 *vp
= BOOLEAN_TO_JSVAL(!!b
);
579 JS_ASSERT(type
& JSVAL_INT
);
580 if (xdr
->mode
== JSXDR_ENCODE
)
581 i
= (uint32
) JSVAL_TO_INT(*vp
);
582 if (!JS_XDRUint32(xdr
, &i
))
584 if (xdr
->mode
== JSXDR_DECODE
)
585 *vp
= INT_TO_JSVAL((int32
) i
);
592 JS_PUBLIC_API(JSBool
)
593 JS_XDRValue(JSXDRState
*xdr
, jsval
*vp
)
597 if (xdr
->mode
== JSXDR_ENCODE
) {
598 if (JSVAL_IS_NULL(*vp
))
599 type
= JSVAL_XDRNULL
;
600 else if (JSVAL_IS_VOID(*vp
))
601 type
= JSVAL_XDRVOID
;
603 type
= JSVAL_TAG(*vp
);
605 return JS_XDRUint32(xdr
, &type
) && XDRValueBody(xdr
, type
, vp
);
609 js_XDRAtom(JSXDRState
*xdr
, JSAtom
**atomp
)
615 if (xdr
->mode
== JSXDR_ENCODE
) {
616 v
= ATOM_KEY(*atomp
);
617 return JS_XDRValue(xdr
, &v
);
621 * Inline JS_XDRValue when decoding to avoid ceation of GC things when
622 * then corresponding atom already exists. See bug 321985.
624 if (!JS_XDRUint32(xdr
, &type
))
626 if (type
== JSVAL_STRING
)
627 return js_XDRStringAtom(xdr
, atomp
);
629 if (type
== JSVAL_DOUBLE
) {
630 if (!XDRDoubleValue(xdr
, &d
))
632 *atomp
= js_AtomizeDouble(xdr
->cx
, d
);
633 return *atomp
!= NULL
;
636 return XDRValueBody(xdr
, type
, &v
) &&
637 js_AtomizePrimitiveValue(xdr
->cx
, v
, atomp
);
641 js_XDRStringAtom(JSXDRState
*xdr
, JSAtom
**atomp
)
648 jschar stackChars
[256];
650 if (xdr
->mode
== JSXDR_ENCODE
) {
651 JS_ASSERT(ATOM_IS_STRING(*atomp
));
652 str
= ATOM_TO_STRING(*atomp
);
653 return JS_XDRString(xdr
, &str
);
657 * Inline JS_XDRString when decoding to avoid JSString allocation
658 * for already existing atoms. See bug 321985.
660 if (!JS_XDRUint32(xdr
, &nchars
))
664 if (nchars
<= JS_ARRAY_LENGTH(stackChars
)) {
668 * This is very uncommon. Don't use the tempPool arena for this as
669 * most allocations here will be bigger than tempPool's arenasize.
671 chars
= (jschar
*) JS_malloc(cx
, nchars
* sizeof(jschar
));
676 if (XDRChars(xdr
, chars
, nchars
))
677 atom
= js_AtomizeChars(cx
, chars
, nchars
, 0);
678 if (chars
!= stackChars
)
687 JS_PUBLIC_API(JSBool
)
688 JS_XDRScript(JSXDRState
*xdr
, JSScript
**scriptp
)
690 if (!js_XDRScript(xdr
, scriptp
, NULL
))
692 if (xdr
->mode
== JSXDR_DECODE
)
693 js_CallNewScriptHook(xdr
->cx
, *scriptp
, NULL
);
697 #define CLASS_REGISTRY_MIN 8
698 #define CLASS_INDEX_TO_ID(i) ((i)+1)
699 #define CLASS_ID_TO_INDEX(id) ((id)-1)
701 typedef struct JSRegHashEntry
{
707 JS_PUBLIC_API(JSBool
)
708 JS_XDRRegisterClass(JSXDRState
*xdr
, JSClass
*clasp
, uint32
*idp
)
710 uintN numclasses
, maxclasses
;
713 numclasses
= xdr
->numclasses
;
714 maxclasses
= xdr
->maxclasses
;
715 if (numclasses
== maxclasses
) {
716 maxclasses
= (maxclasses
== 0) ? CLASS_REGISTRY_MIN
: maxclasses
<< 1;
717 registry
= (JSClass
**)
718 JS_realloc(xdr
->cx
, xdr
->registry
, maxclasses
* sizeof(JSClass
*));
721 xdr
->registry
= registry
;
722 xdr
->maxclasses
= maxclasses
;
724 JS_ASSERT(numclasses
&& numclasses
< maxclasses
);
725 registry
= xdr
->registry
;
728 registry
[numclasses
] = clasp
;
730 JSRegHashEntry
*entry
= (JSRegHashEntry
*)
731 JS_DHashTableOperate((JSDHashTable
*) xdr
->reghash
,
732 clasp
->name
, JS_DHASH_ADD
);
734 JS_ReportOutOfMemory(xdr
->cx
);
737 entry
->name
= clasp
->name
;
738 entry
->index
= numclasses
;
740 *idp
= CLASS_INDEX_TO_ID(numclasses
);
741 xdr
->numclasses
= ++numclasses
;
745 JS_PUBLIC_API(uint32
)
746 JS_XDRFindClassIdByName(JSXDRState
*xdr
, const char *name
)
750 numclasses
= xdr
->numclasses
;
751 if (numclasses
>= 10) {
752 JSRegHashEntry
*entry
;
754 /* Bootstrap reghash from registry on first overpopulated Find. */
757 JS_NewDHashTable(JS_DHashGetStubOps(), NULL
,
758 sizeof(JSRegHashEntry
),
759 JS_DHASH_DEFAULT_CAPACITY(numclasses
));
761 for (i
= 0; i
< numclasses
; i
++) {
762 JSClass
*clasp
= xdr
->registry
[i
];
763 entry
= (JSRegHashEntry
*)
764 JS_DHashTableOperate((JSDHashTable
*) xdr
->reghash
,
765 clasp
->name
, JS_DHASH_ADD
);
766 entry
->name
= clasp
->name
;
772 /* If we managed to create reghash, use it for O(1) Find. */
774 entry
= (JSRegHashEntry
*)
775 JS_DHashTableOperate((JSDHashTable
*) xdr
->reghash
,
776 name
, JS_DHASH_LOOKUP
);
777 if (JS_DHASH_ENTRY_IS_BUSY(&entry
->hdr
))
778 return CLASS_INDEX_TO_ID(entry
->index
);
782 /* Only a few classes, or we couldn't malloc reghash: use linear search. */
783 for (i
= 0; i
< numclasses
; i
++) {
784 if (!strcmp(name
, xdr
->registry
[i
]->name
))
785 return CLASS_INDEX_TO_ID(i
);
790 JS_PUBLIC_API(JSClass
*)
791 JS_XDRFindClassById(JSXDRState
*xdr
, uint32 id
)
793 uintN i
= CLASS_ID_TO_INDEX(id
);
795 if (i
>= xdr
->numclasses
)
797 return xdr
->registry
[i
];
800 #endif /* JS_HAS_XDR */