-
Notifications
You must be signed in to change notification settings - Fork 11
/
algamize.sh
126 lines (102 loc) · 3.23 KB
/
algamize.sh
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
#!/bin/bash
set -e -o nounset
DST_DIR="$1"
VERSION="$2"
mkdir -p "$DST_DIR"
SRC_DIR="src/"
NESTED_FILES=("c25519/f25519" "c25519/fprime" "c25519/sha512" "c25519/c25519" "c25519/ed25519" "c25519/edsign" )
COMPACT_FILES=(compact_wipe compact_x25519 compact_ed25519)
DST_HEADER="$DST_DIR/compact25519.h"
DST_SOURCE="$DST_DIR/compact25519.c"
function remove_header_guard() {
# we reverse lines so that it is easier to detect the last endif to drop
tac | \
awk '
BEGIN{LAST_END_FOUND=0;}
/#endif/ && !LAST_END_FOUND { LAST_END_FOUND=1; next; }
/#.*_H_*$/ { next; }
42
' | \
tac
}
function remove_local_imports() {
sed 's/#include ".*h"//'
}
function merge_includes() {
awk '
/#include .*/ { includes[$0] = 1; next;}
{ other[NR] = $0; next; }
END {
for (i in includes) {
print i;
}
for (i in other) {
print other[i];
}
}
'
}
function remove_double_blank_lines() {
cat -s
}
function make_everything_static() {
sed \
-e 's/^\([^\ \t#{}()\/]\)/static \1/' \
-e 's/static static/static/' \
-e 's/static struct/struct/' \
-e 's/static extern/extern/' \
-e 's/static const/const/'
}
function add_decl_spec() {
sed \
-e 's/^static /static COMPACT_25519_DECL /' \
-e 's/^\([^\ \t#{}()\/*s]\)/COMPACT_25519_DECL \1/' # non static stuff like global header
}
echo "// compact25519 $VERSION
// Source: https://github.com/DavyLandman/compact25519
// Licensed under CC0-1.0
// Based on Daniel Beer's Public Domain c25519 implementation
// https://www.dlbeer.co.nz/oss/c25519.html version: 2017-10-05
#ifndef __COMPACT_25519_H
#define __COMPACT_25519_H
#if defined(__cplusplus)
extern \"C\" {
#endif
// provide your own decl specificier like "-DCOMPACT_25519_DECL=ICACHE_RAM_ATTR"
#ifndef COMPACT_25519_DECL
#define COMPACT_25519_DECL
#endif
" > "$DST_HEADER"
for h in "${COMPACT_FILES[@]}"; do
cat "$SRC_DIR/$h.h" | remove_header_guard
done | merge_includes | remove_double_blank_lines | add_decl_spec >> "$DST_HEADER"
echo "#if defined(__cplusplus)
}
#endif
#endif" >> "$DST_HEADER"
echo "// compact25519 $VERSION
// Source: https://github.com/DavyLandman/compact25519
// Licensed under CC0-1.0
// Based on Daniel Beer's Public Domain c25519 implementation
// https://www.dlbeer.co.nz/oss/c25519.html version: 2017-10-05
#include \"compact25519.h\"
" > "$DST_SOURCE"
for h in "${NESTED_FILES[@]}"; do
echo "// ******* BEGIN: $h.h ********"
cat "$SRC_DIR/$h.h" | remove_header_guard | \
remove_local_imports | remove_double_blank_lines | \
make_everything_static | add_decl_spec
echo "// ******* END: $h.h ********"
done >> "$DST_SOURCE"
for h in "${NESTED_FILES[@]}"; do
echo "// ******* BEGIN: $h.c ********"
cat "$SRC_DIR/$h.c" | remove_local_imports | \
remove_double_blank_lines | make_everything_static | add_decl_spec
echo "// ******* END: $h.c ********"
done >> "$DST_SOURCE"
for h in "${COMPACT_FILES[@]}"; do
echo "// ******* BEGIN: $h.c ********"
cat "$SRC_DIR/$h.c" | remove_local_imports | \
remove_double_blank_lines | add_decl_spec
echo "// ******* END: $h.c ********"
done >> "$DST_SOURCE"