OhGodAPet

Structure Dumper Generator

Sep 22nd, 2022
961
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.83 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include <string.h>
  4. #include <stdbool.h>
  5.  
  6. // Ref struct
  7. /*
  8. typedef struct
  9. {
  10.     uint32_t VersionID;
  11.     uint8_t KeyID[16];
  12.     uint8_t CertifyingKeyID[16];
  13.     uint32_t KeyUsageFlag;
  14.     uint8_t PlatformBindingInfo;
  15.     uint8_t Reserved[15];
  16.     uint32_t PublicExponentSize;
  17.     uint32_t ModulusSize;
  18.     PSPRSAKey RSAKeyData;
  19. } PSPRSAKeyHdr;
  20. * */
  21.  
  22. const char Structure[] =
  23. "typedef struct\n"
  24. "{\n"
  25. "   uint32_t VersionID;\n"
  26. "   uint8_t KeyID[16];\n"
  27. "   uint8_t CertifyingKeyID[16];\n"
  28. "   uint32_t KeyUsageFlag;\n"
  29. "   uint8_t PlatformBindingInfo;\n"
  30. "   uint8_t Reserved[15];\n"
  31. "   uint32_t PublicExponentSize;\n"
  32. "   uint32_t ModulusSize;\n"
  33. "   PSPRSAKey RSAKeyData;\n"
  34. "} PSPRSAKeyHdr;\n";
  35.  
  36. typedef struct StructMember_s
  37. {
  38.     char *TypeStr;
  39.     char *NameStr;
  40.     uint32_t TypeSize;
  41.     uint32_t ArraySize;
  42.     struct StructMember_s *next;
  43. } StructMember;
  44.  
  45. char *CarveStr(const char *Src, size_t StartOffset, size_t Len)
  46. {
  47.     //char *TmpBuf = (char *)malloc(sizeof(char) * (Len + 1));
  48.     char TmpBuf[256];
  49.     char *Output;
  50.    
  51.     memcpy(TmpBuf, Src + StartOffset, Len);
  52.     TmpBuf[Len] = 0x00;
  53.     Output = strdup(TmpBuf);
  54.     //free(TmpBuf);
  55.    
  56.     return(Output);
  57. }
  58.  
  59. int main(int argc, char **argv)
  60. {
  61.     //char *inputstruct = Structure;
  62.     StructMember *MemberList = NULL;
  63.    
  64.     if(argc != 2)
  65.     {
  66.         printf("Usage: %s <structure definition>\n", argv[0]);
  67.         return(-1);
  68.     }
  69.    
  70.     char *inputstruct = (char *)malloc(strlen(argv[1]) + 1);
  71.     strcpy(inputstruct, argv[1]);
  72.    
  73.     for(char *line = strtok(inputstruct, "\n"); line; line = strtok(NULL, "\n"))
  74.     {
  75.         StructMember *memb = MemberList;
  76.        
  77.         // If the line does not have a semicolon, it can be
  78.         // safely ignored.
  79.         if(!strchr(line, ';'))
  80.             continue;
  81.            
  82.         // First, is this really a type?
  83.         uint32_t SizeOfType;
  84.         char *NamePos, *NameEnd, *TypePos, *TypeEnd, *ArrStart, *ArrEnd;
  85.        
  86.         TypePos = strstr(line, "uint");
  87.        
  88.         if(TypePos)
  89.         {
  90.             // Is it uint8_t? If so, stash the info in variables
  91.             if(TypePos[4] == '8' && TypePos[5] == '_' && TypePos[6] == 't')
  92.             {
  93.                 NamePos = TypePos + strlen("uint8_t");
  94.                 SizeOfType = 1;
  95.             }
  96.             // Is it uint16_t? Same deal.
  97.             else if(TypePos[4] == '1' && TypePos[5] == '6' && TypePos[6] == '_' && TypePos[7] == 't')
  98.             {
  99.                 NamePos = TypePos + strlen("uint16_t");
  100.                 SizeOfType = 2;
  101.             }
  102.             // Same for uint32_t...
  103.             else if(TypePos[4] == '3' && TypePos[5] == '2' && TypePos[6] == '_' && TypePos[7] == 't')
  104.             {
  105.                 NamePos = TypePos + strlen("uint32_t");
  106.                 SizeOfType = 4;
  107.             }
  108.             // Ditto for uint64_t.
  109.             else if(TypePos[4] == '6' && TypePos[5] == '4' && TypePos[6] == '_' && TypePos[7] == 't')
  110.             {
  111.                 NamePos = TypePos + strlen("uint64_t");
  112.                 SizeOfType = 8;
  113.             }
  114.            
  115.             if(!memb) MemberList = memb = (StructMember *)malloc(sizeof(StructMember));
  116.             else
  117.             {
  118.                 //  Skip all previous members
  119.                 for(memb = MemberList; memb->next; memb = memb->next);
  120.                
  121.                 memb = memb->next = (StructMember *)malloc(sizeof(StructMember));
  122.             }
  123.            
  124.             memset(memb, 0x00, sizeof(StructMember));
  125.             TypeEnd = TypePos;
  126.            
  127.             while(!isspace(*TypeEnd)) TypeEnd++;
  128.            
  129.             //printf("TypeEnd = %s\nTypePos = %s\n", TypeEnd, TypePos);
  130.             NamePos = TypeEnd + 1;
  131.             NameEnd = strchr(NamePos, ';') - 1;
  132.            
  133.             if(!NameEnd)
  134.             {
  135.                 printf("Error: Malformed line %s\n", line);
  136.                 exit(1);
  137.             }
  138.            
  139.             memb->TypeStr = CarveStr(TypePos, 0x00, (SizeOfType == 1) ? 7 : 8);
  140.             memb->NameStr = CarveStr(NamePos, 0x00, NameEnd - NamePos + 1);
  141.             memb->TypeSize = SizeOfType;
  142.            
  143.             // Is it an array?
  144.             ArrStart = strchr(memb->NameStr, '[');
  145.            
  146.             // If so, get the size.
  147.             if(ArrStart)
  148.             {
  149.                 ArrEnd = strchr(memb->NameStr, ']');
  150.                 memb->ArraySize = strtoul(ArrStart + 1, NULL, 0);
  151.                 // Cut the array notation off the name.
  152.                 ArrStart[0] = 0x00;
  153.             }
  154.            
  155.             memb->next = NULL;
  156.         }
  157.         else
  158.         {
  159.             printf("Warning: Line not recognized: '%s'\n", line);
  160.         }
  161.     }
  162.    
  163.     // Info harvested; time to generate code.
  164.     for(StructMember *CurMemb = MemberList; CurMemb; CurMemb = CurMemb->next)
  165.     {
  166.         char *TypeSpecifier = NULL;
  167.        
  168.         //printf("TypeStr = %s\nNameStr = %s\nArraySize = %d\n", CurMemb->TypeStr, CurMemb->NameStr, CurMemb->ArraySize);
  169.        
  170.         if(CurMemb->TypeSize == 1) TypeSpecifier = "0x%02X";
  171.         else if(CurMemb->TypeSize == 2) TypeSpecifier = "0x%04X";
  172.         else if(CurMemb->TypeSize == 4) TypeSpecifier = "0x%08X";
  173.         else if(CurMemb->TypeSize == 8) TypeSpecifier = "0x%016llX";
  174.         else printf("How did you even get here?");
  175.        
  176.         printf("printf(\"%s: ", CurMemb->NameStr);
  177.        
  178.         if(CurMemb->ArraySize)
  179.         {
  180.             printf("\");\n");
  181.             printf("\nfor(int i = 0; i < %d; ++i)\n", CurMemb->ArraySize);
  182.             printf("\tprintf(\"%s, \", dmp->%s[i]);\n\n", TypeSpecifier, CurMemb->NameStr);
  183.             printf("putchar('\\n');\n");
  184.         }
  185.         else
  186.         {
  187.             printf("%s\\n\", dmp->%s);\n", TypeSpecifier, CurMemb->NameStr);
  188.         }
  189.     }
  190.    
  191.    
  192.    
  193.     return(0);
  194. }
  195.  
Advertisement
Add Comment
Please, Sign In to add comment