Add 6th lab

master
Yury Kurlykov 2020-06-10 20:30:09 +10:00
parent ea3de372a4
commit e69a88c336
Signed by: t1meshift
GPG Key ID: B133F3167ABF94D8
10 changed files with 544 additions and 0 deletions

View File

@ -17,4 +17,5 @@ define_lab(lab2)
define_lab(lab3)
define_lab(lab4)
define_lab(lab5)
define_lab(lab6)
define_lab(lab7)

77
lab6/.execme Executable file
View File

@ -0,0 +1,77 @@
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
function assert() {
local checksum_prg="./lab6_check-$1.c_run"
local input=$2
local expected=$3
local got
echo "Testing $1 on string \`"$input"'..."
got=$($checksum_prg -q <(printf "%s" "$input"))
if diff -q <(echo "$got") <(echo "$expected") > /dev/null; then
echo "OK!"
else
echo "Assertion failed!"
echo "Expected: $expected"
echo "Got: $got"
fi
}
function run_bench() {
local checksum_prg="./lab6_check-$1.c_run"
echo "Benchmarking $1 with file $2..."
$checksum_prg -b "$2"
}
pushd "$1" > /dev/null
# Checked with https://www.scadacore.com/tools/programming-calculators/online-checksum-calculator/
assert xor "123456789" "0x31"
assert xor "444444444444444444444444444" "0x34"
assert xor "4444444444444444444444444444" "0x00"
assert xor "444444444444444444444444444\n" "0x06"
assert xor "Test string" "0x03"
assert xor "Try not to laugh1!%&<>z" "0x0d"
# Checked with https://gchq.github.io/CyberChef/#recipe=Fletcher-16_Checksum()
assert fletcher "44" "0x9c68"
assert fletcher "Test string" "0x6e5b"
assert fletcher "4444444444444444444444444444" "0xcab5"
assert fletcher "444444444444444444444444444\n" "0x3f4c"
assert fletcher "Try not to laugh1!%&<>z" "0x137c"
assert fletcher "123456789" "0x1ede"
# Checked with https://emn178.github.io/online-tools/crc16.html
assert crc "123456789" "0xbb3d"
assert crc "4444444444444444444444444444" "0x5d9d"
assert crc "Test string" "0x5e4c"
assert crc "Try not to laugh1!%&<>z" "0x6579"
assert crc "tEsTTa" "0x0763"
# Big file tests
head -c 128M </dev/urandom >bigfile.bin
run_bench xor bigfile.bin
run_bench fletcher bigfile.bin
run_bench crc bigfile.bin
echo "Create & check checksum..."
head -c 16385 <(yes 4) > testfile.bin
head -c 32766 <(yes KRIPER2004) >> testfile.bin
./lab6_create-csum.c_run testfile.bin testfile.bin.crc8maxim
echo "This should be OK..."
./lab6_check-csum.c_run testfile.bin testfile.bin.crc8maxim
echo "Spoiling the file..."
sed 's/4KRIP/HELLO/' testfile.bin > testfile_broken.bin
echo "This should NOT be OK..."
./lab6_check-csum.c_run testfile_broken.bin testfile.bin.crc8maxim
popd > /dev/null

38
lab6/CMakeLists.txt Normal file
View File

@ -0,0 +1,38 @@
cmake_minimum_required(VERSION 3.16)
set(CMAKE_C_STANDARD 11)
# Lab name
set(LAB_NAME "lab6")
# Lab tasks
list(APPEND SOURCE_FILES
check-xor.c
check-fletcher.c
check-crc.c
create-csum.c
check-csum.c
)
list(APPEND NON_COMPILABLE_SRC
.execme
)
### Here goes the template
project("${LAB_NAME}" C)
add_custom_target("${LAB_NAME}")
foreach (file IN LISTS SOURCE_FILES)
add_executable("${LAB_NAME}_${file}_run" "${file}")
add_dependencies("${LAB_NAME}" "${LAB_NAME}_${file}_run")
endforeach ()
foreach (file IN LISTS NON_COMPILABLE_SRC)
add_custom_command(
TARGET "${LAB_NAME}" POST_BUILD
DEPENDS "${file}"
COMMAND ${CMAKE_COMMAND} -E copy
"${CMAKE_CURRENT_SOURCE_DIR}/${file}"
"${CMAKE_CURRENT_BINARY_DIR}/${file}"
)
endforeach ()

3
lab6/README.md Normal file
View File

@ -0,0 +1,3 @@
# Лабораторная работа №6
## Задание 1

109
lab6/check-crc.c Normal file
View File

@ -0,0 +1,109 @@
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#define USAGE_STRING "Usage: %s [-q] [-b] [--] [file [file...]]\n"
#define MAX(a,b) ((a)>(b)?(a):(b))
#define STR_EQ(a, b) (!strcmp((a),(b)))
#define FATAL(fmt, ...) do {\
fprintf(stderr,fmt,##__VA_ARGS__);\
exit(1);\
} while (0)
#define WARNING(fmt, ...) fprintf(stderr,fmt,##__VA_ARGS__)
static const uint16_t crc16_tab[256] = {
0x0000, 0xc0c1, 0xc181, 0x0140, 0xc301, 0x03c0, 0x0280, 0xc241,
0xc601, 0x06c0, 0x0780, 0xc741, 0x0500, 0xc5c1, 0xc481, 0x0440,
0xcc01, 0x0cc0, 0x0d80, 0xcd41, 0x0f00, 0xcfc1, 0xce81, 0x0e40,
0x0a00, 0xcac1, 0xcb81, 0x0b40, 0xc901, 0x09c0, 0x0880, 0xc841,
0xd801, 0x18c0, 0x1980, 0xd941, 0x1b00, 0xdbc1, 0xda81, 0x1a40,
0x1e00, 0xdec1, 0xdf81, 0x1f40, 0xdd01, 0x1dc0, 0x1c80, 0xdc41,
0x1400, 0xd4c1, 0xd581, 0x1540, 0xd701, 0x17c0, 0x1680, 0xd641,
0xd201, 0x12c0, 0x1380, 0xd341, 0x1100, 0xd1c1, 0xd081, 0x1040,
0xf001, 0x30c0, 0x3180, 0xf141, 0x3300, 0xf3c1, 0xf281, 0x3240,
0x3600, 0xf6c1, 0xf781, 0x3740, 0xf501, 0x35c0, 0x3480, 0xf441,
0x3c00, 0xfcc1, 0xfd81, 0x3d40, 0xff01, 0x3fc0, 0x3e80, 0xfe41,
0xfa01, 0x3ac0, 0x3b80, 0xfb41, 0x3900, 0xf9c1, 0xf881, 0x3840,
0x2800, 0xe8c1, 0xe981, 0x2940, 0xeb01, 0x2bc0, 0x2a80, 0xea41,
0xee01, 0x2ec0, 0x2f80, 0xef41, 0x2d00, 0xedc1, 0xec81, 0x2c40,
0xe401, 0x24c0, 0x2580, 0xe541, 0x2700, 0xe7c1, 0xe681, 0x2640,
0x2200, 0xe2c1, 0xe381, 0x2340, 0xe101, 0x21c0, 0x2080, 0xe041,
0xa001, 0x60c0, 0x6180, 0xa141, 0x6300, 0xa3c1, 0xa281, 0x6240,
0x6600, 0xa6c1, 0xa781, 0x6740, 0xa501, 0x65c0, 0x6480, 0xa441,
0x6c00, 0xacc1, 0xad81, 0x6d40, 0xaf01, 0x6fc0, 0x6e80, 0xae41,
0xaa01, 0x6ac0, 0x6b80, 0xab41, 0x6900, 0xa9c1, 0xa881, 0x6840,
0x7800, 0xb8c1, 0xb981, 0x7940, 0xbb01, 0x7bc0, 0x7a80, 0xba41,
0xbe01, 0x7ec0, 0x7f80, 0xbf41, 0x7d00, 0xbdc1, 0xbc81, 0x7c40,
0xb401, 0x74c0, 0x7580, 0xb541, 0x7700, 0xb7c1, 0xb681, 0x7640,
0x7200, 0xb2c1, 0xb381, 0x7340, 0xb101, 0x71c0, 0x7080, 0xb041,
0x5000, 0x90c1, 0x9181, 0x5140, 0x9301, 0x53c0, 0x5280, 0x9241,
0x9601, 0x56c0, 0x5780, 0x9741, 0x5500, 0x95c1, 0x9481, 0x5440,
0x9c01, 0x5cc0, 0x5d80, 0x9d41, 0x5f00, 0x9fc1, 0x9e81, 0x5e40,
0x5a00, 0x9ac1, 0x9b81, 0x5b40, 0x9901, 0x59c0, 0x5880, 0x9841,
0x8801, 0x48c0, 0x4980, 0x8941, 0x4b00, 0x8bc1, 0x8a81, 0x4a40,
0x4e00, 0x8ec1, 0x8f81, 0x4f40, 0x8d01, 0x4dc0, 0x4c80, 0x8c41,
0x4400, 0x84c1, 0x8581, 0x4540, 0x8701, 0x47c0, 0x4680, 0x8641,
0x8201, 0x42c0, 0x4380, 0x8341, 0x4100, 0x81c1, 0x8081, 0x4040
};
bool flag_end = false;
bool quiet_mode = false;
bool benchmark = false;
int main(int argc, char *argv[]) {
if (argc == 1) {
FATAL(USAGE_STRING, argv[0]);
}
for (int i = 1; i < argc; ++i) {
if (!flag_end) {
if (argv[i][0] != '-') {
flag_end = true;
}
else if (STR_EQ(argv[i], "--")) {
flag_end = true;
continue;
} else {
switch (argv[i][1]) {
case 'q':
quiet_mode = true;
break;
case 'b':
benchmark = true;
break;
default:
FATAL(USAGE_STRING, argv[0]);
}
continue;
}
}
FILE *f = fopen(argv[i], "rb");
if (f == NULL) {
perror("fopen()");
FATAL("Could not open %s\n", argv[i]);
}
struct timeval t0, t1;
int c;
uint16_t s = 0;
gettimeofday(&t0, NULL);
while ((c = fgetc(f)) != EOF) {
s = crc16_tab[(s ^ (uint8_t) c) & 0xFF] ^ (s >> 8);
}
gettimeofday(&t1, NULL);
if (quiet_mode) {
printf("0x%04x\n", s);
} else {
printf("%s: checksum 0x%04x\n", argv[i], s);
}
if (benchmark) {
struct timeval result;
timersub(&t1, &t0, &result);
printf("CRC execution time: %ld.%06lds\n", result.tv_sec, result.tv_usec);
}
fclose(f);
}
}

68
lab6/check-csum.c Normal file
View File

@ -0,0 +1,68 @@
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "crc8.h"
#define USAGE_STRING "Usage: %s <input_file> <checksum_file>\n"
#define MAX(a,b) ((a)>(b)?(a):(b))
#define STR_EQ(a, b) (!strcmp((a),(b)))
#define WARNING(fmt, ...) fprintf(stderr,fmt,##__VA_ARGS__)
#define FATAL(fmt, ...) do {\
WARNING(fmt,##__VA_ARGS__);\
exit(1);\
} while (0)
int main(int argc, char *argv[]) {
if (argc != 3) {
FATAL(USAGE_STRING, argv[0]);
}
FILE *f = fopen(argv[1], "rb");
if (f == NULL) {
perror("fopen()");
FATAL("Could not open %s\n", argv[1]);
}
FILE *csfile = fopen(argv[2], "rb");
if (csfile == NULL) {
perror("fopen()");
FATAL("Could not open %s\n", argv[2]);
}
int rsize = 0;
uint8_t buf[4096] = {0};
int blocks_read = 0;
uint8_t s;
uint8_t block_cs;
while ((rsize = fread(buf, 1, 4096, f)) == 4096) {
s = 0;
for (int i = 0; i < 4096; ++i) {
CRC8_MAXIM_ITER(s, buf[i]);
}
if (!fread(&block_cs, 1, 1, csfile)) {
FATAL("Unexpected end of checksum file\n");
}
if (s != block_cs) {
WARNING("Checksum mismatch at 4K-block #%d: got 0x%02x, expected 0x%02x\n", blocks_read, s, block_cs);
}
++blocks_read;
}
if (rsize > 0) {
s = 0;
for (int i = 0; i < rsize; ++i) {
CRC8_MAXIM_ITER(s, buf[i]);
}
if (!fread(&block_cs, 1, 1, csfile)) {
FATAL("Unexpected end of checksum file\n");
}
if (s != block_cs) {
WARNING("Checksum mismatch at 4K-block #%d: got 0x%02x, expected 0x%02x\n", blocks_read, s, block_cs);
}
}
fclose(f);
fclose(csfile);
}

77
lab6/check-fletcher.c Normal file
View File

@ -0,0 +1,77 @@
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#define USAGE_STRING "Usage: %s [-q] [-b] [--] [file [file...]]\n"
#define MAX(a,b) ((a)>(b)?(a):(b))
#define STR_EQ(a, b) (!strcmp((a),(b)))
#define FATAL(fmt, ...) do {\
fprintf(stderr,fmt,##__VA_ARGS__);\
exit(1);\
} while (0)
#define WARNING(fmt, ...) fprintf(stderr,fmt,##__VA_ARGS__)
bool flag_end = false;
bool quiet_mode = false;
bool benchmark = false;
int main(int argc, char *argv[]) {
if (argc == 1) {
FATAL(USAGE_STRING, argv[0]);
}
for (int i = 1; i < argc; ++i) {
if (!flag_end) {
if (argv[i][0] != '-') {
flag_end = true;
}
else if (STR_EQ(argv[i], "--")) {
flag_end = true;
continue;
} else {
switch (argv[i][1]) {
case 'q':
quiet_mode = true;
break;
case 'b':
benchmark = true;
break;
default:
FATAL(USAGE_STRING, argv[0]);
}
continue;
}
}
FILE *f = fopen(argv[i], "rb");
if (f == NULL) {
perror("fopen()");
FATAL("Could not open %s\n", argv[i]);
}
struct timeval t0, t1;
int c;
uint16_t s1 = 0, s2 = 0, s;
gettimeofday(&t0, NULL);
while ((c = fgetc(f)) != EOF) {
s1 = (s1 + (uint8_t) c) % 255;
s2 = (s2 + s1) % 255;
}
s = (s2 << 8) | s1;
gettimeofday(&t1, NULL);
if (quiet_mode) {
printf("0x%04x\n", s);
} else {
printf("%s: checksum 0x%04x\n", argv[i], s);
}
if (benchmark) {
struct timeval result;
timersub(&t1, &t0, &result);
printf("Fletcher execution time: %ld.%06lds\n", result.tv_sec, result.tv_usec);
}
fclose(f);
}
}

74
lab6/check-xor.c Normal file
View File

@ -0,0 +1,74 @@
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#define USAGE_STRING "Usage: %s [-q] [-b] [--] [file [file...]]\n"
#define MAX(a,b) ((a)>(b)?(a):(b))
#define STR_EQ(a, b) (!strcmp((a),(b)))
#define FATAL(fmt, ...) do {\
fprintf(stderr,fmt,##__VA_ARGS__);\
exit(1);\
} while (0)
#define WARNING(fmt, ...) fprintf(stderr,fmt,##__VA_ARGS__)
bool flag_end = false;
bool quiet_mode = false;
bool benchmark = false;
int main(int argc, char *argv[]) {
if (argc == 1) {
FATAL(USAGE_STRING, argv[0]);
}
for (int i = 1; i < argc; ++i) {
if (!flag_end) {
if (argv[i][0] != '-') {
flag_end = true;
}
else if (STR_EQ(argv[i], "--")) {
flag_end = true;
continue;
} else {
switch (argv[i][1]) {
case 'q':
quiet_mode = true;
break;
case 'b':
benchmark = true;
break;
default:
FATAL(USAGE_STRING, argv[0]);
}
continue;
}
}
FILE *f = fopen(argv[i], "rb");
if (f == NULL) {
perror("fopen()");
FATAL("Could not open %s\n", argv[i]);
}
struct timeval t0, t1;
int c;
uint8_t s = 0;
gettimeofday(&t0, NULL);
while ((c = fgetc(f)) != EOF) {
s ^= (uint8_t) c;
}
gettimeofday(&t1, NULL);
if (quiet_mode) {
printf("0x%02x\n", s);
} else {
printf("%s: checksum 0x%02x\n", argv[i], s);
}
if (benchmark) {
struct timeval result;
timersub(&t1, &t0, &result);
printf("XOR execution time: %ld.%06lds\n", result.tv_sec, result.tv_usec);
}
fclose(f);
}
}

42
lab6/crc8.h Normal file
View File

@ -0,0 +1,42 @@
#pragma once
#include <stdint.h>
static const uint8_t crc8_maxim_table[256] = {
0x00, 0x31, 0x62, 0x53, 0xC4, 0xF5, 0xA6, 0x97,
0xB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A0, 0x91,
0x47, 0x76, 0x25, 0x14, 0x83, 0xB2, 0xE1, 0xD0,
0xFE, 0xCF, 0x9C, 0xAD, 0x3A, 0x0B, 0x58, 0x69,
0x04, 0x35, 0x66, 0x57, 0xC0, 0xF1, 0xA2, 0x93,
0xBD, 0x8C, 0xDF, 0xEE, 0x79, 0x48, 0x1B, 0x2A,
0xC1, 0xF0, 0xA3, 0x92, 0x05, 0x34, 0x67, 0x56,
0x78, 0x49, 0x1A, 0x2B, 0xBC, 0x8D, 0xDE, 0xEF,
0x82, 0xB3, 0xE0, 0xD1, 0x46, 0x77, 0x24, 0x15,
0x3B, 0x0A, 0x59, 0x68, 0xFF, 0xCE, 0x9D, 0xAC
};
#define CRC8_MAXIM_ITER(crc, chr) \
do { \
(crc) = crc8_maxim_table[(crc) ^ (chr)]; \
} while (0)

55
lab6/create-csum.c Normal file
View File

@ -0,0 +1,55 @@
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "crc8.h"
#define USAGE_STRING "Usage: %s <input_file> <output_file>\n"
#define MAX(a,b) ((a)>(b)?(a):(b))
#define STR_EQ(a, b) (!strcmp((a),(b)))
#define FATAL(fmt, ...) do {\
fprintf(stderr,fmt,##__VA_ARGS__);\
exit(1);\
} while (0)
#define WARNING(fmt, ...) fprintf(stderr,fmt,##__VA_ARGS__)
int main(int argc, char *argv[]) {
if (argc != 3) {
FATAL(USAGE_STRING, argv[0]);
}
FILE *f = fopen(argv[1], "rb");
if (f == NULL) {
perror("fopen()");
FATAL("Could not open %s\n", argv[1]);
}
FILE *fw = fopen(argv[2], "wb");
if (fw == NULL) {
perror("fopen()");
FATAL("Could not open %s\n", argv[2]);
}
int rsize = 0;
uint8_t buf[4096] = {0};
uint8_t s;
while ((rsize = fread(buf, 1, 4096, f)) == 4096) {
s = 0;
for (int i = 0; i < 4096; ++i) {
CRC8_MAXIM_ITER(s, buf[i]);
}
fwrite(&s, 1, 1, fw);
}
if (rsize > 0) {
s = 0;
for (int i = 0; i < rsize; ++i) {
CRC8_MAXIM_ITER(s, buf[i]);
}
fwrite(&s, 1, 1, fw);
}
fclose(f);
fclose(fw);
}