rpm2cpio.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 tarball for details.
  8. */
  9. #include "libbb.h"
  10. #include "unarchive.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. unsigned char magic[2];
  61. IF_DESKTOP(long long) int FAST_FUNC (*unpack)(int src_fd, int dst_fd);
  62. xread(rpm_fd, &magic, 2);
  63. unpack = unpack_gz_stream;
  64. if (magic[0] != 0x1f || magic[1] != 0x8b) {
  65. if (!ENABLE_FEATURE_SEAMLESS_BZ2
  66. || magic[0] != 'B' || magic[1] != 'Z'
  67. ) {
  68. bb_error_msg_and_die("invalid gzip"
  69. IF_FEATURE_SEAMLESS_BZ2("/bzip2")
  70. " magic");
  71. }
  72. unpack = unpack_bz2_stream;
  73. }
  74. if (unpack(rpm_fd, STDOUT_FILENO) < 0)
  75. bb_error_msg_and_die("error unpacking");
  76. }
  77. #endif
  78. if (ENABLE_FEATURE_CLEAN_UP) {
  79. close(rpm_fd);
  80. }
  81. return 0;
  82. }