daily pastebin goal
33%
SHARE
TWEET

DOSBox memdump2bin v0.2

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