rpm2cpio.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini rpm2cpio implementation for busybox
  4. *
  5. * Copyright (C) 2001 by Laurence Anderson
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <sys/types.h>
  22. #include <netinet/in.h> /* For ntohl & htonl function */
  23. #include <fcntl.h>
  24. #include <unistd.h>
  25. #include <string.h>
  26. #include "busybox.h"
  27. #include "unarchive.h"
  28. #define RPM_MAGIC "\355\253\356\333"
  29. #define RPM_HEADER_MAGIC "\216\255\350"
  30. struct rpm_lead {
  31. unsigned char magic[4];
  32. uint8_t major, minor;
  33. uint16_t type;
  34. uint16_t archnum;
  35. char name[66];
  36. uint16_t osnum;
  37. uint16_t signature_type;
  38. char reserved[16];
  39. };
  40. struct rpm_header {
  41. char magic[3]; /* 3 byte magic: 0x8e 0xad 0xe8 */
  42. uint8_t version; /* 1 byte version number */
  43. uint32_t reserved; /* 4 bytes reserved */
  44. uint32_t entries; /* Number of entries in header (4 bytes) */
  45. uint32_t size; /* Size of store (4 bytes) */
  46. };
  47. static void skip_header(int rpm_fd)
  48. {
  49. struct rpm_header header;
  50. bb_xread_all(rpm_fd, &header, sizeof(struct rpm_header));
  51. if (strncmp((char *) &header.magic, RPM_HEADER_MAGIC, 3) != 0) {
  52. bb_error_msg_and_die("Invalid RPM header magic"); /* Invalid magic */
  53. }
  54. if (header.version != 1) {
  55. bb_error_msg_and_die("Unsupported RPM header version"); /* This program only supports v1 headers */
  56. }
  57. header.entries = ntohl(header.entries);
  58. header.size = ntohl(header.size);
  59. lseek (rpm_fd, 16 * header.entries, SEEK_CUR); /* Seek past index entries */
  60. lseek (rpm_fd, header.size, SEEK_CUR); /* Seek past store */
  61. }
  62. /* No getopt required */
  63. extern int rpm2cpio_main(int argc, char **argv)
  64. {
  65. struct rpm_lead lead;
  66. int rpm_fd;
  67. unsigned char magic[2];
  68. if (argc == 1) {
  69. rpm_fd = STDIN_FILENO;
  70. } else {
  71. rpm_fd = bb_xopen(argv[1], O_RDONLY);
  72. }
  73. bb_xread_all(rpm_fd, &lead, sizeof(struct rpm_lead));
  74. if (strncmp((char *) &lead.magic, RPM_MAGIC, 4) != 0) {
  75. bb_error_msg_and_die("Invalid RPM magic"); /* Just check the magic, the rest is irrelevant */
  76. }
  77. /* Skip the signature header */
  78. skip_header(rpm_fd);
  79. lseek(rpm_fd, (8 - (lseek(rpm_fd, 0, SEEK_CUR) % 8)) % 8, SEEK_CUR);
  80. /* Skip the main header */
  81. skip_header(rpm_fd);
  82. bb_xread_all(rpm_fd, &magic, 2);
  83. if ((magic[0] != 0x1f) || (magic[1] != 0x8b)) {
  84. bb_error_msg_and_die("Invalid gzip magic");
  85. }
  86. check_header_gzip(rpm_fd);
  87. if (inflate_gunzip(rpm_fd, STDOUT_FILENO) != 0) {
  88. bb_error_msg("Error inflating");
  89. }
  90. close(rpm_fd);
  91. return 0;
  92. }