swap.c 1.23 KB
Newer Older
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1
2
3
4
#if defined (HAVE_CONFIG_H)
#  include "config.h"
#endif

Thomas Jahns's avatar
Thomas Jahns committed
5
#include <inttypes.h>
Uwe Schulzweida's avatar
Uwe Schulzweida committed
6
7
8
9
10
11
12
#include <stdio.h>

#include "error.h"
#include "binary.h"

void swap4byte(void *ptr, size_t size)
{
13
  int32_t *ptrtmp = (int32_t *)ptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
14

Thomas Jahns's avatar
Thomas Jahns committed
15
16
17
18
  if (sizeof (int32_t) == 4)
    for (size_t i = 0; i < size; ++i)
      ptrtmp[i] = (((ptrtmp[i] >> 24) & 0x00ff) | ((ptrtmp[i] & 0x00ff) << 24) |
                   ((ptrtmp[i] >>  8) & 0xff00) | ((ptrtmp[i] & 0xff00) <<  8));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
19
  else
Thomas Jahns's avatar
Thomas Jahns committed
20
    Error("not implemented for %d byte data", sizeof(int32_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
21
22
23
24
}

void swap8byte(void *ptr, size_t size)
{
25
  int64_t *ptrtmp = (int64_t *)ptr;
Thomas Jahns's avatar
Thomas Jahns committed
26
27
28
29
30
31
32

  if (sizeof (int64_t) == 8)
    for (size_t i = 0; i < size; ++i)
      ptrtmp[i] = (((ptrtmp[i] >> 56) & 0x000000ff) | ((ptrtmp[i] & 0x000000ff) << 56) |
                   ((ptrtmp[i] >> 40) & 0x0000ff00) | ((ptrtmp[i] & 0x0000ff00) << 40) |
                   ((ptrtmp[i] >> 24) & 0x00ff0000) | ((ptrtmp[i] & 0x00ff0000) << 24) |
                   ((ptrtmp[i] >>  8) & 0xff000000) | ((ptrtmp[i] & 0xff000000) <<  8));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
33
  else
Thomas Jahns's avatar
Thomas Jahns committed
34
    Error("not implemented for %d byte data", sizeof(int64_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
35
}
36
37
38
39
40
41
42
43
44
/*
 * Local Variables:
 * c-file-style: "Java"
 * c-basic-offset: 2
 * indent-tabs-mode: nil
 * show-trailing-whitespace: t
 * require-trailing-newline: t
 * End:
 */