summaryrefslogtreecommitdiff
path: root/kon/konpack.h
blob: 4d3d23a46dfdb4c5504e4323ad42d72abfa49202 (plain)
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
/* SPDX-License-Identifier: MIT */
/*
 * konpack.h
 *
 * Copyright (c) 2026 KonAki
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#ifndef KONPACK_H
#define KONPACK_H

#include <stdint.h>
#include <stddef.h>

#ifdef __cplusplus
extern "C" {
#endif

/*** definition ***/

#define KON_PACK_KEY_SIZE 32

typedef struct kon_pack kon_pack_t;

/* pass key as NULL for a pack that was made without one */
kon_pack_t *kon_packOpen(const char *path, const uint8_t *key);
void kon_packClose(kon_pack_t *pack);

int kon_packCount(const kon_pack_t *pack);
const char *kon_packName(const kon_pack_t *pack, int index);
uint32_t kon_packSize(const kon_pack_t *pack, int index);

/* -1 if the name is not in the pack. names use / and are relative to the folder the pack was made from */
int kon_packFind(const kon_pack_t *pack, const char *name);

/* returns a malloc'd copy of the file that you free, with one extra 0 byte after the end that is not counted in size.
   NULL if it fails, see kon_packError */
uint8_t *kon_packRead(kon_pack_t *pack, int index, size_t *size);

const char *kon_packError(void);

uint32_t kon_crc32(const uint8_t *data, size_t length);

/* same call encrypts and decrypts, in and out can be the same buffer. nonce must never repeat for the same key */
void kon_chacha20Xor(const uint8_t key[KON_PACK_KEY_SIZE], const uint8_t nonce[12], uint32_t counter, const uint8_t *in, uint8_t *out, size_t length);

#ifdef KONPACK_WRITER
/* files are paths on disk, baseDir is cut off the front to make the names in the pack. key can be NULL for no encryption.
   returns 0 on success, otherwise see kon_packError */
int kon_packWrite(const char *outPath, const uint8_t *key, const char *baseDir, const char *const *files, int count);
#endif


#ifdef __cplusplus
}
#endif
#endif