aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/Array.cpp
blob: 43e786342185b01627b69b93492d35256d106278 (plain) (blame)
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
/**
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0.
 */

#include <aws/core/utils/Array.h>

#include <aws/core/platform/Security.h>

namespace Aws
{
    namespace Utils
    {
            Array<CryptoBuffer> CryptoBuffer::Slice(size_t sizeOfSlice) const
            {
                assert(sizeOfSlice <= GetLength());

                size_t numberOfSlices = (GetLength() + sizeOfSlice - 1) / sizeOfSlice;
                size_t currentSliceIndex = 0;
                Array<CryptoBuffer> slices(numberOfSlices);

                for (size_t i = 0; i < numberOfSlices - 1; ++i)
                {
                    CryptoBuffer newArray(sizeOfSlice);
                    for (size_t cpyIdx = 0; cpyIdx < newArray.GetLength(); ++cpyIdx)
                    {
                        newArray[cpyIdx] = GetItem(cpyIdx + currentSliceIndex);
                    }
                    currentSliceIndex += sizeOfSlice;
                    slices[i] = std::move(newArray);
                }

                CryptoBuffer lastArray(GetLength() % sizeOfSlice == 0 ? sizeOfSlice : GetLength() % sizeOfSlice );
                for (size_t cpyIdx = 0; cpyIdx < lastArray.GetLength(); ++cpyIdx)
                {
                    lastArray[cpyIdx] = GetItem(cpyIdx + currentSliceIndex);
                }
                slices[slices.GetLength() - 1] = std::move(lastArray);

                return slices;
            }            

            CryptoBuffer& CryptoBuffer::operator^(const CryptoBuffer& operand)
            {
                size_t smallestSize = std::min<size_t>(GetLength(), operand.GetLength());
                for (size_t i = 0; i < smallestSize; ++i)
                {
                    (*this)[i] ^= operand[i];
                }

                return *this;
            }

            /**
            * Zero out the array securely
            */
            void CryptoBuffer::Zero()
            {
                if (GetUnderlyingData())
                {
                    Aws::Security::SecureMemClear(GetUnderlyingData(), GetLength());
                }
            }
    }
}