get_header_ar.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* vi: set sw=4 ts=4: */
  2. /* Copyright 2001 Glenn McGrath.
  3. *
  4. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  5. */
  6. #include "libbb.h"
  7. #include "unarchive.h"
  8. static unsigned read_num(const char *str, int base)
  9. {
  10. /* This code works because
  11. * on misformatted numbers bb_strtou returns all-ones */
  12. int err = bb_strtou(str, NULL, base);
  13. if (err == -1)
  14. bb_error_msg_and_die("invalid ar header");
  15. return err;
  16. }
  17. char FAST_FUNC get_header_ar(archive_handle_t *archive_handle)
  18. {
  19. file_header_t *typed = archive_handle->file_header;
  20. unsigned size;
  21. union {
  22. char raw[60];
  23. struct {
  24. char name[16];
  25. char date[12];
  26. char uid[6];
  27. char gid[6];
  28. char mode[8];
  29. char size[10];
  30. char magic[2];
  31. } formatted;
  32. } ar;
  33. #if ENABLE_FEATURE_AR_LONG_FILENAMES
  34. static char *ar_long_names;
  35. static unsigned ar_long_name_size;
  36. #endif
  37. /* dont use xread as we want to handle the error ourself */
  38. if (read(archive_handle->src_fd, ar.raw, 60) != 60) {
  39. /* End Of File */
  40. return EXIT_FAILURE;
  41. }
  42. /* ar header starts on an even byte (2 byte aligned)
  43. * '\n' is used for padding
  44. */
  45. if (ar.raw[0] == '\n') {
  46. /* fix up the header, we started reading 1 byte too early */
  47. memmove(ar.raw, &ar.raw[1], 59);
  48. ar.raw[59] = xread_char(archive_handle->src_fd);
  49. archive_handle->offset++;
  50. }
  51. archive_handle->offset += 60;
  52. /* align the headers based on the header magic */
  53. if (ar.formatted.magic[0] != '`' || ar.formatted.magic[1] != '\n')
  54. bb_error_msg_and_die("invalid ar header");
  55. /* FIXME: more thorough routine would be in order here
  56. * (we have something like that in tar)
  57. * but for now we are lax. */
  58. typed->size = size = read_num(ar.formatted.size, 10);
  59. /* special filenames have '/' as the first character */
  60. if (ar.formatted.name[0] == '/') {
  61. if (ar.formatted.name[1] == ' ') {
  62. /* This is the index of symbols in the file for compilers */
  63. data_skip(archive_handle);
  64. archive_handle->offset += size;
  65. return get_header_ar(archive_handle); /* Return next header */
  66. }
  67. #if ENABLE_FEATURE_AR_LONG_FILENAMES
  68. if (ar.formatted.name[1] == '/') {
  69. /* If the second char is a '/' then this entries data section
  70. * stores long filename for multiple entries, they are stored
  71. * in static variable long_names for use in future entries
  72. */
  73. ar_long_name_size = size;
  74. free(ar_long_names);
  75. ar_long_names = xmalloc(size);
  76. xread(archive_handle->src_fd, ar_long_names, size);
  77. archive_handle->offset += size;
  78. /* Return next header */
  79. return get_header_ar(archive_handle);
  80. }
  81. #else
  82. bb_error_msg_and_die("long filenames not supported");
  83. #endif
  84. }
  85. /* Only size is always present, the rest may be missing in
  86. * long filename pseudo file. Thus we decode the rest
  87. * after dealing with long filename pseudo file.
  88. */
  89. typed->mode = read_num(ar.formatted.mode, 8);
  90. typed->mtime = read_num(ar.formatted.date, 10);
  91. typed->uid = read_num(ar.formatted.uid, 10);
  92. typed->gid = read_num(ar.formatted.gid, 10);
  93. #if ENABLE_FEATURE_AR_LONG_FILENAMES
  94. if (ar.formatted.name[0] == '/') {
  95. unsigned long_offset;
  96. /* The number after the '/' indicates the offset in the ar data section
  97. * (saved in ar_long_names) that conatains the real filename */
  98. long_offset = read_num(&ar.formatted.name[1], 10);
  99. if (long_offset >= ar_long_name_size) {
  100. bb_error_msg_and_die("can't resolve long filename");
  101. }
  102. typed->name = xstrdup(ar_long_names + long_offset);
  103. } else
  104. #endif
  105. {
  106. /* short filenames */
  107. typed->name = xstrndup(ar.formatted.name, 16);
  108. }
  109. typed->name[strcspn(typed->name, " /")] = '\0';
  110. if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) {
  111. archive_handle->action_header(typed);
  112. #if ENABLE_DPKG || ENABLE_DPKG_DEB
  113. if (archive_handle->dpkg__sub_archive) {
  114. while (archive_handle->dpkg__action_data_subarchive(archive_handle->dpkg__sub_archive) == EXIT_SUCCESS)
  115. continue;
  116. } else
  117. #endif
  118. archive_handle->action_data(archive_handle);
  119. } else {
  120. data_skip(archive_handle);
  121. }
  122. archive_handle->offset += typed->size;
  123. /* Set the file pointer to the correct spot, we may have been reading a compressed file */
  124. lseek(archive_handle->src_fd, archive_handle->offset, SEEK_SET);
  125. return EXIT_SUCCESS;
  126. }