File: struct_bytewise.c

package info (click to toggle)
cbmc 5.10-5
  • links: PTS
  • area: main
  • in suites: buster
  • size: 73,416 kB
  • sloc: cpp: 264,330; ansic: 38,268; java: 19,025; python: 4,539; yacc: 4,275; makefile: 2,547; lex: 2,394; sh: 932; perl: 525; xml: 289; pascal: 169
file content (62 lines) | stat: -rw-r--r-- 1,296 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Determine endianness.
// Follows http://wiki.debian.org/ArchitectureSpecificsMemo

#if !defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)

#if defined(__avr32__) || defined(__hppa__)    || defined(__m68k__) || \
    defined(__mips__)  || defined(__powerpc__) || defined(__s390__) || \
    defined(__s390x__) || defined(__sparc__)

#define __BIG_ENDIAN__

#endif

#endif

typedef struct my_struct
{
  // We hope these are 32 bit each on all architectures,
  // and that there is no padding between them.
  unsigned a;
  unsigned b;
} t_logAppl;

static t_logAppl logAppl;
static unsigned char arrayTmp[8];

void CopyBuffer(unsigned char *src) {
  int i;
  for(i=0;i<8;i++){
    arrayTmp[i] = src[i];
  }
}

int main()
{
  logAppl.a=1;
  logAppl.b=0x01000002;
  CopyBuffer((unsigned char *)&logAppl);

  #ifdef __BIG_ENDIAN__
  assert(arrayTmp[0]==0);
  assert(arrayTmp[1]==0);
  assert(arrayTmp[2]==0);
  assert(arrayTmp[3]==1);

  assert(arrayTmp[4]==1);
  assert(arrayTmp[5]==0);
  assert(arrayTmp[6]==0);
  assert(arrayTmp[7]==2);
  #else
  // this is little endian
  assert(arrayTmp[0]==1);
  assert(arrayTmp[1]==0);
  assert(arrayTmp[2]==0);
  assert(arrayTmp[3]==0);

  assert(arrayTmp[4]==2);
  assert(arrayTmp[5]==0);
  assert(arrayTmp[6]==0);
  assert(arrayTmp[7]==1);
  #endif
}