Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
build
tags

# Rust and Cargo
Cargo.lock
target/

#IDE
.idea

# CMake
cmake-build-debug
6 changes: 6 additions & 0 deletions aws_crt_checksums_ws/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[workspace]
members = [
"aws-crt-checksums",
"aws-crt-checksums-sys",
]

13 changes: 13 additions & 0 deletions aws_crt_checksums_ws/aws-crt-checksums-sys/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "aws-crt-checksums-sys"
version = "0.1.0"
authors = ["AWS Common Runtime Team (aws-sdk-common-runtime-team@amazon.com)"]
edition = "2018"
links = "aws-checksums"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[build-dependencies]
aws-crt-c-flags = { git = "https://github.com/awslabs/aws-c-common", branch="move_to_rust_phase1" }

[dependencies]
aws-crt-common-sys = { git = "https://github.com/awslabs/aws-c-common", branch="move_to_rust_phase1" }
73 changes: 73 additions & 0 deletions aws_crt_checksums_ws/aws-crt-checksums-sys/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
use aws_crt_c_flags::{CRTModuleBuildInfo, HeaderType};
use std::path::Path;

fn main() {
let mut build_info = CRTModuleBuildInfo::new("aws-checksums");
build_info.module_links_dependency("aws-c-common");

let include_path = Path::new("../../include/aws");
build_info.include_dir(include_path, HeaderType::Public);

build_info
.file(Path::new("../../source/crc.c"))
.file(Path::new("../../source/crc_sw.c"));

// rustc seems to be wrong on this one.
#[allow(unused_mut)]
#[allow(unused_assignments)]
let mut impl_found = false;

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
{
if build_info.follows_msvc_semantics() {
build_info.file(Path::new(
"../../source/intel/visualc/visualc_crc32c_sse42.c",
));
} else {
build_info.file(Path::new("../../source/intel/asm/crc32c_sse42_asm.c"));
}
impl_found = true;
}

#[cfg(target_arch = "aarch64")]
{
build_info.file(Path::new("../../source/arm/crc32c_arm.c"));

if !build_info.follows_msvc_semantics() {
build_info.private_cflag("-march=armv8-a+crc");
}
impl_found = true;
}

#[cfg(all(target_arch = "arm"))]
{
if !build_info.follows_msvc_semantics() {
if build_info
.try_compile(
"#include <arm_acle.h>
int main() {
int crc = __crc32d(0, 1);
return 0;
}",
)
.is_ok()
{
build_info
.private_cflag("-march=armv8-a+crc")
.private_define("AWS_ARM32_CRC", "1");
build_info.file(Path::new("../../source/arm/crc32c_arm.c"));
impl_found = true;
}
}
}

if !impl_found {
build_info.file(Path::new("../../source/generic/crc32c_null.c"));
}

build_info.build();
}
13 changes: 13 additions & 0 deletions aws_crt_checksums_ws/aws-crt-checksums-sys/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/

/// This module is a rust translation of the relevant declarations from the aws-c-common
/// header files. They are used from higher-level wrapper modules.

#[allow(dead_code)]
extern "C" {
pub fn aws_checksums_crc32(input: *const u8, length: i32, previous_crc: u32) -> u32;
pub fn aws_checksums_crc32c(input: *const u8, length: i32, previous_crc: u32) -> u32;
}
11 changes: 11 additions & 0 deletions aws_crt_checksums_ws/aws-crt-checksums/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "aws-crt-checksums"
version = "0.1.0"
authors = ["AWS Common Runtime Team (aws-sdk-common-runtime-team@amazon.com)"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
aws-crt-checksums-sys = { path = "../aws-crt-checksums-sys" }
aws-crt-common = { git = "https://github.com/awslabs/aws-c-common", branch="move_to_rust_phase1" }
76 changes: 76 additions & 0 deletions aws_crt_checksums_ws/aws-crt-checksums/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
use aws_crt_checksums_sys::{aws_checksums_crc32, aws_checksums_crc32c};

#[derive(Clone)]
pub struct Crc32C {
running_checksum: u32,
}

#[derive(Clone)]
pub struct Crc32 {
running_checksum: u32,
}

/// Trait for computing running checksums.
pub trait Checksum {
type Output;
/// update the checksum with the checksum value of input
/// # Arguments
/// * `input` the input data to be appended to the running checksum.
fn update(&mut self, input: &Vec<u8>);
/// Return the current value of the running checksum.
fn checksum(&self) -> Self::Output;
}

impl Checksum for Crc32C {
type Output = u32;

fn update(&mut self, input: &Vec<u8>) {
unsafe {
self.running_checksum =
aws_checksums_crc32c(input.as_ptr(), input.len() as i32, self.running_checksum);
}
}

fn checksum(&self) -> u32 {
self.running_checksum
}
}

impl Checksum for Crc32 {
type Output = u32;

fn update(&mut self, input: &Vec<u8>) {
unsafe {
self.running_checksum =
aws_checksums_crc32(input.as_ptr(), input.len() as i32, self.running_checksum);
}
}

fn checksum(&self) -> u32 {
self.running_checksum
}
}

impl Crc32C {
/// Creates a new instance of the castagnoli CRC32c (iSCSI) checksum algorithm. Where supported
/// by the hardware, this implementation is hw accelerated.
pub fn new() -> Crc32C {
Crc32C {
running_checksum: 0,
}
}
}

impl Crc32 {
/// Creates a new instance of the CRC32 (Ethernet, gzip) algorithm. Where supported
/// by the hardware, this implementation is hw accelerated.
pub fn new() -> Crc32 {
Crc32 {
running_checksum: 0,
}
}
}
55 changes: 55 additions & 0 deletions aws_crt_checksums_ws/aws-crt-checksums/tests/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/

#[cfg(test)]
mod tests {
use aws_crt_checksums::{Checksum, Crc32, Crc32C};

#[test]
fn test_crc32_zeroes() {
let test_input: Vec<u8> = vec![0; 32];
let expected_crc = 0x190A55AD;

let mut crc_checksum = Crc32::new();
crc_checksum.update(&test_input);
assert_eq!(expected_crc, crc_checksum.checksum());
}

#[test]
fn test_known_crc32() {
let test_input: Vec<u8> = vec![
'1' as u8, '2' as u8, '3' as u8, '4' as u8, '5' as u8, '6' as u8, '7' as u8, '8' as u8,
'9' as u8,
];
let expected_crc = 0xCBF43926;

let mut crc_checksum = Crc32::new();
crc_checksum.update(&test_input);
assert_eq!(expected_crc, crc_checksum.checksum());
}

#[test]
fn test_crc32c_zeroes() {
let test_input: Vec<u8> = vec![0; 32];
let expected_crc = 0x8A9136AA;

let mut crc_checksum = Crc32C::new();
crc_checksum.update(&test_input);
assert_eq!(expected_crc, crc_checksum.checksum());
}

#[test]
fn test_known_crc32c() {
let test_input: Vec<u8> = vec![
'1' as u8, '2' as u8, '3' as u8, '4' as u8, '5' as u8, '6' as u8, '7' as u8, '8' as u8,
'9' as u8,
];
let expected_crc = 0xE3069283;

let mut crc_checksum = Crc32C::new();
crc_checksum.update(&test_input);
assert_eq!(expected_crc, crc_checksum.checksum());
}
}