Skip'ing on a Source does not work as expected - c++

I use Crypto++ 5.6.3 and iI need the FileSource Skip(...) function. Unfortunately this function does nothing!
Here is a example for this function.
string filename = ...;
string str;
FileSource file(filename, false, new HexEncoder(new StringSink(str)));
file.Skip(24);
file.PumpAll();
Can somebody help me?

I use Crypto++ 5.6.3 and iI need the FileSource "skip(...) function. Unfortunately this function does nothing!
I was able to duplicate this using strings under Master, 5.6.3, and 5.6.2 on OS X 10.8.5 and Ubuntu 14.04.
$ cat test.cxx
#include <string>
#include <iostream>
using namespace std;
#include <filters.h>
#include <hex.h>
using namespace CryptoPP;
int main(int argc, char* argv[])
{
string str1, str2;
HexEncoder enc(new StringSink(str1));
for(unsigned int i=0; i < 32; i++)
enc.Put((byte)i);
enc.MessageEnd();
cout << "str1: " << str1 <<endl;
StringSource ss(str1, false, new StringSink(str2));
ss.Skip(10);
ss.PumpAll();
cout << "str2: " << str2 << endl;
return 0;
}
And:
$ ./test.exe
str1: 000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F
str2: 000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F
Crypto++ 5.6.2 is significant because it was the last version Wei worked on before turning the library over to the community. An issue in 5.6.2 is just a latent bug and we encounter them on occasion, just like any other project. ("Wei" bugs are actually kind of rare, and they are closer to "Knuth" bugs in his Art of Computer Programming).
If its a 5.6.3 and above problem, then it means the community broke it. If the community broke it, then we need to perform a post-mortem and analyze how/why we managed to break something that used to work.
Here's the bug report for the library: Issue 248: Skip'ing on a Source does not work. We are trying to determine if its a bug; and if so, then how to proceed.
EDIT 1: I was able to investigate the issue a little more. You can read the analysis at Comment 242890863. The short of it is, Skip is used to discard bytes on an output buffer (an AttachedTransformation()), so things are somewhat working as expected. However, there's nothing intuitive about Skip not working on the Source, and only working on the attached Filter (q.v., we're here).
I also asked for some feedback on the mailing list at Issue 248: Skip'ing on a Source does not work. DB and WD spotted it right away - its a design issue in the library.
Here's the workaround you can use for the moment. Effectively, you Pump() into a null Filter which discards the input as expected. Then you attach the real filter chain to handle the real processing.
#include <string>
#include <iostream>
using namespace std;
#include <filters.h>
#include <hex.h>
using namespace CryptoPP;
int main(int argc, char* argv[])
{
string str1, str2;
HexEncoder enc(new StringSink(str1));
for(unsigned int i=0; i < 32; i++)
enc.Put((byte)i);
enc.MessageEnd();
cout << "str1: " << str1 <<endl;
// 'ss' has a NULL AttachedTransformation()
StringSource ss(str1, false);
ss.Pump(10);
// Attach the real filter chain to 'ss'
ss.Attach(new StringSink(str2));
ss.PumpAll();
cout << "str2: " << str2 << endl;
return 0;
}
It produces the expected output:
$ ./test.exe
str1: 000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F
str2: 05060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F
In your sample program, I believe the workaround would be:
FileSource file(filename, false);
file.Pump(24);
file.Attach(new HexEncoder(new StringSink(str)));
file.PumpAll();
EDIT 2: Here's a slightly more verbose way to achieve the work around (thanks DB). It stresses the point that bytes are being discarded. TheBitBucket() is simply a discard filter, and it serves the same purpose as a null AttachedTransformation().
int main(int argc, char* argv[])
{
string str1, str2;
HexEncoder enc(new StringSink(str1));
for(unsigned int i=0; i < 32; i++)
enc.Put((byte)i);
enc.MessageEnd();
cout << "str1: " << str1 <<endl;
StringSource ss(str1, false, new Redirector(TheBitBucket()));
ss.Pump(10);
ss.Detach(new StringSink(str2));
ss.PumpAll();
cout << "str2: " << str2 << endl;
return 0;
}
There's another subtle difference in the program above: It calls Detach, which free's the former filter chain. If you called Attach, then the former chain would be detached, returned to the caller but not free'd.

Related

Crash when using ofstream

I have a problem with the following code. I would like to write out data into different files in a loop. But when I instantiate ofstreams in a loop, my program silently crashes. I've boiled the code down, so that it doesn't do anything useful. It just demonstrates the behavior that I cannot explain:
#include <iostream>
#include <fstream>
using namespace std;
int main (int argc, char* argv[])
{
ofstream test_a("a.json");
cout << "test a" << endl;
ofstream test_b("b.json");
cout << "test b" << endl;
for (int idx = 0; idx < 3; idx++)
{
cout << "test " << idx << endl;
ofstream test("test_" + to_string(idx) + ".json");
}
return 0;
}
This is the output:
test a
test b
test 0
Nothing else, no error, nothing.
The first two ofstreams are apparently fine, but the loop stops somewhere after the cout. I am using Windows 10 and mingw:
gcc version 8.1.0 (x86_64-win32-seh-rev0, Built by MinGW-W64 project)
Thanks to the helpful comments I found that I linked a std library that must have been from an older version of my compiler. I fixed that and now it works.

Why does the function find of C++ stl string sometimes go wrong sometime go right?

I am trying to do some file reading with C++ in Ubuntu 16.04 (GCC&G++ 5.4 and CMake 3.5.1).
The test file (named 123.txt) have only a line words just like this:
Reprojection error: avg = 0.110258 max = 0.491361
I just want to get the avg error and max error. My method is to get a line and put them into a std::string
and use string::find. My codes are very easy just like this:
#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;
int main()
{
FILE *fp = fopen("123.txt", "r");
char tmp[60];
string str;
fgets(tmp, size_t(tmp), fp);
fclose(fp);
cout << tmp << endl;
str = tmp;
cout << str.size() << endl;
size_t avg = str.find("avg");
size_t max = str.find("max");
cout << avg << endl;
cout << max << endl;
}
I can use g++ to compile it successfully. But I meet a strange issue.
When I first run it in the command, it will get the right result:
Reprojection error: avg = 0.110258 max = 0.491361
52
20
37
If I run codes again, it will go wrong sometimes just like this:
p
2
18446744073709551615
18446744073709551615
The "p" is a disorderly code which can not be shown correctly in the command. I am not good at C++ and feel confused about it. Is there someone who can say something? Thank you!
The expression
fgets(tmp, size_t(tmp), fp);
is ill-formed, size_t(tmp) will not work as you expect, you need sizeof(tmp).
The 52 value you get is because fgets consumes the \n character and this is counted too, actually the string has 51 characters counting with spaces.
That said, in this case you can use better C++ tools to replace the C ones you are using, fopen can be replaced by using the fstream library, fgets can be replaced by getline.
Something like:
#include <iostream>
#include <string>
#include <fstream>
int main()
{
std::ifstream fp("123.txt"); //C++ filestream
if (fp.is_open()) {//check for file opening errors
std::string str;
std::getline(fp, str); //C++ read from file
fp.close();
std::cout << str << std::endl;
std::cout << str.size() << std::endl;
size_t avg = str.find("avg");
size_t max = str.find("max");
std::cout << avg << std::endl;
std::cout << max << std::endl;
}
else{
std::cerr << "Couldn't open file";
}
}
Note that I dind't use using namespace std;, this is for a reason, it's not a good practice, you can check this thread for more details.

How to create directory c++ (using _mkdir)

Today I did a lot of research online about how to create a directory on C++
and found a lot of way to do that, some easier than others.
I tried the _mkdir function using _mkdir("C:/Users/..."); to create a folder. Note that the argument of function will be converted into a const char*.
So far, so good, but when I want to change the path, it does not work (see the code below). I have a default string path "E:/test/new", and I want to create 10 sub-folders: new1, new2, newN, ..., new10.
To do that, I concatenate the string with a number (the counter of the for-loop), converted into char using static_cast, then I transform the string using c_str(), and assign it to a const char* variable.
The compiler has no problem compiling it, but it doesn't work. It prints 10 times "Impossible create folder n". What's wrong?
I probably made a mistake when transforming the string using c_str() to a get a const char*?.
Also, is there a way to create a folder using something else? I looked at CreateDirectory(); (API) but it uses keyword like DWORD HANDLE, etc., that are a little bit difficult to understand for a no-advanced level (I don't know what these mean).
#include <iostream>
#include <Windows.h>
#include<direct.h>
using namespace std;
int main()
{
int stat;
string path_s = "E:/test/new";
for (int i = 1; i <= 10; i++)
{
const char* path_c = (path_s + static_cast<char>(i + '0')).c_str();
stat = _mkdir(path_c);
if (!stat)
cout << "Folder created " << i << endl;
else
cout << "Impossible create folder " << i << endl;
Sleep(10);
}
return 0;
}
If your compiler supports c++17, you can use filesystem library to do what you want.
#include <filesystem>
#include <string>
#include <iostream>
namespace fs = std::filesystem;
int main(){
const std::string path = "E:/test/new";
for(int i = 1; i <= 10; ++i){
try{
if(fs::create_directory(path + std::to_string(i)))
std::cout << "Created a directory\n";
else
std::cerr << "Failed to create a directory\n";\
}catch(const std::exception& e){
std::cerr << e.what() << '\n';
}
}
return 0;
}
The problem is that (path_s + static_cast<char>(i + '0')) creates a temporary object. One whose life-time ends (and is destructed) just after c_str() has been called.
That leaves you with a pointer to a string that no longer exist, and using it in almost any way will lead to undefined behavior.
Instead save the std::string object, and call c_str() just when needed:
std::string path = path_s + std::to_string(i);
_mkdir(path.c_str());
Note that under Linux, you can use the mkdir command as follows:
#include <sys/stat.h>
...
const int dir_err = mkdir("foo", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
if (-1 == dir_err){
printf("Error creating directory!n");
exit(1);
}
More information on it can be gleaned from reading man 2 mkdir.

Passing parameters from command line to C++

Searching the net for examples how to pass command line parameters to a C++ code, I came up with an abandoned post where this process is being explained. This code was not working and after a few amendments I came up with the following (working) code:
#include <iostream>
#include <windows.h>
#include <fstream>
#include <string>
using namespace std;
// When passing char arrays as parameters they must be pointers
int main(int argc, char* argv[]) {
if (argc < 2) { // Check the value of argc. If not enough parameters have been passed, inform user and exit.
std::cout << "Usage is -i <index file name including path and drive letter>\n"; // Inform the user of how to use the program
std::cin.get();
exit(0);
} else { // if we got enough parameters...
char* indFile;
//std::cout << argv[0];
for (int i = 1; i < argc; i++) { /* We will iterate over argv[] to get the parameters stored inside.
* Note that we're starting on 1 because we don't need to know the
* path of the program, which is stored in argv[0] */
if (i + 1 != argc) {// Check that we haven't finished parsing already
if (strcmp(argv[i],"/x")==0) {
// We know the next argument *should* be the filename:
char indFile=*argv[i+1];
std::cout << "This is the value coming from std::cout << argv[i+1]: " << argv[i+1] <<"\n";
std::cout << "This is the value of indFile coming from char indFile=*argv[i+1]: " <<indFile <<"\n";
} else {
std::cout << argv[i];
std::cout << " Not enough or invalid arguments, please try again.\n";
Sleep(2000);
exit(0);
}
//std::cout << argv[i] << " ";
}
//... some more code
std::cin.get();
return 0;
}
}
}
Executing this code from the Windows command line using:
MyProgram.exe /x filename
returns the next output:
This is the attribute of parameter /x: filename
This is the value from *argv[i+1]: f
The original post from cplusplus.com did not compile; the code above does.
As you can see printing the argv[2] gives me the name of the file. When I try to capture the file name into another var so I can use it in the C++ program, I only get the first character (second response line).
Now for my question: How can I read the value from the command line parameter the pointer is pointing to?
Hope someone can help this newbie in C++ :-)
*argv[i+1]
Accesses the 1st char of the char* argv[] argument.
To get the whole value use something like
std::string filename(argv[i+1]);
instead.
You can't store a string in a single char.
Here's the once-an-idiom for copying the main arguments to more manageable objects:
#include <string>
#include <vector>
using namespace std;
void foo( vector<string> const& args )
{
// Whatever
(void) args;
}
auto main( int n, char* raw_args[] )
-> int
{
vector<string> const args{ raw_args, raw_args + n };
foo( args );
}
Do note that this code relies on an assumption that the encoding used for the main arguments can represent the actual command line arguments. That assumption holds in Unix-land, but not in Windows. In Windows, if you want to deal with non-ASCII text in command line arguments, you'd better use a third party solution or roll your own, e.g. using Windows' GetCommandLine API function.

Why in C++ function boost::algorithm::join_if a std::bad_cast exception is thrown?

I found a problem in my code. When I use boost::algorithm::join it works normally, but when I use boost::algorithm::join_if a bad_cast is thrown. My code is below:
#include <iostream>
#include <string>
#include <list>
#include <boost/algorithm/string.hpp>
using namespace std;
main(int argc, char **argv)
{
list<string> players;
players.push_back("ProPlayer98");
players.push_back("King of Darkness");
players.push_back("Noob999");
players.push_back("Daily Queen");
cout << boost::algorithm::join(players, ", ") << endl; // it works
cout << boost::algorithm::join_if(players, ", ", boost::is_alpha()) << endl; // bad_cast
}
The output of my program is:
ProPlayer98, King of Darkness, Noob999, Daily Queen
terminate called after throwing an instance of 'std::bad_cast'
what(): std::bad_cast
Abort trap (core dumped)
I have used some times boost::algorithm functions to play with text, few times I was using
predicates, but none of problems like that ever occurred.
I even tried to replace const char* to std::string:
cout << boost::algorithm::join_if(players, string(", "), boost::is_alpha()) << endl;
but problem is still the same.
EDIT:
I would like a solution which works also in C++ older than C++11
boost::is_alpha is for characters
Use like following:-
cout << boost::algorithm::join_if(players, ", ",
[](const std::string & s){
return boost::all(s,boost::is_alpha());
}) << endl;
Here obviously, you won't get any output as space ' ' and numerals are present in players.
Use boost::alnum() instead.