1 /* @(#)intcvt.h 1.8 13/10/26 Copyright 1986-2013 J. Schilling */ 2 /* 3 * Definitions for conversion to/from integer data types of various size. 4 * 5 * Copyright (c) 1986-2013 J. Schilling 6 */ 7 /* 8 * The contents of this file are subject to the terms of the 9 * Common Development and Distribution License, Version 1.0 only 10 * (the "License"). You may not use this file except in compliance 11 * with the License. 12 * 13 * See the file CDDL.Schily.txt in this distribution for details. 14 * A copy of the CDDL is also available via the Internet at 15 * http://www.opensource.org/licenses/cddl1.txt 16 * 17 * When distributing Covered Code, include this CDDL HEADER in each 18 * file and include the License file CDDL.Schily.txt from this distribution. 19 */ 20 21 #ifndef _SCHILY_INTCVT_H 22 #define _SCHILY_INTCVT_H 23 24 #ifndef _SCHILY_MCONFIG_H 25 #include <schily/mconfig.h> 26 #endif 27 #ifndef _SCHILY_UTYPES_H 28 #include <schily/utypes.h> 29 #endif 30 31 #define i_to_2_byte(a, i) (((Uchar *)(a))[0] = ((i) >> 8) & 0xFF,\ 32 ((Uchar *)(a))[1] = (i) & 0xFF) 33 34 #define i_to_3_byte(a, i) (((Uchar *)(a))[0] = ((i) >> 16)& 0xFF,\ 35 ((Uchar *)(a))[1] = ((i) >> 8) & 0xFF,\ 36 ((Uchar *)(a))[2] = (i) & 0xFF) 37 38 #define i_to_4_byte(a, i) (((Uchar *)(a))[0] = ((i) >> 24)& 0xFF,\ 39 ((Uchar *)(a))[1] = ((i) >> 16)& 0xFF,\ 40 ((Uchar *)(a))[2] = ((i) >> 8) & 0xFF,\ 41 ((Uchar *)(a))[3] = (i) & 0xFF) 42 43 44 45 #define a_to_byte(a) (((Int8_t *)a)[0]) 46 47 #define a_to_u_byte(a) ((UInt8_t) \ 48 (((Uchar *)a)[0] & 0xFF)) 49 50 #define a_to_u_2_byte(a) ((UInt16_t) \ 51 ((((Uchar *)a)[1] & 0xFF) | \ 52 (((Uchar *)a)[0] << 8 & 0xFF00))) 53 54 #define a_to_2_byte(a) (int)(Int16_t)a_to_u_2_byte(a) 55 56 #define a_to_u_3_byte(a) ((Ulong) \ 57 ((((Uchar *)a)[2] & 0xFF) | \ 58 (((Uchar *)a)[1] << 8 & 0xFF00) | \ 59 (((Uchar *)a)[0] << 16 & 0xFF0000))) 60 61 #define a_to_3_byte(a) a_to_u_3_byte(a) /* XXX signed version? */ 62 63 #ifdef __STDC__ 64 # define __TOP_4BYTE 0xFF000000UL 65 #else 66 # define __TOP_4BYTE 0xFF000000 67 #endif 68 69 #define a_to_u_4_byte(a) ((Ulong) \ 70 ((((Uchar*)a)[3] & 0xFF) | \ 71 (((Uchar*)a)[2] << 8 & 0xFF00) | \ 72 (((Uchar*)a)[1] << 16 & 0xFF0000) | \ 73 (((Uchar*)a)[0] << 24 & __TOP_4BYTE))) 74 75 #define a_to_4_byte(a) (long)(Int32_t)a_to_u_4_byte(a) 76 77 /* 78 * Little Endian versions of above macros 79 */ 80 #define li_to_2_byte(a, i) (((Uchar *)(a))[1] = ((i) >> 8) & 0xFF,\ 81 ((Uchar *)(a))[0] = (i) & 0xFF) 82 83 #define li_to_3_byte(a, i) (((Uchar *)(a))[2] = ((i) >> 16)& 0xFF,\ 84 ((Uchar *)(a))[1] = ((i) >> 8) & 0xFF,\ 85 ((Uchar *)(a))[0] = (i) & 0xFF) 86 87 #define li_to_4_byte(a, i) (((Uchar *)(a))[3] = ((i) >> 24)& 0xFF,\ 88 ((Uchar *)(a))[2] = ((i) >> 16)& 0xFF,\ 89 ((Uchar *)(a))[1] = ((i) >> 8) & 0xFF,\ 90 ((Uchar *)(a))[0] = (i) & 0xFF) 91 92 93 #define la_to_u_2_byte(a) ((UInt16_t) \ 94 ((((Uchar*)a)[0] & 0xFF) | \ 95 (((Uchar*)a)[1] << 8 & 0xFF00))) 96 97 #define la_to_2_byte(a) (int)(Int16_t)la_to_u_2_byte(a) 98 99 #define la_to_u_3_byte(a) ((Ulong) \ 100 ((((Uchar*)a)[0] & 0xFF) | \ 101 (((Uchar*)a)[1] << 8 & 0xFF00) | \ 102 (((Uchar*)a)[2] << 16 & 0xFF0000))) 103 104 #define la_to_3_byte(a) la_to_u_3_byte(a) /* XXX signed version? */ 105 106 #define la_to_u_4_byte(a) ((Ulong) \ 107 ((((Uchar*)a)[0] & 0xFF) | \ 108 (((Uchar*)a)[1] << 8 & 0xFF00) | \ 109 (((Uchar*)a)[2] << 16 & 0xFF0000) | \ 110 (((Uchar*)a)[3] << 24 & __TOP_4BYTE))) 111 112 #define la_to_4_byte(a) (long)(Int32_t)la_to_u_4_byte(a) 113 114 #endif /* _SCHILY_INTCVT_H */ 115