-
Notifications
You must be signed in to change notification settings - Fork 20
/
create.c
103 lines (90 loc) · 2.8 KB
/
create.c
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
#include <errno.h>
#include <stdio.h>
#include <inttypes.h>
#include <string.h>
#ifdef __cplusplus
#include "mla.hpp"
#define MLA_STATUS(x) MLAStatus::x
#else
#include "mla.h"
#define MLA_STATUS(x) (x)
#endif
// from samples/test_ed25519.pem
const char *szPubkey = "-----BEGIN PUBLIC KEY-----\n"
"MCowBQYDK2VwAyEA9md4yIIFx+ftwe0c1p2YsJFrobXWKxan54Bs+/jFagE=\n"
"-----END PUBLIC KEY-----\n";
static int32_t callback_write(const uint8_t* pBuffer, uint32_t length, void *context, uint32_t *pBytesWritten)
{
size_t res = fwrite(pBuffer, 1, length, (FILE*)context);
*pBytesWritten = (uint32_t)res;
if (ferror(context))
{
return errno;
}
return 0;
}
static int32_t callback_flush(void *context)
{
if (fflush((FILE*)context) != 0)
{
return errno;
}
return 0;
}
int main()
{
FILE* f = fopen("test.mla", "w");
if (f == NULL)
{
fprintf(stderr, " [!] Could not create output file\n");
return errno;
}
MLAStatus status;
MLAConfigHandle hConfig = NULL;
status = mla_config_default_new(&hConfig);
if (status != MLA_STATUS(MLA_STATUS_SUCCESS))
{
fprintf(stderr, " [!] Config creation failed with code %" PRIX64 "\n", (uint64_t)status);
return (int)status;
}
status = mla_config_add_public_keys(hConfig, szPubkey);
if (status != MLA_STATUS(MLA_STATUS_SUCCESS))
{
fprintf(stderr, " [!] Public key set failed with code %" PRIX64 "\n", (uint64_t)status);
return (int)status;
}
MLAArchiveHandle hArchive = NULL;
status = mla_archive_new(&hConfig, &callback_write, &callback_flush, f, &hArchive);
if (status != MLA_STATUS(MLA_STATUS_SUCCESS))
{
fprintf(stderr, " [!] Archive creation failed with code %" PRIX64 "\n", (uint64_t)status);
return (int)status;
}
MLAArchiveFileHandle hFile = NULL;
status = mla_archive_file_new(hArchive, "test.txt", &hFile);
if (status != MLA_STATUS(MLA_STATUS_SUCCESS))
{
fprintf(stderr, " [!] File creation failed with code %" PRIX64 "\n", (uint64_t)status);
return (int)status;
}
status = mla_archive_file_append(hArchive, hFile, (const uint8_t*)"Hello, World!\n", (uint32_t)strlen("Hello, World!\n"));
if (status != MLA_STATUS(MLA_STATUS_SUCCESS))
{
fprintf(stderr, " [!] File write failed with code %" PRIX64 "\n", (uint64_t)status);
return (int)status;
}
status = mla_archive_file_close(hArchive, &hFile);
if (status != MLA_STATUS(MLA_STATUS_SUCCESS))
{
fprintf(stderr, " [!] File close failed with code %" PRIX64 "\n", (uint64_t)status);
return (int)status;
}
status = mla_archive_close(&hArchive);
if (status != MLA_STATUS(MLA_STATUS_SUCCESS))
{
fprintf(stderr, " [!] Archive close failed with code %" PRIX64 "\n", (uint64_t)status);
return (int)status;
}
fclose(f);
return 0;
}