vfs: check userland buffers before reading them.
[haiku.git] / headers / private / shared / binary-utils.h
blobf14a5f797e4c999606d3ae0f6c4cb1e80db87439
1 /*
2 * Copyright 2009, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Brecht Machiels, brecht@mos6581.org
7 */
9 #ifndef _BINARY_UTILS_H
10 #define _BINARY_UTILS_H
13 /* macro for a one-bit bitmask
16 #define BIT(n) (1ULL << n)
18 /* macro/templates to create bitmasks
21 #define BITMASK(high, low) ((unsigned long long)(BitMask<high, low>::value))
23 template<int h, int l>
24 struct BitMask
26 enum { value = (BitMask<h-l-1,0>::value +
27 (1ULL << h - l)) << l };
31 template<>
32 struct BitMask<-1,0>
34 enum { value = 0ULL };
38 /* macro/templates to enter binary constants
41 #define BINARY(binstring) ((unsigned long long)(Binary<0##binstring>::value))
43 // http://www.eptacom.net/pubblicazioni/pub_eng/binary.html
44 template<const unsigned long long N> struct Binary
46 enum { value = (N % 8ULL) + 2ULL * Binary<N / 8ULL>::value };
50 template<>
51 struct Binary<0>
53 enum { value = 0ULL } ;
57 /* macro/templates to determine offset
59 NOTE: broken for high bit indices
62 #define MASKOFFSET(mask) (MaskOffset<mask, (mask & 1UL)>::count)
64 template<const unsigned long mask, unsigned int firstBit>
65 struct MaskOffset
67 enum { count = MaskOffset<(mask >> 1), ((mask >> 1) & 1UL)>::count + 1 };
71 template<const unsigned long mask>
72 struct MaskOffset<mask, 1>
74 enum { count = 0 };
77 #endif // _BINARY_UTILS_H