/src/sudo/lib/util/roundup.c
Line | Count | Source |
1 | | /* |
2 | | * SPDX-License-Identifier: ISC |
3 | | * |
4 | | * Copyright (c) 2019-2020 Todd C. Miller <Todd.Miller@sudo.ws> |
5 | | * |
6 | | * Permission to use, copy, modify, and distribute this software for any |
7 | | * purpose with or without fee is hereby granted, provided that the above |
8 | | * copyright notice and this permission notice appear in all copies. |
9 | | * |
10 | | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
11 | | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
12 | | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
13 | | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
14 | | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
15 | | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
16 | | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
17 | | */ |
18 | | |
19 | | /* |
20 | | * This is an open source non-commercial project. Dear PVS-Studio, please check it. |
21 | | * PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com |
22 | | */ |
23 | | |
24 | | #include <config.h> |
25 | | |
26 | | #include "sudo_compat.h" |
27 | | #include "sudo_debug.h" |
28 | | #include "sudo_util.h" |
29 | | |
30 | | /* |
31 | | * Round 32-bit unsigned length to the next highest power of two. |
32 | | * Always returns at least 64. |
33 | | * Algorithm from bit twiddling hacks. |
34 | | */ |
35 | | unsigned int |
36 | | sudo_pow2_roundup_v1(unsigned int len) |
37 | 80 | { |
38 | 80 | if (len < 64) |
39 | 64 | return 64; |
40 | 16 | len--; |
41 | 16 | len |= len >> 1; |
42 | 16 | len |= len >> 2; |
43 | 16 | len |= len >> 4; |
44 | 16 | len |= len >> 8; |
45 | 16 | len |= len >> 16; |
46 | 16 | len++; |
47 | 16 | return len; |
48 | 80 | } |