Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.62 KB | None | 0 0
  1. bfd *open_inarch(const char *archive_filename, const char *file)
  2. {
  3. const char *target = 0;
  4. bfd **last_one = 0;
  5. bfd *next_one = 0;
  6. struct stat sbuf;
  7. bfd *arch = 0;
  8. char **matching = 0;
  9. const char *output_filename = 0;
  10.  
  11. bfd_set_error(bfd_error_no_error);
  12.  
  13. target = 0;
  14.  
  15. printf("TESTP0\r\n");
  16. if (stat(archive_filename, &sbuf)) {
  17. printf("TESTP1\r\n");
  18. /* Try to figure out the target to use for the archive from the
  19. first object on the list. */
  20. if (file) {
  21. bfd *obj;
  22.  
  23. obj = bfd_openr(file, 0);
  24. if (obj) {
  25. if (bfd_check_format (obj, bfd_object))
  26. target = bfd_get_target (obj);
  27. (void) bfd_close (obj);
  28. }
  29. }
  30.  
  31. /* Create an empty archive. */
  32. arch = bfd_openw(archive_filename, target);
  33. if (arch == 0 || !bfd_set_format(arch, bfd_archive) || !bfd_close(arch))
  34. bfd_fatal(archive_filename);
  35. /* If we die creating a new archive, don't leave it around. */
  36. output_filename = archive_filename;
  37. }
  38.  
  39. arch = bfd_openr(archive_filename, target);
  40. if (arch == 0) {
  41. printf("TESTP2\r\n");
  42. bloser:
  43. bfd_fatal(archive_filename);
  44. }
  45.  
  46. if (!bfd_check_format_matches(arch, bfd_archive, &matching)) {
  47. printf("TESTP3\r\n");
  48. bfd_nonfatal(archive_filename);
  49. if (bfd_get_error() == bfd_error_file_ambiguously_recognized) {
  50. list_matching_formats (matching);
  51. free(matching);
  52. }
  53. exit(1);
  54. }
  55.  
  56. last_one = &(arch->archive_next);
  57. /* Read all the contents right away, regardless. */
  58. for (next_one = bfd_openr_next_archived_file(arch, 0);
  59. next_one;
  60. next_one = bfd_openr_next_archived_file(arch, next_one)) {
  61. /* PROGRESS (1); */
  62. *last_one = next_one;
  63. last_one = &next_one->archive_next;
  64. }
  65.  
  66. *last_one = (bfd *)0;
  67. if (bfd_get_error() != bfd_error_no_more_archived_files)
  68. goto bloser;
  69. printf("TESTP4\r\n");
  70. return arch;
  71. }
  72.  
  73.  
  74.  
  75. void extract_file(bfd *abfd)
  76. {
  77. FILE *ostream;
  78. char *cbuf = 0;
  79. size_t nread, tocopy;
  80. size_t ncopied = 0;
  81. size_t size;
  82. struct stat buf;
  83. const char *output_filename = 0;
  84. static FILE *output_file = 0;
  85.  
  86. cbuf = (char *)malloc(BUFSIZE);
  87. printf("FN: %s\r\n", abfd->filename);
  88. if (bfd_stat_arch_elt(abfd, &buf))
  89. /* xgettext:c-format */
  90. printf("internal stat error on %s", bfd_get_filename(abfd));
  91. size = buf.st_size;
  92. printf("SIZE: %i\r\n", size);
  93.  
  94. bfd_seek(abfd, (file_ptr)0, SEEK_SET);
  95.  
  96. ostream = 0;
  97. if (size == 0) {
  98. /* Seems like an abstraction violation, eh? Well it's OK! */
  99. output_filename = bfd_get_filename(abfd);
  100. ostream = fopen(output_filename, "wb");
  101. if (ostream == 0) {
  102. printf("ERR1\r\n");
  103. perror(output_filename);
  104. exit(1);
  105. }
  106.  
  107. output_file = ostream;
  108. } else {
  109. while (ncopied < size) {
  110. tocopy = size - ncopied;
  111. if (tocopy > BUFSIZE)
  112. tocopy = BUFSIZE;
  113.  
  114. nread = bfd_bread(cbuf, (bfd_size_type) tocopy, abfd);
  115. if (nread != tocopy)
  116. /* xgettext:c-format */
  117. printf("%s is not a valid archive",
  118. bfd_get_filename(bfd_my_archive(abfd)));
  119.  
  120. /* See comment above; this saves disk arm motion */
  121. if (ostream == 0) {
  122. /* Seems like an abstraction violation, eh? Well it's OK! */
  123. output_filename = bfd_get_filename(abfd);
  124.  
  125. ostream = fopen(output_filename, "wb");
  126. if (ostream == 0) {
  127. perror(output_filename);
  128. exit(1);
  129. }
  130.  
  131. output_file = ostream;
  132. }
  133.  
  134. /* fwrite in mingw32 may return int instead of size_t. Cast
  135. the return value to size_t to avoid comparison between
  136. signed and unsigned values. */
  137. if ((size_t)fwrite(cbuf, 1, nread, ostream) != nread)
  138. printf("%s: %s", output_filename, strerror(errno)); ncopied += tocopy;
  139. }
  140. }
  141.  
  142. if (ostream)
  143. fclose(ostream);
  144.  
  145. output_file = 0;
  146. output_filename = 0;
  147.  
  148. chmod(bfd_get_filename(abfd), buf.st_mode);
  149. free(cbuf);
  150. cbuf = 0;
  151. }
  152.  
  153. static void map_over_members(bfd *arch, void (*function)(bfd *), char **files, int count)
  154. {
  155. bfd *head;
  156. int match_count;
  157.  
  158. printf("COUNT: %i\r\n", count);
  159. if (count == 0) {
  160. for (head = arch->archive_next; head; head = head->archive_next) {
  161. /* PROGRESS (1); */
  162. function(head);
  163. }
  164. return;
  165. }
  166. for (; count > 0; ++files, --count) {
  167. bfd_boolean found = FALSE;
  168.  
  169. match_count = 0;
  170. for (head = arch->archive_next; head; head = head->archive_next) {
  171. const char *filename;
  172.  
  173. /* PROGRESS(1); */
  174. filename = head->filename;
  175. printf("MOM_FN: %s\r\n", filename);
  176. if (filename == 0) {
  177. /* Some archive formats don't get the filenames filled in
  178. until the elements are opened. */
  179. struct stat buf;
  180. bfd_stat_arch_elt(head, &buf);
  181. } else if (bfd_is_thin_archive(arch)) {
  182. /* Thin archives store full pathnames. Need to normalize. */
  183. filename = normalize(filename, arch);
  184. printf("THIN_NORM_FN: %s\r\n", filename);
  185. }
  186.  
  187. if (filename && (!strcmp(normalize(*files, arch), filename))) {
  188. ++match_count;
  189. found = TRUE;
  190. printf("NORM_FN: %s\r\n", filename);
  191. function(head);
  192. }
  193. }
  194.  
  195. if (!found)
  196. // xgettext:c-format
  197. fprintf(stderr, "no entry %s in archive\n", *files);
  198. }
  199. }
  200.  
  201. int main(int argc, char **argv)
  202. {
  203. char *fingerprint = 0;
  204. char *orig_name = 0;
  205. FILE *fpin = 0;
  206. FILE *fpout = 0;
  207. int c = 0;
  208. int i = 0;
  209. char *extracted_files[] = {DEBIAN_BINARY, CONTROL_FILE_DEBIAN, DATA_FILE_DEBIAN, 0};
  210. char *combined_files[] = {DEBIAN_BINARY, CONTROL_FILE_DEBIAN, DATA_FILE_DEBIAN, 0};
  211. bfd *arch;
  212.  
  213. arch = open_inarch(argv[1], extracted_files[0]);
  214. map_over_members(arch, extract_file, extracted_files, sizeof(extracted_files)/sizeof(extracted_files[0])-1);
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement