How to convert c code fread to vb.net - c

I`m writing the VB.net and need to call API(c language DLL)
My test sample code as follow
//Read Source File
char *SourceFilePath = "C:\\Docs\\Scandi\\attach\\verifyTest\\center.xml";
FILE *sourcefile= fopen(SourceFilePath, "rb");
if (!sourcefile)
{
printf("Error=%s\n", *SourceFilePath);
return;
}
fseek(sourcefile,0,SEEK_END);
long src_ch_len = ftell(sourcefile);
rewind(sourcefile);
unsigned char *src_ch =(unsigned char*)malloc(sizeof(char)*src_ch_len);
result = fread(src_ch,1,src_ch_len,sourcefile);
if(result!=src_ch_len)
{
printf("Reading Error=%s\n", *sourcefile);
return;
}
fclose(sourcefile);
//Read Data File
//Skip...
rc = BasicVerify(algorithm, data, dataLen, key, signature, signatureLen);
API Function definition
unsigned long verify(unsigned long algorithm, unsigned char *data, int dataLen,unsigned char *signature, int signatureLen, char *cerFile)
How to convert fopen(SourceFilePath, "rb") and fread(src_ch,1,src_ch_len,sourcefile) to VB.NET
Thanks

The analogue to fopen using the modes "rb" in VB .NET appears to be the FileStream class using the FileAccess.Read mode. From there you can use the FileStream.Read method as an analogue for fread. For example:
dim sourceFile as FileStream
sourceFile = new FileStream("C:\\Docs\\Scandi\\attach\\verifyTest\\center.xml", FileAccess.Read)
dim result as Integer
result = sourceFile.Read(array, 0, array.Length)
However, it seems from the fseek followed by ftell in your C code, you want to read the entire file into memory. This is often frowned upon since a file could be many gigabytes in size. If you really want to do that, use the File.ReadAllBytes method, for example:
dim src_ch as Byte()
src_ch = File.ReadAllBytes("C:\\Docs\\Scandi\\attach\\verifyTest\\center.xml")

Related

how to use zpipe inflate on a binary unsigned char

I have a compressed and base64 encoded string that I want to decompress zpipe.
I did the tutorial here and it worked great. I b64 decoded the string first, saved it to a file and then used the inf() function to decompress it.
int ret;
char *b64_string = (char *)read_b64_string();
size_t my_string_len;
unsigned char *my_string = b64_decode_ex(my_string, strlen(my_string), &my_string_len);
free(b64_string);
write_decoded_b64_to_file(my_string, my_string_len);
free(my_string);
ret = inf();
and then I changed the inf() function to hardcoded files:
int inf()
{
FILE *source;
FILE *dest;
source = fopen("/path/to/my/b64decoded_file/", "r");
dest = fopen("/path/to/my/decompressed_file/", "w");
Now I want to change the inf() function to make it work when the binary is passed as an argument.
int ret;
size_t my_string_len;
unsigned char *my_string = b64_decode_ex(my_string, strlen(my_string), &my_string_len);
ret = inf(my_string);
I think I identified this line
strm.avail_in = fread(in, 1, CHUNK, source);
as the one where I have to read in the binary. fread is only for files though. How can I read this binary file in without a file?
Just use fmemopen() to open my_string as a file.
source = fmemopen(my_string, my_string_len, "rb");
(I put in the b in "rb" by habit. Never hurts. Can help.)

Writing ByteArray to file in c

I have a struct which holds some ByteArray data
typedef struct {
uint32_t length;
uint8_t* bytes;
} FREByteArray;
And here I am trying to save this to a file
FREByteArray byteArray;
if((fileToWrite = fopen(filePath, "wb+")) != NULL){
fwrite(&byteArray.bytes, 1, byteArray.length, fileToWrite);
fclose(fileToWrite);
}
But this doesn't seem to be saving all of the data, the saved file size is 16KB, actual data is about 32KB. I think fwrite is not able to write the whole bytearray to the file.
Is this the correct way to save the ByteArray? Is there a limit how much fwrite can handle in a single call?
Replace
fwrite(&byteArray.bytes, 1, byteArray.length, fileToWrite);
with
fwrite(byteArray.bytes, 1, byteArray.length, fileToWrite);
And as pointed out by #Sourav Ghosh make sure that byteArray.bytes is pointing to the correct source location.

How to set jbytearray directly from file

I have the following native code that copies from a file into a buffer and then copies the
contents of that buffer into a jbytearray.
JNIEXPORT void JNICALL Java_com_test(JNIEnv * env, jobject){
int file_descriptor = 100;
JNIEnv * jni_env = env;
FILE* file = fdopen(file_descriptor, "r");
unsigned char* buffer;
int size_of_file = 1000000;
fread(buffer, 1, static_cast<size_t>(size_of_file), file);
imageArr = static_cast<jbyteArray>(jni_env->NewByteArray(static_cast<jsize> (size_of_file)));
jni_env->SetByteArrayRegion (imageArr, 0, static_cast<jsize>
(size_of_file ), (jbyte*)buffer);
}
As this code runs in a loop, I would like to optimize this as much as possible. Is there any way to directly read from the file to the jbyteArray? I am aware jbyteArray is a pointer to a struct. Is there any way to set the fields of this struct directly instead of using the setByteArrayRegion() function?
If not, is there any other function that I can use to read from a file to a jbytearray?
In short, no. You can probably do it, but it probably wont be much faster and if something with the implementation changed in the JVM your code would stop working. You are dealing with file I/O so I don't think SetByteArrayRegion is your real bottleneck here.

Reading unsingned chars with Qt

Hi fellow stack overflowers,
I'm currently parsing a file which both contains text and binary data. Currently, I'm reading the file in following manner:
QTextStream in(&file);
int index = 0;
while(!in.atEnd()) {
if (index==0) {
QString line = in.readLine(); // parse file here
} else {
QByteArray raw_data(in.readAll().toAscii());
data = new QByteArray(raw_data);
}
index++;
}
where data refers to the binary data I'm looking for. I'm not sure if this is what I want, since the QString is encoded into ascii and I have no idea if some bytes are lost.
I checked the documentation, and it recommends using a QDataStream. How can I combine both approaches, i.e. read lines with an encoding and also read the binary dump, after one line break?
Help is greatly appreciated!
This will do what you want.
QTextStream t(&in);
QString line;
QByteArray raw_data;
if(!in.atEnd()) {line = t.readLine();}
in.reset();
int lineSize = line.toLocal8Bit().size() + 1;
in.seek(lineSize);
if(!in.atEnd())
{
int len = in.size() - lineSize;
QDataStream d(&in);
char *raw = new char[len]();
d.readRawData(raw, len);
raw_data = QByteArray(raw, len);
delete raw;
}
PS: if file format is yours, it will be better to create file with QDataStream and write data with <<, read with >>. This way you can store QByteArray and QString in file without such problems.

fwrite() and file corruption

I'm trying to write a wchar array to a file in C, however there is some sort of corruption and unrelevant data like variables and paths like this
c.:.\.p.r.o.g.r.a.m. .f.i.l.e.s.\.m.i.c.r.o.s.o.f.t. .v.i.s.u.a.l. .s.t.u.d.i.o. 1.0...0.\.v.c.\.i.n.c.l.u.d.e.\.x.s.t.r.i.n.g..l.i.s.t...i.n.s.e.r.t
are written on to the file along with the correct data (example) I have confirmed that the buffer is null-terminated and contains proper data.
Heres my code:
myfile = fopen("logs.txt","ab+");
fseek(myfile,0,SEEK_END);
long int size = ftell(myfile);
fseek(myfile,0,SEEK_SET);
if (size == 0)
{
wchar_t bom_mark = 0xFFFE;
size_t written = fwrite(&bom_mark,sizeof(wchar_t),1,myfile);
}
// in another func
while (true)
{
[..]
unsigned char Temp[512];
iBytesRcvd = recv(sclient_socket,(char*)&Temp,iSize,NULL);
if(iBytesRcvd > 0 )
{
WCHAR* unicode_recv = (WCHAR*)&Temp;
fwrite(unicode_recv,sizeof(WCHAR),wcslen(unicode_recv),myfile);
fflush(myfile);
}
[..]
}
What could be causing this?
recv() will not null-terminate &Temp, so wcslen() runs over the bytes actually written by recv(). You will get correct results if you just use iBytesReceived as byte count for fwrite() instead of using wcslen() and hoping the data received is correctly null-terminated (wide-NULL-terminated, that is):
fwrite(unicode_recv, 1, iBytesReceived, myfile);

Resources