0

I have the C source code of a keygen to generate "Master Codes" for Dell locked BIOSes.

Tried hard, hard to understand it, and got various results after many weeks of reading and asking.

However, there are many things that I don't know how to write them in C# code, which is my goal, for adventure and academic purposes.

For example, I say unsigned char outData[16] on C

In C# I rewrote this as follows:

byte* outData = stackalloc byte[16];

By this way, the byte values stored on C# are of the same size on RAM as a unsigned char on C (based on http://bytes.com/topic/c-sharp/answers/658913-unsigned-char)

Then I can do the following in C#:

int* testPointer = (int*) outData;
*testPointer = 0x67452301;

By doing that, I've modified the positions 0, 1, 2 and 3 of outData (byte is a quarter of a int on C# (8 bits vs 32 bits), same relation as unsigned char and int on C if I'm not wrong).

All the above thing of byte outData and testPointer thing could be done by this way on C:

unsigned char outData[16];
*(int *)(&outData[0]) = 0x67452301;

And then again, positions 1, 2, 3 and 4 of outData are changed (indexes starts from 1 in this case because it's C, not C#).

So, done technically, change values of a unsigned char with a 32 bit integer pointer, ported to C#, and it technically must give same results as on C.

Now, I want to change the values of outData from a function on C# via a 32 bit int pointer, but, I won't to give it as a parameter. So, only one solution, make outData a global variable. Then comes the big trouble... if i put outData as global, it must be contained in a class or a struct if it isn't in a method (variables on methods aren't and can't be globals as long as I know).

If I do that global outData thing via struct/class (only way as per I know), outData becomes a managed data type and it moves from here to there on RAM due to the GC (GarbageCollector), and using pointers to outData becomes a bigger trouble. So, here comes the fixed statement for help to put temporarily outData on a fixed place on RAM, but also with a trouble, casting is not allowed on fixed statements, and then I can't do this: int* testPointer = (int*) outData;, where outData has another block size than testPointer and casting is needed.

Said all the previous stuff, comes the real question. How can I change the values of outData from a function on C#, via a 32 bit int pointer? OR... What algorithm can I use to make these changes to outData whitout using pointers, and supposing that outData would become a standard value type that doesn't needs unsafe contexts?

Really would love much, much more answers for second question, because the real objective that I want to accomplish is port C code to C#, and use a OO (Object Oriented) paradigm.

I'm going crazy to trying to understand a algorithm to make those changes of values stored on RAM via pointers and using different block sizes, and just have a method to do this and be pointers-free to be ready to use on C# and others OO programming languages.

Anticipated thanks for any answer!

And, for reference, here is the complete C code of the program that I wanna port:

http://pastebin.com/w8VQjVBu

I've done around 40% porting, those C functions going to pointers, different block sizes and that stuff has been really a pain on the %*& for weeks!

4
  • 1
    C# is not the same programming language as C. Even though you might be able to do a line-by-line translation of the code, that doesn't make it a good idea. The whole idea behind "porting" is to actually convert the code into idiomatic code for the target language. You don't need to use pointers in C# at all. Commented Mar 31, 2012 at 5:26
  • Right! You understood my point, I want to re-make this keygen using a OO paradigm, and putting all the ideas and algorithms on C#. Again, my real answer-wanted-question is how I port this keygen to C#, the only "but" that I'm facing is that stuff of change values of variables on C# just like C does with pointers of different buffer sizes Commented Mar 31, 2012 at 5:33
  • You just modify the value of the object. Stack Overflow isn't really the place for someone to teach you C#. You really need to get a book and learn the language first. Commented Mar 31, 2012 at 5:34
  • I'm not kinda beginner on C#, but I'm a beginner on C. That's the reason why I've put all my effort on study pointers and C syntax for porting of this keygen, but there are some things that for weeks I can't understand, as the ones that I've asked here. I just want a explaniation of how changes outData, what's the alogrithm to reproduce this on C#, what info/book can I use to get help on this. Sorry if this isn't a place for these questions, if so, I'll wait to get more "not ask this" requests to close this question :) Commented Mar 31, 2012 at 5:38

2 Answers 2

1

It sounds like you're trying to copy the value of 32 bit int into the first four bytes of a byte array.

To do that use the BitConverter.GetBytes method and copy the resulting bytes to your other array:

var n = 0xFAFBFCFD;
var a = new byte[16];
var temp = BitConverter.GetBytes(n);
temp.CopyTo(a, 0);

Now:

a[0] == 0xFD
a[1] == 0xFC
a[2] == 0xFB
a[3] == 0xFA

Like some of the comments say, it's a bad idea to try to write C# as if it were C.

Sign up to request clarification or add additional context in comments.

5 Comments

Just great, great method really! Now, if I want, let's say, not convert 32-bit signed integers to arrays of bytes, but yes to convert mmm... 128 bit decimal numbers to arrays of unsigned 16-bit integers. I've already realized that there are BitConverter for int, double, float, single, etc, etc. Is there something like UInt16Converter for int, double, float, single, etc, etc?
Searching MSDN for all <Type>Converter results, I realize that there are converters for every type of number variables and combinations, but they work more like the "Convert.To<Type>" method. Didn't found some like the GetBytes thing for every type, wich is more what I want (GetUInt32s, GetSBytes, etc)
You'd probably need multiple conversion steps GetBytes -> Byte Array -> ToUint16. The BitConverter class provides a bunch of methods for converting primitives to and from arrays of bytes.
Well, seems like I'm gonna need to get crazier searching for books to understand how this encoding-decoding happens, I've part of my problem solved... but I don't understand how GetBytes does his work. If some starting reference to read is possible, will be much apreciated!
Until now did realize that there are BitConverter.To<Type> methods XD!!. Just like Convert.To<Type>, but with different results... the results that I wanted to get! I don't understand absolutely nothing of how works this encoding-decoding, study and learn is endless in this life, but it's giving me the exact same results as in the code in C for the keygen, and with a much simpler sintax and OO code sniplets :) Got my answer, thanks a lot Andrew Kennan, solved my weeks big trouble un minutes, now I'm gonna study how the !&$# does all the methods of BitConverter works. Thanks again, a lot!
0

As you're talking about reading and writing 16byte chunks made up of integers, I believe you are looking for the BinaryReader and BinaryWriter classes. Google will give you plenty of demonstrations on how to use those classes.

1 Comment

Thanks Brannon, it ends doing the same that GetBytes -> Byte Array -> To<Type> , but it has more interesting things to handle those conversions, and that's the reason why I'll keep with it, and not BitConverter :). Important to notice that they rely more on StreamWriters, only but that makes things larger.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.