1//========================================================================= 2// FILENAME : misc.c 3// DESCRIPTION : Miscelleneous funcs 4//========================================================================= 5// Copyright (c) 2008- NETGEAR, Inc. All Rights Reserved. 6//========================================================================= 7 8/* This program is free software; you can redistribute it and/or modify 9 * it under the terms of the GNU General Public License as published by 10 * the Free Software Foundation; either version 2 of the License, or 11 * (at your option) any later version. 12 * 13 * This program is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * GNU General Public License for more details. 17 * 18 * You should have received a copy of the GNU General Public License 19 * along with this program; if not, write to the Free Software 20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 21 */ 22 23#include <stdio.h> 24#include <string.h> 25#include <endian.h> 26 27#include "misc.h" 28 29inline __u16 30le16_to_cpu(__u16 le16) 31{ 32#if __BYTE_ORDER == __LITTLE_ENDIAN 33 return le16; 34#else 35 __u16 be16 = ((le16 << 8) & 0xff00) | ((le16 >> 8) & 0x00ff); 36 return be16; 37#endif 38} 39 40inline __u32 41le32_to_cpu(__u32 le32) 42{ 43#if __BYTE_ORDER == __LITTLE_ENDIAN 44 return le32; 45#else 46 __u32 be32 = 47 ((le32 << 24) & 0xff000000) | 48 ((le32 << 8) & 0x00ff0000) | 49 ((le32 >> 8) & 0x0000ff00) | 50 ((le32 >> 24) & 0x000000ff); 51 return be32; 52#endif 53} 54 55inline __u64 56le64_to_cpu(__u64 le64) 57{ 58#if __BYTE_ORDER == __LITTLE_ENDIAN 59 return le64; 60#else 61 __u64 be64; 62 __u8 *le64p = (__u8*)&le64; 63 __u8 *be64p = (__u8*)&be64; 64 be64p[0] = le64p[7]; 65 be64p[1] = le64p[6]; 66 be64p[2] = le64p[5]; 67 be64p[3] = le64p[4]; 68 be64p[4] = le64p[3]; 69 be64p[5] = le64p[2]; 70 be64p[6] = le64p[1]; 71 be64p[7] = le64p[0]; 72 return be64; 73#endif 74} 75 76inline __u8 77fget_byte(FILE *fp) 78{ 79 __u8 d; 80 81 (void)fread(&d, sizeof(d), 1, fp); 82 return d; 83} 84 85inline __u16 86fget_le16(FILE *fp) 87{ 88 __u16 d; 89 90 (void)fread(&d, sizeof(d), 1, fp); 91 d = le16_to_cpu(d); 92 return d; 93} 94 95inline __u32 96fget_le32(FILE *fp) 97{ 98 __u32 d; 99 100 (void)fread(&d, sizeof(d), 1, fp); 101 d = le32_to_cpu(d); 102 return d; 103} 104 105inline __u32 106cpu_to_be32(__u32 cpu32) 107{ 108#if __BYTE_ORDER == __LITTLE_ENDIAN 109 __u32 be32 = 110 ((cpu32 << 24) & 0xff000000) | 111 ((cpu32 << 8) & 0x00ff0000) | 112 ((cpu32 >> 8) & 0x0000ff00) | 113 ((cpu32 >> 24) & 0x000000ff); 114 return be32; 115#else 116 return cpu32; 117#endif 118} 119