1 // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 #include "nvs_types.hpp"
15
16 #if defined(LINUX_TARGET)
17 #include "crc.h"
18 #else
19 #include <esp_rom_crc.h>
20 #endif
21
22 namespace nvs
23 {
calculateCrc32() const24 uint32_t Item::calculateCrc32() const
25 {
26 uint32_t result = 0xffffffff;
27 const uint8_t* p = reinterpret_cast<const uint8_t*>(this);
28 result = esp_rom_crc32_le(result, p + offsetof(Item, nsIndex),
29 offsetof(Item, crc32) - offsetof(Item, nsIndex));
30 result = esp_rom_crc32_le(result, p + offsetof(Item, key), sizeof(key));
31 result = esp_rom_crc32_le(result, p + offsetof(Item, data), sizeof(data));
32 return result;
33 }
34
calculateCrc32WithoutValue() const35 uint32_t Item::calculateCrc32WithoutValue() const
36 {
37 uint32_t result = 0xffffffff;
38 const uint8_t* p = reinterpret_cast<const uint8_t*>(this);
39 result = esp_rom_crc32_le(result, p + offsetof(Item, nsIndex),
40 offsetof(Item, datatype) - offsetof(Item, nsIndex));
41 result = esp_rom_crc32_le(result, p + offsetof(Item, key), sizeof(key));
42 result = esp_rom_crc32_le(result, p + offsetof(Item, chunkIndex), sizeof(chunkIndex));
43 return result;
44 }
45
calculateCrc32(const uint8_t * data,size_t size)46 uint32_t Item::calculateCrc32(const uint8_t* data, size_t size)
47 {
48 uint32_t result = 0xffffffff;
49 result = esp_rom_crc32_le(result, data, size);
50 return result;
51 }
52
53 } // namespace nvs
54