PageRenderTime 27ms CodeModel.GetById 10ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

/src/endian_conversion.h

http://ext3grep.googlecode.com/
C++ Header | 85 lines | 30 code | 12 blank | 43 comment | 0 complexity | 90b804bce1cf073b77a1a770dcd03f6d MD5 | raw file
 1// ext3grep -- An ext3 file system investigation and undelete tool
 2//
 3//! @file endian_conversion.h Endianess conversion functions.
 4//
 5// Copyright (C) 2008, by
 6// 
 7// Carlo Wood, Run on IRC <carlo@alinoe.com>
 8// RSA-1024 0x624ACAD5 1997-01-26                    Sign & Encrypt
 9// Fingerprint16 = 32 EC A7 B6 AC DB 65 A6  F6 F6 55 DD 1C DC FF 61
10// 
11// Stanislaw T. Findeisen <sf181257 at students mimuw edu pl>
12//
13// This program is free software: you can redistribute it and/or modify
14// it under the terms of the GNU General Public License as published by
15// the Free Software Foundation, either version 2 of the License, or
16// (at your option) any later version.
17// 
18// This program is distributed in the hope that it will be useful,
19// but WITHOUT ANY WARRANTY; without even the implied warranty of
20// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21// GNU General Public License for more details.
22// 
23// You should have received a copy of the GNU General Public License
24// along with this program.  If not, see <http://www.gnu.org/licenses/>.
25//
26// ChangeLog
27//
28// 2008-07-07  STF
29//     * (read_le32): Add. Read 32-bit little endian integers from a
30//       chain of bytes.
31//     * (__le16_to_cpu, __le32_to_cpu): Add. Convert little endian
32//       integers to native integers.
33
34#ifndef ENDIAN_CONVERSION_H
35#define ENDIAN_CONVERSION_H
36
37#ifndef USE_PCH
38#include <stdint.h>	// Needed for uint16_t and uint32_t
39#include "ext3.h"	// Needed for __u8
40#endif
41
42// We keep using these types (actually defined in kernel headers) for clarity (to know when something is little endian and when big endian on DISK).
43// ext3grep assumes uint16_t and uint32_t to be little endian (intel cpu).
44typedef uint16_t __le16;
45typedef uint16_t __be16;
46typedef uint32_t __le32;
47typedef uint32_t __be32;
48inline uint32_t __be32_to_cpu(__be32 x) { return x << 24 | x >> 24 | (x & (uint32_t)0x0000ff00UL) << 8 | (x & (uint32_t)0x00ff0000UL) >> 8; }
49inline uint16_t __be16_to_cpu(__be16 x) { return x << 8 | x >> 8; }
50
51// Convert Big Endian to Little Endian.
52inline __le32 be2le(__be32 v) { return __be32_to_cpu(v); }
53inline __le16 be2le(__be16 v) { return __be16_to_cpu(v); }
54inline __u8 be2le(__u8 v) { return v; }
55
56// Using the headers from e2fsprogs, the big endian journal structs
57// use normal types. However, since WE read raw data into them,
58// they are really still big endian. Calling be2le on those
59// types therefore still needs to do the conversion.
60inline __le32 be2le(__s32 const& v) { return be2le(*reinterpret_cast<__be32 const*>(&v)); }
61
62// ext3grep assumes uint16_t and uint32_t to be little endian (intel cpu).
63inline uint32_t __le32_to_cpu(__le32 x) { return x; }
64inline uint16_t __le16_to_cpu(__le16 x) { return x; }
65
66/**
67 * Reads __le32 from a string.
68 */
69inline __le32 read_le32(unsigned char* s)
70{
71  __le32 v = s[3];
72
73  v <<= 8;
74  v |= s[2];
75
76  v <<= 8;
77  v |= s[1];
78
79  v <<= 8;
80  v |= s[0];
81
82  return v;
83}
84
85#endif // ENDIAN_CONVERSION_H