get_header_ar.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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_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. #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. if (ar.formatted.magic[0] != '`' || ar.formatted.magic[1] != '\n')
  53. bb_error_msg_and_die("invalid ar header");
  54. /*
  55. * Note that the fields MUST be read in reverse order as
  56. * read_num() clobbers the next byte after the field!
  57. * Order is: name, date, uid, gid, mode, size, magic.
  58. */
  59. typed->size = size = read_num(ar.formatted.size, 10,
  60. sizeof(ar.formatted.size));
  61. /* special filenames have '/' as the first character */
  62. if (ar.formatted.name[0] == '/') {
  63. if (ar.formatted.name[1] == ' ') {
  64. /* This is the index of symbols in the file for compilers */
  65. data_skip(archive_handle);
  66. archive_handle->offset += size;
  67. return get_header_ar(archive_handle); /* Return next header */
  68. }
  69. #if ENABLE_FEATURE_AR_LONG_FILENAMES
  70. if (ar.formatted.name[1] == '/') {
  71. /* If the second char is a '/' then this entries data section
  72. * stores long filename for multiple entries, they are stored
  73. * in static variable long_names for use in future entries
  74. */
  75. ar_long_name_size = size;
  76. free(ar_long_names);
  77. ar_long_names = xmalloc(size);
  78. xread(archive_handle->src_fd, ar_long_names, size);
  79. archive_handle->offset += size;
  80. /* Return next header */
  81. return get_header_ar(archive_handle);
  82. }
  83. #else
  84. bb_error_msg_and_die("long filenames not supported");
  85. #endif
  86. }
  87. /* Only size is always present, the rest may be missing in
  88. * long filename pseudo file. Thus we decode the rest
  89. * after dealing with long filename pseudo file.
  90. */
  91. typed->mode = read_num(ar.formatted.mode, 8, sizeof(ar.formatted.mode));
  92. typed->gid = read_num(ar.formatted.gid, 10, sizeof(ar.formatted.gid));
  93. typed->uid = read_num(ar.formatted.uid, 10, sizeof(ar.formatted.uid));
  94. typed->mtime = read_num(ar.formatted.date, 10, sizeof(ar.formatted.date));
  95. #if ENABLE_FEATURE_AR_LONG_FILENAMES
  96. if (ar.formatted.name[0] == '/') {
  97. unsigned long_offset;
  98. /* The number after the '/' indicates the offset in the ar data section
  99. * (saved in ar_long_names) that conatains the real filename */
  100. long_offset = read_num(&ar.formatted.name[1], 10,
  101. sizeof(ar.formatted.name) - 1);
  102. if (long_offset >= ar_long_name_size) {
  103. bb_error_msg_and_die("can't resolve long filename");
  104. }
  105. typed->name = xstrdup(ar_long_names + long_offset);
  106. } else
  107. #endif
  108. {
  109. /* short filenames */
  110. typed->name = xstrndup(ar.formatted.name, 16);
  111. }
  112. typed->name[strcspn(typed->name, " /")] = '\0';
  113. if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) {
  114. archive_handle->action_header(typed);
  115. #if ENABLE_DPKG || ENABLE_DPKG_DEB
  116. if (archive_handle->dpkg__sub_archive) {
  117. struct archive_handle_t *sa = archive_handle->dpkg__sub_archive;
  118. while (archive_handle->dpkg__action_data_subarchive(sa) == EXIT_SUCCESS)
  119. continue;
  120. create_symlinks_from_list(sa->symlink_placeholders);
  121. } else
  122. #endif
  123. archive_handle->action_data(archive_handle);
  124. } else {
  125. data_skip(archive_handle);
  126. }
  127. archive_handle->offset += typed->size;
  128. /* Set the file pointer to the correct spot, we may have been reading a compressed file */
  129. lseek(archive_handle->src_fd, archive_handle->offset, SEEK_SET);
  130. return EXIT_SUCCESS;
  131. }