-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathMakefile
More file actions
195 lines (168 loc) · 7.22 KB
/
Makefile
File metadata and controls
195 lines (168 loc) · 7.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
.PHONY: clean fmt install profile test uninstall
OPTFLAGS ?= -O2 -finline-functions
WARNFLAGS ?= -Wall -Wextra -Wpedantic -Wshadow -Werror -Wfatal-errors -Wconversion -Wsign-conversion -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations
CFLAGS ?= -std=c11 -pipe -fPIC $(OPTFLAGS) $(WARNFLAGS)
UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S),Darwin)
CFLAGS += -fstack-protector-strong -D_GNU_SOURCE
else ifeq ($(OS),Windows_NT)
CFLAGS += -D_WIN32 -s
else
CFLAGS += -fstack-protector-strong -D_GNU_SOURCE -fno-plt -Wl,-z,now -D_FORTIFY_SOURCE=3
endif
PREFIX ?= /usr
BINDIR ?= $(PREFIX)/bin
DESTDIR ?=
VERSION ?= 1.3.15
RELEASE_DIR := tinyxxd-$(VERSION)
RELEASE_TARBALL := $(RELEASE_DIR).tar.gz
RELEASE_FILES := main.c Makefile COPYING README.md
tinyxxd: main.c
$(CC) $(CFLAGS) -o $@ $<
tinyxxd_debug: main.c
$(CC) $(CFLAGS) -g -o $@ $<
tinyxxd_asan: main.c
$(CC) $(CFLAGS) -g -fsanitize=address,undefined -o $@ $<
profile: tinyxxd_debug
dd if=/dev/random of=sample.bin bs=1M count=1
valgrind --dump-instr=yes --collect-jumps=yes --tool=callgrind ./tinyxxd_debug sample.bin
@rm -f sample.bin
@kcachegrind || echo 'Profile generated. Use kcachegrind or a similar tool to view the callgrind output.'
bench:
@rm -f -- *.bin *.dat *.hex *.pkl xxd testfiles/xxd.c
@python3 bench.py -q
benchfull:
@rm -f -- *.bin *.dat *.hex *.pkl xxd testfiles/xxd.c
@python3 bench.py
fmt: main.c
clang-format -style=WebKit -i main.c
test: xxd tinyxxd_asan
@echo 'Preparing tests...'
@echo -n 'This is a test file' > sample.bin
@printf '\x00\x09\x0a\x0d\x20\x41\x7e\x7f\x80\xff' > colorbytes.bin
@printf '\x00\x05\x0d\x25\x40\x4b\x5a\x81\xc0\xff' > ebcdicbytes.bin
@echo 'Running tests...'
@$(MAKE) run_test CMD='-a testfiles/somezeros.bin' DESC='Show nul-lines as single asterisk'
@$(MAKE) run_test CMD='-Ralways -g1 -c256 -d -o 9223372036854775808 testfiles/somezeros.bin' DESC='Test for buffer overflow'
@$(MAKE) run_test CMD='-s +5 sample.bin' DESC='Seek +5'
@$(MAKE) run_test CMD='-s -5 sample.bin' DESC='Seek -5'
@$(MAKE) run_test CMD='-l 7 sample.bin' DESC='Stop after len=7'
@$(MAKE) run_test CMD='-c 8 sample.bin' DESC='Hex dump with 8 columns'
@$(MAKE) run_test CMD='-c 0 -ps sample.bin' DESC='PostScript with no line wrapping (-c 0)'
@$(MAKE) run_test CMD='-p sample.bin' DESC='Plain hex dump (PostScript style)'
@$(MAKE) run_test CMD='-i sample.bin' DESC='C include file style'
@$(MAKE) run_test CMD='-e sample.bin' DESC='Little-endian hex dump'
@$(MAKE) run_test CMD='-b sample.bin' DESC='Binary digit dump'
@$(MAKE) run_test CMD='-u sample.bin' DESC='Capitalized hex output'
@$(MAKE) run_test CMD='-E sample.bin' DESC='Show EBCDIC'
@$(MAKE) run_test CMD='-R always colorbytes.bin' DESC='ASCII color output (all color categories)'
@$(MAKE) run_test CMD='-R never colorbytes.bin' DESC='ASCII no-color output'
@$(MAKE) run_test CMD='-R always -E ebcdicbytes.bin' DESC='EBCDIC color output'
@$(MAKE) run_test CMD='-R never -E ebcdicbytes.bin' DESC='EBCDIC no-color output'
@$(MAKE) run_test CMD='-g 1 sample.bin' DESC='Group bytes in 1s'
@$(MAKE) run_test CMD='-g 4 sample.bin' DESC='Group bytes in 4s'
@$(MAKE) run_test CMD='-d sample.bin' DESC='Decimal offset'
@$(MAKE) run_test CMD='-o 0x100 sample.bin' DESC='Display offset +0x100'
@$(MAKE) run_test CMD='-i -C sample.bin' DESC='C include capitalized'
@$(MAKE) run_test CMD='-i -n myvar sample.bin' DESC='C include custom name'
@$(MAKE) run_test CMD='-i -b sample.bin' DESC='C include binary format'
@$(MAKE) run_test CMD='-i -t sample.bin' DESC='C include with terminating NUL'
@$(MAKE) run_test CMD='-i -b -t sample.bin' DESC='C include binary with terminating NUL'
@$(MAKE) verify_conversion_test
@$(MAKE) test_revert_crlf
@$(MAKE) test_le_padding
@$(MAKE) test_help_stdout
@$(MAKE) test_error_stderr
@rm -f -- *.hex sample.bin colorbytes.bin ebcdicbytes.bin tinyxxd_output.txt xxd_output.txt
@echo 'All tests complete.'
run_test:
@echo "Running test: $(DESC)"
@./tinyxxd_asan $(CMD) > tinyxxd_output.txt
@./xxd $(CMD) > xxd_output.txt
@if diff -q tinyxxd_output.txt xxd_output.txt > /dev/null; then \
echo 'Test passed'; \
else \
echo 'Test failed'; \
diff tinyxxd_output.txt xxd_output.txt; \
fi
verify_conversion_test:
@echo "Running conversion and verification test..."
@./tinyxxd_asan sample.bin > sample_tinyxxd.bin
@./tinyxxd_asan -r sample_tinyxxd.bin > sample_restored.bin
@if diff -q sample.bin sample_restored.bin > /dev/null; then \
echo -e "\033[0;32mConversion and verification test passed\033[0m"; \
else \
echo -e "\033[0;31mConversion and verification test failed\033[0m"; \
exit 1; \
fi
@rm -f sample_tinyxxd.bin sample_restored.bin
test_revert_crlf:
@echo "Running test: Revert hex dump with CRLF line endings"
@printf "00000000: 3132 3334\r\n00000004: 3536 3738\r\n" > repro_crlf.hex
@./tinyxxd_asan -r repro_crlf.hex > repro_crlf.bin
@printf "12345678" > expected_crlf.bin
@if diff -q repro_crlf.bin expected_crlf.bin > /dev/null; then \
echo 'Test passed'; \
else \
echo 'Test failed'; \
exit 1; \
fi
@rm -f repro_crlf.hex repro_crlf.bin expected_crlf.bin
test_le_padding:
@echo "Running test: Little-endian hex dump with padding"
@printf "ABCDEFG" > 7.bin
@./tinyxxd_asan -e -c 8 7.bin > le_padding_output.txt
@printf "00000000: 44434241 474645 ABCDEFG\n" > le_padding_expected.txt
@if diff -q le_padding_output.txt le_padding_expected.txt > /dev/null; then \
echo 'Test passed'; \
else \
echo 'Test failed'; \
diff le_padding_output.txt le_padding_expected.txt; \
exit 1; \
fi
@rm -f 7.bin le_padding_output.txt le_padding_expected.txt
test_help_stdout:
@echo "Running test: -h outputs to stdout"
@if ./tinyxxd_asan -h 2>/dev/null | grep -q 'Usage'; then \
echo 'Test passed'; \
else \
echo 'Test failed: -h should write to stdout'; \
exit 1; \
fi
test_error_stderr:
@echo "Running test: bad option outputs to stderr"
@if ./tinyxxd_asan -Z 2>&1 >/dev/null | grep -q 'Usage'; then \
echo 'Test passed'; \
else \
echo 'Test failed: bad option should write to stderr'; \
exit 1; \
fi
update-version:
@echo "Updating version in main.c to $(VERSION)"
@sed -i 's/const char\* version = "tinyxxd [0-9]*\.[0-9]*\.[0-9]*"/const char* version = "tinyxxd $(VERSION)"/' main.c
release: fmt test update-version
@echo "Creating release version $(VERSION)"
$(foreach file,$(RELEASE_FILES),$(if $(wildcard $(file)),,$(error Missing file $(file))))
mkdir -p "$(RELEASE_DIR)"
cp -v $(RELEASE_FILES) "$(RELEASE_DIR)/"
tar zcvf $(RELEASE_TARBALL) "$(RELEASE_DIR)/"
rm -rf "$(RELEASE_DIR)/"
@echo "Release package created: $(RELEASE_TARBALL)"
tinyxxd_fuzz: main.c
afl-gcc-fast $(CFLAGS) -o $@ $<
fuzz: tinyxxd_fuzz
export AFL_PATH=$(which afl-fuzz)
export AFL_SKIP_CPUFREQ=1
@mkdir -p input_dir
@dd if=/dev/urandom of=input_dir/sample.bin count=1 bs=128
afl-fuzz -i input_dir -o fuzz_output -- ./tinyxxd_fuzz @@
testfiles/xxd.c:
cd testfiles && curl -sOL "https://raw.githubusercontent.com/vim/vim/master/src/xxd/xxd.c"
xxd: testfiles/xxd.c
$(CC) -std=c11 -pipe -D_GNU_SOURCE $(OPTFLAGS) -o $@ $<
install: tinyxxd
install -D -m 755 tinyxxd "$(DESTDIR)$(BINDIR)/tinyxxd"
uninstall:
rm -f "$(DESTDIR)$(BINDIR)/tinyxxd"
clean:
rm -r -f -- *.bin *.dat *.hex *.o *.pkl *.tar.gz callgrind.out.* og_xxd* output_* tinyxxd tinyxxd_* tinyxxd_debug xxd testfiles/xxd.c xxd_*