data.h 3.0 KB

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