3
0

rpm2cpio.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. #define RPM_MAGIC 0xedabeedb
  12. #define RPM_MAGIC_STR "\355\253\356\333"
  13. struct rpm_lead {
  14. uint32_t magic;
  15. uint8_t major, minor;
  16. uint16_t type;
  17. uint16_t archnum;
  18. char name[66];
  19. uint16_t osnum;
  20. uint16_t signature_type;
  21. char reserved[16];
  22. };
  23. #define RPM_HEADER_MAGICnVER 0x8eade801
  24. #define RPM_HEADER_MAGIC_STR "\216\255\350"
  25. struct rpm_header {
  26. uint32_t magic_and_ver; /* 3 byte magic: 0x8e 0xad 0xe8; 1 byte version */
  27. uint32_t reserved; /* 4 bytes reserved */
  28. uint32_t entries; /* Number of entries in header (4 bytes) */
  29. uint32_t size; /* Size of store (4 bytes) */
  30. };
  31. enum { rpm_fd = STDIN_FILENO };
  32. static unsigned skip_header(void)
  33. {
  34. struct rpm_header header;
  35. unsigned len;
  36. xread(rpm_fd, &header, sizeof(header));
  37. // if (strncmp((char *) &header.magic, RPM_HEADER_MAGIC_STR, 3) != 0) {
  38. // bb_error_msg_and_die("invalid RPM header magic");
  39. // }
  40. // if (header.version != 1) {
  41. // bb_error_msg_and_die("unsupported RPM header version");
  42. // }
  43. if (header.magic_and_ver != htonl(RPM_HEADER_MAGICnVER)) {
  44. bb_error_msg_and_die("invalid RPM header magic or unsupported version");
  45. // ": %x != %x", header.magic_and_ver, htonl(RPM_HEADER_MAGICnVER));
  46. }
  47. /* Seek past index entries, and past store */
  48. len = 16 * ntohl(header.entries) + ntohl(header.size);
  49. seek_by_jump(rpm_fd, len);
  50. return sizeof(header) + len;
  51. }
  52. /* No getopt required */
  53. int rpm2cpio_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  54. int rpm2cpio_main(int argc UNUSED_PARAM, char **argv)
  55. {
  56. struct rpm_lead lead;
  57. unsigned pos;
  58. unsigned char magic[2];
  59. IF_DESKTOP(long long) int FAST_FUNC (*unpack)(int src_fd, int dst_fd);
  60. if (argv[1]) {
  61. xmove_fd(xopen(argv[1], O_RDONLY), rpm_fd);
  62. }
  63. xread(rpm_fd, &lead, sizeof(lead));
  64. /* Just check the magic, the rest is irrelevant */
  65. if (lead.magic != htonl(RPM_MAGIC)) {
  66. bb_error_msg_and_die("invalid RPM magic");
  67. }
  68. /* Skip the signature header, align to 8 bytes */
  69. pos = skip_header();
  70. seek_by_jump(rpm_fd, (8 - pos) & 7);
  71. /* Skip the main header */
  72. skip_header();
  73. xread(rpm_fd, &magic, 2);
  74. unpack = unpack_gz_stream;
  75. if (magic[0] != 0x1f || magic[1] != 0x8b) {
  76. if (!ENABLE_FEATURE_SEAMLESS_BZ2
  77. || magic[0] != 'B' || magic[1] != 'Z'
  78. ) {
  79. bb_error_msg_and_die("invalid gzip"
  80. IF_FEATURE_SEAMLESS_BZ2("/bzip2")
  81. " magic");
  82. }
  83. unpack = unpack_bz2_stream;
  84. }
  85. if (unpack(rpm_fd, STDOUT_FILENO) < 0) {
  86. bb_error_msg_and_die("error unpacking");
  87. }
  88. if (ENABLE_FEATURE_CLEAN_UP) {
  89. close(rpm_fd);
  90. }
  91. return 0;
  92. }