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