lppf.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*****************************************************************************
  2. Linux PPF patcher v0.1-rc1
  3. Copyright (C), Daniel Ekström <dv01dem@cs.umu.se>, 2007 - 2008
  4. More information can be found at http://oakstream.mine.nu
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. ******************************************************************************/
  17. #include <getopt.h>
  18. #include <stdio.h>
  19. #include "libppf.h"
  20. #define VERSION "v0.1-rc1"
  21. /*
  22. * Returns a string representing the error code
  23. */
  24. const char *errorCode(int error, ppf_t *ppf) {
  25. static char errorString[1024];
  26. switch (error) {
  27. case 0x01:
  28. sprintf(errorString, "File %s is NOT a PPF file!", ppf->ppfName);
  29. case 0x02:
  30. sprintf(errorString, "PPF version not supported or unknown");
  31. case 0x03:
  32. sprintf(errorString, "No such PPF file: %s", ppf->ppfName);
  33. case 0x04:
  34. sprintf(errorString, "Error opening PPF file: %s", ppf->ppfName);
  35. case 0x05:
  36. sprintf(errorString, "Error closing PPF file: %s", ppf->ppfName);
  37. case 0x06:
  38. sprintf(errorString, "Error reading from PPF file: %s", ppf->ppfName);
  39. case 0x07:
  40. sprintf(errorString, "PPF file hasn't been loaded");
  41. case 0x08:
  42. sprintf(errorString, "No undo data available");
  43. case 0x11:
  44. sprintf(errorString, "No such file: %s", ppf->isoName);
  45. case 0x12:
  46. sprintf(errorString, "Error opening file: %s", ppf->isoName);
  47. case 0x13:
  48. sprintf(errorString, "Error closing file: %s", ppf->isoName);
  49. case 0x14:
  50. sprintf(errorString, "Error reading from file: %s", ppf->isoName);
  51. case 0x15:
  52. sprintf(errorString, "Error writing to file: %s", ppf->isoName);
  53. default:
  54. sprintf(errorString, "Unknown error code!");
  55. }
  56. return errorString;
  57. }
  58. /*
  59. * Print usage
  60. */
  61. void usage() {
  62. fprintf(stderr, "Linux PPF patcher " VERSION "\n\n"
  63. "Usage: lppf ppf-patch [action] [image]\n"
  64. "Actions:\n"
  65. " -p image patch the given image\n"
  66. " -u image undo the given image\n"
  67. " -h print this help\n"
  68. " -v print version number and exit\n\n"
  69. "The undo action only works on v3.0 patches "
  70. "with undo data available.\n"
  71. "If nothing except the patch is given, lppf will "
  72. "print patch info and exit.\n\n"
  73. "Copyright (C), 2007 - 2008 Daniel Ekström "
  74. "<dv01dem@cs.umu.se>\n");
  75. }
  76. int main(int argc, char **argv) {
  77. ppf_t ppf;
  78. int error, opt;
  79. char *isoName;
  80. // First argument must be a patch file and dump it if no more options
  81. // are given
  82. if (argc == 1) {
  83. usage();
  84. return 0;
  85. }
  86. ppf_init(&ppf);
  87. // Parse argument with getopt
  88. while ((opt = getopt(argc, argv, "hp:u:v")) != -1) {
  89. switch (opt) {
  90. case 'h':
  91. usage();
  92. return 0;
  93. case 'p':
  94. // Load PPF first
  95. if ((error = ppf_loadPatch(&ppf, argv[1])) != 0) {
  96. fprintf(stderr, "%s\n", errorCode(error, &ppf));
  97. return 1;
  98. }
  99. // Apply PPF data to file
  100. isoName = argv[optind - 1];
  101. fprintf(stderr, "Applying PPF data from %s...\n", ppf.ppfName);
  102. if ((error = ppf_applyPatch(&ppf, isoName, false)) != 0) {
  103. fprintf(stderr, "%s\n", errorCode(error, &ppf));
  104. } else {
  105. fprintf(stderr, "%d bytes in %d chunks successfully written to %s!\n",
  106. ppf.totalSize, ppf.chunkCount, ppf.isoName);
  107. }
  108. return 0;
  109. case 'u':
  110. // Load PPF first
  111. if ((error = ppf_loadPatch(&ppf, argv[1])) != 0) {
  112. fprintf(stderr, "%s\n", errorCode(error, &ppf));
  113. return 1;
  114. }
  115. // Apply PPF undo data to file
  116. isoName = argv[optind - 1];
  117. fprintf(stderr, "Applying PPF undo data from %s...\n", ppf.ppfName);
  118. if ((error = ppf_applyPatch(&ppf, isoName, true)) != 0) {
  119. fprintf(stderr, "%s\n", errorCode(error, &ppf));
  120. } else {
  121. fprintf(stderr, "%d bytes in %d chunks successfully written to %s!\n",
  122. ppf.totalSize, ppf.chunkCount, ppf.isoName);
  123. }
  124. return 0;
  125. case 'v':
  126. fprintf(stderr, "lppf-" VERSION "\n");
  127. return 0;
  128. case ':':
  129. fprintf(stderr, "Error: option '%c' needs an argument, aborting...\n", optopt);
  130. return 0;
  131. case '?':
  132. fprintf(stderr, "Error: unknown option '%c', aborting...\n", optopt);
  133. return 0;
  134. }
  135. }
  136. // If we've gotten this far without returning, no arguments was given
  137. if ((error = ppf_loadPatch(&ppf, argv[1])) != 0) {
  138. fprintf(stderr, "%s\n", errorCode(error, &ppf));
  139. return 1;
  140. }
  141. ppf_dumpInfo(&ppf);
  142. return 0;
  143. }