get_header_ar.c 4.0 KB

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