SHARE
TWEET

DOSBox memdump2bin v0.1

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