📄 endianrw.h
字号:
#ifndef ArchSwap16static inline Uint16 ArchSwap16(Uint16 D) { return((D<<8)|(D>>8));}#endif#ifndef ArchSwap32static inline Uint32 ArchSwap32(Uint32 D) { return((D<<24)|((D<<8)&0x00FF0000)|((D>>8)&0x0000FF00)|(D>>24));}#endif#ifdef MGUI_HAS_64BIT_TYPE#ifndef ArchSwap64static inline Uint64 ArchSwap64(Uint64 val) { Uint32 hi, lo; /* Separate into high and low 32-bit values and swap them */ lo = (Uint32)(val&0xFFFFFFFF); val >>= 32; hi = (Uint32)(val&0xFFFFFFFF); val = ArchSwap32(lo); val <<= 32; val |= ArchSwap32(hi); return(val);}#endif#else#ifndef ArchSwap64/* This is mainly to keep compilers from complaining in MGUI code. If there is no real 64-bit datatype, then compilers will complain about the fake 64-bit datatype that MGUI provides when it compiles user code.*/#define ArchSwap64(X) (X)#endif#endif /* MGUI_HAS_64BIT_TYPE *//* Byteswap item from the specified endianness to the native endianness */#if MGUI_BYTEORDER == MGUI_LIL_ENDIAN/** Swaps a 16-bit little endian integer to the native endianness. */#define ArchSwapLE16(X) (X)/** Swaps a 32-bit little endian integer to the native endianness. */#define ArchSwapLE32(X) (X)/** Swaps a 64-bit little endian integer to the native endianness. */#define ArchSwapLE64(X) (X)/** Swaps a 16-bit big endian integer to the native endianness. */#define ArchSwapBE16(X) ArchSwap16(X)/** Swaps a 32-bit big endian integer to the native endianness. */#define ArchSwapBE32(X) ArchSwap32(X)/** Swaps a 64-bit big endian integer to the native endianness. */#define ArchSwapBE64(X) ArchSwap64(X)#else#define ArchSwapLE16(X) ArchSwap16(X)#define ArchSwapLE32(X) ArchSwap32(X)#define ArchSwapLE64(X) ArchSwap64(X)#define ArchSwapBE16(X) (X)#define ArchSwapBE32(X) (X)#define ArchSwapBE64(X) (X)#endif/** * \fn Uint16 MGUI_ReadLE16(MG_RWops *src) * \brief Reads a 16-bit little endian integer from a MG_RWops object. * * This function reads a 16-bit little endian integer from the data source * pointed to by \a src, and return it in native format. * * \param src The pointer to the MG_RWops object. * \return The integer in native endianness. * * \sa MGUI_WriteLE16 */extern Uint16 MGUI_ReadLE16(MG_RWops *src);/** * \fn Uint16 MGUI_ReadBE16(MG_RWops *src) * \brief Reads a 16-bit big endian integer from a MG_RWops object. * * This function reads a 16-bit big endian integer from the data source * pointed to by \a src, and return it in native format. * * \param src The pointer to the MG_RWops object. * \return The integer in native endianness. * * \sa MGUI_WriteBE16 */extern Uint16 MGUI_ReadBE16(MG_RWops *src);/** * \fn Uint32 MGUI_ReadLE32(MG_RWops *src) * \brief Reads a 32-bit little endian integer from a MG_RWops object. * * This function reads a 32-bit little endian integer from the data source * pointed to by \a src, and return it in native format. * * \param src The pointer to the MG_RWops object. * \return The integer in native endianness. * * \sa MGUI_WriteLE32 */extern Uint32 MGUI_ReadLE32(MG_RWops *src);/** * \fn Uint32 MGUI_ReadBE32(MG_RWops *src) * \brief Reads a 32-bit big endian integer from a MG_RWops object. * * This function reads a 32-bit big endian integer from the data source * pointed to by \a src, and return it in native format. * * \param src The pointer to the MG_RWops object. * \return The integer in native endianness. * * \sa MGUI_WriteBE32 */extern Uint32 MGUI_ReadBE32(MG_RWops *src);/** * \fn Uint64 MGUI_ReadLE64(MG_RWops *src) * \brief Reads a 64-bit little endian integer from a MG_RWops object. * * This function reads a 64-bit little endian integer from the data source * pointed to by \a src, and return it in native format. * * \param src The pointer to the MG_RWops object. * \return The integer in native endianness. * * \sa MGUI_WriteLE64 */extern Uint64 MGUI_ReadLE64(MG_RWops *src);/** * \fn Uint64 MGUI_ReadBE64(MG_RWops *src) * \brief Reads a 64-bit big endian integer from a MG_RWops object. * * This function reads a 64-bit big endian integer from the data source * pointed to by \a src, and return it in native format. * * \param src The pointer to the MG_RWops object. * \return The integer in native endianness. * * \sa MGUI_WriteBE64 */extern Uint64 MGUI_ReadBE64(MG_RWops *src);/** * \fn int MGUI_WriteLE16(MG_RWops *src, Uint16 value) * \brief Writes an 16-bit integer of native format to a MG_RWops object in littlen endianness. * * This function writes a 16-bit integer of native format to the data source * pointed to by \a src in littlen endiannes. * * \param src The pointer to the MG_RWops object. * \param value The 16-bit integer in native endianness. * \return Returns 1 on success, else indicates an error. * * \sa MGUI_ReadLE16 */extern int MGUI_WriteLE16(MG_RWops *dst, Uint16 value);/** * \fn int MGUI_WriteBE16(MG_RWops *src, Uint16 value) * \brief Writes an 16-bit integer of native format to a MG_RWops object in big endianness. * * This function writes a 16-bit integer of native format to the data source * pointed to by \a src in big endiannes. * * \param src The pointer to the MG_RWops object. * \param value The 16-bit integer in native endianness. * \return Returns 1 on success, else indicates an error. * * \sa MGUI_ReadLE16 */extern int MGUI_WriteBE16(MG_RWops *dst, Uint16 value);/** * \fn int MGUI_WriteLE32(MG_RWops *src, Uint32 value) * \brief Writes an 32-bit integer of native format to a MG_RWops object in littlen endianness. * * This function writes a 32-bit integer of native format to the data source * pointed to by \a src in littlen endiannes. * * \param src The pointer to the MG_RWops object. * \param value The 32-bit integer in native endianness. * \return Returns 1 on success, else indicates an error. * * \sa MGUI_ReadLE32 */extern int MGUI_WriteLE32(MG_RWops *dst, Uint32 value);/** * \fn int MGUI_WriteBE32(MG_RWops *src, Uint32 value) * \brief Writes an 32-bit integer of native format to a MG_RWops object in big endianness. * * This function writes a 32-bit integer of native format to the data source * pointed to by \a src in big endiannes. * * \param src The pointer to the MG_RWops object. * \param value The 32-bit integer in native endianness. * \return Returns 1 on success, else indicates an error. * * \sa MGUI_ReadLE32 */extern int MGUI_WriteBE32(MG_RWops *dst, Uint32 value);/** * \fn int MGUI_WriteLE64(MG_RWops *src, Uint64 value) * \brief Writes an 64-bit integer of native format to a MG_RWops object in littlen endianness. * * This function writes a 64-bit integer of native format to the data source * pointed to by \a src in littlen endiannes. * * \param src The pointer to the MG_RWops object. * \param value The 64-bit integer in native endianness. * \return Returns 1 on success, else indicates an error. * * \sa MGUI_ReadLE64 */extern int MGUI_WriteLE64(MG_RWops *dst, Uint64 value);/** * \fn int MGUI_WriteBE64(MG_RWops *src, Uint64 value) * \brief Writes an 64-bit integer of native format to a MG_RWops object in big endianness. * * This function writes a 64-bit integer of native format to the data source * pointed to by \a src in big endiannes. * * \param src The pointer to the MG_RWops object. * \param value The 64-bit integer in native endianness. * \return Returns 1 on success, else indicates an error. * * \sa MGUI_ReadLE64 */extern int MGUI_WriteBE64(MG_RWops *dst, Uint64 value);/** * \fn Uint16 MGUI_ReadLE16FP(FILE *src) * \brief Reads a 16-bit little endian integer from a stdio FILE object. * * This function reads a 16-bit little endian integer from the stdio FILE object * pointed to by \a src, and return it in native format. * * \param src The pointer to the stdio FILE object. * \return The integer in native endianness. * * \sa MGUI_WriteLE16FP, MGUI_ReadLE16 */extern Uint16 MGUI_ReadLE16FP(FILE *src);/** * \fn Uint32 MGUI_ReadLE32FP(FILE *src) * \brief Reads a 32-bit little endian integer from a stdio FILE object. * * This function reads a 32-bit little endian integer from the stdio FILE object * pointed to by \a src, and return it in native format. * * \param src The pointer to the stdio FILE object. * \return The integer in native endianness. * * \sa MGUI_WriteLE32FP, MGUI_ReadLE32 */extern Uint32 MGUI_ReadLE32FP(FILE *src);/** * \fn int MGUI_WriteLE16FP(FILE *dst, Uint16 value) * \brief Writes an 16-bit integer of native format to a stdio FILE object in littlen endianness. * * This function writes a 16-bit integer of native format to the stdio FILE object * pointed to by \a src in littlen endiannes. * * \param dst The pointer to the MG_RWops object. * \param value The 16-bit integer in native endianness. * \return Returns 1 on success, else indicates an error. * * \sa MGUI_ReadLE16FP, MGUI_WriteLE16 */extern int MGUI_WriteLE16FP(FILE *dst, Uint16 value);/** * \fn int MGUI_WriteLE32FP(FILE *dst, Uint32 value) * \brief Writes an 32-bit integer of native format to a stdio FILE object in littlen endianness. * * This function writes a 32-bit integer of native format to the stdio FILE object * pointed to by \a src in littlen endiannes. * * \param dst The pointer to the MG_RWops object. * \param value The 32-bit integer in native endianness. * \return Returns 1 on success, else indicates an error. * * \sa MGUI_ReadLE32FP, MGUI_WriteLE32 */extern int MGUI_WriteLE32FP(FILE *dst, Uint32 value);static inline Uint16 MGUI_ReadLE16Mem (const Uint8** data){#if 1 Uint16 h1, h2; h1 = *((*data)++); h2 = *((*data)++);#if MGUI_BYTEORDER == MGUI_LIL_ENDIAN return ((h2<<8)|h1);#else return ((h1<<8)|h2);#endif#else Uint16 u; memcpy (&u, *data, sizeof (Uint16)); u = ArchSwapLE16 (u); *data += sizeof (Uint16); return u;#endif}static inline Uint32 MGUI_ReadLE32Mem (const Uint8** data){#if 1 Uint32 q1, q2, q3, q4; q1 = *((*data)++); q2 = *((*data)++); q3 = *((*data)++); q4 = *((*data)++);#if MGUI_BYTEORDER == MGUI_LIL_ENDIAN return ((q4<<24)|(q3<<16)|(q2<<8)|(q1));#else return ((q1<<24)|(q2<<16)|(q3<<8)|(q4));#endif#else Uint32 u; memcpy (&u, *data, sizeof (Uint32)); u = ArchSwapLE32 (u); *data += sizeof (Uint32); return u;#endif}static inline Uint16 MGUI_ReadBE16Mem (const Uint8** data){#if 1 Uint16 h1, h2; h1 = *((*data)++); h2 = *((*data)++);#if MGUI_BYTEORDER == MGUI_LIL_ENDIAN return ((h1<<8)|h2);#else return ((h2<<8)|h1);#endif#else Uint16 u; memcpy (&u, *data, sizeof (Uint16)); u = ArchSwapBE16 (u); *data += sizeof (Uint16); return u;#endif}static inline Uint32 MGUI_ReadBE32Mem (const Uint8** data){#if 1 Uint32 q1, q2, q3, q4; q1 = *((*data)++); q2 = *((*data)++); q3 = *((*data)++); q4 = *((*data)++);#if MGUI_BYTEORDER == MGUI_LIL_ENDIAN return ((q1<<24)|(q2<<16)|(q3<<8)|(q4));#else return ((q4<<24)|(q3<<16)|(q2<<8)|(q1));#endif#else Uint32 u; memcpy (&u, *data, sizeof (Uint32)); u = ArchSwapBE32 (u); *data += sizeof (Uint32); return u;#endif} /** @} end of endian_rw_fns */ /** @} end of global_fns */ /** @} end of fns *//* Ends C function definitions when using C++ */#ifdef __cplusplus}#endif#endif /* _MGUI_ENDIAN_RW_H */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -