-
Notifications
You must be signed in to change notification settings - Fork 0
/
clean-libsodium-di.d
86 lines (77 loc) · 2.9 KB
/
clean-libsodium-di.d
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
// Cleans up the sodium.di file by removing any extra declarations
// we don't care about, as well as patching a few things up.
module clean;
import std : writeln, lineSplitter, readText, Appender, canFind, endsWith, array, substitute;
import std.file : write;
const LIBSODIUM_LICENSE = `
/*
* ISC License
*
* Copyright (c) 2013-2023
* Frank Denis <j at pureftpd dot org>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
`;
void main()
{
const input = readText("sodium.di");
const lines = input.lineSplitter.array;
Appender!(char[]) output;
output.reserve(input.length);
output.put("// This file was automatically generated by devops/scripts/gen-libsodium-di.bash\n");
output.put("// The preserved libsodium ISC license is presented below, and extends to this file.\n");
output.put(LIBSODIUM_LICENSE);
output.put("module juptune.crypto.libsodium;\n");
output.put("extern(C) @nogc nothrow:\n");
for(size_t i = 0; i < lines.length; i++)
{
const line = lines[i];
if(
line.canFind("extern __gshared")
|| line.canFind("static __gshared")
) // A non-libsodium declaration
{
continue;
}
else if(
line.canFind("__gshared")
&& (line.canFind(" crypto_") || line.canFind(" sodium_") || line.canFind(" randombytes_"))
) // A libsodium function declaration
{
output.put(line);
output.put("\n");
}
else if(
line.canFind("struct")
&& (line.canFind(" crypto_") || line.canFind(" sodium_") || line.canFind(" randombytes_"))
) // A libsodium struct
{
if(line.endsWith(";"))
continue;
output.put(line); output.put("\n");
output.put(lines[++i]); output.put("\n"); // {
while(!lines[i].endsWith("}"))
{
// Lines like `const struct my_struct s = void` -> `my_struct s`
output.put(lines[++i].substitute(
"const struct", "",
"struct", "",
));
output.put("\n");
}
output.put("\n");
}
}
write("libsodium.di", output.data);
}