get_header_ar.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License as published by
  4. * the Free Software Foundation; either version 2 of the License, or
  5. * (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU Library General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. */
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <unistd.h>
  20. #include "unarchive.h"
  21. #include "libbb.h"
  22. extern char get_header_ar(archive_handle_t *archive_handle)
  23. {
  24. file_header_t *typed = archive_handle->file_header;
  25. union {
  26. char raw[60];
  27. struct {
  28. char name[16];
  29. char date[12];
  30. char uid[6];
  31. char gid[6];
  32. char mode[8];
  33. char size[10];
  34. char magic[2];
  35. } formated;
  36. } ar;
  37. #ifdef CONFIG_FEATURE_AR_LONG_FILENAMES
  38. static char *ar_long_names;
  39. static unsigned int ar_long_name_size;
  40. #endif
  41. /* dont use bb_xread as we want to handle the error ourself */
  42. if (read(archive_handle->src_fd, ar.raw, 60) != 60) {
  43. /* End Of File */
  44. return(EXIT_FAILURE);
  45. }
  46. /* ar header starts on an even byte (2 byte aligned)
  47. * '\n' is used for padding
  48. */
  49. if (ar.raw[0] == '\n') {
  50. /* fix up the header, we started reading 1 byte too early */
  51. memmove(ar.raw, &ar.raw[1], 59);
  52. ar.raw[59] = bb_xread_char(archive_handle->src_fd);
  53. archive_handle->offset++;
  54. }
  55. archive_handle->offset += 60;
  56. /* align the headers based on the header magic */
  57. if ((ar.formated.magic[0] != '`') || (ar.formated.magic[1] != '\n')) {
  58. bb_error_msg_and_die("Invalid ar header");
  59. }
  60. typed->mode = strtol(ar.formated.mode, NULL, 8);
  61. typed->mtime = atoi(ar.formated.date);
  62. typed->uid = atoi(ar.formated.uid);
  63. typed->gid = atoi(ar.formated.gid);
  64. typed->size = atoi(ar.formated.size);
  65. /* long filenames have '/' as the first character */
  66. if (ar.formated.name[0] == '/') {
  67. #ifdef CONFIG_FEATURE_AR_LONG_FILENAMES
  68. if (ar.formated.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. ar_long_name_size = typed->size;
  73. ar_long_names = xmalloc(ar_long_name_size);
  74. bb_xread_all(archive_handle->src_fd, ar_long_names, ar_long_name_size);
  75. archive_handle->offset += ar_long_name_size;
  76. /* This ar entries data section only contained filenames for other records
  77. * they are stored in the static ar_long_names for future reference */
  78. return (get_header_ar(archive_handle)); /* Return next header */
  79. } else if (ar.formated.name[1] == ' ') {
  80. /* This is the index of symbols in the file for compilers */
  81. data_skip(archive_handle);
  82. archive_handle->offset += typed->size;
  83. return (get_header_ar(archive_handle)); /* Return next header */
  84. } else {
  85. /* The number after the '/' indicates the offset in the ar data section
  86. (saved in variable long_name) that conatains the real filename */
  87. const unsigned int long_offset = atoi(&ar.formated.name[1]);
  88. if (long_offset >= ar_long_name_size) {
  89. bb_error_msg_and_die("Cant resolve long filename");
  90. }
  91. typed->name = bb_xstrdup(ar_long_names + long_offset);
  92. }
  93. #else
  94. bb_error_msg_and_die("long filenames not supported");
  95. #endif
  96. } else {
  97. /* short filenames */
  98. typed->name = bb_xstrndup(ar.formated.name, 16);
  99. }
  100. typed->name[strcspn(typed->name, " /")] = '\0';
  101. if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) {
  102. archive_handle->action_header(typed);
  103. if (archive_handle->sub_archive) {
  104. while (archive_handle->action_data_subarchive(archive_handle->sub_archive) == EXIT_SUCCESS);
  105. } else {
  106. archive_handle->action_data(archive_handle);
  107. }
  108. } else {
  109. data_skip(archive_handle);
  110. }
  111. archive_handle->offset += typed->size;
  112. /* Set the file pointer to the correct spot, we may have been reading a compressed file */
  113. lseek(archive_handle->src_fd, archive_handle->offset, SEEK_SET);
  114. return(EXIT_SUCCESS);
  115. }