what the snippet of code does - c++

I would like to know what the snippet of code does..
Drive[0] = 'A';
Drive[1] = ':';
Drive[2] = '\\';
Drive[3] = 0;
DriveMask = GetLogicalDrives();
for( anIndex = 0; anIndex < 26;
anIndex++ )
{
if( DriveMask & 1 )
{
Drive[0] = 'A' + anIndex;
DriveMask >>= 1;
}
}
Please let me know your answer.
Thank you for your time to read my post.

It checks if the lowest bit is set i.e. if there is an A drive. See GetLogicalDrives

It's enumerating all the possible attached drives between A:\ and Z:\ and checking to see whether they're removable (eg CD, floppy).
It loops 26 times, and each time
DriveMask >>= 1;
causes the bitmask to be shifted right by 1 bit, so that each logical drive can be tested for via the
if( DriveMask & 1 )
in succession.
GetDriveType() requires a drive path, so the label is constructed by adding the loop count to the letter A (so A, B, C, D, ..., Z) and leaving the previously-initialized :\ part in-place.

In C++ the & is a bitwise and.
So take the value Drives and do a bitwise with 0x00000001. The result should be 1 if the number is odd (only way to have an odd number is with the least significant bit is 1). Since 0 anded with 1 = 0, it basically zeroes out all the values except for the least significant bit. If that bit is 1, then the result is 1 and evaluates to true.
Otherwise it's 0, and you don't hit the if.

It checks if the number is odd.
& is a bit-wise AND comparison.
0101 (5)
& 0001 (1)
= 0001 (1 -- true)
1110 (14)
& 0001 (1)
= 0000 (0 -- false)
In this case, GetLogicalDrives returns a number whose bits indicate the presence of certain drives. The least significant bit (20, 1) indicates the A drive.

The expression Drives & 1 is testing to see that the result of a logical and between Drives and 0x00000001 is non-zero. Thus it is checking to see if Drives is odd.

actually api returns reply in binary format :- here what MSDN says about it
"
If the function succeeds, the return value is a bitmask representing the currently available disk drives. Bit position 0 (the least-significant bit) is drive A, bit position 1 is drive B, bit position 2 is drive C, and so on.
"
means
if( Drives & 1 ) // i dont understand this if condition here that what it checks ? {
}
Condition checking for digit drive presense.

The GetLogicalDrives function returns the set of logical drives where each drive is encoded as a bit (a binary digit, can be either 0 or 1). The drive labels start at "A" in bit 0 (the least significant bit). The bit is 1 if the drive is present, else it's 0. The & in the above code is a logical-AND operation to test bit 0. Essentially this code checks if the system has an "A:\" drive.

This piece of code does not do absolutely anything in the common understanding of the word do. This code contains only non-modifying query-type operations with no side effects, i.e. it makes some queries and verifies some conditions, but it doesn't make any actions based on the results of these conditions.
In other words, if this code was fed into some hypothetical super-optimizing compiler, which also knows the Windows API, that compiler would simply throw out (optimize away) the entire code, since it doesn't do anything.
Apparently, the code you provided is fake - it is not the whole code. Without the whole thing, it is impossible to say what it was supposed to do. However, if we guess that some useful functionality was supposed to be present between the {} in the following if
if( GetDriveType( Drive ) == DRIVE_REMOVABLE )
{
// Actually DO something here
}
then we can make an educated guess about what it was supposed to do. This code iterates though all possible single-letter drive designations in a Windows system. It checks whether a logical drive designated by that letter is present in the system. And if the drive is present, it checks whether this drive works with removable media. And, finally, if it is true, then it does something useful that you are not showing us. I don't know what it was. Nobody does.

Related

How to deal with 128bit variable in MinGM32 bit compiler for Encryption (Diffie Hellman Algorithm) in Qt

I want to use the below equation in one of the code
A = g^a mod p; //g raise to a modulus p.
(something like 2^5 % 3) = 32%3 = 2
(This equation looks like Diffie Hellman algorithm for security)
Where:
^ is (power)
g is fixed number 0x05
a is 128bit(16bytes) randomly generated number,
p is fixed hex number of 128bit(16bytes). Something like (0x0xD4A283974897234CE908B3478387A3).
I am using:
Qt 4.8.7
Compiler MinGW32 (checked with boost library boost 1.70)
The solutions which I found which didn`t work for me are listed below:
one can use __int128 but to support that one should have used
latest GCC compiler or MinGW64 bit compiler, neither of that I am using now.
I found one latest version of Qt has QSslDiffieHellmanParameters class,
but again not supported in our Qt version.
I found some libraries like boost/multiprecision/cpp_int.hpp (boost 1.70))
that does have data type such as int128_t and int256_t, but due to
our compiler isssue or something else, we are not able to store
128bit number, meaning
if I do:
int128_t ptval128 = 0xAB1232423243434343BAE3453345E34B;
cout << "ptval128 = " << std::hex << ptval128 << endl;
//will print only 0xAB12324232434343;//half digits only,
I tried using Bigint which much more useful, but again
5^(128bit number) is way too big, it takes hours to compute things,
(I waited till 1 hour and 16 mins and kill the application).
int myGval = 0x05;
128_bit_data_type myPVal= 0xD4A283974897234CE908B3478387A3;
128_bit_data_type 128_bit_variable = 128_bit_random_data;
myVal = (myGval)^(128_bit_variable) % (myPVal);
That is not how to do modular exponentiation! The first problem is that 5 ^ 128_bit_variable is huge, so big that it won't fit into memory in any computers available today. To keep the required storage space within bounds, you have to take the remainder % myPVal after every operation.
The second problem is that you can't compute 5 ^ 128_bit_variable simply by multiplying by 5 by itself 128_bit_variable times -- that would take longer than the age of the universe. You need to use an exponentiation ladder, which requires just 128 squarings and at most 128 multiplications. See this Wikipedia article for the details. In the end, the operation 5 ^ 128_bit_number should take a fraction of a second.

How would one record player kills in CS:GO using an external program?

I have been trying to write a small program that runs behind Counter-Strike: Global Offensive. The program must be completely external, as there is no way to access live match data or the API during a competitive ranked match. So far, the best approach I could find is as follows:
case WM_TIMER:
COLORREF curColor, safetyColor;
for(int a = 1750;a < 1830;a++)
for (int b = 75;b < 100;b++)
{
curColor = GetPixel(csgoDC, a, b);
safetyColor = GetPixel(csgoDC, 1856, 1060);
if (GetRValue(curColor) == 255 && GetGValue(curColor) == 255 && GetBValue(curColor) == 255 && GetRValue(safetyColor) != 255)
PlaySound(_T("C:\\headshot.wav"), NULL, SND_FILENAME | SND_ASYNC);
}
break;
This is triggered by a timer. It does not work reliably, and I am fairly certain it won't work at all in fullscreen mode (Hard to test when it isn't reliable to begin with). The indices in the nested for loop correspond to the place on the screen where the killfeed shows up. Here is an example of the UI (From Google)
Screenshot
Note that I am not specifically going for a pixel-based approach; this is just the only solution I could think of.
A much more work intensive way of doing this would be to get runtime information from your machine, i.e. variables and associated values in memory. This would require at least to some degree reverse engineering of the CS:GO client code. But, if you could manage it, I imagine it would be the most reliable way of determining the information you desire. (Be wary of a ban-hammer to the face though.)

Rainflow algorithm - Fortran conversion to Matlab

I am trying to convert a Rainflow cycle counting algorithm which is in Fortran, which is a language I am not familiar with, into Matlab.
There is a ready made Rainflow I've downloaded for Matlab but that does not fit the requirements of my project so I'm trying to build one from scratch.
Here is the Fortran code:
INTEGER BUFFER (4096), INDEX, VALUE, RANGE, MEAN, X, Y
INDEX = 0
10 CONTINUE
call 'get next peak/valley', VALUE
INDEX = INDEX + 1
BUFFER (INDEX) = VALUE
20 CONTINUE
IF (INDEX.LT.3) THEN
not enough points to form a cycle
GOTO 10
ELSE
X = ABS (BUFFER(INDEX) - BUFFER(INDEX - 1))
Y = ABS (BUFFER(INDEX - 1) - BUFFER(INDEX - 2))
IF (X.GE.Y) THEN
c -- cycle has been closed
RANGE = Y
MEAN = (BUFFER(INDEX-1) + BUFFER(INDEX-2))/2
c -- remove the cycle
INDEX = INDEX - 2
BUFFER(INDEX) = BUFFER(INDEX+2)
c -- see if this value closes any more cycles
GOTO 20
ELSE
GOTO 10
END IF
END IF
I had downloaded f2matlab (a Fortran to Matlab converter) but it requires a Fortran compiler which I do not have.
The bits I don't really understand how I can convert are:
The call 'get next… line (is this an input()?)
The BUFFER(4096) etc (is this a bit large to be a matrix in matlab?)
The GOTO/CONTINUE structure.
What do they mean, in English (or Matlab)?
I have seen
How to translate fortran goto state to matlab
and
translating loop from Fortran to MATLAB
but they do not help me very much.
This
call 'get next peak/valley', VALUE
isn't (currently) syntactically valid Fortran and I'm not sure whether any compiler of yore would have understood it either. I guess that it means get a VALUE for use in the following bits of code.
INTEGER BUFFER (4096)
is a simple declaration that BUFFER is a vector of 4096 integers, nothing to scare Matlab in that volume of data.
Finally, GOTO is an unconditional jump and the number following it is the label of the line to jump to, so GOTO 10 means execute the line with label 10 next. It was fairly common in FORTRAN of the vintage you are showing us to jump to a CONTINUE statement which is, in this context, a no-operation, execution continues to the next line.
In another context, with DO loops CONTINUE would have marked the end of the block of code inside the scope of the loop and would have a subtly different effect.

Checking volatile value of address in C++

I'm trying to implement a mailbox write for the Raspberry Pi. According to the info I found, I can write to address 0x2000B8A0 when mailbox is empty, meaning 0x2000B898 has not the last bit set. I wrote it like this:
uint32_t *mailbox = reinterpret_cast<uint32_t*>(0x2000B880);
while((mailbox[6] & 0x80000000) != 0);
mailbox[8] = value + channel;
But the disassembly shows that the value at mailbox[6] is only loaded once, before the loop, then it just repeats the check with that one value.
I could not find a solution because I don't even know the proper words for this problem. I'm sure it's simple but googling brought nothing for this special case.
Answer lies in title of your question.
You should use the following:
volatile uint32_t *mailbox = const_cast<volatile uint32_t *>(reinterpret_cast<uint32_t*>(0x2000B880));
This will make sure the value is loaded each time in your loop. If you see any application not responding, consider adding some sleep or delay or yield in while.

Regex match 1024KB convert to 1MB?

I get the file size from the index of the page, it's 1024KB and I want it to print 1MB stead of 1024KB, what should I do? (completely noob here)
I got this:
if($row[2]==1) // Rapidshare Check
{
$index=getpage($row[1]);
if(strpos($index,"FILE DOWNLOAD")===false) //check if page contains the word file download if not = bad link
{
mysql_query("UPDATE `v2links` SET `checked`='-1',`lastcheck`=NOW() WHERE `id`=".$row[0]);
print "bad link\n";
logstr("log-c.txt","bad link\n");
}
else
{
preg_match("**/([^\/\|\#\<\(\>\;\s][0-9]*[\s][KB]{2})/**",$index,$match);
$fsize=$match[1];
print $fsize."\n";
logstr("log-c.txt","bad link\n");
//logstr("log-c.txt","$caption | $fsize\n");
mysql_query("UPDATE `v2links` SET `checked`='1',`fsize`='$fsize',`lastcheck`=NOW() WHERE `id`=".$row[0]);
unset($match);
}
}
Thanks
How accurate do you want the conversion to be?
s/0*([0-9]+)[0-9]{3}K/\1M/g
will lop off the last three numbers of a four or greater digit size... but it's a truncation, and doesn't even consider math. (For example, 1999K -> 1M)
A possibly better method would be to s/K/000/g and s/M/000000/g to turn it into a (crude) number of bytes, and then convert as you want back down.
The best possible method would be to process it, if it has a M multiply by 1024*1024; if it has K multiply by 1024. (if it has G, multiply by 1024*1024*1024). Then, process the resulting size however you're trying to.
--Note that it'd be a good plan to store file size as an integer, rather than a string.
For processing and output, a series of if's are probably good enough, and you can set precise tollerances for how big something must be to be displayed as M instead of K.
--Or if you just want M, there's no if, and you just divide by 1024*1024.