Symbian platform (C++)
SDK native APIs, runtime and tooling
Loading...
Searching...
No Matches
checksum.h
Go to the documentation of this file.
1#ifndef SYMBIAN_ANALYSIS_CHECKSUM_H_
2#define SYMBIAN_ANALYSIS_CHECKSUM_H_
3
4#include <array>
5#include <cstddef>
6#include <cstdint>
7#include <string_view>
8
10
11// Symbian CRC16: polynomial 0x1021, initial zero, no final complement.
12inline uint16_t Crc16(std::string_view bytes) {
13 uint16_t crc = 0;
14 for (const char value : bytes) {
15 const auto byte = static_cast<uint8_t>(value);
16 crc ^= static_cast<uint16_t>(byte << 8);
17 for (int bit = 0; bit < 8; ++bit) {
18 crc = static_cast<uint16_t>((crc << 1) ^ ((crc & 0x8000) ? 0x1021 : 0));
19 }
20 }
21 return crc;
22}
23
24// Callers supply at least twelve bytes containing three little-endian UIDs.
25inline uint32_t UidChecksum(std::string_view bytes) {
26 std::array<uint16_t, 2> crc{};
27 for (size_t i = 0; i < 12; ++i) {
28 uint16_t& value = crc[i % 2];
29 value ^= static_cast<uint16_t>(static_cast<uint8_t>(bytes[i]) << 8);
30 for (int bit = 0; bit < 8; ++bit) {
31 value =
32 static_cast<uint16_t>((value << 1) ^ ((value & 0x8000) ? 0x1021 : 0));
33 }
34 }
35 return (static_cast<uint32_t>(crc[1]) << 16) | crc[0];
36}
37
38} // namespace symbian::analysis::internal
39
40#endif // SYMBIAN_ANALYSIS_CHECKSUM_H_
Definition bytes.h:9
uint32_t UidChecksum(std::string_view bytes)
Definition checksum.h:25
uint16_t Crc16(std::string_view bytes)
Definition checksum.h:12
std::string_view bytes
Definition exports.cc:74
libusb_context * value
Definition usb.cc:35