Thread: Examining MBR and Printing Total Disk Size in Bytes

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    58

    Examining MBR and Printing Total Disk Size in Bytes

    I am trying to figure out the disc size in bytes. I am multiplying the total number of sectors by 512 bytes. I get a negative number!!!

    here is the code:

    Code:
    #include "windows.h"
    #include "stdlib.h"
    #include "stdio.h"
    
    /* buffer to hold integers */
    char comma[64] = {0};
    
    int main()
    {
    
       int i;
       unsigned char data[512];
       DWORD dwBytesRead = 0;
       
       HANDLE hFile = NULL;
       
       hFile = CreateFile("\\\\.\\E:",       /* drive to open */
                    GENERIC_READ,            /* allows drive read */
                    FILE_SHARE_READ,         /* shares read access */
                    NULL,                    /* Security Descriptor */
                    OPEN_EXISTING,           /* declares creation */
                    FILE_ATTRIBUTE_READONLY, /* access is read only */
                    NULL);                   /* handle to template */
    
       if(hFile != NULL)
       {
          /* read the boot */
          if (!ReadFile(hFile, &data, 512, &dwBytesRead, NULL))
          {
             printf("Error in reading the specified drive!\n");
          }
          else
          {
             /* perform hexdump of boot information */
             for (i = 0; i<100; i++)
                 printf("%d %X\n",i,data[i]);
                 
    
             
             /* turn the data into usable information for output*/
             int sb = *((int *)(data + 11)) & 0xFFFF;
             int cs = *((int *)(data + 13)) & 0xFF;
             int ns = *((int *)(data + 32)) & 0xFFFFFFFF;
             int nh = *((int *)(data + 26)) & 0xFFFF;
             int spf = *((int *)(data + 22)) & 0xFFFF;
             int rdcn = *((int *)(data + 44)) & 0xFFFFFFFF;
             int cncrd =  *((int *)(data + 50)) & 0xFFFFFFFF;
             int tnob = sb * ns; /* where sb= sector bytes, ns= number of sectors*/
    
             /* prints necessary output to screen */
             printf("\n  ---- Disk Information ----");
    
             /* variables for inserting commas */
             char *instring = comma;
             char outstring[64];
             char *ptr, *optr;
             int ii, length, commas;
    
             /* sprintf converts integer to string, holds data in (char data[]) */
    
             sprintf(comma, "%d", sb);
             printf("\nSector size (in bytes): %s", comma);
    
             sprintf(comma, "%d", cs);
             printf("\nCluster size (in sectors): %s",comma);
    
             /*************************************************************/
             sprintf(comma, "%d", ns);
    
             /* move ptr to end of instring */
             for ( ptr = instring; *ptr; ptr++ );
     
             /*calculate offset with commas*/
             length = ptr - instring;
             commas = ( length - 1 ) / 3;
             optr = outstring + length + commas;
    
             /*copy instring into outstring backwards inserting commas */
             *optr-- = *ptr--;
             for ( ii = 1; ptr >= instring; ii++ )
             {
                *optr-- = *ptr--;
                if ( ( ii % 3 ) == 0 )
                *optr-- = ',';
             }
    
             /* show the result */
             printf ( "\nTotal number of sectors: %s", outstring );
             /*************************************************************/
    
             sprintf(comma, "%d", nh);
             printf("\nTotal number of heads: %s",comma);
    
             sprintf(comma, "%d", spf);
             printf("\nSectors per FAT: %s",comma);
    
             sprintf(comma, "%d", rdcn);
             printf("\nRoot directory cluster number: %s",comma);
    
             sprintf(comma, "%d", cncrd);
             printf("\nCluster number of the copy of the root directory: %s",comma);
    
             /*********************************************************************/
             sprintf(comma, "%d", tnob);
             
             /* move ptr to end of instring */
             for ( ptr = instring; *ptr; ptr++ );
    
             /*calculate offset with commas*/
             length = ptr - instring;
             commas = ( length - 1 ) / 3;
             optr = outstring + length + commas;
    
             /*copy instring into outstring backwards inserting commas */
             *optr-- = *ptr--;
             for ( ii = 1; ptr >= instring; ii++ )
             {
                *optr-- = *ptr--;
                if ( ( ii % 3 ) == 0 )
                *optr-- = ',';
             }
    
             /* show the result */
             printf ( "\nTotal disk size in bytes: %s", outstring );
             /************************************************************/
    
             /* closes the handle */
             CloseHandle(hFile);
          }
       }
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Welcome to the forum!

    It sounds like you have a variable that is exceeding it's highest range, and going negative "wrapping around" on you.

    Stepping through your program with a few added print statements should show you right where the problem lies.

    You might be able to blame this all on the HD makers, since the HD's today are so much larger.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    58
    Ahh wrapping around, that makes sense. How would i go about doing the print statements you mentioned.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    First, do you already know the variable that is going negative?

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    58
    yes. int tnob is going negative. running the progrma, sb=512, and ns=7839682. i tried making them unsigned long int, and that too produced the same result as if it was an int.

    Code:
    int sb = *((int *)(data + 11)) & 0xFFFF;
             int cs = *((int *)(data + 13)) & 0xFF;
             int ns = *((int *)(data + 32)) & 0xFFFFFFFF;
             int nh = *((int *)(data + 26)) & 0xFFFF;
             int spf = *((int *)(data + 22)) & 0xFFFF;
             int rdcn = *((int *)(data + 44)) & 0xFFFFFFFF;
             int cncrd =  *((int *)(data + 50)) & 0xFFFFFFFF;
             int tnob = sb * ns;
    
    printf("%i", tnob);
    Last edited by pantherman34; 04-30-2010 at 03:04 PM.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    That result should fit nicely into an unsigned long.

    Are you by chance, still printing it as a regular int with %d?

  7. #7
    Registered User
    Join Date
    Apr 2010
    Posts
    58
    I believe I am... how would you print it as an unsigned long int?

    *** nevermind found it! thanks for the help Adak.
    Last edited by pantherman34; 04-30-2010 at 03:16 PM. Reason: found answer

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Have you not heard of the torrid affair between U and lu ?

  9. #9
    Registered User
    Join Date
    Apr 2010
    Posts
    58
    lawlz. just the torrid affair between me and the code!!!!

Popular pages Recent additions subscribe to a feed

Tags for this Thread