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.

116 lines
3.4KB

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