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