Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.65 KB | None | 0 0
  1. // SystemTest.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <Windows.h>
  6. #include <direct.h>
  7. #include <string>
  8. #include <iostream>
  9. #include <stdio.h>
  10. #include <stdint.h>
  11. #include "FileReader.h"
  12. #include "FileWriter.h"
  13.  
  14. using namespace std;
  15.  
  16. void RunDriveSpaceCheck(){
  17.     int const drive = _getdrive();
  18.     struct _diskfree_t diskfree;
  19.  
  20.     _getdiskfree(drive, &diskfree);
  21.  
  22.     unsigned __int64 totalDisk = diskfree.total_clusters;
  23.     totalDisk*=diskfree.sectors_per_cluster;
  24.     totalDisk*=diskfree.bytes_per_sector;
  25.     totalDisk /= 1048576; //To MB
  26.  
  27.     unsigned __int64 availDisk = diskfree.avail_clusters;
  28.     availDisk*=diskfree.sectors_per_cluster;
  29.     availDisk*=diskfree.bytes_per_sector;
  30.     availDisk /= 1048576;
  31.  
  32.     cout << "Hard drive check:" << endl;
  33.  
  34.     //cout <<"  Bytes per sector: " << diskfree.bytes_per_sector << endl;
  35.     //cout <<"  Sectors per cluster: " << diskfree.sectors_per_cluster << endl;
  36.     //cout <<"  ===========================" << endl;
  37.     cout <<"    Total disk space: " << totalDisk << " MB      <= Is this correct?" << endl;
  38.     cout <<"    Available disk space : " << availDisk << " MB  <= Is this correct?" << endl;
  39. }
  40.  
  41. void RunRAMCheck(){
  42.     MEMORYSTATUSEX status; //Can also grab Virtual RAM
  43.     status.dwLength=sizeof(status);
  44.     GlobalMemoryStatusEx(&status);
  45.    
  46.     unsigned __int64 PRAM = status.ullTotalPhys / 1048576; //in MB
  47.     unsigned __int64 VRAM = status.ullTotalVirtual / 1048576; //in MB
  48.  
  49.     cout << "RAM check:" << endl;
  50.  
  51.     cout <<"    Total Physical RAM: " << PRAM << " MB  <= Is this correct?" << endl;
  52.     cout <<"    Total Virtual RAM: " << VRAM << " MB" << endl;
  53. }
  54.  
  55. bool VerifyEndian(){
  56.     /*short int word = 0x0001;
  57.     char *byte = (char *) &word;
  58.  
  59.     if(byte[0]==0)
  60.         return true;  //Big Endian
  61.     else
  62.         return false; //LE*/
  63.  
  64.     int x = 1;
  65.     if(*(char *)&x == 1)
  66.         return false; //LE
  67.     else   
  68.         return true; //BE
  69.  
  70.  
  71. }
  72.  
  73. short ShortSwap( short s )
  74. {
  75.   unsigned char b1, b2;
  76.  
  77.   b1 = s & 255;
  78.   b2 = (s >> 8) & 255;
  79.  
  80.   return (b1 << 8) + b2;
  81. }
  82. int IntSwap (int i)
  83. {
  84.   unsigned char b1, b2, b3, b4;
  85.  
  86.   b1 = i & 255;
  87.   b2 = ( i >> 8 ) & 255;
  88.   b3 = ( i>>16 ) & 255;
  89.   b4 = ( i>>24 ) & 255;
  90.  
  91.   return ((int)b1 << 24) + ((int)b2 << 16) + ((int)b3 << 8) + b4;
  92. }
  93.  
  94.  
  95. /*void TestWrite(){
  96.     FileWriter file("test1.bin");
  97.  
  98.     file.WriteCharString( "CharSTRING", 10);
  99.     file.WriteShort(32700);
  100.     file.WriteChar('x');
  101.     file.WriteString("STRING");
  102.     file.WriteInt(0x0FAA00FF);
  103. }
  104. void TestSwappedWrite(){
  105.     FileWriter file("test2.bin");
  106.  
  107.     file.WriteCharString( "CharSTRING", 10);
  108.     file.WriteShort(ShortSwap(32700));
  109.     file.WriteChar('x');
  110.     file.WriteString("STRING");
  111.     file.WriteInt(IntSwap(0x0FAA00FF));
  112. }*/
  113.  
  114. bool TestRead(string str, char endian){
  115.     FileReader file(str);
  116.  
  117.     int test = 0x0FAA00FF;
  118.  
  119.     string s(10, '0');
  120.     short sh=0;
  121.     char ch=0;
  122.     string s2(6, '0');
  123.     int in=0;
  124.  
  125.     if(file.IsValid()){
  126.         s = file.ReadString(10);
  127.         sh = file.ReadShort();
  128.         ch = file.ReadChar();
  129.         s2 = file.ReadString(6);
  130.         in = file.ReadInt();
  131.     }else{
  132.         cout << "   Converted reading failed! (EC: 0x00)" << endl;
  133.  
  134.         FileWriter wfile("Native.dump");
  135.         wfile.WriteString( s);
  136.         wfile.WriteShort(sh);
  137.         wfile.WriteChar(ch);
  138.         wfile.WriteString(s2);
  139.         wfile.WriteInt(in);
  140.         wfile.WriteChar('0');
  141.         wfile.WriteChar(endian);
  142.         return true;
  143.     }
  144.  
  145.     if(s=="CharSTRING" && sh==32700 && ch=='x' && s2=="STRING" && in==0x0FAA00FF){
  146.         cout << "   Native reading success!" << endl;
  147.         return false;
  148.     }else{
  149.         cout << "   Native reading failed! (EC: 0x01)" << endl;
  150.  
  151.         FileWriter wfile("Native.dump");
  152.         wfile.WriteString( s);
  153.         wfile.WriteShort(sh);
  154.         wfile.WriteChar(ch);
  155.         wfile.WriteString(s2);
  156.         wfile.WriteInt(in);
  157.         wfile.WriteChar('1');
  158.         wfile.WriteChar(endian);
  159.         return true;
  160.     }
  161. }
  162. bool TestSwappedRead(string str, char endian){
  163.     FileReader file(str);
  164.  
  165.     string s(10, '0');
  166.     short sh=0;
  167.     char ch=0;
  168.     string s2(6, '0');
  169.     int in=0;
  170.  
  171.     if(file.IsValid()){
  172.         s = file.ReadString(10);
  173.         sh = ShortSwap(file.ReadShort());
  174.         ch = file.ReadChar();
  175.         s2 = file.ReadString(6);
  176.         in = IntSwap(file.ReadInt());
  177.     }else{
  178.         cout << "   Converted reading failed! (EC: 0x00)" << endl;
  179.  
  180.         FileWriter wfile("Native.dump");
  181.         wfile.WriteString( s);
  182.         wfile.WriteShort(sh);
  183.         wfile.WriteChar(ch);
  184.         wfile.WriteString(s2);
  185.         wfile.WriteInt(in);
  186.         wfile.WriteChar('0');
  187.         wfile.WriteChar(endian);
  188.         return true;
  189.     }
  190.  
  191.     if(s=="CharSTRING" && sh==32700 && ch=='x' && s2=="STRING" && in==0x0FAA00FF){
  192.         cout << "   Converted reading success!" << endl;
  193.         return false;
  194.     }else{
  195.         cout << "   Converted reading failed! (EC: 0x01)" << endl;
  196.  
  197.         FileWriter wfile("Converted.dump");
  198.         wfile.WriteString( s);
  199.         wfile.WriteShort(sh);
  200.         wfile.WriteChar(ch);
  201.         wfile.WriteString(s2);
  202.         wfile.WriteInt(in);
  203.         wfile.WriteChar('1');
  204.         wfile.WriteInt(endian);
  205.         return true;
  206.     }
  207. }
  208.  
  209. int _tmain(int argc, _TCHAR* argv[])
  210. {
  211.     using namespace std;
  212.  
  213.     cout << endl;
  214.  
  215.     RunDriveSpaceCheck();
  216.     cout << endl << endl;
  217.  
  218.     RunRAMCheck();
  219.     cout << endl << endl;
  220.  
  221.     //TestWrite();
  222.     //TestSwappedWrite();
  223.  
  224.     bool error=false;
  225.  
  226.     if(VerifyEndian()){
  227.         cout << "System Configuration Big Endian:" << endl;
  228.         error=TestRead("testBE.bin", 1);
  229.         error=TestSwappedRead("testLE.bin", 1);
  230.     }else{
  231.         cout << "System Configuration Little Endian:" << endl;
  232.         error=TestRead("testLE.bin", 2);
  233.         error=TestSwappedRead("testBE.bin", 2);
  234.     }
  235.  
  236.     cout << endl << endl;
  237.     //error=true;
  238.     if(error){
  239.         cout << "One or both tests have failed. Please pass me the created dump files:"<<endl;
  240.         cout << "\"Native.dump\" and/or \"Converted.dump\"." << endl;
  241.         cout << endl << endl;
  242.     }
  243.  
  244.     cout<<"Thank you for your help. Press any key to close this console..."; getchar();
  245.  
  246.     return 0;
  247. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement