1 /**
2 * Low Level Types
3 * 
4 * Copyright:
5 * (C) 1999-2007 Jack Lloyd
6 * (C) 2014-2015 Etienne Cimon
7 *
8 * License:
9 * Botan is released under the Simplified BSD License (see LICENSE.md)
10 */
11 module botan.utils.types;
12 
13 public import memutils.vector : Vector, Array, SecureVector, SecureArray;
14 public import memutils.utils;
15 public import memutils.refcounted;
16 public import memutils.unique;
17 public import botan.utils.exceptn;
18 public import std.typecons : scoped;
19 public import std.conv : to;
20 
21 alias Scoped(T) = typeof(scoped!T());
22 
23 /**
24 * The two possible directions for cipher filters, determining whether they
25 * actually perform encryption or decryption.
26 */
27 alias CipherDir = bool;
28 enum : CipherDir { ENCRYPTION, DECRYPTION }
29 
30 struct Pair(T, U) {
31     import std.typecons : Tuple;
32     Tuple!(T,U) m_obj;
33 
34     @property inout(T) first() inout {
35         return m_obj[0];
36     }
37 
38     @property inout(U) second() inout {
39         return m_obj[1];
40     }
41 
42     this(T a, U b) {
43         m_obj = Tuple!(T,U)(a, b);
44     }
45 
46     this(in T a, in U b) {
47         m_obj = Tuple!(T,U)(*cast(T*) &a,*cast(U*) &b);
48     }
49 
50     alias m_obj this;
51 }
52 
53 Pair!(T, U) makePair(T, U)(const T first, const U second)
54 {
55     return Pair!(UnConst!T, UnConst!U)(first, second);
56 }
57 
58 Pair!(T, U) makePair(T, U)(T first, U second)
59 {
60     return Pair!(T, U)(first, second);
61 }
62 
63 
64 private template UnConst(T) {
65     static if (is(T U == const(U))) {
66         alias UnConst = U;
67     } else static if (is(T V == immutable(V))) {
68         alias UnConst = V;
69     } else alias UnConst = T;
70 }
71 
72 
73 /**
74 * Existence check for values
75 */
76 bool valueExists(T, Alloc)(auto const ref Vector!(T, Alloc) vec, in T val)
77 {
78     for (size_t i = 0; i != vec.length; ++i)
79         if (vec[i] == val)
80             return true;
81     return false;
82 }