?? framemgr.c
字號:
/******************************************************************Copyright 1993, 1994 by Digital Equipment Corporation, Maynard, Massachusetts, All Rights Reserved Permission to use, copy, modify, and distribute this software and its documentation for any purpose and without fee is hereby granted, provided that the above copyright notice appear in all copies and thatboth that copyright notice and this permission notice appear in supporting documentation, and that the names of Digital or MIT not beused in advertising or publicity pertaining to distribution of thesoftware without specific, written prior permission. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDINGALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALLDIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES ORANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THISSOFTWARE. Author: Hiroyuki Miyamoto Digital Equipment Corporation miyamoto@jrd.dec.com This version tidied and debugged by Steve Underwood May 1999******************************************************************/#include <X11/Xlibint.h>#include <stdlib.h>#include "FrameMgr.h"/* Convenient macro */#define _UNIT(n) ((int)(n) & 0xFF)#define _NUMBER(n) (((int)(n) >> 8) & 0xFF)/* For byte swapping */#define Swap16(p, n) ((p)->byte_swap ? \(((n) << 8 & 0xFF00) | \ ((n) >> 8 & 0xFF) \) : n)#define Swap32(p, n) ((p)->byte_swap ? \ (((n) << 24 & 0xFF000000) | \ ((n) << 8 & 0xFF0000) | \ ((n) >> 8 & 0xFF00) | \ ((n) >> 24 & 0xFF) \ ) : n)#define Swap64(p, n) ((p)->byte_swap ? \ (((n) << 56 & 0xFF00000000000000) | \ ((n) << 40 & 0xFF000000000000) | \ ((n) << 24 & 0xFF0000000000) | \ ((n) << 8 & 0xFF00000000) | \ ((n) >> 8 & 0xFF000000) | \ ((n) >> 24 & 0xFF0000) | \ ((n) >> 40 & 0xFF00) | \ ((n) >> 56 & 0xFF) \ ) : n)/* Type definition */typedef struct _Iter *Iter;typedef struct _FrameInst *FrameInst;typedef union{ int num; /* For BARRAY */ FrameInst fi; /* For POINTER */ Iter iter; /* For ITER */} ExtraDataRec, *ExtraData;typedef struct _Chain{ ExtraDataRec d; int frame_no; struct _Chain *next;} ChainRec, *Chain;typedef struct _ChainMgr{ Chain top; Chain tail;} ChainMgrRec, *ChainMgr;typedef struct _ChainIter{ Chain cur;} ChainIterRec, *ChainIter;typedef struct _FrameIter{ Iter iter; Bool counting; unsigned int counter; int end; struct _FrameIter* next;} FrameIterRec, *FrameIter;typedef struct _FrameInst{ XimFrame template; ChainMgrRec cm; int cur_no;} FrameInstRec;typedef void (*IterStartWatchProc) (Iter it, void *client_data);typedef struct _Iter{ XimFrame template; int max_count; Bool allow_expansion; ChainMgrRec cm; int cur_no; IterStartWatchProc start_watch_proc; void *client_data; Bool start_counter;} IterRec;typedef struct _FrameMgr{ XimFrame frame; FrameInst fi; char *area; int idx; Bool byte_swap; int total_size; FrameIter iters;} FrameMgrRec;typedef union{ int num; /* For BARRAY and PAD */ struct { /* For COUNTER_* */ Iter iter; Bool is_byte_len; } counter;} XimFrameTypeInfoRec, *XimFrameTypeInfo;/* Special values */#define NO_VALUE -1#define NO_VALID_FIELD -2static FrameInst FrameInstInit(XimFrame frame);static void FrameInstFree(FrameInst fi);static XimFrameType FrameInstGetNextType(FrameInst fi, XimFrameTypeInfo info);static XimFrameType FrameInstPeekNextType(FrameInst fi, XimFrameTypeInfo info);static FmStatus FrameInstSetSize(FrameInst fi, int num);static FmStatus FrameInstSetIterCount(FrameInst fi, int num);static int FrameInstGetTotalSize(FrameInst fi);static void FrameInstReset(FrameInst fi);static Iter IterInit(XimFrame frame, int count);static void IterFree(Iter it);static int FrameInstGetSize(FrameInst fi);static int IterGetSize(Iter it);static XimFrameType IterGetNextType(Iter it, XimFrameTypeInfo info);static XimFrameType IterPeekNextType(Iter it, XimFrameTypeInfo info);static FmStatus IterSetSize(Iter it, int num);static FmStatus IterSetIterCount(Iter it, int num);static int IterGetTotalSize(Iter it);static void IterReset(Iter it);static Bool IterIsLoopEnd(Iter it, Bool* myself);static void IterSetStartWatch(Iter it, IterStartWatchProc proc, void* client_data);static void _IterStartWatch(Iter it, void* client_data);static ExtraData ChainMgrGetExtraData(ChainMgr cm, int frame_no);static ExtraData ChainMgrSetData(ChainMgr cm, int frame_no, ExtraDataRec data);static Bool ChainIterGetNext(ChainIter ci, int* frame_no, ExtraData d);static int _FrameInstIncrement(XimFrame frame, int count);static int _FrameInstDecrement(XimFrame frame, int count);static int _FrameInstGetItemSize(FrameInst fi, int cur_no);static Bool FrameInstIsIterLoopEnd(FrameInst fi);static FrameIter _FrameMgrAppendIter(FrameMgr fm, Iter it, int end);static FrameIter _FrameIterCounterIncr(FrameIter fitr, int i);static void _FrameMgrRemoveIter(FrameMgr fm, FrameIter it);static Bool _FrameMgrIsIterLoopEnd(FrameMgr fm);static Bool _FrameMgrProcessPadding(FrameMgr fm, FmStatus* status);#define IterGetIterCount(it) ((it)->allow_expansion ? \NO_VALUE : (it)->max_count)#define IterFixIteration(it) ((it)->allow_expansion = False)#define IterSetStarter(it) ((it)->start_counter = True)#define ChainMgrInit(cm) (cm)->top = (cm)->tail = NULL#define ChainMgrFree(cm) \{ \ Chain tmp; \ Chain cur = (cm)->top; \ \ while (cur) \ { \ tmp = cur->next; \ Xfree (cur); \ cur = tmp; \ } \}#define ChainIterInit(ci, cm) \{ \ (ci)->cur = (cm)->top; \}/* ChainIterFree has nothing to do. */#define ChainIterFree(ci)#define FrameInstIsEnd(fi) ((fi)->template[(fi)->cur_no].type == EOL)FrameMgr FrameMgrInit (XimFrame frame, char* area, Bool byte_swap){ FrameMgr fm; fm = (FrameMgr) Xmalloc (sizeof (FrameMgrRec)); fm->frame = frame; fm->fi = FrameInstInit (frame); fm->area = (char *) area; fm->idx = 0; fm->byte_swap = byte_swap; fm->total_size = NO_VALUE; fm->iters = NULL; return fm;}void FrameMgrInitWithData (FrameMgr fm, XimFrame frame, void * area, Bool byte_swap){ fm->frame = frame; fm->fi = FrameInstInit (frame); fm->area = (char *) area; fm->idx = 0; fm->byte_swap = byte_swap; fm->total_size = NO_VALUE;}void FrameMgrFree (FrameMgr fm){ FrameInstFree (fm->fi); Xfree (fm);}FmStatus FrameMgrSetBuffer (FrameMgr fm, void* area){ if (fm->area) return FmBufExist; fm->area = (char *) area; return FmSuccess;}FmStatus _FrameMgrPutToken (FrameMgr fm, void *data, int data_size){ XimFrameType type; XimFrameTypeInfoRec info; if (fm->total_size != NO_VALUE && fm->idx >= fm->total_size) return FmNoMoreData; /*endif*/ type = FrameInstGetNextType(fm->fi, &info); if (type & COUNTER_MASK) { unsigned long input_length; if (info.counter.is_byte_len) { if ((input_length = IterGetTotalSize (info.counter.iter)) == NO_VALUE) { return FmCannotCalc; } /*endif*/ } else { if ((input_length = IterGetIterCount (info.counter.iter)) == NO_VALUE) { return FmCannotCalc; } /*endif*/ } /*endif*/ switch (type) { case COUNTER_BIT8: *(CARD8 *) (fm->area + fm->idx) = input_length; fm->idx++; break; case COUNTER_BIT16: *(CARD16 *) (fm->area + fm->idx) = Swap16 (fm, input_length); fm->idx += 2; break; case COUNTER_BIT32: *(CARD32 *) (fm->area + fm->idx) = Swap32 (fm, input_length); fm->idx += 4; break;#if defined(_NEED64BIT) case COUNTER_BIT64: *(CARD64 *) (fm->area + fm->idx) = Swap64 (fm, input_length); fm->idx += 8; break;#endif } /*endswitch*/ _FrameMgrPutToken(fm, data, data_size); return FmSuccess; } /*endif*/ switch (type) { case BIT8: if (data_size == sizeof (unsigned char)) { unsigned long num = *(unsigned char *) data; *(CARD8 *) (fm->area + fm->idx) = num; } else if (data_size == sizeof (unsigned short)) { unsigned long num = *(unsigned short *) data; *(CARD8 *) (fm->area + fm->idx) = num; } else if (data_size == sizeof (unsigned int)) { unsigned long num = *(unsigned int *) data; *(CARD8 *) (fm->area + fm->idx) = num; } else if (data_size == sizeof (unsigned long)) { unsigned long num = *(unsigned long *) data; *(CARD8 *) (fm->area + fm->idx) = num; } else { ; /* Should never be reached */ } /*endif*/ fm->idx++; return FmSuccess; case BIT16: if (data_size == sizeof (unsigned char)) { unsigned long num = *(unsigned char *) data; *(CARD16*)(fm->area + fm->idx) = Swap16 (fm, num); } else if (data_size == sizeof (unsigned short)) { unsigned long num = *(unsigned short *) data; *(CARD16 *) (fm->area + fm->idx) = Swap16 (fm, num); } else if (data_size == sizeof (unsigned int)) { unsigned long num = *(unsigned int *) data; *(CARD16 *) (fm->area + fm->idx) = Swap16 (fm, num); } else if (data_size == sizeof (unsigned long)) { unsigned long num = *(unsigned long *) data; *(CARD16 *) (fm->area + fm->idx) = Swap16 (fm, num); } else { ; /* Should never reached */ } /*endif*/ fm->idx += 2; return FmSuccess; case BIT32: if (data_size == sizeof (unsigned char)) { unsigned long num = *(unsigned char *) data; *(CARD32 *) (fm->area + fm->idx) = Swap32 (fm, num); } else if (data_size == sizeof (unsigned short)) { unsigned long num = *(unsigned short *) data; *(CARD32 *) (fm->area + fm->idx) = Swap32 (fm, num); } else if (data_size == sizeof (unsigned int)) { unsigned long num = *(unsigned int *) data; *(CARD32 *) (fm->area + fm->idx) = Swap32 (fm, num); } else if (data_size == sizeof (unsigned long)) { unsigned long num = *(unsigned long *) data; *(CARD32 *) (fm->area + fm->idx) = Swap32 (fm, num); } else { ; /* Should never reached */ } /*endif*/ fm->idx += 4; return FmSuccess;#if defined(_NEED64BIT) case BIT64: if (data_size == sizeof (unsigned char)) { unsigned long num = *(unsigned char *) data; *(CARD64 *) (fm->area + fm->idx) = Swap64 (fm, num); } else if (data_size == sizeof (unsigned short)) { unsigned long num = *(unsigned short *) data; *(CARD64 *) (fm->area + fm->idx) = Swap64 (fm, num); } else if (data_size == sizeof (unsigned int)) { unsigned long num = *(unsigned int *) data; *(CARD64 *) (fm->area + fm->idx) = Swap64 (fm, num); } else if (data_size == sizeof (unsigned long)) { unsigned long num = *(unsigned long *) data; *(CARD64 *) (fm->area + fm->idx) = Swap64 (fm, num); } else { ; /* Should never reached */ } /*endif*/ fm->idx += 4; return FmSuccess;#endif case BARRAY: if (info.num == NO_VALUE) return FmInvalidCall; /*endif*/ if (info.num > 0) { bcopy (*(char **) data, fm->area + fm->idx, info.num); fm->idx += info.num; } /*endif*/ return FmSuccess; case PADDING: if (info.num == NO_VALUE) return FmInvalidCall; /*endif*/ fm->idx += info.num; return _FrameMgrPutToken(fm, data, data_size); case ITER: return FmInvalidCall; case EOL: return FmEOD; } /*endswitch*/ return (FmStatus) NULL; /* Should never be reached */}FmStatus _FrameMgrGetToken (FrameMgr fm , void* data, int data_size){ XimFrameType type; static XimFrameTypeInfoRec info; /* memory */ FrameIter fitr; if (fm->total_size != NO_VALUE && fm->idx >= fm->total_size) return FmNoMoreData; /*endif*/ type = FrameInstGetNextType(fm->fi, &info); if (type & COUNTER_MASK) { int end; FrameIter client_data; type &= ~COUNTER_MASK; switch (type) { case BIT8: end = *(CARD8 *) (fm->area + fm->idx); break; case BIT16: end = Swap16 (fm, *(CARD16 *) (fm->area + fm->idx)); break; case BIT32: end = Swap32 (fm, *(CARD32 *) (fm->area + fm->idx)); break;#if defined(_NEED64BIT) case BIT64: end = Swap64 (fm, *(CARD64 *) (fm->area + fm->idx)); break;#endif } /*endswitch*/ if ((client_data = _FrameMgrAppendIter (fm, info.counter.iter, end))) { IterSetStarter (info.counter.iter); IterSetStartWatch (info.counter.iter, _IterStartWatch, (void *) client_data); } /*endif*/ } /*endif*/ type &= ~COUNTER_MASK; switch (type) { case BIT8: if (data_size == sizeof (unsigned char)) { *(unsigned char*) data = *(CARD8 *) (fm->area + fm->idx); } else if (data_size == sizeof (unsigned short)) { *(unsigned short *) data = *(CARD8 *) (fm->area + fm->idx); } else if (data_size == sizeof (unsigned int)) { *(unsigned int *) data = *(CARD8 *) (fm->area + fm->idx); } else if (data_size == sizeof (unsigned long)) { *(unsigned long *) data = *(CARD8 *) (fm->area + fm->idx); } else { ; /* Should never reached */ } /*endif*/ fm->idx++; if ((fitr = _FrameIterCounterIncr (fm->iters, 1/*BIT8*/))) _FrameMgrRemoveIter (fm, fitr); /*endif*/ return FmSuccess; case BIT16: if (data_size == sizeof (unsigned char)) { *(unsigned char *) data = Swap16 (fm, *(CARD16 *) (fm->area + fm->idx)); } else if (data_size == sizeof (unsigned short)) { *(unsigned short *) data = Swap16 (fm, *(CARD16 *) (fm->area + fm->idx)); } else if (data_size == sizeof (unsigned int)) { *(unsigned int *) data = Swap16 (fm, *(CARD16 *) (fm->area + fm->idx)); } else if (data_size == sizeof (unsigned long)) { *(unsigned long *) data = Swap16 (fm, *(CARD16 *) (fm->area + fm->idx)); } else { ; /* Should never reached */ } /*endif*/ fm->idx += 2; if ((fitr = _FrameIterCounterIncr (fm->iters, 2/*BIT16*/))) _FrameMgrRemoveIter(fm, fitr); /*endif*/ return FmSuccess; case BIT32: if (data_size == sizeof (unsigned char)) { *(unsigned char *) data = Swap32 (fm, *(CARD32 *) (fm->area + fm->idx)); } else if (data_size == sizeof (unsigned short)) { *(unsigned short *) data = Swap32 (fm, *(CARD32 *) (fm->area + fm->idx)); } else if (data_size == sizeof (unsigned int)) { *(unsigned int *) data = Swap32 (fm, *(CARD32 *) (fm->area + fm->idx));
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -