I'm trying to get image that are stored in BLOB and then save it as jpg.
Here i retrieve the binary data and save it in str;
string str;
SQLCHAR buf[500] = {0};
while ((SQL_SUCCEEDED(SQLGetData(StmtHandle, colnum, SQL_C_BINARY, buf, sizeof(buf), NULL))))
{
string data(reinterpret_cast< const char* >(buf), reinterpret_cast< const char* >(buf) + sizeof(buf));
str = str + data;
}
Then i write it in the file
ofstream file;
file.open("C:\\Users\\tom\\Desktop\\img.jpeg");
file << str;
file.close();
and i get the incorrect image.
What's wrong with this method of data extraction (i used this) ?
I'm not familiar with ODBC programming, but at first sight, one issue I can see is that you assume your data length is multiple of your buffer size. But the last read is not guaranteed to return exactly 500 bytes of data.
You should write something like that. Maybe:
string str;
SQLCHAR buf[500];
SQLLEN cbLeft; // #bytes remained
while ((SQL_SUCCEEDED(SQLGetData(StmtHandle,
colnum,
SQL_C_BINARY,
buf,
sizeof(buf),
&cbLeft))))
// ^^^^^^^
{
string data(reinterpret_cast< const char* >(buf),
reinterpret_cast< const char* >(buf)
+ cbLeft);
// ^^^^^^
str = str + data;
Please take a few minutes to review Using Length/Indicator Values in order to check how the length/indicator value is used.
Related
Want to send image through RabbitMQ-C but the image file too big. Receiver cannot retrieve the image. So, I converted image to base64 then put it in JSON.
const char *msg;
FILE *image1;
if (image1 = fopen(path, "rb")) {
fseek(image1, 0, SEEK_END); //used to move file pointer to a specific position
// if fseek(0 success, return 0; not successful, return non-zero value
//SEEK_END: end of file
length = ftell(image1); //ftell(): used to get total size of file after moving the file pointer at the end of the file
sprintf(tmp, "size of file: %d bytes", length);
//convert image to base64
std::string line;
std::ifstream myfile;
myfile.open(path, std::ifstream::binary);
std::vector<char> data((std::istreambuf_iterator<char>(myfile)), std::istreambuf_iterator<char>() );
std::string base64_encode(unsigned char const* bytes_to_encode, unsigned int in_len);
std::string code = base64_encode((unsigned char*)&data[0], (unsigned int)data.size());
//convert std::string to const char
const char* base64_Image = code.c_str();
json j ;
j.push_back("Title");
j.push_back("content");
j.push_back(base64_Image);
std::string sa = j.dump();
msg = sa.c_str(); //convert std::string to const char*
}
else {
return;
}
Using RabbitMQ-C to send the message(msg) to receiver but failed
[error point to here]
is const char* cannot use amqp_cstring_bytes(msg) to convert to amqp_bytes_t??
respo = amqp_basic_publish(conn, 1, amqp_cstring_bytes(exchange), amqp_cstring_bytes(routing_key),0, 0, NULL, amqp_cstring_bytes(msg));
and get this error
If there is a handler for this exception, the program may be safely continued.```
Anyone know how to send image as JSON using RabbitMQ-C & C++ ?
amqp_cstring_bytes expects a C string, which is normally terminated by a NUL byte. Your PNG file is almost guaranteed to contain a NUL byte, so that explains why your message got cut off midway through.
As for the code in your paste: the pointer returned by sa.c_str() is only valid while sa is alive and unmodified. Once you exit the block containing sa's definition, the variable is dead and buried.
Instead, get a buffer of the appropriate size with amqp_bytes_alloc and return that:
amqp_bytes_t bytes = amqp_bytes_malloc(sa.length());
strncpy((char *)bytes.bytes, sa.c_str(), sa.length());
then pass the bytes object to amqp_basic_publish. Don't forget to ampqp_bytes_free it when you're done.
I'm serializing data into binary file using ofstream/ifstream. Data is divided in 2 vectors of strings, one for data names and other for data values, std::vector<std::string> dataNames, std::vector<std::string> dataValues.
I'm writting the data using this function:
void Data::SaveData(std::string path)
{
std::ofstream outfile(path, std::ofstream::binary);
outfile.write(reinterpret_cast<const char *>(&dataNames[0]), dataNames.size() * sizeof(std::string));
outfile.write(reinterpret_cast<const char *>(&dataValues[0]), dataValues.size() * sizeof(std::string));
outfile.close();
}
And reading it using:
bool Data::LoadData(std::string path)
{
bool ret = false;
std::ifstream file(path, std::ifstream::in | std::ifstream::binary);
if (file.is_open())
{
// get length of file:
file.seekg(0, file.end);
int length = file.tellg();
file.seekg(0, file.beg);
char * buffer = new char[length];
file.read(buffer, length);
if (file)
{
char* cursor = buffer;
uint32_t bytes = length / 2;
dataNames.resize(bytes / sizeof(std::string));
memcpy(dataNames.data(), cursor, bytes);
cursor += bytes;
dataValues.resize(bytes / sizeof(std::string));
memcpy(dataValues.data(), cursor, bytes);
delete[] buffer;
buffer = nullptr;
}
file.close();
ret = true;
}
return ret;
}
It works. I can write and read it correctly. Except if any of the strings in dataNames or dataValues has 16 chars or more.
Example of data using strings with less than 16 chars:
dataNames[0] = "Type"
dataNames[1] = "GameObjectCount"
dataValues[0] = "Scene"
dataValues[1] = "5"
data 15 chars
Example of data using strings with more than 16 chars:
dataNames[0] = "Type"
dataNames[1] = "GameObjectsCount" //Added a s. Now have 16 chars
dataValues[0] = "Scene"
dataValues[1] = "5"
data 16 chars
Here you can see that word "GameObjectsCount" doesn't appear and extrange characters are shown.
When reading this file the string is not valid. Sometimes it's empty, sometimes says "Error reading characters of string", sometimes is a radom letter...
Any idea?
Reinterpreting a vector in the manner you have above isn't correct.
outfile.write(reinterpret_cast<const char *>(&dataNames[0]), dataNames.size() * sizeof(std::string));
You don't know how the vector stores data (on the heap, etc..), and you can't assume that you can blindly cast the pointer and write whatever you see out to a file as a method to serialize the data. Furthermore, a std::string isn't necessarily an in-place character array of the size of the input. It's more likely a pointer to an object on the heap.
So, if you want to serialize the data in a vector or another stdlib type, you'll need to write a function to do that manually by iterating over the items and writing them in a properly delimited way.
I'm trying to prefix the length of message so its 4 bytes and use big-endian in C++, but i don't understand how to do that. In Node.JS i'm doing it like that:
var buffer = new Buffer("HELLO WORLD");
//create a buffer with +4 bytes
var consolidatedBuffer = new Buffer(4 + buffer.length);
//write at the beginning of the buffer, the total size
consolidatedBuffer.writeInt32BE(buffer.length, 0);
//Copy the message buffer to the consolidated buffer at position 4 (after the 4 bytes about the size)
buffer.copy(consolidatedBuffer, 4);
i'm trying to achieve the same result in C++, but don't know how to do the same, can someone please show me how that is done in C++? the result should be string byte array
I'm doing that on Mac:
std::string data = "HELLO WORLD";
uint32_t length = htonl( data.length() );
std::string payload;
payload.append(std::to_string(length));
payload.append(data);
One way of doing this would be something like
std::vector<char> pack(const std::string& str) {
const uint32_t sz = str.size();
const uint32_t n_sz = htonl(sz);
std::vector<char> result(sizeof(sz) + sz);
memcpy(result.data(), &n_sz, sizeof(n_sz));
memcpy(result.data() + sizeof(sz), str.data(), sz);
return result;
}
I have the following method that serialize my protocol buffer message and this works perfectly :
string DroolsMsgTransmission::serialize(const google::protobuf::Message* msg, const HeaderMsg& header)const
{
unsigned char buffer[20000];
google::protobuf::io::ArrayOutputStream arr(buffer, sizeof(buffer));
google::protobuf::io::CodedOutputStream output(&arr);
output.WriteVarint32(header.ByteSize());
header.SerializeToCodedStream(&output);
output.WriteVarint32(msg->ByteSize());
msg->SerializeToCodedStream(&output);
return string((char*)buffer, output.ByteCount());
}
Is it possible to use a dynamic buffer instead? I tried the following:
string DroolsMsgTransmission::serialize(const google::protobuf::Message* msg, const HeaderMsg& header)const
{
char* buffer = new char[header.ByteSize() + msg->ByteSize()]();
google::protobuf::io::ArrayOutputStream arr(buffer, sizeof(buffer));
google::protobuf::io::CodedOutputStream output(&arr);
output.WriteVarint32(header.ByteSize());
header.SerializeToCodedStream(&output);
output.WriteVarint32(msg->ByteSize());
msg->SerializeToCodedStream(&output);
string str = string(buffer);
delete buffer;
return str;
}
But this is not working. When I am trying the line above, the returned string does not contain the serialized data at all.
I also tried to use OstreamOutputStream instead of ArrayOutputStream, but no luck.
EDIT
Thanks to comments, I almost made it working :
string DroolsMsgTransmission::serialize(const google::protobuf::Message* msg, const HeaderMsg& header)const
{
char* buffer = new char[header.ByteSize() + msg->ByteSize()]();
google::protobuf::io::ArrayOutputStream arr(buffer, header.ByteSize() + msg->ByteSize());
google::protobuf::io::CodedOutputStream output(&arr);
output.WriteVarint32(header.ByteSize());
header.SerializeToCodedStream(&output);
output.WriteVarint32(msg->ByteSize());
msg->SerializeToCodedStream(&output);
string str = string(buffer ,output.ByteCount());
//return string((char*)buffer, output.ByteCount());
int toto = header.ByteSize() + msg->ByteSize();
int tata = output.ByteCount();
int titi = sizeof(buffer);
delete buffer;
return str;
}
What I did, I replaced this line
google::protobuf::io::ArrayOutputStream arr(buffer, sizeof(buffer));
by this line
google::protobuf::io::ArrayOutputStream arr(buffer, header.ByteSize() + msg->ByteSize());
I it better now, but I still have a problem where the returned string seems a little bit tuncated at the end. It might be related with WriteVarint32, but I don't understand. Somebody can explain why?
Thank you.
You're writing 4 things (the size of the header, the header, the size of the message, the message) but only allocating space for two of them (the header and message).
Hint: a Varint32 never takes more than 5 bytes.
(Also: you need the size in two places - when allocating the buffer and constructing arr. Compute it once and store it in a local variable.)
this is my first post. i am currently taking a networking class and i am required to write a client program that can download all emails from imap.gmail.com:993 to text files. i am required to write this program using winsock and openssl. I was able to connect to the server and fetch the emails. For emails with small data, i had no problem receiving it. But for emails with large data such as an images that is base64-decoded, i was able to download only a part of it.
so my question is How can i tell the client to wait until it received all the data from the server?
Here is what i have done so far:
void fetchMail(SSL *sslConnection,int lowerLimit, int UpperLimit)
{
SYSTEMTIME lt;
ofstream outfile;
GetLocalTime(<);
char szFile[MAX_PATH + 1];
char szPath[MAX_PATH+1];
char message[BUFSIZE];
char result[BUFSIZE];
::GetModuleFileName( NULL, szPath, MAX_PATH );
// Change file name to current full path
LPCTSTR psz = strchr( szPath, '\\');
if( psz != NULL )
{
szPath[psz-szPath] = '\0';
}
char szMailBox[MAX_PATH+1];
memset( szMailBox, 0, sizeof(szMailBox));
wsprintf( szMailBox, "%s\\inbox", szPath );
// Create a folder to store emails
::CreateDirectory( szMailBox, NULL );
for(int i = lowerLimit; i < UpperLimit; ++i)
{
// Create a folder to store emails
memset( szFile, 0, sizeof(szFile));
memset( result, 0, sizeof(result));
memset( message, 0, sizeof(message));
::sprintf(szFile,"%s\\%d%d%d%d%d%d.txt", szMailBox, lt.wHour, lt.wMinute,lt.wMinute,lt.wSecond, lt.wMilliseconds,i);
string Result;//string which will contain the result
stringstream convert; // stringstream used for the conversion
const char * num;
convert << i;//add the value of Number to the characters in the stream
Result = convert.str();//set Result to the content of the stream
num = Result.c_str();
strcpy(result, "tag FETCH ");
strcat(result, num);
strcat(result, " (BODY[TEXT])\r\n");
int n = 0;
cout << "\nFETCHING : \n";
SSL_write(sslConnection, result, strlen(result));
outfile.open(szFile );
SSL_read(sslConnection, message, sizeof(message)-1);
outfile <<message ;
outfile.close();
}
}
First of all some points on your code:
You use strcpy, strcat and all those unchecked, unsafe C functions. You might easily get buffer overflows and other kinds of errors. Consider to use C++ strings, vectors, arrays.
You do a lot of different things in that function, on different levels of abstraction. AFAICS only the two SSL_* function calls are really about fetching that mail. Consider to break out some functions to improve readability.
Now to your problem: Googling a bit about SSL_read, you will see that it returns an int, denoting how many bytes were actually read. You should use that return value - not only for this issue but also for error handling. If the mail data is longer than your buffer, the function will read until the buffer is filled and return its size. You should continue to call the function until all bytes have been read.