3
0

rpm2cpio.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini rpm2cpio implementation for busybox
  4. *
  5. * Copyright (C) 2001 by Laurence Anderson
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  8. */
  9. #include "libbb.h"
  10. #include "archive.h"
  11. #include "rpm.h"
  12. enum { rpm_fd = STDIN_FILENO };
  13. static unsigned skip_header(void)
  14. {
  15. struct rpm_header header;
  16. unsigned len;
  17. xread(rpm_fd, &header, sizeof(header));
  18. // if (strncmp((char *) &header.magic, RPM_HEADER_MAGIC_STR, 3) != 0) {
  19. // bb_error_msg_and_die("invalid RPM header magic");
  20. // }
  21. // if (header.version != 1) {
  22. // bb_error_msg_and_die("unsupported RPM header version");
  23. // }
  24. if (header.magic_and_ver != htonl(RPM_HEADER_MAGICnVER)) {
  25. bb_error_msg_and_die("invalid RPM header magic or unsupported version");
  26. // ": %x != %x", header.magic_and_ver, htonl(RPM_HEADER_MAGICnVER));
  27. }
  28. /* Seek past index entries, and past store */
  29. len = 16 * ntohl(header.entries) + ntohl(header.size);
  30. seek_by_jump(rpm_fd, len);
  31. return sizeof(header) + len;
  32. }
  33. /* No getopt required */
  34. int rpm2cpio_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  35. int rpm2cpio_main(int argc UNUSED_PARAM, char **argv)
  36. {
  37. struct rpm_lead lead;
  38. unsigned pos;
  39. if (argv[1]) {
  40. xmove_fd(xopen(argv[1], O_RDONLY), rpm_fd);
  41. }
  42. xread(rpm_fd, &lead, sizeof(lead));
  43. /* Just check the magic, the rest is irrelevant */
  44. if (lead.magic != htonl(RPM_LEAD_MAGIC)) {
  45. bb_error_msg_and_die("invalid RPM magic");
  46. }
  47. /* Skip the signature header, align to 8 bytes */
  48. pos = skip_header();
  49. seek_by_jump(rpm_fd, (-(int)pos) & 7);
  50. /* Skip the main header */
  51. skip_header();
  52. #if 0
  53. /* This works, but doesn't report uncompress errors (they happen in child) */
  54. setup_unzip_on_fd(rpm_fd /*fail_if_not_detected: 1*/);
  55. if (bb_copyfd_eof(rpm_fd, STDOUT_FILENO) < 0)
  56. bb_error_msg_and_die("error unpacking");
  57. #else
  58. /* BLOAT */
  59. {
  60. union {
  61. uint8_t b[4];
  62. uint16_t b16[2];
  63. uint32_t b32[1];
  64. } magic;
  65. IF_DESKTOP(long long) int FAST_FUNC (*unpack)(int src_fd, int dst_fd);
  66. xread(rpm_fd, magic.b16, sizeof(magic.b16[0]));
  67. if (magic.b16[0] == GZIP_MAGIC) {
  68. unpack = unpack_gz_stream;
  69. } else
  70. if (ENABLE_FEATURE_SEAMLESS_BZ2
  71. && magic.b16[0] == BZIP2_MAGIC
  72. ) {
  73. unpack = unpack_bz2_stream;
  74. } else
  75. if (ENABLE_FEATURE_SEAMLESS_XZ
  76. && magic.b16[0] == XZ_MAGIC1
  77. ) {
  78. xread(rpm_fd, magic.b32, sizeof(magic.b32[0]));
  79. if (magic.b32[0] != XZ_MAGIC2)
  80. goto no_magic;
  81. /* unpack_xz_stream wants fd at position 6, no need to seek */
  82. //xlseek(rpm_fd, -6, SEEK_CUR);
  83. unpack = unpack_xz_stream;
  84. } else {
  85. no_magic:
  86. bb_error_msg_and_die("no gzip"
  87. IF_FEATURE_SEAMLESS_BZ2("/bzip2")
  88. IF_FEATURE_SEAMLESS_XZ("/xz")
  89. " magic");
  90. }
  91. if (unpack(rpm_fd, STDOUT_FILENO) < 0)
  92. bb_error_msg_and_die("error unpacking");
  93. }
  94. #endif
  95. if (ENABLE_FEATURE_CLEAN_UP) {
  96. close(rpm_fd);
  97. }
  98. return 0;
  99. }