Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
ccan
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
mirror
ccan
Commits
96d12822
Commit
96d12822
authored
Feb 25, 2009
by
Rusty Russell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Endianness module.
parent
39859e26
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
366 additions
and
0 deletions
+366
-0
ccan/endian/_info.c
ccan/endian/_info.c
+51
-0
ccan/endian/endian.h
ccan/endian/endian.h
+207
-0
ccan/endian/test/run.c
ccan/endian/test/run.c
+106
-0
config.h
config.h
+2
-0
No files found.
ccan/endian/_info.c
0 → 100644
View file @
96d12822
#include <stdio.h>
#include <string.h>
#include "config.h"
/**
* endian - endian conversion macros for simple types
*
* Portable protocols (such as on-disk formats, or network protocols)
* are often defined to be a particular endian: little-endian (least
* significant bytes first) or big-endian (most significant bytes
* first).
*
* Similarly, some CPUs lay out values in memory in little-endian
* order (most commonly, Intel's 8086 and derivatives), or big-endian
* order (almost everyone else).
*
* This module provides conversion routines, inspired by the linux kernel.
*
* Example:
* #include <stdio.h>
* #include <err.h>
* #include <ccan/endian/endian.h>
*
* //
* int main(int argc, char *argv[])
* {
* uint32_t value;
*
* if (argc != 2)
* errx(1, "Usage: %s <value>", argv[0]);
*
* printf("native: %08x\n", value);
* printf("little-endian: %08x\n", cpu_to_le32(value));
* printf("big-endian: %08x\n", cpu_to_be32(value));
* printf("byte-reversed: %08x\n", swab_u32(value));
* exit(0);
* }
*
* Licence: LGPL (2 or any later version)
*/
int
main
(
int
argc
,
char
*
argv
[])
{
if
(
argc
!=
2
)
return
1
;
if
(
strcmp
(
argv
[
1
],
"depends"
)
==
0
)
/* Nothing */
return
0
;
return
1
;
}
ccan/endian/endian.h
0 → 100644
View file @
96d12822
#ifndef CCAN_ENDIAN_H
#define CCAN_ENDIAN_H
#include <stdint.h>
#include "config.h"
/**
* swab_u16 - reverse bytes in a uint16_t value.
* @val: value whose bytes to swap.
*/
static
inline
uint16_t
swab_u16
(
uint16_t
val
)
{
return
((
val
&
(
uint16_t
)
0x00ffU
)
<<
8
)
|
((
val
&
(
uint16_t
)
0xff00U
)
>>
8
);
}
/**
* swab_u32 - reverse bytes in a uint32_t value.
* @val: value whose bytes to swap.
*/
static
inline
uint32_t
swab_u32
(
uint32_t
val
)
{
return
((
val
&
(
uint32_t
)
0x000000ffUL
)
<<
24
)
|
((
val
&
(
uint32_t
)
0x0000ff00UL
)
<<
8
)
|
((
val
&
(
uint32_t
)
0x00ff0000UL
)
>>
8
)
|
((
val
&
(
uint32_t
)
0xff000000UL
)
>>
24
);
}
/**
* swab_u64 - reverse bytes in a uint64_t value.
* @val: value whose bytes to swap.
*/
static
inline
uint64_t
swab_u64
(
uint64_t
val
)
{
return
((
val
&
(
uint64_t
)
0x00000000000000ffULL
)
<<
56
)
|
((
val
&
(
uint64_t
)
0x000000000000ff00ULL
)
<<
40
)
|
((
val
&
(
uint64_t
)
0x0000000000ff0000ULL
)
<<
24
)
|
((
val
&
(
uint64_t
)
0x00000000ff000000ULL
)
<<
8
)
|
((
val
&
(
uint64_t
)
0x000000ff00000000ULL
)
>>
8
)
|
((
val
&
(
uint64_t
)
0x0000ff0000000000ULL
)
>>
24
)
|
((
val
&
(
uint64_t
)
0x00ff000000000000ULL
)
>>
40
)
|
((
val
&
(
uint64_t
)
0xff00000000000000ULL
)
>>
56
);
}
/* Sanity check the defines. We don't handle weird endianness. */
#if !HAVE_LITTLE_ENDIAN && !HAVE_BIG_ENDIAN
#error "Unknown endian"
#elif HAVE_LITTLE_ENDIAN && HAVE_BIG_ENDIAN
#error "Can't compile for both big and little endian."
#endif
/**
* cpu_to_le64 - convert a uint64_t value to little-endian
* @native: value to convert
*/
static
inline
uint64_t
cpu_to_le64
(
uint64_t
native
)
{
#if HAVE_LITTLE_ENDIAN
return
native
;
#else
return
swab_u64
(
native
);
#endif
}
/**
* cpu_to_le32 - convert a uint32_t value to little-endian
* @native: value to convert
*/
static
inline
uint32_t
cpu_to_le32
(
uint32_t
native
)
{
#if HAVE_LITTLE_ENDIAN
return
native
;
#else
return
swab_u32
(
native
);
#endif
}
/**
* cpu_to_le16 - convert a uint16_t value to little-endian
* @native: value to convert
*/
static
inline
uint16_t
cpu_to_le16
(
uint16_t
native
)
{
#if HAVE_LITTLE_ENDIAN
return
native
;
#else
return
swab_u16
(
native
);
#endif
}
/**
* le64_to_cpu - convert a little-endian uint64_t value
* @le_val: little-endian value to convert
*/
static
inline
uint64_t
le64_to_cpu
(
uint64_t
le_val
)
{
#if HAVE_LITTLE_ENDIAN
return
le_val
;
#else
return
swab_u64
(
le_val
);
#endif
}
/**
* le32_to_cpu - convert a little-endian uint32_t value
* @le_val: little-endian value to convert
*/
static
inline
uint32_t
le32_to_cpu
(
uint32_t
le_val
)
{
#if HAVE_LITTLE_ENDIAN
return
le_val
;
#else
return
swab_u32
(
le_val
);
#endif
}
/**
* le16_to_cpu - convert a little-endian uint16_t value
* @le_val: little-endian value to convert
*/
static
inline
uint16_t
le16_to_cpu
(
uint16_t
le_val
)
{
#if HAVE_LITTLE_ENDIAN
return
le_val
;
#else
return
swab_u16
(
le_val
);
#endif
}
/**
* cpu_to_be64 - convert a uint64_t value to big endian.
* @native: value to convert
*/
static
inline
uint64_t
cpu_to_be64
(
uint64_t
native
)
{
#if HAVE_LITTLE_ENDIAN
return
swab_u64
(
native
);
#else
return
native
;
#endif
}
/**
* cpu_to_be32 - convert a uint32_t value to big endian.
* @native: value to convert
*/
static
inline
uint32_t
cpu_to_be32
(
uint32_t
native
)
{
#if HAVE_LITTLE_ENDIAN
return
swab_u32
(
native
);
#else
return
native
;
#endif
}
/**
* cpu_to_be16 - convert a uint16_t value to big endian.
* @native: value to convert
*/
static
inline
uint16_t
cpu_to_be16
(
uint16_t
native
)
{
#if HAVE_LITTLE_ENDIAN
return
swab_u16
(
native
);
#else
return
native
;
#endif
}
/**
* be64_to_cpu - convert a big-endian uint64_t value
* @be_val: big-endian value to convert
*/
static
inline
uint64_t
be64_to_cpu
(
uint64_t
be_val
)
{
#if HAVE_LITTLE_ENDIAN
return
swab_u64
(
be_val
);
#else
return
be_val
;
#endif
}
/**
* be32_to_cpu - convert a big-endian uint32_t value
* @be_val: big-endian value to convert
*/
static
inline
uint32_t
be32_to_cpu
(
uint32_t
be_val
)
{
#if HAVE_LITTLE_ENDIAN
return
swab_u32
(
be_val
);
#else
return
be_val
;
#endif
}
/**
* be16_to_cpu - convert a big-endian uint16_t value
* @be_val: big-endian value to convert
*/
static
inline
uint16_t
be16_to_cpu
(
uint16_t
be_val
)
{
#if HAVE_LITTLE_ENDIAN
return
swab_u16
(
be_val
);
#else
return
be_val
;
#endif
}
#endif
/* CCAN_ENDIAN_H */
ccan/endian/test/run.c
0 → 100644
View file @
96d12822
#include <stdlib.h>
#include <stddef.h>
#include <tap/tap.h>
#include "endian/endian.h"
int
main
(
int
argc
,
char
*
argv
[])
{
union
{
uint64_t
u64
;
unsigned
char
u64_bytes
[
8
];
}
u64
;
union
{
uint32_t
u32
;
unsigned
char
u32_bytes
[
4
];
}
u32
;
union
{
uint16_t
u16
;
unsigned
char
u16_bytes
[
2
];
}
u16
;
plan_tests
(
48
);
/* Straight swap tests. */
u64
.
u64_bytes
[
0
]
=
0x00
;
u64
.
u64_bytes
[
1
]
=
0x11
;
u64
.
u64_bytes
[
2
]
=
0x22
;
u64
.
u64_bytes
[
3
]
=
0x33
;
u64
.
u64_bytes
[
4
]
=
0x44
;
u64
.
u64_bytes
[
5
]
=
0x55
;
u64
.
u64_bytes
[
6
]
=
0x66
;
u64
.
u64_bytes
[
7
]
=
0x77
;
u64
.
u64
=
swab_u64
(
u64
.
u64
);
ok1
(
u64
.
u64_bytes
[
7
]
==
0x00
);
ok1
(
u64
.
u64_bytes
[
6
]
==
0x11
);
ok1
(
u64
.
u64_bytes
[
5
]
==
0x22
);
ok1
(
u64
.
u64_bytes
[
4
]
==
0x33
);
ok1
(
u64
.
u64_bytes
[
3
]
==
0x44
);
ok1
(
u64
.
u64_bytes
[
2
]
==
0x55
);
ok1
(
u64
.
u64_bytes
[
1
]
==
0x66
);
ok1
(
u64
.
u64_bytes
[
0
]
==
0x77
);
u32
.
u32_bytes
[
0
]
=
0x00
;
u32
.
u32_bytes
[
1
]
=
0x11
;
u32
.
u32_bytes
[
2
]
=
0x22
;
u32
.
u32_bytes
[
3
]
=
0x33
;
u32
.
u32
=
swab_u32
(
u32
.
u32
);
ok1
(
u32
.
u32_bytes
[
3
]
==
0x00
);
ok1
(
u32
.
u32_bytes
[
2
]
==
0x11
);
ok1
(
u32
.
u32_bytes
[
1
]
==
0x22
);
ok1
(
u32
.
u32_bytes
[
0
]
==
0x33
);
u16
.
u16_bytes
[
0
]
=
0x00
;
u16
.
u16_bytes
[
1
]
=
0x11
;
u16
.
u16
=
swab_u16
(
u16
.
u16
);
ok1
(
u16
.
u16_bytes
[
1
]
==
0x00
);
ok1
(
u16
.
u16_bytes
[
0
]
==
0x11
);
/* Endian tests. */
u64
.
u64
=
cpu_to_le64
(
0x0011223344556677ULL
);
ok1
(
u64
.
u64_bytes
[
0
]
==
0x77
);
ok1
(
u64
.
u64_bytes
[
1
]
==
0x66
);
ok1
(
u64
.
u64_bytes
[
2
]
==
0x55
);
ok1
(
u64
.
u64_bytes
[
3
]
==
0x44
);
ok1
(
u64
.
u64_bytes
[
4
]
==
0x33
);
ok1
(
u64
.
u64_bytes
[
5
]
==
0x22
);
ok1
(
u64
.
u64_bytes
[
6
]
==
0x11
);
ok1
(
u64
.
u64_bytes
[
7
]
==
0x00
);
ok1
(
le64_to_cpu
(
u64
.
u64
)
==
0x0011223344556677ULL
);
u64
.
u64
=
cpu_to_be64
(
0x0011223344556677ULL
);
ok1
(
u64
.
u64_bytes
[
7
]
==
0x77
);
ok1
(
u64
.
u64_bytes
[
6
]
==
0x66
);
ok1
(
u64
.
u64_bytes
[
5
]
==
0x55
);
ok1
(
u64
.
u64_bytes
[
4
]
==
0x44
);
ok1
(
u64
.
u64_bytes
[
3
]
==
0x33
);
ok1
(
u64
.
u64_bytes
[
2
]
==
0x22
);
ok1
(
u64
.
u64_bytes
[
1
]
==
0x11
);
ok1
(
u64
.
u64_bytes
[
0
]
==
0x00
);
ok1
(
be64_to_cpu
(
u64
.
u64
)
==
0x0011223344556677ULL
);
u32
.
u32
=
cpu_to_le32
(
0x00112233
);
ok1
(
u32
.
u32_bytes
[
0
]
==
0x33
);
ok1
(
u32
.
u32_bytes
[
1
]
==
0x22
);
ok1
(
u32
.
u32_bytes
[
2
]
==
0x11
);
ok1
(
u32
.
u32_bytes
[
3
]
==
0x00
);
ok1
(
le32_to_cpu
(
u32
.
u32
)
==
0x00112233
);
u32
.
u32
=
cpu_to_be32
(
0x00112233
);
ok1
(
u32
.
u32_bytes
[
3
]
==
0x33
);
ok1
(
u32
.
u32_bytes
[
2
]
==
0x22
);
ok1
(
u32
.
u32_bytes
[
1
]
==
0x11
);
ok1
(
u32
.
u32_bytes
[
0
]
==
0x00
);
ok1
(
be32_to_cpu
(
u32
.
u32
)
==
0x00112233
);
u16
.
u16
=
cpu_to_le16
(
0x0011
);
ok1
(
u16
.
u16_bytes
[
0
]
==
0x11
);
ok1
(
u16
.
u16_bytes
[
1
]
==
0x00
);
ok1
(
le16_to_cpu
(
u16
.
u16
)
==
0x0011
);
u16
.
u16
=
cpu_to_be16
(
0x0011
);
ok1
(
u16
.
u16_bytes
[
1
]
==
0x11
);
ok1
(
u16
.
u16_bytes
[
0
]
==
0x00
);
ok1
(
be16_to_cpu
(
u16
.
u16
)
==
0x0011
);
exit
(
exit_status
());
}
config.h
View file @
96d12822
...
...
@@ -6,3 +6,5 @@
#define HAVE_ATTRIBUTE_PRINTF 1
#define HAVE_BUILTIN_TYPES_COMPATIBLE_P 1
#define HAVE_BUILTIN_CHOOSE_EXPR 1
#define HAVE_LITTLE_ENDIAN 1
#define HAVE_BIG_ENDIAN 0
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment