SHARE
TWEET

DOSBox memdump2bin v0.1

8bitbubsy Feb 26th, 2016 (edited) 138 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // memdump2bin v0.1 - Converts a DOSBox memdump.txt to binary - by 8bitbubsy
  2. // win32 only for now (strtok_s)
  3.  
  4. #define WIN32_LEAN_AND_MEAN
  5.  
  6. #include <stdio.h>
  7. #include <stdint.h>
  8. #include <string.h>
  9. #include <stdlib.h>
  10. #include <windows.h>
  11.  
  12. int main(int argc, char *argv[])
  13. {
  14.     uint8_t *fileBuffer;
  15.     uint32_t fileSize;
  16.     uint32_t bytesWritten;
  17.     uint32_t linesProcessed;
  18.     char *linePointer;
  19.     char *hexDataPointer;
  20.     char *hexByte;
  21.     char *token1;
  22.     char *token2;
  23.     char b1;
  24.     char b2;
  25.     FILE *in;
  26.     FILE *out;
  27.  
  28.     printf("memdump2bin v0.1 - by 8bitbubsy\n");
  29.     printf("===============================\n");
  30.  
  31.     // check if the number of arguments is correct
  32.     if (argc != 2)
  33.     {
  34.         printf("Usage: memdump2bin <filename>\n");
  35.         printf("Converts file and writes it to memdump.bin.\n");
  36.         system("PAUSE");
  37.         return (-1);
  38.     }
  39.  
  40.     // open input file
  41.     in = fopen(argv[1], "rb");
  42.     if (in == NULL)
  43.     {
  44.         printf("Error: Couldn't open input file!\n");
  45.         system("PAUSE");
  46.         return (1);
  47.     }
  48.  
  49.     // open output file
  50.     out = fopen("memdump.bin", "wb");
  51.     if (out == NULL)
  52.     {
  53.         printf("Error: Couldn't open output file!\n");
  54.         fclose(in);
  55.         system("PAUSE");
  56.         return (1);
  57.     }
  58.  
  59.     // get file size
  60.     fseek(in, 0, SEEK_END);
  61.     fileSize = ftell(in);
  62.     rewind(in);
  63.  
  64.     // 17 is the absolute minimum memdump length...
  65.     if (fileSize < 17)
  66.     {
  67.         printf("Error: Input file is not a valid memdump file!\n");
  68.         fclose(in);
  69.         fclose(out);
  70.         system("PAUSE");
  71.         return (1);
  72.     }
  73.  
  74.     // allocate memory for file buffer
  75.     fileBuffer = (uint8_t *)(malloc(fileSize));
  76.     if (fileBuffer == NULL)
  77.     {
  78.         printf("Error: Out of memory!\n");
  79.         fclose(in);
  80.         fclose(out);
  81.         system("PAUSE");
  82.         return (1);
  83.     }
  84.  
  85.     // read file to buffer and close input file handle
  86.     if (fread(fileBuffer, 1, fileSize, in) != fileSize)
  87.     {
  88.         printf("Error: I/O error while reading from input file!\n");
  89.         fclose(in);
  90.         fclose(out);
  91.         system("PAUSE");
  92.         return (1);
  93.     }
  94.  
  95.     // we're done with the input file now...
  96.     fclose(in);
  97.  
  98.     token1 = NULL;
  99.     token2 = NULL;
  100.     bytesWritten = 0;
  101.     linesProcessed = 0;
  102.  
  103.     // do the parsing...
  104.     printf("Parsing buffer...\n");
  105.     linePointer = strtok_s((char *)(fileBuffer), "\r\n", &token1);
  106.     while (linePointer != NULL)
  107.     {
  108.         if (strlen(linePointer) < 12)
  109.             goto parsingError;
  110.  
  111.         hexDataPointer = strstr(linePointer, "   ");
  112.         if (hexDataPointer != NULL)
  113.         {
  114.             if (strlen(hexDataPointer) < 4)
  115.                 goto parsingError;
  116.  
  117.             hexByte = strtok_s(hexDataPointer + 3, " ", &token2);
  118.             while (hexByte != NULL)
  119.             {
  120.                 if (strlen(hexByte) != 2)
  121.                     goto parsingError;
  122.  
  123.                 // convert first hex character
  124.                      if ((hexByte[0] >= '0') && (hexByte[0] <= '9')) b1 = hexByte[0] - '0';
  125.                 else if ((hexByte[0] >= 'A') && (hexByte[0] <= 'F')) b1 = 10 + (hexByte[0] - 'A');
  126.                 else goto parsingError;
  127.  
  128.                 // convert second hex character
  129.                      if ((hexByte[1] >= '0') && (hexByte[1] <= '9')) b2 = hexByte[1] - '0';
  130.                 else if ((hexByte[1] >= 'A') && (hexByte[1] <= 'F')) b2 = 10 + (hexByte[1] - 'A');
  131.                 else goto parsingError;
  132.  
  133.                 // write resulting byte
  134.                 if (fputc((b1 * 16) + b2, out) == EOF)
  135.                 {
  136.                     printf("Error: I/O error while writing to output file!\n");
  137.                     free(fileBuffer);
  138.                     fclose(out);
  139.                     system("PAUSE");
  140.                     return (1);
  141.                 }
  142.  
  143.                 bytesWritten++;
  144.                 hexByte = strtok_s(NULL, " ", &token2);
  145.             }
  146.         }
  147.  
  148.         linesProcessed++;
  149.         linePointer = strtok_s(NULL, "\r\n", &token1);
  150.     }
  151.  
  152.     if ((linesProcessed == 0) || (bytesWritten == 0))
  153.         goto parsingError;
  154.  
  155.     // we're done! free stuff
  156.     printf("Successfully parsed %d line(s). Wrote %d byte(s) to memdump.bin.\n", linesProcessed - 1, bytesWritten);
  157.     free(fileBuffer);
  158.     fclose(out);
  159.     system("PAUSE");
  160.     return (0);
  161.  
  162. parsingError:
  163.     printf("Error: Couldn't parse data, input file is messed up!\n");
  164.     free(fileBuffer);
  165.     fclose(out);
  166.     system("PAUSE");
  167.     return (1);
  168. }
RAW Paste Data
Top