virendian.h 3.9 KB
Newer Older
E
Eric Blake 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * virendian.h: aid for reading endian-specific data
 *
 * Copyright (C) 2013 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see
 * <http://www.gnu.org/licenses/>.
 *
 */

22
#pragma once
E
Eric Blake 已提交
23

24
#include "internal.h"
E
Eric Blake 已提交
25 26 27 28 29 30 31 32 33 34 35

/* The interfaces in this file are provided as macros for speed.  */

/**
 * virReadBufInt64BE:
 * @buf: byte to start reading at (can be 'char*' or 'unsigned char*');
 *       evaluating buf must not have any side effects
 *
 * Read 8 bytes at BUF as a big-endian 64-bit number.  Caller is
 * responsible to avoid reading beyond array bounds.
 */
36
#define virReadBufInt64BE(buf) \
37 38 39 40 41 42 43
    (((uint64_t)(uint8_t)((buf)[0]) << 56) | \
     ((uint64_t)(uint8_t)((buf)[1]) << 48) | \
     ((uint64_t)(uint8_t)((buf)[2]) << 40) | \
     ((uint64_t)(uint8_t)((buf)[3]) << 32) | \
     ((uint64_t)(uint8_t)((buf)[4]) << 24) | \
     ((uint64_t)(uint8_t)((buf)[5]) << 16) | \
     ((uint64_t)(uint8_t)((buf)[6]) << 8) | \
E
Eric Blake 已提交
44 45 46 47 48 49 50 51 52 53
     (uint64_t)(uint8_t)((buf)[7]))

/**
 * virReadBufInt64LE:
 * @buf: byte to start reading at (can be 'char*' or 'unsigned char*');
 *       evaluating buf must not have any side effects
 *
 * Read 8 bytes at BUF as a little-endian 64-bit number.  Caller is
 * responsible to avoid reading beyond array bounds.
 */
54
#define virReadBufInt64LE(buf) \
55 56 57 58 59 60 61
    ((uint64_t)(uint8_t)((buf)[0]) | \
     ((uint64_t)(uint8_t)((buf)[1]) << 8) | \
     ((uint64_t)(uint8_t)((buf)[2]) << 16) | \
     ((uint64_t)(uint8_t)((buf)[3]) << 24) | \
     ((uint64_t)(uint8_t)((buf)[4]) << 32) | \
     ((uint64_t)(uint8_t)((buf)[5]) << 40) | \
     ((uint64_t)(uint8_t)((buf)[6]) << 48) | \
E
Eric Blake 已提交
62 63 64 65 66 67 68 69 70 71
     ((uint64_t)(uint8_t)((buf)[7]) << 56))

/**
 * virReadBufInt32BE:
 * @buf: byte to start reading at (can be 'char*' or 'unsigned char*');
 *       evaluating buf must not have any side effects
 *
 * Read 4 bytes at BUF as a big-endian 32-bit number.  Caller is
 * responsible to avoid reading beyond array bounds.
 */
72
#define virReadBufInt32BE(buf) \
73 74 75
    (((uint32_t)(uint8_t)((buf)[0]) << 24) | \
     ((uint32_t)(uint8_t)((buf)[1]) << 16) | \
     ((uint32_t)(uint8_t)((buf)[2]) << 8) | \
E
Eric Blake 已提交
76 77 78 79 80 81 82 83 84 85
     (uint32_t)(uint8_t)((buf)[3]))

/**
 * virReadBufInt32LE:
 * @buf: byte to start reading at (can be 'char*' or 'unsigned char*');
 *       evaluating buf must not have any side effects
 *
 * Read 4 bytes at BUF as a little-endian 32-bit number.  Caller is
 * responsible to avoid reading beyond array bounds.
 */
86
#define virReadBufInt32LE(buf) \
87 88 89
    ((uint32_t)(uint8_t)((buf)[0]) | \
     ((uint32_t)(uint8_t)((buf)[1]) << 8) | \
     ((uint32_t)(uint8_t)((buf)[2]) << 16) | \
E
Eric Blake 已提交
90 91
     ((uint32_t)(uint8_t)((buf)[3]) << 24))

92 93 94 95 96 97 98 99
/**
 * virReadBufInt16BE:
 * @buf: byte to start reading at (can be 'char*' or 'unsigned char*');
 *       evaluating buf must not have any side effects
 *
 * Read 2 bytes at BUF as a big-endian 16-bit number.  Caller is
 * responsible to avoid reading beyond array bounds.
 */
100
#define virReadBufInt16BE(buf) \
101
    (((uint16_t)(uint8_t)((buf)[0]) << 8) | \
102 103 104 105 106 107 108 109 110 111
     (uint16_t)(uint8_t)((buf)[1]))

/**
 * virReadBufInt16LE:
 * @buf: byte to start reading at (can be 'char*' or 'unsigned char*');
 *       evaluating buf must not have any side effects
 *
 * Read 2 bytes at BUF as a little-endian 16-bit number.  Caller is
 * responsible to avoid reading beyond array bounds.
 */
112
#define virReadBufInt16LE(buf) \
113
    ((uint16_t)(uint8_t)((buf)[0]) | \
114
     ((uint16_t)(uint8_t)((buf)[1]) << 8))