get_header_ar.c 4.5 KB

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