Advertisement
Guest User

dsf_pack.c

a guest
Jan 18th, 2011
3,233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.83 KB | None | 0 0
  1. /*
  2.  *
  3.  * dsf_pack: HDi Dune Media Player service file packer.
  4.  *
  5.  * Accident 2009: Epoch, encode small files, needs extra work for
  6.  *                encodes over one page (I think) Never tried it.
  7.  *
  8.  * This is woefully incomplete. But it was enough for my requirements.
  9.  *
  10.  * For example: create a file with:
  11.  * #!/bin/sh
  12.  * /usr/sbin/telnetd &
  13.  * exit 0
  14.  *
  15.  * Then pack it up. Note that the file must be named
  16.  * "dune_service_XXXX.dsf" for it to be recognised.
  17.  *
  18.  * When the Dune runs it, it copies it to /tmp, gunzips it as
  19.  * /tmp/service and runs it.
  20.  *
  21.  */
  22.  
  23. #include <stdio.h>
  24. #include <fcntl.h>
  25. #include <stdint.h>
  26. #include <unistd.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29.  
  30.  
  31. extern int getopt();
  32. extern char *optarg;
  33. extern int optind;
  34.  
  35. static char *arg_decode      = NULL;
  36. static char *arg_encode      = NULL;
  37. static char *arg_outfilename = NULL;
  38. static char *arg_comparename = NULL;
  39. static int   arg_verbose     = 0;
  40.  
  41.  
  42. // XOR block pulled from "shell".
  43. static uint8_t key[] = {
  44.         0xDA, 0xDF, 0xDD, 0x05, 0x53, 0x40, 0x45, 0xb3, 0xee, 0xcc, 0x26, 0x5e,
  45.         0xb8, 0x0b, 0x25, 0xdb, 0xa2, 0xe6, 0xec, 0x63, 0xf2, 0xe1, 0x19, 0x76,
  46.         0x08, 0x43, 0x38, 0x6f, 0xc5, 0xc1, 0x85, 0x46
  47. };
  48.  
  49.  
  50.  
  51. static void options(char *prog)
  52. {
  53.         printf("\n");
  54.         printf("%s - Dune .dsf encode/decode program\n\n", prog);
  55.         printf("  options:\n");
  56.         printf("  -h          : display usage help (this output)\n");
  57.         printf("  -d filename : decode .dsf to outfile.gz\n");
  58.         printf("  -e filename : encode infile.gz to .dsf\n");
  59.         printf("  -o filename : specify different output filename\n");
  60.         printf("  -c filename : compare encode with file\n");
  61.         printf("  -v          : verbose it up!\n");
  62.         printf("\n");
  63.         printf("Be aware that dune service files have to be named dune_service_*.dsf\n\n");
  64.         printf("Dune encoding tool by Accident.\nResist the temptation to do bad!\n\n");
  65.         exit(0);
  66. }
  67.  
  68.  
  69.  
  70. void arguments(int argc, char **argv)
  71. {
  72.         int opt;
  73.    
  74.         while ((opt=getopt(argc, argv,
  75.                                            "hd:e:o:vc:"
  76.                                            )) != -1) {
  77.  
  78.                 switch(opt) {
  79.  
  80.                 case 'h':
  81.                         options(argv[0]);
  82.                         break;
  83.                 case 'd':
  84.                         arg_decode = strdup(optarg);
  85.                         break;
  86.                 case 'e':
  87.                         arg_encode = strdup(optarg);
  88.                         break;
  89.                 case 'o':
  90.                         arg_outfilename = strdup(optarg);
  91.                         break;
  92.                 case 'c':
  93.                         arg_comparename = strdup(optarg);
  94.                         break;
  95.                 case 'v':
  96.                         arg_verbose++;
  97.                         break;
  98.                 }
  99.         }
  100.  
  101.         if (!arg_decode && !arg_encode)
  102.                 options(argv[0]);
  103. }
  104.  
  105.  
  106.  
  107.  
  108. int main(int argc, char **argv)
  109. {
  110.         FILE *fd = NULL, *out = NULL;
  111.         uint8_t buffer[0x1000], v0,v1,a1,nv0;
  112.         uint32_t red, i, half_red;
  113.         uint32_t decode = 0;
  114.  
  115.  
  116.         arguments(argc, argv);
  117.  
  118.         if (arg_decode)
  119.                 dune_decode(arg_decode);
  120.  
  121.         if (arg_encode)
  122.                 dune_encode(arg_encode);
  123.  
  124. }
  125.  
  126.  
  127.  
  128. int dune_decode(char *infilename)
  129. {
  130.         FILE *fd = NULL, *out = NULL;
  131.         uint8_t buffer[0x1000], v0,v1,a1,nv0;
  132.         uint32_t red, i, half_red;
  133.         uint32_t decode = 0;
  134.  
  135.  
  136.         fd = fopen(infilename, "rb");
  137.         if (!fd) goto failed;
  138.  
  139.         out = fopen(arg_outfilename ? arg_outfilename : "outfile.gz", "wb");
  140.         if (!out) goto failed;
  141.  
  142.         // read header
  143.         if (fread(buffer, 0x141, 1, fd) != 1) goto failed;
  144.  
  145.         // Read blocks:
  146.         while ((red = fread(buffer, 1, sizeof(buffer), fd)) > 0) {
  147.                 // Decode block
  148.  
  149.                 if (arg_verbose)
  150.                         printf("read %x \n", red);
  151.  
  152.                 half_red = red>>1;
  153.  
  154.                 for (i = 0; i < half_red; i++) {
  155.  
  156.                         v0 = buffer[i + half_red] ;
  157.                         v1 = key[decode];
  158.                         a1 = buffer[ i ];
  159.  
  160.                         nv0 = v0;
  161.                         nv0 ^= v1;
  162.                         nv0 ^= a1;
  163.  
  164.                         if (arg_verbose)
  165.                                 printf("  [%x]=%02X, xor %02X, [%x]=%02X, storing %02X\n",
  166.                                            i+half_red, v0, v1, i, a1, nv0);
  167.  
  168.                         buffer[i] = nv0;
  169.  
  170.                         decode++;
  171.                         if (decode >= sizeof(key))
  172.                                 decode = 0;
  173.                 }
  174.  
  175.  
  176.                 for (i = half_red; i < red; i++) {
  177.  
  178.                         v1 = key[decode];
  179.                         a1 = buffer[ i ];
  180.  
  181.                         nv0 = v1;
  182.                         nv0 ^= a1;
  183.  
  184.                         if (arg_verbose)
  185.                                 printf("  xor %02X, [%x]=%02X, storing %02X\n",
  186.                                            v1, i, a1, nv0);
  187.  
  188.                         buffer[i] = nv0;
  189.  
  190.                         decode++;
  191.                         if (decode >= sizeof(key))
  192.                                 decode = 0;
  193.                 }
  194.  
  195.  
  196.                 // Write block
  197.                 fwrite(buffer, red, 1, out);
  198.  
  199.         }
  200.  
  201.         fclose(fd);
  202.         fclose(out);
  203.  
  204.         printf("Decode complete\n");
  205.  
  206.         return 0;
  207.  
  208.  failed:
  209.         perror(":");
  210.         printf("failed\n");
  211.         if (fd)
  212.                 fclose(fd);
  213.         else
  214.                 fprintf(stderr, "Failed to open infile '%s'\r\n", infilename);
  215.  
  216.         if (out)
  217.                 fclose(out);
  218.         else if (fd)
  219.                 fprintf(stderr, "Failed to open outfile '%s'\r\n",
  220.                                 arg_outfilename ? arg_outfilename : "outfile.gz");
  221.  
  222. }
  223.  
  224.  
  225.  
  226. int dune_encode(char *infilename)
  227. {
  228.  
  229.         FILE *fd = NULL, *out = NULL, *compare=NULL;
  230.         uint8_t buffer[0x1000], v0,v1,a1,nv0;
  231.         uint8_t compbuf[0x1000];
  232.         uint32_t red, i, half_red;
  233.         uint32_t decode = 0;
  234.         unsigned char stuff[] = {
  235.                 0x1d,0x25,0x7a,0xe4,0x27,0xff,0xe9,0x8c,0x58,0x0a,0x8b,0x18,0x70,0xbb,0xf2,0x87,
  236.                 0x3a,0xf2,0x41,0x68,0x9e,0x81,0x85,0xc6,0x82,0xe1,0x92,0x7d,0xa0,0x4e,0x5b,0xbd,
  237.                 0x73,0xd5,0xa1,0x9a,0xd4,0x8a,0x27,0x2c,0x95,0xb2,0x44,0x05,0x6d,0x37,0x8c,0xa7,
  238.                 0x29,0xce,0x0f,0xc7,0x4f,0x94,0x8e,0xd2,0x75,0x20,0x4f,0x15,0x6e,0xaa,0xd3,0xe1,0x7f
  239.         };
  240.  
  241.  
  242.         fd = fopen(infilename, "rb");
  243.         if (!fd) goto failed;
  244.  
  245.         out = fopen(arg_outfilename ? arg_outfilename : "outfile.dsf", "wb");
  246.         if (!out) goto failed;
  247.  
  248.         if (arg_comparename)
  249.                 compare=fopen(arg_comparename, "rb");
  250.  
  251.         // read header
  252.         fwrite("DUNE SERVICE FILE", 18, 1, out);
  253.         // One day work out what these are.
  254.         fwrite("86673687", 9, 1, out);
  255.         fwrite("54139262", 9, 1, out);
  256.         fwrite("53151487", 9, 1, out);
  257.         fwrite("50852538", 9, 1, out);
  258.         fwrite("38155310", 9, 1, out);
  259.         fwrite("9047468", 8, 1, out);
  260.         fwrite("46935104", 9, 1, out);
  261.         fwrite("64658461", 9, 1, out);
  262.         fwrite("64007208", 9, 1, out);
  263.         fwrite("93556737", 9, 1, out);
  264.         fwrite("88478556", 9, 1, out);
  265.         fwrite("6195915", 8, 1, out);
  266.         fwrite("677883", 7, 1, out);
  267.         fwrite("9851071", 8, 1, out);
  268.         fwrite("82589884", 9, 1, out);
  269.         fwrite("81953245", 9, 1, out);
  270.  
  271.         // First header thing is 0x100.
  272.         fseek(out, 0x100, SEEK_SET);
  273.  
  274.         // Then there is 0x41 bytes of something.
  275.         fwrite(stuff, sizeof(stuff), 1, out);
  276.  
  277.  
  278.         if (compare)
  279.                 fseek(compare, 0x141, SEEK_SET);
  280.  
  281.  
  282.         // Read blocks:
  283.         while ((red = fread(buffer, 1, sizeof(buffer), fd)) > 0) {
  284.                 // Encode block
  285.                 if (arg_verbose)
  286.                         printf("read %x \n", red);
  287.  
  288.                 if (compare)
  289.                         fread(compbuf, 1, red, compare);
  290.  
  291.  
  292.                 half_red = red>>1;
  293.                 decode = half_red % sizeof(key);
  294.                 if (arg_verbose) printf("Decode start %d\n", decode);
  295.  
  296.                 for (i = half_red; i < red; i++) {
  297.  
  298.                         v1 = key[decode];
  299.                         a1 = buffer[ i ];
  300.  
  301.                         nv0 = v1;
  302.                         nv0 ^= a1;
  303.  
  304.                         if (arg_verbose)
  305.                                 printf("  xor %02X, [%x]=%02X, storing %02X\n",
  306.                                            v1, i, a1, nv0);
  307.  
  308.                         buffer[i] = nv0;
  309.  
  310.                         if (compare &&
  311.                                 buffer[i] != compbuf[i])
  312.                                 printf("Compare buf has %02X\n", compbuf[i]);
  313.  
  314.                         decode++;
  315.                         if (decode >= sizeof(key))
  316.                                 decode = 0;
  317.                 }
  318.  
  319.                 decode = 0;
  320.                 if (arg_verbose) printf("Halfway point as decode %d\n", decode);
  321.  
  322.                 for (i = 0; i < half_red; i++) {
  323.  
  324.                         v0 = buffer[i + half_red] ;
  325.                         v1 = key[decode];
  326.                         a1 = buffer[ i ];
  327.  
  328.                         nv0 = v0;
  329.                         nv0 ^= v1;
  330.                         nv0 ^= a1;
  331.  
  332.                         if (arg_verbose)
  333.                                 printf("  [%x]=%02X, xor %02X, [%x]=%02X, storing %02X\n",
  334.                                            i+half_red, v0, v1, i, a1, nv0);
  335.  
  336.                         buffer[i] = nv0;
  337.  
  338.                         if (compare &&
  339.                                 buffer[i] != compbuf[i])
  340.                                 printf("Compare buf has %02X\n", compbuf[i]);
  341.  
  342.                         decode++;
  343.                         if (decode >= sizeof(key))
  344.                                 decode = 0;
  345.                 }
  346.  
  347.                 // Write block
  348.                 fwrite(buffer, red, 1, out);
  349.  
  350.  
  351.         }
  352.  
  353.         fclose(fd);
  354.         fclose(out);
  355.         if (compare)
  356.                 fclose(compare);
  357.  
  358.         printf("Encode complete\n");
  359.  
  360.         return 0;
  361.  
  362.  failed:
  363.         perror(":");
  364.         printf("failed\n");
  365.         if (fd)
  366.                 fclose(fd);
  367.         else
  368.                 fprintf(stderr, "Failed to open infile '%s'\r\n", infilename);
  369.  
  370.         if (out)
  371.                 fclose(out);
  372.         else if (fd)
  373.                 fprintf(stderr, "Failed to open outfile '%s'\r\n",
  374.                                 arg_outfilename ? arg_outfilename : "outfile.gz");
  375.  
  376.         if (compare)
  377.                 fclose(compare);
  378.  
  379. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement