forked from ANSSI-FR/MLA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mla.hpp
175 lines (147 loc) · 7.99 KB
/
mla.hpp
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
/* Automatically generated with cbindgen --config cbindgen_cpp.toml (do not modify) */
#pragma once
#include <cstdarg>
#include <cstdint>
#include <cstdlib>
#include <ostream>
#include <new>
enum class MLAStatus : uint64_t {
MLA_STATUS_SUCCESS = 0,
MLA_STATUS_IO_ERROR = 65536,
MLA_STATUS_WRONG_MAGIC = 131072,
MLA_STATUS_UNSUPPORTED_VERSION = 196608,
MLA_STATUS_INVALID_ECC_KEY_FORMAT = 262144,
MLA_STATUS_WRONG_BLOCK_SUB_FILE_TYPE = 327680,
MLA_STATUS_UTF8_CONVERSION_ERROR = 393216,
MLA_STATUS_FILENAME_TOO_LONG = 458752,
MLA_STATUS_WRONG_ARCHIVE_WRITER_STATE = 524288,
MLA_STATUS_ASSERTION_ERROR = 589824,
MLA_STATUS_WRONG_READER_STATE = 655360,
MLA_STATUS_WRONG_WRITER_STATE = 720896,
MLA_STATUS_RAND_ERROR = 851968,
MLA_STATUS_PRIVATE_KEY_NEEDED = 917504,
MLA_STATUS_DESERIALIZATION_ERROR = 983040,
MLA_STATUS_SERIALIZATION_ERROR = 1048576,
MLA_STATUS_MISSING_METADATA = 1114112,
MLA_STATUS_BAD_API_ARGUMENT = 1179648,
MLA_STATUS_END_OF_STREAM = 1245184,
MLA_STATUS_CONFIG_ERROR_INCOHERENT_PERSISTENT_CONFIG = 1310721,
MLA_STATUS_CONFIG_ERROR_COMPRESSION_LEVEL_OUT_OF_RANGE = 1310722,
MLA_STATUS_CONFIG_ERROR_ENCRYPTION_KEY_IS_MISSING = 1310723,
MLA_STATUS_CONFIG_ERROR_PRIVATE_KEY_NOT_SET = 1310724,
MLA_STATUS_CONFIG_ERROR_PRIVATE_KEY_NOT_FOUND = 1310725,
MLA_STATUS_CONFIG_ERROR_ECIES_COMPUTATION_ERROR = 1310726,
MLA_STATUS_DUPLICATE_FILENAME = 1376256,
MLA_STATUS_AUTHENTICATED_DECRYPTION_WRONG_TAG = 1441792,
MLA_STATUS_HKDF_INVALID_KEY_LENGTH = 1507328,
MLA_STATUS_CURVE25519_PARSER_ERROR = 15794176,
};
using MLAConfigHandle = void*;
/// Implemented by the developper. Takes a buffer of a certain number of bytes of MLA
/// file, and does whatever it wants with it (e.g. write it to a file, to a HTTP stream, etc.)
/// If successful, returns 0 and sets the number of bytes actually written to its last
/// parameter. Otherwise, returns an error code on failure.
using MLAWriteCallback = int32_t(*)(const uint8_t *buffer,
uint32_t buffer_len,
void *context,
uint32_t *bytes_written);
/// Implemented by the developper. Should ask the underlying medium (file buffering, HTTP
/// buffering, etc.) to flush any internal buffer.
using MLAFlushCallback = int32_t(*)(void *context);
using MLAArchiveHandle = void*;
using MLAArchiveFileHandle = void*;
/// Implemented by the developper. Read between 0 and buffer_len into buffer.
/// If successful, returns 0 and sets the number of bytes actually read to its last
/// parameter. Otherwise, returns an error code on failure.
using MlaReadCallback = int32_t(*)(uint8_t *buffer,
uint32_t buffer_len,
void *context,
uint32_t *bytes_read);
/// Implemented by the developper. Seek in the source data.
/// If successful, returns 0 and sets the new position to its last
/// parameter. Otherwise, returns an error code on failure.
using MlaSeekCallback = int32_t(*)(int64_t offset, int32_t whence, void *context, uint64_t *new_pos);
struct FileWriter {
MLAWriteCallback write_callback;
MLAFlushCallback flush_callback;
void *context;
};
/// Implemented by the developper
/// Return the desired output path which is expected to be writable.
/// The callback developper is responsible all security checks and parent path creation.
using MlaFileCalback = int32_t(*)(void *context,
const uint8_t *filename,
uintptr_t filename_len,
FileWriter *file_writer);
/// Structure for MLA archive info
struct ArchiveInfo {
uint32_t version;
uint8_t layers;
};
extern "C" {
/// Create a new configuration with default options, and return a handle to it.
MLAStatus mla_config_default_new(MLAConfigHandle *handle_out);
/// Appends the given public key(s) to an existing given configuration
/// (referenced by the handle returned by mla_config_default_new()).
MLAStatus mla_config_add_public_keys(MLAConfigHandle config, const char *public_keys);
/// Sets the compression level in an existing given configuration
/// (referenced by the handle returned by mla_config_default_new()).
/// Currently this level can only be an integer N with 0 <= N <= 11,
/// and bigger values cause denser but slower compression.
MLAStatus mla_config_set_compression_level(MLAConfigHandle config, uint32_t level);
/// Create an empty ReaderConfig
MLAStatus mla_reader_config_new(MLAConfigHandle *handle_out);
/// Appends the given private key to an existing given configuration
/// (referenced by the handle returned by mla_reader_config_new()).
MLAStatus mla_reader_config_add_private_key(MLAConfigHandle config, const char *private_key);
/// Open a new MLA archive using the given configuration, which is consumed and freed
/// (its handle cannot be reused to create another archive). The archive is streamed
/// through the write_callback, and flushed at least at the end when the last byte is
/// written. The context pointer can be used to hold any information, and is passed
/// as an argument when any of the two callbacks are called.
MLAStatus mla_archive_new(MLAConfigHandle *config,
MLAWriteCallback write_callback,
MLAFlushCallback flush_callback,
void *context,
MLAArchiveHandle *handle_out);
/// Open a new file in the archive identified by the handle returned by
/// mla_archive_new(). The given name must be a unique NULL-terminated string.
/// Returns MLA_STATUS_SUCCESS on success, or an error code.
MLAStatus mla_archive_file_new(MLAArchiveHandle archive,
const char *file_name,
MLAArchiveFileHandle *handle_out);
/// Append data to the end of an already opened file identified by the
/// handle returned by mla_archive_file_new(). Returns MLA_STATUS_SUCCESS on
/// success, or an error code.
MLAStatus mla_archive_file_append(MLAArchiveHandle archive,
MLAArchiveFileHandle file,
const uint8_t *buffer,
uint64_t length);
/// Flush any data to be written buffered in MLA to the write_callback,
/// then calls the flush_callback given during archive initialization.
/// Returns MLA_STATUS_SUCCESS on success, or an error code.
MLAStatus mla_archive_flush(MLAArchiveHandle archive);
/// Close the given file, which queues its End-Of-File marker and integrity
/// checks to be written to the callback. Must be called before closing the
/// archive. The file handle must be passed as a mutable reference so it is
/// cleared and cannot be reused after free by accident. Returns
/// MLA_STATUS_SUCCESS on success, or an error code.
MLAStatus mla_archive_file_close(MLAArchiveHandle archive, MLAArchiveFileHandle *file);
/// Close the given archive (must only be called after all files have been
/// closed), flush the output and free any allocated resource. The archive
/// handle must be passed as a mutable reference so it is cleared and
/// cannot be reused after free by accident. Returns MLA_STATUS_SUCCESS on success,
/// or an error code.
MLAStatus mla_archive_close(MLAArchiveHandle *archive);
/// Open and extract an existing MLA archive, using the given configuration.
/// read_callback and seek_callback are used to read the archive data
/// file_callback is used to convert each archive file's name to pathes where extract the data
/// The caller is responsible of all security checks related to callback provided paths
MLAStatus mla_roarchive_extract(MLAConfigHandle *config,
MlaReadCallback read_callback,
MlaSeekCallback seek_callback,
MlaFileCalback file_callback,
void *context);
/// Get info on an existing MLA archive
MLAStatus mla_roarchive_info(MlaReadCallback read_callback, void *context, ArchiveInfo *info_out);
} // extern "C"