648db22b |
1 | /* |
2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
3 | * All rights reserved. |
4 | * |
5 | * This source code is licensed under both the BSD-style license (found in the |
6 | * LICENSE file in the root directory of this source tree) and the GPLv2 (found |
7 | * in the COPYING file in the root directory of this source tree). |
8 | * You may select, at your option, one of the above-listed licenses. |
9 | */ |
10 | |
11 | #ifndef DATA_H |
12 | #define DATA_H |
13 | |
14 | #include <stddef.h> |
15 | #include <stdint.h> |
16 | |
17 | typedef enum { |
18 | data_type_file = 1, /**< This data is a file. *.zst */ |
19 | data_type_dir = 2, /**< This data is a directory. *.tar.zst */ |
20 | } data_type_t; |
21 | |
22 | typedef struct { |
23 | char const* url; /**< Where to get this resource. */ |
24 | uint64_t xxhash64; /**< Hash of the url contents. */ |
25 | char const* path; /**< The path of the unpacked resource (derived). */ |
26 | } data_resource_t; |
27 | |
28 | typedef struct { |
29 | data_resource_t data; |
30 | data_resource_t dict; |
31 | data_type_t type; /**< The type of the data. */ |
32 | char const* name; /**< The logical name of the data (no extension). */ |
33 | } data_t; |
34 | |
35 | /** |
36 | * The NULL-terminated list of data objects. |
37 | */ |
38 | extern data_t const* const* data; |
39 | |
40 | |
41 | int data_has_dict(data_t const* data); |
42 | |
43 | /** |
44 | * Initializes the data module and downloads the data necessary. |
45 | * Caches the downloads in dir. We add a stamp file in the directory after |
46 | * a successful download. If a stamp file already exists, and matches our |
47 | * current data stamp, we will use the cached data without downloading. |
48 | * |
49 | * @param dir The directory to cache the downloaded data into. |
50 | * |
51 | * @returns 0 on success. |
52 | */ |
53 | int data_init(char const* dir); |
54 | |
55 | /** |
56 | * Must be called at exit to free resources allocated by data_init(). |
57 | */ |
58 | void data_finish(void); |
59 | |
60 | typedef struct { |
61 | uint8_t* data; |
62 | size_t size; |
63 | size_t capacity; |
64 | } data_buffer_t; |
65 | |
66 | /** |
67 | * Read the file that data points to into a buffer. |
68 | * NOTE: data must be a file, not a directory. |
69 | * |
70 | * @returns The buffer, which is NULL on failure. |
71 | */ |
72 | data_buffer_t data_buffer_get_data(data_t const* data); |
73 | |
74 | /** |
75 | * Read the dictionary that the data points to into a buffer. |
76 | * |
77 | * @returns The buffer, which is NULL on failure. |
78 | */ |
79 | data_buffer_t data_buffer_get_dict(data_t const* data); |
80 | |
81 | /** |
82 | * Read the contents of filename into a buffer. |
83 | * |
84 | * @returns The buffer, which is NULL on failure. |
85 | */ |
86 | data_buffer_t data_buffer_read(char const* filename); |
87 | |
88 | /** |
89 | * Create a buffer with the specified capacity. |
90 | * |
91 | * @returns The buffer, which is NULL on failure. |
92 | */ |
93 | data_buffer_t data_buffer_create(size_t capacity); |
94 | |
95 | /** |
96 | * Calls memcmp() on the contents [0, size) of both buffers. |
97 | */ |
98 | int data_buffer_compare(data_buffer_t buffer1, data_buffer_t buffer2); |
99 | |
100 | /** |
101 | * Frees an allocated buffer. |
102 | */ |
103 | void data_buffer_free(data_buffer_t buffer); |
104 | |
105 | |
106 | typedef struct { |
107 | data_buffer_t const* buffers; |
108 | size_t size; |
109 | } data_buffers_t; |
110 | |
111 | /** |
112 | * @returns a list of buffers for every file in data. It is zero sized on error. |
113 | */ |
114 | data_buffers_t data_buffers_get(data_t const* data); |
115 | |
116 | /** |
117 | * Frees the data buffers. |
118 | */ |
119 | void data_buffers_free(data_buffers_t buffers); |
120 | |
121 | #endif |