Commit 5dff6bcc authored by Etienne Pierre-Doray's avatar Etienne Pierre-Doray Committed by Commit Bot

[Zucchini] crc32 implementation and unittests.

This introduces a single function CalculateCrc32, adapted from LZMA SDK, with stl-like interface.

Bug: 729154
Change-Id: Ie15fb95b143e92b8e0a1650386ef01b7b2de5401
Reviewed-on: https://chromium-review.googlesource.com/563876
Commit-Queue: Etienne Pierre-Doray <etiennep@chromium.org>
Reviewed-by: default avatarErik Chen <erikchen@chromium.org>
Reviewed-by: default avatarSamuel Huang <huangs@chromium.org>
Cr-Commit-Position: refs/heads/master@{#485476}
parent 319fe68b
......@@ -8,6 +8,8 @@ import("//testing/test.gni")
static_library("zucchini_lib") {
sources = [
"buffer_view.h",
"crc32.cc",
"crc32.h",
"disassembler.cc",
"disassembler.h",
"image_utils.h",
......@@ -41,6 +43,7 @@ if (is_win) {
test("zucchini_unittests") {
sources = [
"buffer_view_unittest.cc",
"crc32_unittest.cc",
"typed_value_unittest.cc",
]
......
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/installer/zucchini/crc32.h"
#include <array>
#include "base/logging.h"
namespace zucchini {
namespace {
std::array<uint32_t, 256> MakeCrc32Table() {
constexpr uint32_t kCrc32Poly = 0xEDB88320;
std::array<uint32_t, 256> crc32Table;
for (uint32_t i = 0; i < 256; ++i) {
uint32_t r = i;
for (int j = 0; j < 8; ++j)
r = (r >> 1) ^ (kCrc32Poly & ~((r & 1) - 1));
crc32Table[i] = r;
}
return crc32Table;
}
} // namespace
// Minimalistic CRC-32 implementation for Zucchini usage. Adapted from LZMA SDK
// (found at third_party/lzma_sdk/7zCrc.c), which is public domain.
uint32_t CalculateCrc32(const uint8_t* first, const uint8_t* last) {
DCHECK_GE(last, first);
static const std::array<uint32_t, 256> kCrc32Table = MakeCrc32Table();
uint32_t ret = 0xFFFFFFFF;
for (; first != last; ++first)
ret = kCrc32Table[(ret ^ *first) & 0xFF] ^ (ret >> 8);
return ret ^ 0xFFFFFFFF;
}
} // namespace zucchini
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_INSTALLER_ZUCCHINI_CRC32_H_
#define CHROME_INSTALLER_ZUCCHINI_CRC32_H_
#include <cstdint>
namespace zucchini {
// Calculates CRC-32 of the given range [|first|, |last|).
uint32_t CalculateCrc32(const uint8_t* first, const uint8_t* last);
} // namespace zucchini
#endif // CHROME_INSTALLER_ZUCCHINI_CRC32_H_
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/installer/zucchini/crc32.h"
#include <cstdint>
#include <iterator>
#include "base/test/gtest_util.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace zucchini {
constexpr uint8_t bytes[] = {0x10, 0x32, 0x54, 0x76, 0x98,
0xBA, 0xDC, 0xFE, 0x10, 0x00};
TEST(Crc32Test, All) {
// Results can be verified with any CRC-32 calculator found online.
// Empty region.
EXPECT_EQ(0x00000000U, CalculateCrc32(std::begin(bytes), std::begin(bytes)));
// Single byte.
EXPECT_EQ(0xCFB5FFE9U,
CalculateCrc32(std::begin(bytes), std::begin(bytes) + 1));
// Same byte (0x10) appearing at different location.
EXPECT_EQ(0xCFB5FFE9U,
CalculateCrc32(std::begin(bytes) + 8, std::begin(bytes) + 9));
// Single byte of 0.
EXPECT_EQ(0xD202EF8DU,
CalculateCrc32(std::begin(bytes) + 9, std::end(bytes)));
// Whole region.
EXPECT_EQ(0xA86FD7D6U, CalculateCrc32(std::begin(bytes), std::end(bytes)));
// Whole region excluding 0 at end.
EXPECT_EQ(0x0762F38BU,
CalculateCrc32(std::begin(bytes), std::begin(bytes) + 9));
EXPECT_DCHECK_DEATH(CalculateCrc32(std::begin(bytes) + 1, std::begin(bytes)));
}
} // namespace zucchini
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment