physfs_byteorder.c
changeset 321 50986060bee8
parent 214 19846c18071b
child 497 40cc2b2f3a09
equal deleted inserted replaced
320:8e1d8cd29e9e 321:50986060bee8
    13 #endif
    13 #endif
    14 
    14 
    15 #include <stdio.h>
    15 #include <stdio.h>
    16 #include <stdlib.h>
    16 #include <stdlib.h>
    17 #include <assert.h>
    17 #include <assert.h>
    18 #include "physfs.h"
    18 
       
    19 #define __PHYSICSFS_INTERNAL__
       
    20 #include "physfs_internal.h"
    19 
    21 
    20 /* This byteorder stuff was lifted from SDL. http://www.libsdl.org/ */
    22 /* This byteorder stuff was lifted from SDL. http://www.libsdl.org/ */
    21 #define PHYSFS_LIL_ENDIAN  1234
    23 #define PHYSFS_LIL_ENDIAN  1234
    22 #define PHYSFS_BIG_ENDIAN  4321
    24 #define PHYSFS_BIG_ENDIAN  4321
    23 
    25 
   116 PHYSFS_sint32 PHYSFS_swapSBE32(PHYSFS_sint32 x) { return(x); }
   118 PHYSFS_sint32 PHYSFS_swapSBE32(PHYSFS_sint32 x) { return(x); }
   117 PHYSFS_uint64 PHYSFS_swapUBE64(PHYSFS_uint64 x) { return(x); }
   119 PHYSFS_uint64 PHYSFS_swapUBE64(PHYSFS_uint64 x) { return(x); }
   118 PHYSFS_sint64 PHYSFS_swapSBE64(PHYSFS_sint64 x) { return(x); }
   120 PHYSFS_sint64 PHYSFS_swapSBE64(PHYSFS_sint64 x) { return(x); }
   119 #endif
   121 #endif
   120 
   122 
       
   123 
       
   124 int PHYSFS_readSLE16(PHYSFS_file *file, PHYSFS_sint16 *val)
       
   125 {
       
   126     PHYSFS_sint16 in;
       
   127     BAIL_IF_MACRO(val == NULL, ERR_INVALID_ARGUMENT, 0);
       
   128     BAIL_IF_MACRO(PHYSFS_read(file, &in, sizeof (in), 1) != 1, NULL, 0);
       
   129     *val = PHYSFS_swapSLE16(in);
       
   130     return(1);
       
   131 } /* PHYSFS_readSLE16 */
       
   132 
       
   133 
       
   134 int PHYSFS_readULE16(PHYSFS_file *file, PHYSFS_uint16 *val)
       
   135 {
       
   136     PHYSFS_uint16 in;
       
   137     BAIL_IF_MACRO(val == NULL, ERR_INVALID_ARGUMENT, 0);
       
   138     BAIL_IF_MACRO(PHYSFS_read(file, &in, sizeof (in), 1) != 1, NULL, 0);
       
   139     *val = PHYSFS_swapULE16(in);
       
   140     return(1);
       
   141 } /* PHYSFS_readULE16 */
       
   142 
       
   143 
       
   144 int PHYSFS_readSBE16(PHYSFS_file *file, PHYSFS_sint16 *val)
       
   145 {
       
   146     PHYSFS_sint16 in;
       
   147     BAIL_IF_MACRO(val == NULL, ERR_INVALID_ARGUMENT, 0);
       
   148     BAIL_IF_MACRO(PHYSFS_read(file, &in, sizeof (in), 1) != 1, NULL, 0);
       
   149     *val = PHYSFS_swapSBE16(in);
       
   150     return(1);
       
   151 } /* PHYSFS_readSBE16 */
       
   152 
       
   153 
       
   154 int PHYSFS_readUBE16(PHYSFS_file *file, PHYSFS_uint16 *val)
       
   155 {
       
   156     PHYSFS_uint16 in;
       
   157     BAIL_IF_MACRO(val == NULL, ERR_INVALID_ARGUMENT, 0);
       
   158     BAIL_IF_MACRO(PHYSFS_read(file, &in, sizeof (in), 1) != 1, NULL, 0);
       
   159     *val = PHYSFS_swapUBE16(in);
       
   160     return(1);
       
   161 } /* PHYSFS_readUBE16 */
       
   162 
       
   163 
       
   164 int PHYSFS_readSLE32(PHYSFS_file *file, PHYSFS_sint32 *val)
       
   165 {
       
   166     PHYSFS_sint32 in;
       
   167     BAIL_IF_MACRO(val == NULL, ERR_INVALID_ARGUMENT, 0);
       
   168     BAIL_IF_MACRO(PHYSFS_read(file, &in, sizeof (in), 1) != 1, NULL, 0);
       
   169     *val = PHYSFS_swapSLE32(in);
       
   170     return(1);
       
   171 } /* PHYSFS_readSLE32 */
       
   172 
       
   173 
       
   174 int PHYSFS_readULE32(PHYSFS_file *file, PHYSFS_uint32 *val)
       
   175 {
       
   176     PHYSFS_uint32 in;
       
   177     BAIL_IF_MACRO(val == NULL, ERR_INVALID_ARGUMENT, 0);
       
   178     BAIL_IF_MACRO(PHYSFS_read(file, &in, sizeof (in), 1) != 1, NULL, 0);
       
   179     *val = PHYSFS_swapULE32(in);
       
   180     return(1);
       
   181 } /* PHYSFS_readULE32 */
       
   182 
       
   183 
       
   184 int PHYSFS_readSBE32(PHYSFS_file *file, PHYSFS_sint32 *val)
       
   185 {
       
   186     PHYSFS_sint32 in;
       
   187     BAIL_IF_MACRO(val == NULL, ERR_INVALID_ARGUMENT, 0);
       
   188     BAIL_IF_MACRO(PHYSFS_read(file, &in, sizeof (in), 1) != 1, NULL, 0);
       
   189     *val = PHYSFS_swapSBE32(in);
       
   190     return(1);
       
   191 } /* PHYSFS_readSBE32 */
       
   192 
       
   193 
       
   194 int PHYSFS_readUBE32(PHYSFS_file *file, PHYSFS_uint32 *val)
       
   195 {
       
   196     PHYSFS_uint32 in;
       
   197     BAIL_IF_MACRO(val == NULL, ERR_INVALID_ARGUMENT, 0);
       
   198     BAIL_IF_MACRO(PHYSFS_read(file, &in, sizeof (in), 1) != 1, NULL, 0);
       
   199     *val = PHYSFS_swapUBE32(in);
       
   200     return(1);
       
   201 } /* PHYSFS_readUBE32 */
       
   202 
       
   203 
       
   204 int PHYSFS_readSLE64(PHYSFS_file *file, PHYSFS_sint64 *val)
       
   205 {
       
   206     PHYSFS_sint64 in;
       
   207     BAIL_IF_MACRO(val == NULL, ERR_INVALID_ARGUMENT, 0);
       
   208     BAIL_IF_MACRO(PHYSFS_read(file, &in, sizeof (in), 1) != 1, NULL, 0);
       
   209     *val = PHYSFS_swapSLE64(in);
       
   210     return(1);
       
   211 } /* PHYSFS_readSLE64 */
       
   212 
       
   213 
       
   214 int PHYSFS_readULE64(PHYSFS_file *file, PHYSFS_uint64 *val)
       
   215 {
       
   216     PHYSFS_uint64 in;
       
   217     BAIL_IF_MACRO(val == NULL, ERR_INVALID_ARGUMENT, 0);
       
   218     BAIL_IF_MACRO(PHYSFS_read(file, &in, sizeof (in), 1) != 1, NULL, 0);
       
   219     *val = PHYSFS_swapULE64(in);
       
   220     return(1);
       
   221 } /* PHYSFS_readULE64 */
       
   222 
       
   223 
       
   224 int PHYSFS_readSBE64(PHYSFS_file *file, PHYSFS_sint64 *val)
       
   225 {
       
   226     PHYSFS_sint64 in;
       
   227     BAIL_IF_MACRO(val == NULL, ERR_INVALID_ARGUMENT, 0);
       
   228     BAIL_IF_MACRO(PHYSFS_read(file, &in, sizeof (in), 1) != 1, NULL, 0);
       
   229     *val = PHYSFS_swapSBE64(in);
       
   230     return(1);
       
   231 } /* PHYSFS_readSBE64 */
       
   232 
       
   233 
       
   234 int PHYSFS_readUBE64(PHYSFS_file *file, PHYSFS_uint64 *val)
       
   235 {
       
   236     PHYSFS_uint64 in;
       
   237     BAIL_IF_MACRO(val == NULL, ERR_INVALID_ARGUMENT, 0);
       
   238     BAIL_IF_MACRO(PHYSFS_read(file, &in, sizeof (in), 1) != 1, NULL, 0);
       
   239     *val = PHYSFS_swapUBE64(in);
       
   240     return(1);
       
   241 } /* PHYSFS_readUBE64 */
       
   242 
       
   243 
       
   244 
       
   245 int PHYSFS_writeSLE16(PHYSFS_file *file, PHYSFS_sint16 val)
       
   246 {
       
   247     PHYSFS_sint16 out = PHYSFS_swapSLE16(val);
       
   248     BAIL_IF_MACRO(PHYSFS_write(file, &out, sizeof (out), 1) != 1, NULL, 0);
       
   249     return(1);
       
   250 } /* PHYSFS_writeSLE16 */
       
   251 
       
   252 
       
   253 int PHYSFS_writeULE16(PHYSFS_file *file, PHYSFS_uint16 val)
       
   254 {
       
   255     PHYSFS_uint16 out = PHYSFS_swapULE16(val);
       
   256     BAIL_IF_MACRO(PHYSFS_write(file, &out, sizeof (out), 1) != 1, NULL, 0);
       
   257     return(1);
       
   258 } /* PHYSFS_writeULE16 */
       
   259 
       
   260 
       
   261 int PHYSFS_writeSBE16(PHYSFS_file *file, PHYSFS_sint16 val)
       
   262 {
       
   263     PHYSFS_sint16 out = PHYSFS_swapSBE16(val);
       
   264     BAIL_IF_MACRO(PHYSFS_write(file, &out, sizeof (out), 1) != 1, NULL, 0);
       
   265     return(1);
       
   266 } /* PHYSFS_writeSBE16 */
       
   267 
       
   268 
       
   269 int PHYSFS_writeUBE16(PHYSFS_file *file, PHYSFS_uint16 val)
       
   270 {
       
   271     PHYSFS_uint16 out = PHYSFS_swapUBE16(val);
       
   272     BAIL_IF_MACRO(PHYSFS_write(file, &out, sizeof (out), 1) != 1, NULL, 0);
       
   273     return(1);
       
   274 } /* PHYSFS_writeUBE16 */
       
   275 
       
   276 
       
   277 int PHYSFS_writeSLE32(PHYSFS_file *file, PHYSFS_sint32 val)
       
   278 {
       
   279     PHYSFS_sint32 out = PHYSFS_swapSLE32(val);
       
   280     BAIL_IF_MACRO(PHYSFS_write(file, &out, sizeof (out), 1) != 1, NULL, 0);
       
   281     return(1);
       
   282 } /* PHYSFS_writeSLE32 */
       
   283 
       
   284 
       
   285 int PHYSFS_writeULE32(PHYSFS_file *file, PHYSFS_uint32 val)
       
   286 {
       
   287     PHYSFS_uint32 out = PHYSFS_swapULE32(val);
       
   288     BAIL_IF_MACRO(PHYSFS_write(file, &out, sizeof (out), 1) != 1, NULL, 0);
       
   289     return(1);
       
   290 } /* PHYSFS_writeULE32 */
       
   291 
       
   292 
       
   293 int PHYSFS_writeSBE32(PHYSFS_file *file, PHYSFS_sint32 val)
       
   294 {
       
   295     PHYSFS_sint32 out = PHYSFS_swapSBE32(val);
       
   296     BAIL_IF_MACRO(PHYSFS_write(file, &out, sizeof (out), 1) != 1, NULL, 0);
       
   297     return(1);
       
   298 } /* PHYSFS_writeSBE32 */
       
   299 
       
   300 
       
   301 int PHYSFS_writeUBE32(PHYSFS_file *file, PHYSFS_uint32 val)
       
   302 {
       
   303     PHYSFS_uint32 out = PHYSFS_swapUBE32(val);
       
   304     BAIL_IF_MACRO(PHYSFS_write(file, &out, sizeof (out), 1) != 1, NULL, 0);
       
   305     return(1);
       
   306 } /* PHYSFS_writeUBE32 */
       
   307 
       
   308 
       
   309 int PHYSFS_writeSLE64(PHYSFS_file *file, PHYSFS_sint64 val)
       
   310 {
       
   311     PHYSFS_sint64 out = PHYSFS_swapSLE64(val);
       
   312     BAIL_IF_MACRO(PHYSFS_write(file, &out, sizeof (out), 1) != 1, NULL, 0);
       
   313     return(1);
       
   314 } /* PHYSFS_writeSLE64 */
       
   315 
       
   316 
       
   317 int PHYSFS_writeULE64(PHYSFS_file *file, PHYSFS_uint64 val)
       
   318 {
       
   319     PHYSFS_uint64 out = PHYSFS_swapULE64(val);
       
   320     BAIL_IF_MACRO(PHYSFS_write(file, &out, sizeof (out), 1) != 1, NULL, 0);
       
   321     return(1);
       
   322 } /* PHYSFS_writeULE64 */
       
   323 
       
   324 
       
   325 int PHYSFS_writeSBE64(PHYSFS_file *file, PHYSFS_sint64 val)
       
   326 {
       
   327     PHYSFS_sint64 out = PHYSFS_swapSBE64(val);
       
   328     BAIL_IF_MACRO(PHYSFS_write(file, &out, sizeof (out), 1) != 1, NULL, 0);
       
   329     return(1);
       
   330 } /* PHYSFS_writeSBE64 */
       
   331 
       
   332 
       
   333 int PHYSFS_writeUBE64(PHYSFS_file *file, PHYSFS_uint64 val)
       
   334 {
       
   335     PHYSFS_uint64 out = PHYSFS_swapUBE64(val);
       
   336     BAIL_IF_MACRO(PHYSFS_write(file, &out, sizeof (out), 1) != 1, NULL, 0);
       
   337     return(1);
       
   338 } /* PHYSFS_writeUBE64 */
       
   339 
   121 /* end of physfs_byteorder.c ... */
   340 /* end of physfs_byteorder.c ... */
   122 
   341