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.

98 lines
2.9KB

  1. #include <string.h>
  2. #include <stdio.h>
  3. #include <time.h>
  4. #include "showinfo.h"
  5. #include "util.h"
  6. void showinfo(metainfo_t* m) {
  7. const char* s;
  8. int slen;
  9. long int lint;
  10. char str_buff[100] = {0};
  11. if (metainfo_name(m, &s, &slen) != 0) {
  12. s = "[UNKNOWN (>_<)]";
  13. slen = strlen("[UNKNOWN (>_<)]");
  14. }
  15. printf("Name: %.*s\n", slen, s);
  16. util_byte2hex((const unsigned char*)metainfo_infohash(m), sizeof(sha1sum_t), 0, str_buff);
  17. printf("Info hash: %s\n", str_buff);
  18. lint = metainfo_piece_count(m);
  19. printf("Piece count: %ld\n", lint);
  20. lint = metainfo_piece_size(m);
  21. if (util_byte2human(lint, 1, 0, str_buff, sizeof(str_buff)) == -1)
  22. strncpy(str_buff, "err", sizeof(str_buff));
  23. printf("Piece size: %ld (%s)\n", lint, str_buff);
  24. printf("Is multi file: %s\n", metainfo_is_multi_file(m) ? "Yes" : "No");
  25. if (metainfo_is_multi_file(m)) {
  26. printf("File count is: %ld\n", metainfo_file_count(m));
  27. }
  28. printf("Is private: %s\n", metainfo_is_private(m) ? "Yes" : "No");
  29. if (metainfo_announce(m, &s, &slen) != -1) {
  30. printf("Tracker: %.*s\n", slen, s);
  31. }
  32. lint = metainfo_creation_date(m);
  33. if (lint != -1) {
  34. struct tm* time = localtime(&lint);
  35. if (strftime(str_buff, sizeof(str_buff), "%F %T", time) == 0)
  36. strncpy(str_buff, "overflow", sizeof(str_buff));
  37. printf("Creation date: %s\n", str_buff);
  38. }
  39. if (metainfo_created_by(m, &s, &slen) != -1) {
  40. printf("Created by: %.*s\n", slen, s);
  41. }
  42. if (metainfo_source(m, &s, &slen) != -1) {
  43. printf("Source: %.*s\n", slen, s);
  44. }
  45. printf("Files:\n");
  46. unsigned long total_size = 0;
  47. fileinfo_t f;
  48. if (metainfo_is_multi_file(m)) {
  49. fileiter_t fi;
  50. if (metainfo_fileiter_create(m, &fi) == 0) {
  51. while (metainfo_file_next(&fi, &f) == 0) {
  52. int pathlen = metainfo_fileinfo_path(&f, NULL);
  53. char pathbuff[pathlen];
  54. metainfo_fileinfo_path(&f, pathbuff);
  55. if (util_byte2human(f.size, 1, -1, str_buff, sizeof(str_buff)) == -1) {
  56. strncpy(str_buff, "err", sizeof(str_buff));
  57. }
  58. printf("\t%8s %.*s\n", str_buff, pathlen, pathbuff);
  59. total_size += metainfo_fileinfo_size(&f);
  60. }
  61. }
  62. } else {
  63. metainfo_fileinfo(m, &f);
  64. int pathlen = metainfo_fileinfo_path(&f, NULL);
  65. char pathbuff[pathlen];
  66. metainfo_fileinfo_path(&f, pathbuff);
  67. if (util_byte2human(f.size, 1, -1, str_buff, sizeof(str_buff)) == -1) {
  68. strncpy(str_buff, "err", sizeof(str_buff));
  69. }
  70. printf("\t%8s %.*s\n", str_buff, pathlen, pathbuff);
  71. total_size = metainfo_fileinfo_size(&f);
  72. }
  73. if (util_byte2human(total_size, 1, -1, str_buff, sizeof(str_buff)) == -1)
  74. strncpy(str_buff, "err", sizeof(str_buff));
  75. printf("Total size: %s\n", str_buff);
  76. }