1 | /* |
---|
2 | * libcucul Canvas for ultrafast compositing of Unicode letters |
---|
3 | * libcaca Colour ASCII-Art library |
---|
4 | * Copyright (c) 2006 Sam Hocevar <sam@zoy.org> |
---|
5 | * All Rights Reserved |
---|
6 | * |
---|
7 | * $Id: common.h 863 2006-04-24 20:56:07Z sam $ |
---|
8 | * |
---|
9 | * This library is free software; you can redistribute it and/or |
---|
10 | * modify it under the terms of the Do What The Fuck You Want To |
---|
11 | * Public License, Version 2, as published by Sam Hocevar. See |
---|
12 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
13 | */ |
---|
14 | |
---|
15 | /* |
---|
16 | * This file contains replacements for commonly found object types and |
---|
17 | * function prototypes that are sometimes missing. |
---|
18 | */ |
---|
19 | |
---|
20 | #if defined(HAVE_INTTYPES_H) |
---|
21 | # include <inttypes.h> |
---|
22 | #else |
---|
23 | typedef signed char int8_t; |
---|
24 | typedef signed short int16_t; |
---|
25 | typedef signed long int int32_t; |
---|
26 | |
---|
27 | typedef unsigned char uint8_t; |
---|
28 | typedef unsigned short uint16_t; |
---|
29 | typedef unsigned long int uint32_t; |
---|
30 | |
---|
31 | typedef long int intptr_t; |
---|
32 | typedef unsigned long int uintptr_t; |
---|
33 | #endif |
---|
34 | |
---|
35 | #if !defined(HAVE_HTONS) && !defined(HAVE_NETINET_IN_H) |
---|
36 | # if defined(HAVE_ENDIAN_H) |
---|
37 | # include <endian.h> |
---|
38 | # endif |
---|
39 | static inline uint16_t htons(uint16_t x) |
---|
40 | { |
---|
41 | #if defined(HAVE_ENDIAN_H) |
---|
42 | if(__BYTE_ORDER == __BIG_ENDIAN) |
---|
43 | #else |
---|
44 | /* This is compile-time optimised with at least -O1 or -Os */ |
---|
45 | uint32_t const dummy = 0x12345678; |
---|
46 | if(*(uint8_t const *)&dummy == 0x12) |
---|
47 | #endif |
---|
48 | return x; |
---|
49 | else |
---|
50 | return (x >> 8) | (x << 8); |
---|
51 | } |
---|
52 | |
---|
53 | static inline uint32_t htonl(uint32_t x) |
---|
54 | { |
---|
55 | #if defined(HAVE_ENDIAN_H) |
---|
56 | if(__BYTE_ORDER == __BIG_ENDIAN) |
---|
57 | #else |
---|
58 | /* This is compile-time optimised with at least -O1 or -Os */ |
---|
59 | uint32_t const dummy = 0x12345678; |
---|
60 | if(*(uint8_t const *)&dummy == 0x12) |
---|
61 | #endif |
---|
62 | return x; |
---|
63 | else |
---|
64 | return (x >> 24) | ((x >> 8) & 0x0000ff00) |
---|
65 | | ((x << 8) & 0x00ff0000) | (x << 24); |
---|
66 | } |
---|
67 | #endif |
---|
68 | |
---|