get_header_ar.c 4.4 KB

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