1 /**
2 * XOR operations
3 * 
4 * Copyright:
5 * (C) 1999-2008 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.xor_buf;
12 
13 import botan.constants;
14 import botan.utils.types;
15 pure {
16     /**
17     * XOR arrays. Postcondition output[i] = input[i] ^ output[i] forall i = 0...length
18     * Params:
19     *  output = the input/output buffer
20     *  input = the read-only input buffer
21     *  length = the length of the buffers
22     */
23     void xorBuf(T)(T* output, const(T)* input, size_t length)
24     {
25         while (length >= 8)
26         {
27             output[0 .. 8] ^= input[0 .. 8];
28 
29             output += 8; input += 8; length -= 8;
30         }
31 
32         output[0 .. length] ^= input[0 .. length];
33     }
34 
35     /**
36     * XOR arrays. Postcondition output[i] = input[i] ^ in2[i] forall i = 0...length
37     * Params:
38     *  output = the output buffer
39     *  input = the first input buffer
40     *  input2 = the second output buffer
41     *  length = the length of the three buffers
42     */
43     void xorBuf(T)(T* output,
44                    const(T)* input,
45                    const(T)* input2,
46                    size_t length)
47     {
48         while (length >= 8)
49         {
50             output[0 .. 8] = input[0 .. 8] ^ input2[0 .. 8];
51 
52             input += 8; input2 += 8; output += 8; length -= 8;
53         }
54 
55         output[0 .. length] = input[0 .. length] ^ input2[0 .. length];
56     }
57 
58     version(none) {
59         static if (BOTAN_TARGET_UNALIGNED_MEMORY_ACCESS_OK) {
60 
61             void xorBuf(ubyte* output, const(ubyte)* input, size_t length)
62             {
63                 while (length >= 8)
64                 {
65                     *cast(ulong*)(output) ^= *cast(const ulong*)(input);
66                     output += 8; input += 8; length -= 8;
67                 }
68 
69                 output[0 .. length] ^= input[0 .. length];
70             }
71 
72             void xorBuf(ubyte* output,
73                          const(ubyte)* input,
74                          const(ubyte)* input2,
75                          size_t length)
76             {
77                 while (length >= 8)
78                 {
79                     *cast(ulong*)(output) = (*cast(const ulong*) input) ^ (*cast(const ulong*)input2);
80 
81                     input += 8; input2 += 8; output += 8; length -= 8;
82                 }
83 
84                 output[0 .. length] = input[0 .. length] ^ input2[0 .. length];
85             }
86 
87         }
88     }
89 }
90 void xorBuf(Alloc, Alloc2)(ref Vector!( ubyte, Alloc ) output,
91                            ref Vector!( ubyte, Alloc2 ) input,
92                            size_t n)
93 {
94     xorBuf(output.ptr, input.ptr, n);
95 }
96 
97 void xorBuf(Alloc)(ref Vector!( ubyte, Alloc ) output,
98                    const(ubyte)* input,
99                    size_t n)
100 {
101     xorBuf(output.ptr, input, n);
102 }
103 
104 void xorBuf(Alloc, Alloc2)(ref Vector!( ubyte, Alloc ) output,
105                            const(ubyte)* input,
106                            ref Vector!( ubyte, Alloc2 ) input2,
107                            size_t n)
108 {
109     xorBuf(output.ptr, input, input2.ptr, n);
110 }