daily pastebin goal
23%
SHARE
TWEET

DOSBox memdump2bin v0.1

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