00001
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #ifndef _MGUI_ENDIAN_RW_H
00035 #define _MGUI_ENDIAN_RW_H
00036
00037
00038 #ifdef __cplusplus
00039 extern "C" {
00040 #endif
00041
00042
00043
00064 #define RWAREA_TYPE_UNKNOWN 0
00065 #define RWAREA_TYPE_STDIO 1
00066 #define RWAREA_TYPE_MEM 2
00067
00071 typedef struct _MG_RWops {
00077 int (*seek)(struct _MG_RWops *context, int offset, int whence);
00078
00084 int (*read)(struct _MG_RWops *context, void *ptr, int objsize, int num);
00085
00091 int (*write)(struct _MG_RWops *context, const void *ptr, int objsize,
00092 int num);
00093
00094 #ifdef _USE_OWN_STDIO
00095
00096 int (*ungetc)(struct _MG_RWops *context, unsigned char c);
00097 #endif
00098
00102 int (*close)(struct _MG_RWops *context);
00103
00107 int (*eof)(struct _MG_RWops *context);
00108
00119 Uint32 type;
00120
00121 union {
00122 struct {
00123 int autoclose;
00124 FILE *fp;
00125 } stdio;
00126 struct {
00127 Uint8 *base;
00128 Uint8 *here;
00129 Uint8 *stop;
00130 } mem;
00131 struct {
00132 void *data1;
00133 } unknown;
00134 } hidden;
00135 } MG_RWops;
00136
00151 MG_EXPORT MG_RWops* MGUI_RWFromFile(const char *file, const char *mode);
00152
00167 MG_EXPORT MG_RWops* MGUI_RWFromFP(FILE *fp, int autoclose);
00168
00183 MG_EXPORT MG_RWops* MGUI_RWFromMem(void *mem, int size);
00184
00200 MG_EXPORT void MGUI_InitMemRW (MG_RWops* area, void *mem, int size);
00201
00213 MG_EXPORT MG_RWops* MGUI_AllocRW(void);
00214
00225 MG_EXPORT void MGUI_FreeRW(MG_RWops *area);
00226
00248 #define MGUI_RWseek(ctx, offset, whence) (ctx)->seek(ctx, offset, whence)
00249
00263 #define MGUI_RWtell(ctx) (ctx)->seek(ctx, 0, SEEK_CUR)
00264
00280 #define MGUI_RWread(ctx, ptr, size, n) (ctx)->read(ctx, ptr, size, n)
00281
00297 #define MGUI_RWwrite(ctx, ptr, size, n) (ctx)->write(ctx, ptr, size, n)
00298
00312 #define MGUI_RWclose(ctx) (ctx)->close(ctx)
00313
00327 #define MGUI_RWeof(ctx) (ctx)->eof(ctx)
00328
00343 MG_EXPORT int MGUI_RWgetc (MG_RWops* area);
00344
00347
00348
00371
00372
00373 #ifdef linux
00374 #include <endian.h>
00375 #ifdef __arch__swab16
00376 #define ArchSwap16 __arch__swab16
00377 #endif
00378 #ifdef __arch__swab32
00379 #define ArchSwap32 __arch__swab32
00380 #endif
00381 #endif
00382
00383
00384
00385
00386
00387
00388 #ifndef ArchSwap16
00389 static inline Uint16 ArchSwap16(Uint16 D) {
00390 return((D<<8)|(D>>8));
00391 }
00392 #endif
00393 #ifndef ArchSwap32
00394 static inline Uint32 ArchSwap32(Uint32 D) {
00395 return((D<<24)|((D<<8)&0x00FF0000)|((D>>8)&0x0000FF00)|(D>>24));
00396 }
00397 #endif
00398 #ifdef MGUI_HAS_64BIT_TYPE
00399 #ifndef ArchSwap64
00400 static inline Uint64 ArchSwap64(Uint64 val) {
00401 Uint32 hi, lo;
00402
00403
00404 lo = (Uint32)(val&0xFFFFFFFF);
00405 val >>= 32;
00406 hi = (Uint32)(val&0xFFFFFFFF);
00407 val = ArchSwap32(lo);
00408 val <<= 32;
00409 val |= ArchSwap32(hi);
00410 return(val);
00411 }
00412 #endif
00413 #else
00414 #ifndef ArchSwap64
00415
00416
00417
00418
00419 #define ArchSwap64(X) (X)
00420 #endif
00421 #endif
00422
00423
00424 #if MGUI_BYTEORDER == MGUI_LIL_ENDIAN
00425
00426 #define ArchSwapLE16(X) (X)
00427
00428 #define ArchSwapLE32(X) (X)
00429
00430 #define ArchSwapLE64(X) (X)
00431
00432 #define ArchSwapBE16(X) ArchSwap16(X)
00433
00434 #define ArchSwapBE32(X) ArchSwap32(X)
00435
00436 #define ArchSwapBE64(X) ArchSwap64(X)
00437 #else
00438 #define ArchSwapLE16(X) ArchSwap16(X)
00439 #define ArchSwapLE32(X) ArchSwap32(X)
00440 #define ArchSwapLE64(X) ArchSwap64(X)
00441 #define ArchSwapBE16(X) (X)
00442 #define ArchSwapBE32(X) (X)
00443 #define ArchSwapBE64(X) (X)
00444 #endif
00445
00458 extern Uint16 MGUI_ReadLE16(MG_RWops *src);
00459
00472 extern Uint16 MGUI_ReadBE16(MG_RWops *src);
00473
00486 extern Uint32 MGUI_ReadLE32(MG_RWops *src);
00487
00500 extern Uint32 MGUI_ReadBE32(MG_RWops *src);
00501
00514 extern Uint64 MGUI_ReadLE64(MG_RWops *src);
00515
00528 extern Uint64 MGUI_ReadBE64(MG_RWops *src);
00529
00544 extern int MGUI_WriteLE16(MG_RWops *dst, Uint16 value);
00545
00560 extern int MGUI_WriteBE16(MG_RWops *dst, Uint16 value);
00561
00576 extern int MGUI_WriteLE32(MG_RWops *dst, Uint32 value);
00577
00592 extern int MGUI_WriteBE32(MG_RWops *dst, Uint32 value);
00593
00608 extern int MGUI_WriteLE64(MG_RWops *dst, Uint64 value);
00609
00624 extern int MGUI_WriteBE64(MG_RWops *dst, Uint64 value);
00625
00638 extern Uint16 MGUI_ReadLE16FP(FILE *src);
00639
00652 extern Uint32 MGUI_ReadLE32FP(FILE *src);
00653
00668 extern int MGUI_WriteLE16FP(FILE *dst, Uint16 value);
00669
00684 extern int MGUI_WriteLE32FP(FILE *dst, Uint32 value);
00685
00686 static inline Uint16 MGUI_ReadLE16Mem (const Uint8** data)
00687 {
00688 #if 1
00689 Uint16 h1, h2;
00690
00691 h1 = *(*data); (*data)++;
00692 h2 = *(*data); (*data)++;
00693 return ((h2<<8)|h1);
00694 #else
00695 Uint16 u;
00696 memcpy (&u, *data, sizeof (Uint16));
00697 u = ArchSwapLE16 (u);
00698 *data += sizeof (Uint16);
00699 return u;
00700 #endif
00701 }
00702
00703 static inline Uint32 MGUI_ReadLE32Mem (const Uint8** data)
00704 {
00705 #if 1
00706 Uint32 q1, q2, q3, q4;
00707
00708 q1 = *(*data); (*data)++;
00709 q2 = *(*data); (*data)++;
00710 q3 = *(*data); (*data)++;
00711 q4 = *(*data); (*data)++;
00712 return ((q4<<24)|(q3<<16)|(q2<<8)|(q1));
00713 #else
00714 Uint32 u;
00715 memcpy (&u, *data, sizeof (Uint32));
00716 u = ArchSwapLE32 (u);
00717 *data += sizeof (Uint32);
00718 return u;
00719 #endif
00720 }
00721
00722 static inline Uint16 MGUI_ReadBE16Mem (const Uint8** data)
00723 {
00724 #if 1
00725 Uint16 h1, h2;
00726
00727 h1 = *(*data); (*data)++;
00728 h2 = *(*data); (*data)++;
00729 return ((h1<<8)|h2);
00730 #else
00731 Uint16 u;
00732 memcpy (&u, *data, sizeof (Uint16));
00733 u = ArchSwapBE16 (u);
00734 *data += sizeof (Uint16);
00735 return u;
00736 #endif
00737 }
00738
00739 static inline Uint32 MGUI_ReadBE32Mem (const Uint8** data)
00740 {
00741 #if 1
00742 Uint32 q1, q2, q3, q4;
00743
00744 q1 = *(*data); (*data)++;
00745 q2 = *(*data); (*data)++;
00746 q3 = *(*data); (*data)++;
00747 q4 = *(*data); (*data)++;
00748 return ((q1<<24)|(q2<<16)|(q3<<8)|(q4));
00749 #else
00750 Uint32 u;
00751 memcpy (&u, *data, sizeof (Uint32));
00752 u = ArchSwapBE32 (u);
00753 *data += sizeof (Uint32);
00754 return u;
00755 #endif
00756 }
00757
00764
00765 #ifdef __cplusplus
00766 }
00767 #endif
00768
00769 #endif
00770