Back to index

lightning-sunbird  0.9+nobinonly
xpt_xdr.h
Go to the documentation of this file.
00001 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 /*
00039  * Basic APIs for streaming typelib structures to/from disk.
00040  */
00041 
00042 #ifndef __xpt_xdr_h__
00043 #define __xpt_xdr_h__
00044 
00045 #include "xpt_struct.h"
00046 
00047 PR_BEGIN_EXTERN_C
00048 
00049 typedef struct XPTState         XPTState;
00050 typedef struct XPTDatapool      XPTDatapool;
00051 typedef struct XPTCursor        XPTCursor;
00052 
00053 /* Opaque type, for internal use */
00054 typedef struct XPTHashTable     XPTHashTable;
00055 
00056 extern XPT_PUBLIC_API(PRBool)
00057 XPT_DoString(XPTArena *arena, XPTCursor *cursor, XPTString **strp);
00058 
00059 extern XPT_PUBLIC_API(PRBool)
00060 XPT_DoStringInline(XPTArena *arena, XPTCursor *cursor, XPTString **strp);
00061 
00062 extern XPT_PUBLIC_API(PRBool)
00063 XPT_DoCString(XPTArena *arena, XPTCursor *cursor, char **strp);
00064 
00065 extern XPT_PUBLIC_API(PRBool)
00066 XPT_DoIID(XPTCursor *cursor, nsID *iidp);
00067 
00068 extern XPT_PUBLIC_API(PRBool)
00069 XPT_Do64(XPTCursor *cursor, PRInt64 *u64p);
00070 
00071 extern XPT_PUBLIC_API(PRBool)
00072 XPT_Do32(XPTCursor *cursor, PRUint32 *u32p);
00073 
00074 extern XPT_PUBLIC_API(PRBool)
00075 XPT_Do16(XPTCursor *cursor, PRUint16 *u16p);
00076 
00077 extern XPT_PUBLIC_API(PRBool)
00078 XPT_Do8(XPTCursor *cursor, PRUint8 *u8p);
00079 
00080 extern XPT_PUBLIC_API(PRBool)
00081 XPT_DoHeaderPrologue(XPTArena *arena, XPTCursor *cursor, XPTHeader **headerp, PRUint32 * ide_offset);
00082 extern XPT_PUBLIC_API(PRBool)
00083 XPT_DoHeader(XPTArena *arena, XPTCursor *cursor, XPTHeader **headerp);
00084 
00085 typedef enum {
00086     XPT_ENCODE,
00087     XPT_DECODE
00088 } XPTMode;
00089 
00090 typedef enum {
00091     XPT_HEADER = 0,
00092     XPT_DATA = 1
00093 } XPTPool;
00094 
00095 struct XPTState {
00096     XPTMode          mode;
00097     PRUint32         data_offset;
00098     PRUint32         next_cursor[2];
00099     XPTDatapool      *pool;
00100     XPTArena         *arena;
00101 };
00102 
00103 struct XPTDatapool {
00104     XPTHashTable     *offset_map;
00105     char             *data;
00106     PRUint32         count;
00107     PRUint32         allocated;
00108 };
00109 
00110 struct XPTCursor {
00111     XPTState    *state;
00112     XPTPool     pool;
00113     PRUint32    offset;
00114     PRUint8     bits;
00115 };
00116 
00117 extern XPT_PUBLIC_API(XPTState *)
00118 XPT_NewXDRState(XPTMode mode, char *data, PRUint32 len);
00119 
00120 extern XPT_PUBLIC_API(PRBool)
00121 XPT_MakeCursor(XPTState *state, XPTPool pool, PRUint32 len, XPTCursor *cursor);
00122 
00123 extern XPT_PUBLIC_API(PRBool)
00124 XPT_SeekTo(XPTCursor *cursor, PRUint32 offset);
00125 
00126 extern XPT_PUBLIC_API(void)
00127 XPT_DestroyXDRState(XPTState *state);
00128 
00129 /* Set file_length based on the data used in the state.  (Only ENCODE.) */
00130 extern XPT_PUBLIC_API(PRBool)
00131 XPT_UpdateFileLength(XPTState *state);
00132 
00133 /* returns the length of the specified data block */
00134 extern XPT_PUBLIC_API(void)
00135 XPT_GetXDRDataLength(XPTState *state, XPTPool pool, PRUint32 *len);
00136 
00137 extern XPT_PUBLIC_API(void)
00138 XPT_GetXDRData(XPTState *state, XPTPool pool, char **data, PRUint32 *len);
00139 
00140 /* set or get the data offset for the state, depending on mode */
00141 extern XPT_PUBLIC_API(void)
00142 XPT_DataOffset(XPTState *state, PRUint32 *data_offsetp);
00143 
00144 extern XPT_PUBLIC_API(void)
00145 XPT_SetDataOffset(XPTState *state, PRUint32 data_offset);
00146 
00147 extern XPT_PUBLIC_API(PRUint32)
00148 XPT_GetOffsetForAddr(XPTCursor *cursor, void *addr);
00149 
00150 extern XPT_PUBLIC_API(PRBool)
00151 XPT_SetOffsetForAddr(XPTCursor *cursor, void *addr, PRUint32 offset);
00152 
00153 extern XPT_PUBLIC_API(PRBool)
00154 XPT_SetAddrForOffset(XPTCursor *cursor, PRUint32 offset, void *addr);
00155 
00156 extern XPT_PUBLIC_API(void *)
00157 XPT_GetAddrForOffset(XPTCursor *cursor, PRUint32 offset);
00158 
00159 /* all data structures are big-endian */
00160 
00161 #if defined IS_BIG_ENDIAN
00162 #  define XPT_SWAB32(x) x
00163 #  define XPT_SWAB16(x) x
00164 #elif defined IS_LITTLE_ENDIAN
00165 #  define XPT_SWAB32(x) (((x) >> 24) |                                        \
00166              (((x) >> 8) & 0xff00) |                                          \
00167              (((x) << 8) & 0xff0000) |                                        \
00168              ((x) << 24))
00169 #  define XPT_SWAB16(x) (((x) >> 8) | ((x) << 8))
00170 #else
00171 #  error "unknown byte order"
00172 #endif
00173 
00174 /*
00175  * If we're decoding, we want to read the offset before we check
00176  * for already-decoded values.
00177  *
00178  * Then we check for repetition: CheckForRepeat will see if we've already
00179  * encoded/decoded this value, and if so will set offset/addr correctly
00180  * and make already be true.  If not, it will set up the cursor for
00181  * encoding (reserve space) or decoding (seek to correct location) as
00182  * appropriate.  In the encode case, it will also set the addr->offset
00183  * mapping.
00184  */
00185 
00186 #define XPT_PREAMBLE_(cursor, addrp, pool, size, new_curs, already)           \
00187     XPTMode mode = cursor->state->mode;                                       \
00188     if (!(mode == XPT_ENCODE || XPT_Do32(cursor, &new_curs.offset)) ||        \
00189         !CheckForRepeat(cursor, (void **)addrp, pool,                         \
00190                         mode == XPT_ENCODE ? size : 0u, &new_curs,            \
00191                         &already) ||                                          \
00192         !(mode == XPT_DECODE || XPT_Do32(cursor, &new_curs.offset)))          \
00193         return PR_FALSE;                                                      \
00194     if (already)                                                              \
00195         return PR_TRUE;                                                       \
00196 
00197 #define XPT_PREAMBLE_NO_ALLOC(cursor, addrp, pool, size, new_curs, already)   \
00198   {                                                                           \
00199     XPT_PREAMBLE_(cursor, addrp, pool, size, new_curs, already)               \
00200   }
00201 
00202 #define XPT_ERROR_HANDLE(arena, free_it)                                      \
00203  error:                                                                       \
00204     if (cursor->state->mode == XPT_DECODE)                                    \
00205     XPT_FREEIF(arena, free_it);                                               \
00206     return PR_FALSE;
00207 
00208 
00209 PR_END_EXTERN_C
00210 
00211 #endif /* __xpt_xdr_h__ */