get_header_ar.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. char get_header_ar(archive_handle_t *archive_handle)
  9. {
  10. int err;
  11. file_header_t *typed = archive_handle->file_header;
  12. union {
  13. char raw[60];
  14. struct {
  15. char name[16];
  16. char date[12];
  17. char uid[6];
  18. char gid[6];
  19. char mode[8];
  20. char size[10];
  21. char magic[2];
  22. } formatted;
  23. } ar;
  24. #if ENABLE_FEATURE_AR_LONG_FILENAMES
  25. static char *ar_long_names;
  26. static unsigned ar_long_name_size;
  27. #endif
  28. /* dont use xread as we want to handle the error ourself */
  29. if (read(archive_handle->src_fd, ar.raw, 60) != 60) {
  30. /* End Of File */
  31. return EXIT_FAILURE;
  32. }
  33. /* ar header starts on an even byte (2 byte aligned)
  34. * '\n' is used for padding
  35. */
  36. if (ar.raw[0] == '\n') {
  37. /* fix up the header, we started reading 1 byte too early */
  38. memmove(ar.raw, &ar.raw[1], 59);
  39. ar.raw[59] = xread_char(archive_handle->src_fd);
  40. archive_handle->offset++;
  41. }
  42. archive_handle->offset += 60;
  43. /* align the headers based on the header magic */
  44. if (ar.formatted.magic[0] != '`' || ar.formatted.magic[1] != '\n')
  45. bb_error_msg_and_die("invalid ar header");
  46. /* FIXME: more thorough routine would be in order here */
  47. /* (we have something like that in tar) */
  48. /* but for now we are lax. This code works because */
  49. /* on misformatted numbers bb_strtou returns all-ones */
  50. typed->mode = err = bb_strtou(ar.formatted.mode, NULL, 8);
  51. if (err == -1) bb_error_msg_and_die("invalid ar header");
  52. typed->mtime = err = bb_strtou(ar.formatted.date, NULL, 10);
  53. if (err == -1) bb_error_msg_and_die("invalid ar header");
  54. typed->uid = err = bb_strtou(ar.formatted.uid, NULL, 10);
  55. if (err == -1) bb_error_msg_and_die("invalid ar header");
  56. typed->gid = err = bb_strtou(ar.formatted.gid, NULL, 10);
  57. if (err == -1) bb_error_msg_and_die("invalid ar header");
  58. typed->size = err = bb_strtou(ar.formatted.size, NULL, 10);
  59. if (err == -1) bb_error_msg_and_die("invalid ar header");
  60. /* long filenames have '/' as the first character */
  61. if (ar.formatted.name[0] == '/') {
  62. #if ENABLE_FEATURE_AR_LONG_FILENAMES
  63. unsigned long_offset;
  64. if (ar.formatted.name[1] == '/') {
  65. /* If the second char is a '/' then this entries data section
  66. * stores long filename for multiple entries, they are stored
  67. * in static variable long_names for use in future entries */
  68. ar_long_name_size = typed->size;
  69. ar_long_names = xmalloc(ar_long_name_size);
  70. xread(archive_handle->src_fd, ar_long_names, ar_long_name_size);
  71. archive_handle->offset += ar_long_name_size;
  72. /* This ar entries data section only contained filenames for other records
  73. * they are stored in the static ar_long_names for future reference */
  74. return get_header_ar(archive_handle); /* Return next header */
  75. }
  76. if (ar.formatted.name[1] == ' ') {
  77. /* This is the index of symbols in the file for compilers */
  78. data_skip(archive_handle);
  79. archive_handle->offset += typed->size;
  80. return get_header_ar(archive_handle); /* Return next header */
  81. }
  82. /* The number after the '/' indicates the offset in the ar data section
  83. * (saved in variable long_name) that conatains the real filename */
  84. long_offset = atoi(&ar.formatted.name[1]);
  85. if (long_offset >= ar_long_name_size) {
  86. bb_error_msg_and_die("can't resolve long filename");
  87. }
  88. typed->name = xstrdup(ar_long_names + long_offset);
  89. #else
  90. bb_error_msg_and_die("long filenames not supported");
  91. #endif
  92. } else {
  93. /* short filenames */
  94. typed->name = xstrndup(ar.formatted.name, 16);
  95. }
  96. typed->name[strcspn(typed->name, " /")] = '\0';
  97. if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) {
  98. archive_handle->action_header(typed);
  99. if (archive_handle->sub_archive) {
  100. while (archive_handle->action_data_subarchive(archive_handle->sub_archive) == EXIT_SUCCESS)
  101. /* repeat */;
  102. } else {
  103. archive_handle->action_data(archive_handle);
  104. }
  105. } else {
  106. data_skip(archive_handle);
  107. }
  108. archive_handle->offset += typed->size;
  109. /* Set the file pointer to the correct spot, we may have been reading a compressed file */
  110. lseek(archive_handle->src_fd, archive_handle->offset, SEEK_SET);
  111. return EXIT_SUCCESS;
  112. }