Back to index

lightning-sunbird  0.9+nobinonly
jsxdrapi.c
Go to the documentation of this file.
00001 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
00002  *
00003  * ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is Mozilla Communicator client code, released
00017  * March 31, 1998.
00018  *
00019  * The Initial Developer of the Original Code is
00020  * Netscape Communications Corporation.
00021  * Portions created by the Initial Developer are Copyright (C) 1998
00022  * the Initial Developer. All Rights Reserved.
00023  *
00024  * Contributor(s):
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either of the GNU General Public License Version 2 or later (the "GPL"),
00028  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 #include "jsstddef.h"
00040 #include "jsconfig.h"
00041 
00042 #if JS_HAS_XDR
00043 
00044 #include <string.h>
00045 #include "jstypes.h"
00046 #include "jsutil.h" /* Added by JSIFY */
00047 #include "jsdhash.h"
00048 #include "jsprf.h"
00049 #include "jsapi.h"
00050 #include "jscntxt.h"
00051 #include "jsnum.h"
00052 #include "jsobj.h"              /* js_XDRObject */
00053 #include "jsscript.h"           /* js_XDRScript */
00054 #include "jsstr.h"
00055 #include "jsxdrapi.h"
00056 
00057 #ifdef DEBUG
00058 #define DBG(x) x
00059 #else
00060 #define DBG(x) ((void)0)
00061 #endif
00062 
00063 typedef struct JSXDRMemState {
00064     JSXDRState  state;
00065     char        *base;
00066     uint32      count;
00067     uint32      limit;
00068 } JSXDRMemState;
00069 
00070 #define MEM_BLOCK       8192
00071 #define MEM_PRIV(xdr)   ((JSXDRMemState *)(xdr))
00072 
00073 #define MEM_BASE(xdr)   (MEM_PRIV(xdr)->base)
00074 #define MEM_COUNT(xdr)  (MEM_PRIV(xdr)->count)
00075 #define MEM_LIMIT(xdr)  (MEM_PRIV(xdr)->limit)
00076 
00077 #define MEM_LEFT(xdr, bytes)                                                  \
00078     JS_BEGIN_MACRO                                                            \
00079         if ((xdr)->mode == JSXDR_DECODE &&                                    \
00080             MEM_COUNT(xdr) + bytes > MEM_LIMIT(xdr)) {                        \
00081             JS_ReportErrorNumber((xdr)->cx, js_GetErrorMessage, NULL,         \
00082                                  JSMSG_END_OF_DATA);                          \
00083             return 0;                                                         \
00084         }                                                                     \
00085     JS_END_MACRO
00086 
00087 #define MEM_NEED(xdr, bytes)                                                  \
00088     JS_BEGIN_MACRO                                                            \
00089         if ((xdr)->mode == JSXDR_ENCODE) {                                    \
00090             if (MEM_LIMIT(xdr) &&                                             \
00091                 MEM_COUNT(xdr) + bytes > MEM_LIMIT(xdr)) {                    \
00092                 uint32 limit_ = JS_ROUNDUP(MEM_COUNT(xdr) + bytes, MEM_BLOCK);\
00093                 void *data_ = JS_realloc((xdr)->cx, MEM_BASE(xdr), limit_);   \
00094                 if (!data_)                                                   \
00095                     return 0;                                                 \
00096                 MEM_BASE(xdr) = data_;                                        \
00097                 MEM_LIMIT(xdr) = limit_;                                      \
00098             }                                                                 \
00099         } else {                                                              \
00100             MEM_LEFT(xdr, bytes);                                             \
00101         }                                                                     \
00102     JS_END_MACRO
00103 
00104 #define MEM_DATA(xdr)        ((void *)(MEM_BASE(xdr) + MEM_COUNT(xdr)))
00105 #define MEM_INCR(xdr,bytes)  (MEM_COUNT(xdr) += (bytes))
00106 
00107 static JSBool
00108 mem_get32(JSXDRState *xdr, uint32 *lp)
00109 {
00110     MEM_LEFT(xdr, 4);
00111     *lp = *(uint32 *)MEM_DATA(xdr);
00112     MEM_INCR(xdr, 4);
00113     return JS_TRUE;
00114 }
00115 
00116 static JSBool
00117 mem_set32(JSXDRState *xdr, uint32 *lp)
00118 {
00119     MEM_NEED(xdr, 4);
00120     *(uint32 *)MEM_DATA(xdr) = *lp;
00121     MEM_INCR(xdr, 4);
00122     return JS_TRUE;
00123 }
00124 
00125 static JSBool
00126 mem_getbytes(JSXDRState *xdr, char *bytes, uint32 len)
00127 {
00128     MEM_LEFT(xdr, len);
00129     memcpy(bytes, MEM_DATA(xdr), len);
00130     MEM_INCR(xdr, len);
00131     return JS_TRUE;
00132 }
00133 
00134 static JSBool
00135 mem_setbytes(JSXDRState *xdr, char *bytes, uint32 len)
00136 {
00137     MEM_NEED(xdr, len);
00138     memcpy(MEM_DATA(xdr), bytes, len);
00139     MEM_INCR(xdr, len);
00140     return JS_TRUE;
00141 }
00142 
00143 static void *
00144 mem_raw(JSXDRState *xdr, uint32 len)
00145 {
00146     void *data;
00147     if (xdr->mode == JSXDR_ENCODE) {
00148         MEM_NEED(xdr, len);
00149     } else if (xdr->mode == JSXDR_DECODE) {
00150         MEM_LEFT(xdr, len);
00151     }
00152     data = MEM_DATA(xdr);
00153     MEM_INCR(xdr, len);
00154     return data;
00155 }
00156 
00157 static JSBool
00158 mem_seek(JSXDRState *xdr, int32 offset, JSXDRWhence whence)
00159 {
00160     switch (whence) {
00161       case JSXDR_SEEK_CUR:
00162         if ((int32)MEM_COUNT(xdr) + offset < 0) {
00163             JS_ReportErrorNumber(xdr->cx, js_GetErrorMessage, NULL,
00164                                  JSMSG_SEEK_BEYOND_START);
00165             return JS_FALSE;
00166         }
00167         if (offset > 0)
00168             MEM_NEED(xdr, offset);
00169         MEM_COUNT(xdr) += offset;
00170         return JS_TRUE;
00171       case JSXDR_SEEK_SET:
00172         if (offset < 0) {
00173             JS_ReportErrorNumber(xdr->cx, js_GetErrorMessage, NULL,
00174                                  JSMSG_SEEK_BEYOND_START);
00175             return JS_FALSE;
00176         }
00177         if (xdr->mode == JSXDR_ENCODE) {
00178             if ((uint32)offset > MEM_COUNT(xdr))
00179                 MEM_NEED(xdr, offset - MEM_COUNT(xdr));
00180             MEM_COUNT(xdr) = offset;
00181         } else {
00182             if ((uint32)offset > MEM_LIMIT(xdr)) {
00183                 JS_ReportErrorNumber(xdr->cx, js_GetErrorMessage, NULL,
00184                                      JSMSG_SEEK_BEYOND_END);
00185                 return JS_FALSE;
00186             }
00187             MEM_COUNT(xdr) = offset;
00188         }
00189         return JS_TRUE;
00190       case JSXDR_SEEK_END:
00191         if (offset >= 0 ||
00192             xdr->mode == JSXDR_ENCODE ||
00193             (int32)MEM_LIMIT(xdr) + offset < 0) {
00194             JS_ReportErrorNumber(xdr->cx, js_GetErrorMessage, NULL,
00195                                  JSMSG_END_SEEK);
00196             return JS_FALSE;
00197         }
00198         MEM_COUNT(xdr) = MEM_LIMIT(xdr) + offset;
00199         return JS_TRUE;
00200       default: {
00201         char numBuf[12];
00202         JS_snprintf(numBuf, sizeof numBuf, "%d", whence);
00203         JS_ReportErrorNumber(xdr->cx, js_GetErrorMessage, NULL,
00204                              JSMSG_WHITHER_WHENCE, numBuf);
00205         return JS_FALSE;
00206       }
00207     }
00208 }
00209 
00210 static uint32
00211 mem_tell(JSXDRState *xdr)
00212 {
00213     return MEM_COUNT(xdr);
00214 }
00215 
00216 static void
00217 mem_finalize(JSXDRState *xdr)
00218 {
00219     JS_free(xdr->cx, MEM_BASE(xdr));
00220 }
00221 
00222 static JSXDROps xdrmem_ops = {
00223     mem_get32,      mem_set32,      mem_getbytes,   mem_setbytes,
00224     mem_raw,        mem_seek,       mem_tell,       mem_finalize
00225 };
00226 
00227 JS_PUBLIC_API(void)
00228 JS_XDRInitBase(JSXDRState *xdr, JSXDRMode mode, JSContext *cx)
00229 {
00230     xdr->mode = mode;
00231     xdr->cx = cx;
00232     xdr->registry = NULL;
00233     xdr->numclasses = xdr->maxclasses = 0;
00234     xdr->reghash = NULL;
00235     xdr->userdata = NULL;
00236     xdr->script = NULL;
00237 }
00238 
00239 JS_PUBLIC_API(JSXDRState *)
00240 JS_XDRNewMem(JSContext *cx, JSXDRMode mode)
00241 {
00242     JSXDRState *xdr = (JSXDRState *) JS_malloc(cx, sizeof(JSXDRMemState));
00243     if (!xdr)
00244         return NULL;
00245     JS_XDRInitBase(xdr, mode, cx);
00246     if (mode == JSXDR_ENCODE) {
00247         if (!(MEM_BASE(xdr) = JS_malloc(cx, MEM_BLOCK))) {
00248             JS_free(cx, xdr);
00249             return NULL;
00250         }
00251     } else {
00252         /* XXXbe ok, so better not deref MEM_BASE(xdr) if not ENCODE */
00253         MEM_BASE(xdr) = NULL;
00254     }
00255     xdr->ops = &xdrmem_ops;
00256     MEM_COUNT(xdr) = 0;
00257     MEM_LIMIT(xdr) = MEM_BLOCK;
00258     return xdr;
00259 }
00260 
00261 JS_PUBLIC_API(void *)
00262 JS_XDRMemGetData(JSXDRState *xdr, uint32 *lp)
00263 {
00264     if (xdr->ops != &xdrmem_ops)
00265         return NULL;
00266     *lp = MEM_COUNT(xdr);
00267     return MEM_BASE(xdr);
00268 }
00269 
00270 JS_PUBLIC_API(void)
00271 JS_XDRMemSetData(JSXDRState *xdr, void *data, uint32 len)
00272 {
00273     if (xdr->ops != &xdrmem_ops)
00274         return;
00275     MEM_LIMIT(xdr) = len;
00276     MEM_BASE(xdr) = data;
00277     MEM_COUNT(xdr) = 0;
00278 }
00279 
00280 JS_PUBLIC_API(uint32)
00281 JS_XDRMemDataLeft(JSXDRState *xdr)
00282 {
00283     if (xdr->ops != &xdrmem_ops)
00284         return 0;
00285     return MEM_LIMIT(xdr) - MEM_COUNT(xdr);
00286 }
00287 
00288 JS_PUBLIC_API(void)
00289 JS_XDRMemResetData(JSXDRState *xdr)
00290 {
00291     if (xdr->ops != &xdrmem_ops)
00292         return;
00293     MEM_COUNT(xdr) = 0;
00294 }
00295 
00296 JS_PUBLIC_API(void)
00297 JS_XDRDestroy(JSXDRState *xdr)
00298 {
00299     JSContext *cx = xdr->cx;
00300     xdr->ops->finalize(xdr);
00301     if (xdr->registry) {
00302         JS_free(cx, xdr->registry);
00303         if (xdr->reghash)
00304             JS_DHashTableDestroy(xdr->reghash);
00305     }
00306     JS_free(cx, xdr);
00307 }
00308 
00309 JS_PUBLIC_API(JSBool)
00310 JS_XDRUint8(JSXDRState *xdr, uint8 *b)
00311 {
00312     uint32 l = *b;
00313     if (!JS_XDRUint32(xdr, &l))
00314         return JS_FALSE;
00315     *b = (uint8) l;
00316     return JS_TRUE;
00317 }
00318 
00319 JS_PUBLIC_API(JSBool)
00320 JS_XDRUint16(JSXDRState *xdr, uint16 *s)
00321 {
00322     uint32 l = *s;
00323     if (!JS_XDRUint32(xdr, &l))
00324         return JS_FALSE;
00325     *s = (uint16) l;
00326     return JS_TRUE;
00327 }
00328 
00329 JS_PUBLIC_API(JSBool)
00330 JS_XDRUint32(JSXDRState *xdr, uint32 *lp)
00331 {
00332     JSBool ok = JS_TRUE;
00333     if (xdr->mode == JSXDR_ENCODE) {
00334         uint32 xl = JSXDR_SWAB32(*lp);
00335         ok = xdr->ops->set32(xdr, &xl);
00336     } else if (xdr->mode == JSXDR_DECODE) {
00337         ok = xdr->ops->get32(xdr, lp);
00338         *lp = JSXDR_SWAB32(*lp);
00339     }
00340     return ok;
00341 }
00342 
00343 JS_PUBLIC_API(JSBool)
00344 JS_XDRBytes(JSXDRState *xdr, char *bytes, uint32 len)
00345 {
00346     uint32 padlen;
00347     static char padbuf[JSXDR_ALIGN-1];
00348 
00349     if (xdr->mode == JSXDR_ENCODE) {
00350         if (!xdr->ops->setbytes(xdr, bytes, len))
00351             return JS_FALSE;
00352     } else {
00353         if (!xdr->ops->getbytes(xdr, bytes, len))
00354             return JS_FALSE;
00355     }
00356     len = xdr->ops->tell(xdr);
00357     if (len % JSXDR_ALIGN) {
00358         padlen = JSXDR_ALIGN - (len % JSXDR_ALIGN);
00359         if (xdr->mode == JSXDR_ENCODE) {
00360             if (!xdr->ops->setbytes(xdr, padbuf, padlen))
00361                 return JS_FALSE;
00362         } else {
00363             if (!xdr->ops->seek(xdr, padlen, JSXDR_SEEK_CUR))
00364                 return JS_FALSE;
00365         }
00366     }
00367     return JS_TRUE;
00368 }
00369 
00375 JS_PUBLIC_API(JSBool)
00376 JS_XDRCString(JSXDRState *xdr, char **sp)
00377 {
00378     uint32 len;
00379 
00380     if (xdr->mode == JSXDR_ENCODE)
00381         len = strlen(*sp);
00382     JS_XDRUint32(xdr, &len);
00383     if (xdr->mode == JSXDR_DECODE) {
00384         if (!(*sp = (char *) JS_malloc(xdr->cx, len + 1)))
00385             return JS_FALSE;
00386     }
00387     if (!JS_XDRBytes(xdr, *sp, len)) {
00388         if (xdr->mode == JSXDR_DECODE)
00389             JS_free(xdr->cx, *sp);
00390         return JS_FALSE;
00391     }
00392     if (xdr->mode == JSXDR_DECODE) {
00393         (*sp)[len] = '\0';
00394     } else if (xdr->mode == JSXDR_FREE) {
00395         JS_free(xdr->cx, *sp);
00396         *sp = NULL;
00397     }
00398     return JS_TRUE;
00399 }
00400 
00401 JS_PUBLIC_API(JSBool)
00402 JS_XDRCStringOrNull(JSXDRState *xdr, char **sp)
00403 {
00404     uint32 null = (*sp == NULL);
00405     if (!JS_XDRUint32(xdr, &null))
00406         return JS_FALSE;
00407     if (null) {
00408         *sp = NULL;
00409         return JS_TRUE;
00410     }
00411     return JS_XDRCString(xdr, sp);
00412 }
00413 
00414 static JSBool
00415 XDRChars(JSXDRState *xdr, jschar *chars, uint32 nchars)
00416 {
00417     uint32 i, padlen, nbytes;
00418     jschar *raw;
00419 
00420     nbytes = nchars * sizeof(jschar);
00421     padlen = nbytes % JSXDR_ALIGN;
00422     if (padlen) {
00423         padlen = JSXDR_ALIGN - padlen;
00424         nbytes += padlen;
00425     }
00426     if (!(raw = (jschar *) xdr->ops->raw(xdr, nbytes)))
00427         return JS_FALSE;
00428     if (xdr->mode == JSXDR_ENCODE) {
00429         for (i = 0; i != nchars; i++)
00430             raw[i] = JSXDR_SWAB16(chars[i]);
00431         if (padlen)
00432             memset((char *)raw + nbytes - padlen, 0, padlen);
00433     } else if (xdr->mode == JSXDR_DECODE) {
00434         for (i = 0; i != nchars; i++)
00435             chars[i] = JSXDR_SWAB16(raw[i]);
00436     }
00437     return JS_TRUE;
00438 }
00439 
00440 /*
00441  * Convert between a JS (Unicode) string and the XDR representation.
00442  */
00443 JS_PUBLIC_API(JSBool)
00444 JS_XDRString(JSXDRState *xdr, JSString **strp)
00445 {
00446     uint32 nchars;
00447     jschar *chars;
00448 
00449     if (xdr->mode == JSXDR_ENCODE)
00450         nchars = JSSTRING_LENGTH(*strp);
00451     if (!JS_XDRUint32(xdr, &nchars))
00452         return JS_FALSE;
00453 
00454     if (xdr->mode == JSXDR_DECODE) {
00455         chars = (jschar *) JS_malloc(xdr->cx, (nchars + 1) * sizeof(jschar));
00456         if (!chars)
00457             return JS_FALSE;
00458     } else {
00459         chars = JSSTRING_CHARS(*strp);
00460     }
00461 
00462     if (!XDRChars(xdr, chars, nchars))
00463         goto bad;
00464     if (xdr->mode == JSXDR_DECODE) {
00465         chars[nchars] = 0;
00466         *strp = JS_NewUCString(xdr->cx, chars, nchars);
00467         if (!*strp)
00468             goto bad;
00469     }
00470     return JS_TRUE;
00471 
00472 bad:
00473     if (xdr->mode == JSXDR_DECODE)
00474         JS_free(xdr->cx, chars);
00475     return JS_FALSE;
00476 }
00477 
00478 JS_PUBLIC_API(JSBool)
00479 JS_XDRStringOrNull(JSXDRState *xdr, JSString **strp)
00480 {
00481     uint32 null = (*strp == NULL);
00482     if (!JS_XDRUint32(xdr, &null))
00483         return JS_FALSE;
00484     if (null) {
00485         *strp = NULL;
00486         return JS_TRUE;
00487     }
00488     return JS_XDRString(xdr, strp);
00489 }
00490 
00491 static JSBool
00492 XDRDoubleValue(JSXDRState *xdr, jsdouble *dp)
00493 {
00494     jsdpun u;
00495 
00496     if (xdr->mode == JSXDR_ENCODE)
00497         u.d = *dp;
00498     if (!JS_XDRUint32(xdr, &u.s.lo) || !JS_XDRUint32(xdr, &u.s.hi))
00499         return JS_FALSE;
00500     if (xdr->mode == JSXDR_DECODE)
00501         *dp = u.d;
00502     return JS_TRUE;
00503 }
00504 
00505 JS_PUBLIC_API(JSBool)
00506 JS_XDRDouble(JSXDRState *xdr, jsdouble **dpp)
00507 {
00508     jsdouble d;
00509 
00510     if (xdr->mode == JSXDR_ENCODE)
00511         d = **dpp;
00512     if (!XDRDoubleValue(xdr, &d))
00513         return JS_FALSE;
00514     if (xdr->mode == JSXDR_DECODE) {
00515         *dpp = JS_NewDouble(xdr->cx, d);
00516         if (!*dpp)
00517             return JS_FALSE;
00518     }
00519     return JS_TRUE;
00520 }
00521 
00522 /* These are magic pseudo-tags: see jsapi.h, near the top, for real tags. */
00523 #define JSVAL_XDRNULL   0x8
00524 #define JSVAL_XDRVOID   0xA
00525 
00526 static JSBool
00527 XDRValueBody(JSXDRState *xdr, uint32 type, jsval *vp)
00528 {
00529     switch (type) {
00530       case JSVAL_XDRNULL:
00531         *vp = JSVAL_NULL;
00532         break;
00533       case JSVAL_XDRVOID:
00534         *vp = JSVAL_VOID;
00535         break;
00536       case JSVAL_STRING: {
00537         JSString *str;
00538         if (xdr->mode == JSXDR_ENCODE)
00539             str = JSVAL_TO_STRING(*vp);
00540         if (!JS_XDRString(xdr, &str))
00541             return JS_FALSE;
00542         if (xdr->mode == JSXDR_DECODE)
00543             *vp = STRING_TO_JSVAL(str);
00544         break;
00545       }
00546       case JSVAL_DOUBLE: {
00547         jsdouble *dp;
00548         if (xdr->mode == JSXDR_ENCODE)
00549             dp = JSVAL_TO_DOUBLE(*vp);
00550         if (!JS_XDRDouble(xdr, &dp))
00551             return JS_FALSE;
00552         if (xdr->mode == JSXDR_DECODE)
00553             *vp = DOUBLE_TO_JSVAL(dp);
00554         break;
00555       }
00556       case JSVAL_OBJECT: {
00557         JSObject *obj;
00558         if (xdr->mode == JSXDR_ENCODE)
00559             obj = JSVAL_TO_OBJECT(*vp);
00560         if (!js_XDRObject(xdr, &obj))
00561             return JS_FALSE;
00562         if (xdr->mode == JSXDR_DECODE)
00563             *vp = OBJECT_TO_JSVAL(obj);
00564         break;
00565       }
00566       case JSVAL_BOOLEAN: {
00567         uint32 b;
00568         if (xdr->mode == JSXDR_ENCODE)
00569             b = (uint32) JSVAL_TO_BOOLEAN(*vp);
00570         if (!JS_XDRUint32(xdr, &b))
00571             return JS_FALSE;
00572         if (xdr->mode == JSXDR_DECODE)
00573             *vp = BOOLEAN_TO_JSVAL((JSBool) b);
00574         break;
00575       }
00576       default: {
00577         uint32 i;
00578 
00579         JS_ASSERT(type & JSVAL_INT);
00580         if (xdr->mode == JSXDR_ENCODE)
00581             i = (uint32) JSVAL_TO_INT(*vp);
00582         if (!JS_XDRUint32(xdr, &i))
00583             return JS_FALSE;
00584         if (xdr->mode == JSXDR_DECODE)
00585             *vp = INT_TO_JSVAL((int32) i);
00586         break;
00587       }
00588     }
00589     return JS_TRUE;
00590 }
00591 
00592 JS_PUBLIC_API(JSBool)
00593 JS_XDRValue(JSXDRState *xdr, jsval *vp)
00594 {
00595     uint32 type;
00596 
00597     if (xdr->mode == JSXDR_ENCODE) {
00598         if (JSVAL_IS_NULL(*vp))
00599             type = JSVAL_XDRNULL;
00600         else if (JSVAL_IS_VOID(*vp))
00601             type = JSVAL_XDRVOID;
00602         else
00603             type = JSVAL_TAG(*vp);
00604     }
00605     return JS_XDRUint32(xdr, &type) && XDRValueBody(xdr, type, vp);
00606 }
00607 
00608 JSBool
00609 js_XDRAtom(JSXDRState *xdr, JSAtom **atomp)
00610 {
00611     jsval v;
00612     uint32 type;
00613     jsdouble d;
00614     JSAtom *atom;
00615 
00616     if (xdr->mode == JSXDR_ENCODE) {
00617         v = ATOM_KEY(*atomp);
00618         return JS_XDRValue(xdr, &v);
00619     }
00620 
00621     /*
00622      * Inline JS_XDRValue when decoding to avoid ceation of GC things when
00623      * then corresponding atom already exists. See bug 321985.
00624      */
00625     if (!JS_XDRUint32(xdr, &type))
00626         return JS_FALSE;
00627     if (type == JSVAL_STRING)
00628         return js_XDRStringAtom(xdr, atomp);
00629 
00630     if (type == JSVAL_DOUBLE) {
00631         if (!XDRDoubleValue(xdr, &d))
00632             return JS_FALSE;
00633         atom = js_AtomizeDouble(xdr->cx, d, 0);
00634     } else {
00635         if (!XDRValueBody(xdr, type, &v))
00636             return JS_FALSE;
00637         atom = js_AtomizeValue(xdr->cx, v, 0);
00638     }
00639 
00640     if (!atom)
00641         return JS_FALSE;
00642     *atomp = atom;
00643     return JS_TRUE;
00644 }
00645 
00646 extern JSBool
00647 js_XDRStringAtom(JSXDRState *xdr, JSAtom **atomp)
00648 {
00649     JSString *str;
00650     uint32 nchars;
00651     JSAtom *atom;
00652     JSContext *cx;
00653     void *mark;
00654     jschar *chars;
00655 
00656     if (xdr->mode == JSXDR_ENCODE) {
00657         JS_ASSERT(ATOM_IS_STRING(*atomp));
00658         str = ATOM_TO_STRING(*atomp);
00659         return JS_XDRString(xdr, &str);
00660     }
00661 
00662     /*
00663      * Inline JS_XDRString when decoding to avoid JSString allocation
00664      * for already existing atoms. See bug 321985.
00665      */
00666     if (!JS_XDRUint32(xdr, &nchars))
00667         return JS_FALSE;
00668     atom = NULL;
00669     cx = xdr->cx;
00670     mark = JS_ARENA_MARK(&cx->tempPool);
00671     JS_ARENA_ALLOCATE_CAST(chars, jschar *, &cx->tempPool,
00672                            nchars * sizeof(jschar));
00673     if (!chars)
00674         JS_ReportOutOfMemory(cx);
00675     else if (XDRChars(xdr, chars, nchars))
00676         atom = js_AtomizeChars(cx, chars, nchars, 0);
00677     JS_ARENA_RELEASE(&cx->tempPool, mark);
00678     if (!atom)
00679         return JS_FALSE;
00680     *atomp = atom;
00681     return JS_TRUE;
00682 }
00683 
00684 /*
00685  * FIXME: This performs lossy conversion and we need to switch to
00686  * js_XDRStringAtom while allowing to read older XDR files. See bug 325202.
00687  */
00688 JSBool
00689 js_XDRCStringAtom(JSXDRState *xdr, JSAtom **atomp)
00690 {
00691     char *bytes;
00692     uint32 nbytes;
00693     JSAtom *atom;
00694     JSContext *cx;
00695     void *mark;
00696 
00697     if (xdr->mode == JSXDR_ENCODE) {
00698         JS_ASSERT(ATOM_IS_STRING(*atomp));
00699         bytes = JS_GetStringBytes(ATOM_TO_STRING(*atomp));
00700         return JS_XDRCString(xdr, &bytes);
00701     }
00702 
00703     /*
00704      * Inline JS_XDRCString when decoding not to malloc temporary buffer
00705      * just to free it after atomization. See bug 321985.
00706      */
00707     if (!JS_XDRUint32(xdr, &nbytes))
00708         return JS_FALSE;
00709     atom = NULL;
00710     cx = xdr->cx;
00711     mark = JS_ARENA_MARK(&cx->tempPool);
00712     JS_ARENA_ALLOCATE_CAST(bytes, char *, &cx->tempPool,
00713                            nbytes * sizeof *bytes);
00714     if (!bytes)
00715         JS_ReportOutOfMemory(cx);
00716     else if (JS_XDRBytes(xdr, bytes, nbytes))
00717         atom = js_Atomize(cx, bytes, nbytes, 0);
00718     JS_ARENA_RELEASE(&cx->tempPool, mark);
00719     if (!atom)
00720         return JS_FALSE;
00721     *atomp = atom;
00722     return JS_TRUE;
00723 }
00724 
00725 JS_PUBLIC_API(JSBool)
00726 JS_XDRScript(JSXDRState *xdr, JSScript **scriptp)
00727 {
00728     if (!js_XDRScript(xdr, scriptp, NULL))
00729         return JS_FALSE;
00730     if (xdr->mode == JSXDR_DECODE)
00731         js_CallNewScriptHook(xdr->cx, *scriptp, NULL);
00732     return JS_TRUE;
00733 }
00734 
00735 #define CLASS_REGISTRY_MIN      8
00736 #define CLASS_INDEX_TO_ID(i)    ((i)+1)
00737 #define CLASS_ID_TO_INDEX(id)   ((id)-1)
00738 
00739 typedef struct JSRegHashEntry {
00740     JSDHashEntryHdr hdr;
00741     const char      *name;
00742     uint32          index;
00743 } JSRegHashEntry;
00744 
00745 JS_PUBLIC_API(JSBool)
00746 JS_XDRRegisterClass(JSXDRState *xdr, JSClass *clasp, uint32 *idp)
00747 {
00748     uintN numclasses, maxclasses;
00749     JSClass **registry;
00750 
00751     numclasses = xdr->numclasses;
00752     maxclasses = xdr->maxclasses;
00753     if (numclasses == maxclasses) {
00754         maxclasses = (maxclasses == 0) ? CLASS_REGISTRY_MIN : maxclasses << 1;
00755         registry = (JSClass **)
00756             JS_realloc(xdr->cx, xdr->registry, maxclasses * sizeof(JSClass *));
00757         if (!registry)
00758             return JS_FALSE;
00759         xdr->registry = registry;
00760         xdr->maxclasses = maxclasses;
00761     } else {
00762         JS_ASSERT(numclasses && numclasses < maxclasses);
00763         registry = xdr->registry;
00764     }
00765 
00766     registry[numclasses] = clasp;
00767     if (xdr->reghash) {
00768         JSRegHashEntry *entry = (JSRegHashEntry *)
00769             JS_DHashTableOperate(xdr->reghash, clasp->name, JS_DHASH_ADD);
00770         if (!entry) {
00771             JS_ReportOutOfMemory(xdr->cx);
00772             return JS_FALSE;
00773         }
00774         entry->name = clasp->name;
00775         entry->index = numclasses;
00776     }
00777     *idp = CLASS_INDEX_TO_ID(numclasses);
00778     xdr->numclasses = ++numclasses;
00779     return JS_TRUE;
00780 }
00781 
00782 JS_PUBLIC_API(uint32)
00783 JS_XDRFindClassIdByName(JSXDRState *xdr, const char *name)
00784 {
00785     uintN i, numclasses;
00786 
00787     numclasses = xdr->numclasses;
00788     if (numclasses >= 10) {
00789         JSRegHashEntry *entry;
00790 
00791         /* Bootstrap reghash from registry on first overpopulated Find. */
00792         if (!xdr->reghash) {
00793             xdr->reghash = JS_NewDHashTable(JS_DHashGetStubOps(), NULL,
00794                                             sizeof(JSRegHashEntry),
00795                                             numclasses);
00796             if (xdr->reghash) {
00797                 for (i = 0; i < numclasses; i++) {
00798                     JSClass *clasp = xdr->registry[i];
00799                     entry = (JSRegHashEntry *)
00800                         JS_DHashTableOperate(xdr->reghash, clasp->name,
00801                                              JS_DHASH_ADD);
00802                     entry->name = clasp->name;
00803                     entry->index = i;
00804                 }
00805             }
00806         }
00807 
00808         /* If we managed to create reghash, use it for O(1) Find. */
00809         if (xdr->reghash) {
00810             entry = (JSRegHashEntry *)
00811                 JS_DHashTableOperate(xdr->reghash, name, JS_DHASH_LOOKUP);
00812             if (JS_DHASH_ENTRY_IS_BUSY(&entry->hdr))
00813                 return CLASS_INDEX_TO_ID(entry->index);
00814         }
00815     }
00816 
00817     /* Only a few classes, or we couldn't malloc reghash: use linear search. */
00818     for (i = 0; i < numclasses; i++) {
00819         if (!strcmp(name, xdr->registry[i]->name))
00820             return CLASS_INDEX_TO_ID(i);
00821     }
00822     return 0;
00823 }
00824 
00825 JS_PUBLIC_API(JSClass *)
00826 JS_XDRFindClassById(JSXDRState *xdr, uint32 id)
00827 {
00828     uintN i = CLASS_ID_TO_INDEX(id);
00829 
00830     if (i >= xdr->numclasses)
00831         return NULL;
00832     return xdr->registry[i];
00833 }
00834 
00835 #endif /* JS_HAS_XDR */