1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 *
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/
10 *
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
14 * License.
15 *
16 * The Original Code is Mozilla Communicator client code, released
17 * March 31, 1998.
18 *
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.
23 *
24 * Contributor(s):
25 *
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.
37 *
38 * ***** END LICENSE BLOCK ***** */
40 #ifndef jsxdrapi_h___
41 #define jsxdrapi_h___
43 /*
44 * JS external data representation interface API.
45 *
46 * The XDR system is comprised of three major parts:
47 *
48 * - the state serialization/deserialization APIs, which allow consumers
49 * of the API to serialize JS runtime state (script bytecodes, atom maps,
50 * object graphs, etc.) for later restoration. These portions
51 * are implemented in various appropriate files, such as jsscript.c
52 * for the script portions and jsobj.c for object state.
53 * - the callback APIs through which the runtime requests an opaque
54 * representation of a native object, and through which the runtime
55 * constructs a live native object from an opaque representation. These
56 * portions are the responsibility of the native object implementor.
57 * - utility functions for en/decoding of primitive types, such as
58 * JSStrings. This portion is implemented in jsxdrapi.c.
59 *
60 * Spiritually guided by Sun's XDR, where appropriate.
61 */
63 #include "jspubtd.h"
64 #include "jsprvtd.h"
66 JS_BEGIN_EXTERN_C
68 /* We use little-endian byteorder for all encoded data */
70 #if defined IS_LITTLE_ENDIAN
71 #define JSXDR_SWAB32(x) x
72 #define JSXDR_SWAB16(x) x
73 #elif defined IS_BIG_ENDIAN
74 #define JSXDR_SWAB32(x) (((uint32)(x) >> 24) | \
75 (((uint32)(x) >> 8) & 0xff00) | \
76 (((uint32)(x) << 8) & 0xff0000) | \
77 ((uint32)(x) << 24))
78 #define JSXDR_SWAB16(x) (((uint16)(x) >> 8) | ((uint16)(x) << 8))
79 #else
80 #error "unknown byte order"
81 #endif
83 #define JSXDR_ALIGN 4
85 typedef enum JSXDRMode {
86 JSXDR_ENCODE,
87 JSXDR_DECODE,
88 JSXDR_FREE
89 } JSXDRMode;
91 typedef enum JSXDRWhence {
92 JSXDR_SEEK_SET,
93 JSXDR_SEEK_CUR,
94 JSXDR_SEEK_END
95 } JSXDRWhence;
97 typedef struct JSXDROps {
98 JSBool (*get32)(JSXDRState *, uint32 *);
99 JSBool (*set32)(JSXDRState *, uint32 *);
100 JSBool (*getbytes)(JSXDRState *, char *, uint32);
101 JSBool (*setbytes)(JSXDRState *, char *, uint32);
102 void * (*raw)(JSXDRState *, uint32);
103 JSBool (*seek)(JSXDRState *, int32, JSXDRWhence);
104 uint32 (*tell)(JSXDRState *);
105 void (*finalize)(JSXDRState *);
106 } JSXDROps;
108 struct JSXDRState {
109 JSXDRMode mode;
110 JSXDROps *ops;
111 JSContext *cx;
112 JSClass **registry;
113 uintN numclasses;
114 uintN maxclasses;
115 void *reghash;
116 void *userdata;
117 };
119 extern JS_PUBLIC_API(void)
120 JS_XDRInitBase(JSXDRState *xdr, JSXDRMode mode, JSContext *cx);
122 extern JS_PUBLIC_API(JSXDRState *)
123 JS_XDRNewMem(JSContext *cx, JSXDRMode mode);
125 extern JS_PUBLIC_API(void *)
126 JS_XDRMemGetData(JSXDRState *xdr, uint32 *lp);
128 extern JS_PUBLIC_API(void)
129 JS_XDRMemSetData(JSXDRState *xdr, void *data, uint32 len);
131 extern JS_PUBLIC_API(uint32)
132 JS_XDRMemDataLeft(JSXDRState *xdr);
134 extern JS_PUBLIC_API(void)
135 JS_XDRMemResetData(JSXDRState *xdr);
137 extern JS_PUBLIC_API(void)
138 JS_XDRDestroy(JSXDRState *xdr);
140 extern JS_PUBLIC_API(JSBool)
141 JS_XDRUint8(JSXDRState *xdr, uint8 *b);
143 extern JS_PUBLIC_API(JSBool)
144 JS_XDRUint16(JSXDRState *xdr, uint16 *s);
146 extern JS_PUBLIC_API(JSBool)
147 JS_XDRUint32(JSXDRState *xdr, uint32 *lp);
149 extern JS_PUBLIC_API(JSBool)
150 JS_XDRBytes(JSXDRState *xdr, char *bytes, uint32 len);
152 extern JS_PUBLIC_API(JSBool)
153 JS_XDRCString(JSXDRState *xdr, char **sp);
155 extern JS_PUBLIC_API(JSBool)
156 JS_XDRCStringOrNull(JSXDRState *xdr, char **sp);
158 extern JS_PUBLIC_API(JSBool)
159 JS_XDRString(JSXDRState *xdr, JSString **strp);
161 extern JS_PUBLIC_API(JSBool)
162 JS_XDRStringOrNull(JSXDRState *xdr, JSString **strp);
164 extern JS_PUBLIC_API(JSBool)
165 JS_XDRDouble(JSXDRState *xdr, jsdouble **dp);
167 extern JS_PUBLIC_API(JSBool)
168 JS_XDRValue(JSXDRState *xdr, jsval *vp);
170 extern JS_PUBLIC_API(JSBool)
171 JS_XDRScript(JSXDRState *xdr, JSScript **scriptp);
173 extern JS_PUBLIC_API(JSBool)
174 JS_XDRRegisterClass(JSXDRState *xdr, JSClass *clasp, uint32 *lp);
176 extern JS_PUBLIC_API(uint32)
177 JS_XDRFindClassIdByName(JSXDRState *xdr, const char *name);
179 extern JS_PUBLIC_API(JSClass *)
180 JS_XDRFindClassById(JSXDRState *xdr, uint32 id);
182 /*
183 * Magic numbers.
184 */
185 #define JSXDR_MAGIC_SCRIPT_1 0xdead0001
186 #define JSXDR_MAGIC_SCRIPT_2 0xdead0002
187 #define JSXDR_MAGIC_SCRIPT_3 0xdead0003
188 #define JSXDR_MAGIC_SCRIPT_4 0xdead0004
189 #define JSXDR_MAGIC_SCRIPT_CURRENT JSXDR_MAGIC_SCRIPT_4
191 JS_END_EXTERN_C
193 #endif /* ! jsxdrapi_h___ */