rpm2cpio.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 "\355\253\356\333"
  12. #define RPM_HEADER_MAGIC "\216\255\350"
  13. struct rpm_lead {
  14. unsigned char magic[4];
  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. struct rpm_header {
  24. char magic[3]; /* 3 byte magic: 0x8e 0xad 0xe8 */
  25. uint8_t version; /* 1 byte version number */
  26. uint32_t reserved; /* 4 bytes reserved */
  27. uint32_t entries; /* Number of entries in header (4 bytes) */
  28. uint32_t size; /* Size of store (4 bytes) */
  29. };
  30. static void skip_header(int rpm_fd)
  31. {
  32. struct rpm_header header;
  33. xread(rpm_fd, &header, sizeof(struct rpm_header));
  34. if (strncmp((char *) &header.magic, RPM_HEADER_MAGIC, 3) != 0) {
  35. bb_error_msg_and_die("invalid RPM header magic"); /* Invalid magic */
  36. }
  37. if (header.version != 1) {
  38. bb_error_msg_and_die("unsupported RPM header version"); /* This program only supports v1 headers */
  39. }
  40. header.entries = ntohl(header.entries);
  41. header.size = ntohl(header.size);
  42. lseek (rpm_fd, 16 * header.entries, SEEK_CUR); /* Seek past index entries */
  43. lseek (rpm_fd, header.size, SEEK_CUR); /* Seek past store */
  44. }
  45. /* No getopt required */
  46. int rpm2cpio_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  47. int rpm2cpio_main(int argc, char **argv)
  48. {
  49. struct rpm_lead lead;
  50. int rpm_fd;
  51. unsigned char magic[2];
  52. if (argc == 1) {
  53. rpm_fd = STDIN_FILENO;
  54. } else {
  55. rpm_fd = xopen(argv[1], O_RDONLY);
  56. }
  57. xread(rpm_fd, &lead, sizeof(struct rpm_lead));
  58. if (strncmp((char *) &lead.magic, RPM_MAGIC, 4) != 0) {
  59. bb_error_msg_and_die("invalid RPM magic"); /* Just check the magic, the rest is irrelevant */
  60. }
  61. /* Skip the signature header */
  62. skip_header(rpm_fd);
  63. lseek(rpm_fd, (8 - (lseek(rpm_fd, 0, SEEK_CUR) % 8)) % 8, SEEK_CUR);
  64. /* Skip the main header */
  65. skip_header(rpm_fd);
  66. xread(rpm_fd, &magic, 2);
  67. if ((magic[0] != 0x1f) || (magic[1] != 0x8b)) {
  68. bb_error_msg_and_die("invalid gzip magic");
  69. }
  70. if (unpack_gz_stream(rpm_fd, STDOUT_FILENO) < 0) {
  71. bb_error_msg("error inflating");
  72. }
  73. close(rpm_fd);
  74. return 0;
  75. }