Verify bittorrent .torrent metainfo files.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

166 lines
3.6KB

  1. #ifndef METAFILE_H
  2. #define METAFILE_H
  3. #include <bencode.h>
  4. /* 128 MiB */
  5. #define MAX_TORRENT_SIZE 128*1024*1024
  6. /* This file will parse the .torrent file and make accessor functions */
  7. typedef struct {
  8. const char* str;
  9. int len;
  10. } lenstr_t;
  11. typedef struct {
  12. bencode_t path;
  13. long int size;
  14. } fileinfo_t;
  15. typedef struct {
  16. bencode_t filelist;
  17. } fileiter_t;
  18. typedef unsigned char sha1sum_t[20];
  19. /*
  20. typedef struct __attribute__((packed)) sha1sum_t {
  21. unsigned char hash[20];
  22. } sha1sum_t;
  23. */
  24. typedef struct {
  25. char* bytes;
  26. int bytes_size;
  27. sha1sum_t info_hash;
  28. const sha1sum_t* pieces;
  29. int piece_count;
  30. long int piece_length, creation_date;
  31. bencode_t announce, created_by, name, source, comment;
  32. long int is_private;
  33. int is_multi_file;
  34. union {
  35. long int file_size;
  36. bencode_t files;
  37. };
  38. } metainfo_t;
  39. /*
  40. * Returns 0 on success, -1 on error
  41. */
  42. int metainfo_create(metainfo_t* metai, const char* path);
  43. void metainfo_destroy(metainfo_t* metai);
  44. /*
  45. * Get the info_hash of the torrent as a pointer
  46. */
  47. const sha1sum_t* metainfo_infohash(metainfo_t* metai);
  48. /*
  49. * Get the announce url
  50. */
  51. int metainfo_announce(metainfo_t* metai, const char** str, int* len);
  52. /*
  53. * Get the created by string
  54. */
  55. int metainfo_created_by(metainfo_t* metai, const char** str, int* len);
  56. /*
  57. * Get the creation date, as a unix timestamp
  58. */
  59. int metainfo_creation_date(metainfo_t* metai);
  60. /*
  61. * Get the source
  62. */
  63. int metainfo_source(metainfo_t* metai, const char** str, int* len);
  64. /*
  65. * Get the name of the torrent
  66. */
  67. int metainfo_name(metainfo_t* metai, const char** str, int* len);
  68. /*
  69. * Get the comment of the torrent
  70. */
  71. int metainfo_comment(metainfo_t* metai, const char** str, int* len);
  72. /*
  73. * Is this torrent private?
  74. */
  75. int metainfo_is_private(metainfo_t* metai);
  76. /*
  77. * Get the array of pieces
  78. */
  79. int metainfo_pieces(metainfo_t* metai, const sha1sum_t** piece_hash);
  80. /*
  81. * Get the number of pieces, this will return the number
  82. */
  83. long int metainfo_piece_count(metainfo_t* metai);
  84. /*
  85. * Get the index'th piece hash, returns -1 if invalid
  86. */
  87. int metainfo_piece_index(metainfo_t* metai, int index, \
  88. const sha1sum_t** piece_hash);
  89. /*
  90. * Get the size of 1 piece
  91. */
  92. int metainfo_piece_size(metainfo_t* metai);
  93. /*
  94. * Return 1 if the torrent has multiple files, or 0 if has only 1
  95. */
  96. int metainfo_is_multi_file(metainfo_t* metai);
  97. /*
  98. * Return the number of files, if it's a multi file. This is slow
  99. */
  100. long int metainfo_file_count(metainfo_t* metai);
  101. /*
  102. * Get the index'th file information, if it's a multi file
  103. * This is slow and shouldn't be used
  104. */
  105. int metainfo_file_index(metainfo_t* metai, int index, fileinfo_t* finfo);
  106. /*
  107. * Create a file iterator.
  108. * This interface should be used to get the file infos.
  109. * This doesn't need to be freed.
  110. */
  111. int metainfo_fileiter_create(const metainfo_t* metai, fileiter_t* fileiter);
  112. /*
  113. * Get the next fileinfo in a multi file torrent
  114. * Return -1 if there is no more files.
  115. */
  116. int metainfo_file_next(fileiter_t* iter, fileinfo_t* finfo);
  117. /*
  118. * Get the file information, if torrent has only 1 file
  119. */
  120. int metainfo_fileinfo(metainfo_t* metai, fileinfo_t* finfo);
  121. /*
  122. * Copy the file path from a fileinto_t struct into the buffer at 'out_str',
  123. * adding separators according to the current platform.
  124. * if 'out_str' is NULL, return the required bytes to store the name,
  125. * otherwise the number of bytes copied, or -1 if error.
  126. * The count does NOT include the null terminator
  127. */
  128. int metainfo_fileinfo_path(fileinfo_t* len, char* out_str);
  129. /*
  130. * Return the size of the file in bytes, ofc
  131. */
  132. long int metainfo_fileinfo_size(fileinfo_t* finfo);
  133. #endif