aboutsummaryrefslogtreecommitdiff
path: root/refimpl/bvb_util.h
blob: 63c150b4bcb995dc3cb7c669b8c2961360de8768 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy,
 * modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#if !defined (BVB_INSIDE_BVB_REFIMPL_H) && !defined (BVB_REFIMPL_COMPILATION)
#error "Never include this file directly, include bvb_refimpl.h instead."
#endif

#ifndef BVB_UTIL_H_
#define BVB_UTIL_H_

#include "bvb_boot_image_header.h"
#include "bvb_sysdeps.h"

#ifdef __cplusplus
extern "C" {
#endif

/* Converts a 32-bit unsigned integer from big-endian to host byte order. */
uint32_t bvb_be32toh(uint32_t in);

/* Converts a 64-bit unsigned integer from big-endian to host byte order. */
uint64_t bvb_be64toh(uint64_t in);

/* Adds |value_to_add| to |value| with overflow protection.
 *
 * Returns zero if the addition overflows, non-zero otherwise. In
 * either case, |value| is always modified.
 */
int bvb_safe_add_to(uint64_t *value, uint64_t value_to_add);

/* Adds |a| and |b| with overflow protection, returning the value in
 * |out_result|.
 *
 * It's permissible to pass NULL for |out_result| if you just want to
 * check that the addition would not overflow.
 *
 * Returns zero if the addition overflows, non-zero otherwise.
 */
int bvb_safe_add(uint64_t *out_result, uint64_t a, uint64_t b);

/* Copies |src| to |dest|, byte-swapping fields in the process. */
void bvb_boot_image_header_to_host_byte_order(
    const BvbBootImageHeader* src,
    BvbBootImageHeader* dest);

/* Copies |header| to |dest|, byte-swapping fields in the process. */
void bvb_rsa_public_key_header_to_host_byte_order(
    const BvbRSAPublicKeyHeader* src,
    BvbRSAPublicKeyHeader* dest);

#ifdef __cplusplus
}
#endif

#endif  /* BVB_UTIL_H_ */