Skip to content

Commit c51221c

Browse files
committed
[add] Sample scanner
Add scanner for sample.
1 parent 04a0746 commit c51221c

File tree

5 files changed

+79
-0
lines changed

5 files changed

+79
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
bin/*

Makefile

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
CC = g++
2+
SUFFIX = .cc
3+
CFLAGS = -g -Wall -std=c++17
4+
SOURCE = ./src/sample.cc
5+
TARGET = ./bin/libsample.so
6+
TEST_SOURCE = ./src/tests.cc
7+
TEST_TARGET = ./bin/tests
8+
9+
build: $(SOURCE)
10+
$(CC) $(SOURCE) $(CFLAGS) -shared -fPIC -o $(TARGET)
11+
12+
test: $(TESTS)
13+
$(CC) $(TEST_SOURCE) $(CFLAGS ) -lgtest -o $(TEST_TARGET)
14+
15+
clean:
16+
rm -f $(TARGET) $(TRUE_TARGET) $(FALSE_TARGET)
17+
18+
rebuild:
19+
make clean && make build

scanner.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
loadable:
2+
bin/libsample.so

src/sample.cc

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
#include <stdlib.h>
4+
5+
extern "C" {
6+
bool scan(unsigned char *target, size_t target_size, unsigned char *signature, size_t signature_size) {
7+
char *_target = (char *)malloc((target_size + 1) * sizeof(unsigned char));
8+
char *_signature = (char *)malloc((signature_size + 1) * sizeof(unsigned char));
9+
10+
memcpy(_target, target, target_size);
11+
memcpy(_signature, signature, signature_size);
12+
13+
*(_target + target_size) = 0x00;
14+
*(_signature + signature_size) = 0x00;
15+
16+
bool result = strcmp(_target, _signature) == 0;
17+
18+
printf("target:\n%s\n\n", _target);
19+
printf("signature:\n%s\n\n", _signature);
20+
printf("result:\n%d\n\n", result);
21+
22+
free(_target);
23+
free(_signature);
24+
return result;
25+
}
26+
}
27+
28+

src/tests.cc

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <gtest/gtest.h>
2+
3+
#include "../src/sample.cc"
4+
5+
class SampleScannerTest : public ::testing::Test {};
6+
7+
8+
char fizz[] = "fizz";
9+
char buzz[] = "buzz";
10+
size_t size = sizeof(fizz);
11+
12+
TEST_F(SampleScannerTest, Hit) {
13+
char fizz[] = "fizz";
14+
bool result = scan((unsigned char *)fizz, size, (unsigned char *)fizz, size);
15+
ASSERT_TRUE(result);
16+
}
17+
18+
TEST_F(SampleScannerTest, UnHit) {
19+
bool result = scan((unsigned char *)fizz, size, (unsigned char *)buzz, size);
20+
ASSERT_FALSE(result);
21+
}
22+
23+
int main(int argc, char **argv) {
24+
printf("hello");
25+
::testing::InitGoogleTest(&argc, argv);
26+
return RUN_ALL_TESTS();
27+
}
28+
29+

0 commit comments

Comments
 (0)