Changeset 525


Ignore:
Timestamp:
Apr 22, 2011, 4:12:56 AM (7 years ago)
Author:
Sam Hocevar
Message:

core: replace all byte swapping macros with inline functions.

Location:
abuse/trunk/src
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • abuse/trunk/src/common.h

    r524 r525  
    1616//
    1717#include <stdint.h>
     18#include <stdio.h>
    1819
    1920//
     
    4041}
    4142
    42 static inline uint16_t Swap16(uint16_t x)
     43static inline uint16_t lstl(uint16_t x)
    4344{
    44     return ((uint16_t)x << 8 ) | ((uint16_t)x >> 8);
     45    if (BigEndian())
     46        return ((uint16_t)x << 8 ) | ((uint16_t)x >> 8);
     47    return x;
    4548}
    4649
    47 static inline uint32_t Swap32(uint32_t x)
     50static inline uint32_t lltl(uint32_t x)
    4851{
    49     return ((uint32_t)x >> 24) | (((uint32_t)x & 0x00ff0000) >> 8)
    50          | (((uint32_t)x & 0x0000ff00) << 8) | ((uint32_t)x << 24);
     52    if (BigEndian())
     53        return ((uint32_t)x >> 24) | (((uint32_t)x & 0x00ff0000) >> 8)
     54             | (((uint32_t)x & 0x0000ff00) << 8) | ((uint32_t)x << 24);
     55    return x;
    5156}
    5257
    53 #define uint16_to_intel(x) (BigEndian() ? Swap16((x)) : (x))
    54 #define uint32_to_intel(x) (BigEndian() ? Swap32((x)) : (x))
    55 #define big_uint16_to_local(x) (BigEndian() ? (x) : Swap16((x)))
    56 #define big_uint32_to_local(x) (BigEndian() ? (x) : Swap32((x)))
    57 #define uint16_to_local(x) (BigEndian() ? Swap16((x)) : (x))
    58 #define uint32_to_local(x) (BigEndian() ? Swap32((x)) : (x))
    59 
    60 #define bltl(x) big_uint32_to_local(x)
    61 #define bstl(x) big_uint16_to_local(x)
    62 #define lltl(x) uint32_to_intel(x)
    63 #define lstl(x) uint16_to_intel(x)
    64 
    65 #include <stdio.h>
    6658#define ERROR(x,st) { if (!(x)) \
    6759   { printf("Error on line %d of %s : %s\n", \
  • abuse/trunk/src/imlib/specs.cpp

    r524 r525  
    600600  uint16_t x;
    601601  read(&x,2);
    602   return uint16_to_local(x);
     602  return lstl(x);
    603603}
    604604
     
    608608  uint32_t x;
    609609  read(&x,4);
    610   return uint32_to_local(x);
     610  return lltl(x);
    611611}
    612612
     
    618618void bFILE::write_uint16(uint16_t x)
    619619{
    620   x=uint16_to_local(x);
     620  x=lstl(x);
    621621  write(&x,2);
    622622}
     
    625625void bFILE::write_uint32(uint32_t x)
    626626{
    627   x=uint32_to_local(x);
     627  x=lltl(x);
    628628  write(&x,4);
    629629}
     
    888888    if (fp->write(&flags,1)!=1)                     return 0;
    889889
    890     data_size=uint32_to_intel((*e)->size);
     890    data_size=lltl((*e)->size);
    891891    if (fp->write((char *)&data_size,4)!=4)              return 0;
    892     offset=uint32_to_intel((*e)->offset);
     892    offset=lltl((*e)->offset);
    893893    if (fp->write((char *)&offset,4)!=4)                  return 0;
    894894
     
    914914  uint16_t x;
    915915  fread(&x,1,2,fp);
    916   return uint16_to_local(x);
     916  return lstl(x);
    917917}
    918918
     
    921921  uint32_t x;
    922922  fread(&x,1,4,fp);
    923   return uint32_to_local(x);
     923  return lltl(x);
    924924}
    925925void write_uint16(FILE *fp, uint16_t x)
    926926{
    927   x=uint16_to_local(x);
     927  x=lstl(x);
    928928  fwrite(&x,1,2,fp);
    929929}
     
    931931void write_uint32(FILE *fp, uint32_t x)
    932932{
    933   x=uint32_to_local(x);
     933  x=lltl(x);
    934934  fwrite(&x,1,4,fp);
    935935}
     
    937937uint8_t read_uint8(FILE *fp) { return fgetc(fp)&0xff; }
    938938void write_uint8(FILE *fp, uint8_t x) { fputc((unsigned char)x,fp); }
    939 
    940 uint32_t read_other_uint32(FILE *fp)
    941 {
    942   uint32_t x;
    943   fread(&x,1,4,fp);
    944   return big_uint32_to_local(x);
    945 }
    946 
    947 uint16_t read_other_uint16(FILE *fp)
    948 {
    949   uint16_t x;
    950   fread(&x,1,2,fp);
    951   return big_uint16_to_local(x);
    952 }
    953 
    954 
    955 void write_other_uint16(FILE *fp, uint16_t x)
    956 {
    957   x=big_uint16_to_local(x);
    958   fwrite(&x,1,2,fp);
    959 }
    960 
    961 void write_other_int32(FILE *fp, uint32_t x)
    962 {
    963   x=big_uint32_to_local(x);
    964   fwrite(&x,1,4,fp);
    965 }
    966939
    967940void spec_directory::remove(spec_entry *e)
  • abuse/trunk/src/imlib/specs.h

    r524 r525  
    207207uint16_t read_uint16(FILE *fp);
    208208uint32_t read_uint32(FILE *fp);
    209 uint32_t read_other_uint32(FILE *fp);
    210 uint16_t read_other_uint16(FILE *fp);
    211209uint8_t read_uint8(FILE *fp);
    212210
    213211void write_uint16(FILE *fp, uint16_t x);
    214212void write_uint32(FILE *fp, uint32_t x);
    215 void write_other_uint16(FILE *fp, uint16_t x);
    216 void write_other_uint32(FILE *fp, uint32_t x);
    217213void write_uint8(FILE *fp, uint8_t x);
    218214
Note: See TracChangeset for help on using the changeset viewer.