Libzip - Error: Error while opening the archive : no error - c++

I'm trying to find out the solution to solve a problem;
In fact, i'm writing my own tool to make saves using libzip in C++ to compress the files.
Absolutly not finished but i wanted to make some tests, then i do and obtain a "funny" error from the log.
Here's my function:
void save(std::vector<std::string> filepath, std::string savepath){
int err;
savepath += time(NULL);
zip* saveArchive = zip_open(savepath.c_str(), ZIP_CREATE , &err);
if(err != ZIP_ER_OK) throw xif::sys_error("Error while opening the archive", zip_strerror(saveArchive));
for(int i = 0; i < filepath.size(); i++){
if(filepath[i].find("/") == std::string::npos){}
if(filepath[i].find(".cfg") == std::string::npos){
err = (int) zip_file_add(saveArchive, filepath[i].c_str(), NULL, NULL);
if(err == -1) throw xif::sys_error("Error while adding the files", zip_strerror(saveArchive));
}
}
if(zip_close(saveArchive) == -1) throw xif::sys_error("Error while closing the archive", zip_strerror(saveArchive));
}
I get a => Error : Error while opening the archive : No error
And, of course, i didn't have any .zip written.
If you could help me, thanks to you !

The documentation for zip_open says that it only sets *errorp if the open fails. Either test for saveArchive == nullptr or initialize err to
ZIP_ER_OK.
P.S. The search for '/' does nothing. Did you mean to put a continue in that block?
The other problematic line is:
savepath += time(NULL);
If that is the standard time function, that returns a time in seconds since the epoch. That will probably get truncated to a char, and then that char appended to the file name. That will cause strange characters to appear in the filename! I suggest using std::chrono to convert to text.

Related

Steam Protocol C++ Unzip Multi message

I'm writting a plugin for Steam protocol in C++. I'm using https://github.com/seishun/SteamPP which uses protobufs from https://github.com/SteamRE/SteamKit and generally it works. I can communicate Steam, I can send and receive single messages (including logging in) without problems, but Steam sends often few messages zipped in one message (EMsg::Multi from protobuf) and here is my problem. I cannot unzip them correctly. I can't understand what I'm doing wrong.
std::string unzip(std::string &input) {
auto archive = archive_read_new();
auto result = archive_read_support_filter_all(archive);
assert(result == ARCHIVE_OK);
result = archive_read_support_format_zip(archive);
assert(result == ARCHIVE_OK);
result = archive_read_open_memory(archive, &input[0], input.size());
assert(result == ARCHIVE_OK);
archive_entry* entry;
result = archive_read_next_header(archive, &entry);
if (result != ARCHIVE_OK) {
return "read next header error " + std::to_string(result);
}
assert(result == ARCHIVE_OK);
std::string output;
output.resize(archive_entry_size(entry));
auto length = archive_read_data(archive, &output[0], output.size());
assert(length == output.size());
if (length != output.size()) {
return "hello world" + std::to_string(length);
}
assert(archive_read_next_header(archive, &entry) == ARCHIVE_EOF);
result = archive_read_free(archive);
assert(result == ARCHIVE_OK);
return output;
}
in this function (libarchive) archive_read_data returns -25 which is an error code and next assert throws error. What is wrong? It's working well in C# SteamKit version and also in node.js version. I have tried also Crypto++ Gunzip but it throws an CryptoPP::Gunzip::HeaderErr exception.
CHECK DEBUG GIF
I think you are missing Zlib in your Libarchive, because Steam messages are deflated and you need Zlib to process them. Now libarchive couldn't process them so it returns -25 because of unsupported file type. Try to recompile libarchive with Zlib attached in CMake.

Error opening physical file with _Ropen

I'm trying to put physical file on IFS.
So trying to open file just to guarantee its presence on source.
This is how I do it:
errno=0;
if ( ((pf = _Ropen(pfname, "rr, nullcap=Y")) == NULL) || (errno!=0) )
{
printf("\nError: Cannot open file %s\n",pfname);
//...
}
However, the file not gets opened with pf = SPP:*NULL result
That won't be the issue, but I can't also view errno.
The eval errno gives Syntax error occurred. without any clue what has happened.
I'm still able to view contents of pfname: it looks like 'MYLIB/MYFILE'
Absolute path was also tried:
'/QSYS.LIB/MYLIB.LIB/MYFILE.FILE', without any difference - same error persists.
IBM IFS explorer clearly shows contents of MYLIB and there is a MYFILE inside this lib.
UPD
I've added some debug logging just to extcact error description or error code:
numbytes = sprintf( (char *)NULL, "%s", strerror(errno) );
ret = (char *)malloc( ( numbytes + 1 ) * sizeof( char ) );
sprintf( ret, "%s", strerror(errno) );
And the result of ret is SPP:*NULL.
Any ideas to try?
It turned out to be super simple - null byte was missing so _Ropen can not really access the file thus no error to be passed as well.
To handle this the argument of null-terminated byte array needs to be passed from caller.
In case of rpg solution looks like 'MYFILE/MYLIB' + X'00'

c++ popen, fgetc, fscanf, impossible to read a line, non error

I can open a process
(i perform a shell script whose output is
1443772262175; URL filter
1443772267339; URL blocked ...) with popen, no problem
but after impossible to read it
I try fscanf, fgets(buff_out, sizeof(buff_out)-1, pf)
int lineno = 0;
if ((pf = popen(files_to_open, "r")) != NULL)
while (!feof(pf))
{
fscanf(pf, "%s %s", buffer, afficher);
message << buffer << " " << afficher ;
}
or while (fgets(buff_out, sizeof(buff_out)-1, pf) != NULL) {
}
i try with a program test it works fine but with a program more complexe (parallel) that i can't debug (the programm complexe) it fails no error just nothing to scan or read
Should i abandon popen?
Thanks for help

libzip: validating a zip file before loading

I use libzip to open zip files in my application and in order to ensure good behavior in case of corrupt zip files I manually corrupted a zip file (by removing a few random lines with a text editor) and try to load that file. However this hangs the entire app because zip_fread() never returns.
Is there a where to determine if a zip file is valid before loading it to avoid such situations?
Update
The behavior seems to depend on the version, so I probably only need to update. This is the code I use on Windows, Mac OS and Linux:
int err;
zip *z= zip_open(zipfile.c_str(), 0, &err);
if (!z)
{
if (err == ZIP_ER_NOZIP)
throw std::runtime_error("The file is not a Workbench document.");
else if (err == ZIP_ER_MEMORY)
throw grt::os_error("Cannot allocate enough memory to open document.");
else if (err == ZIP_ER_NOENT)
throw grt::os_error("File not found.");
int len= zip_error_to_str(NULL, 0, 0, err);
std::string msg;
if (len > 0)
{
char *buf= (char*)g_malloc(len+1);
zip_error_to_str(buf, len+1, 0, err);
msg= buf;
g_free(buf);
}
else
msg= "error opening zip archive";
zip_close(z);
throw std::runtime_error(strfmt(_("Cannot open document file: %s"), msg.c_str()));
}
On OS X this fragment does not return an error (I used the same file for all platforms). Instead the following zip_read() call just hangs. On the other platforms zip_read() immediately returns with a result < 0, so it's easy to catch the error there.

"Access violation writing location" with file.getline? (Only in release build)

getting this error in an application written in C++ (VS 2010):
Unhandled exception at 0x77648da9 in divt.exe: 0xC0000005: Access
violation writing location 0x00000014.
it points to this function in free.c:
void __cdecl _free_base (void * pBlock)
{
int retval = 0;
if (pBlock == NULL)
return;
RTCCALLBACK(_RTC_Free_hook, (pBlock, 0));
retval = HeapFree(_crtheap, 0, pBlock);
if (retval == 0) //<-----------------------right here
{
errno = _get_errno_from_oserr(GetLastError());
}
}
Via debugging I was able to determine where its actually crashing:
void MenuState::LoadContentFromFile(char* File,std::string &Content)
{
std::string strbuf;
char buffer[1028];
std::fstream file;
file.open(File,std::ios_base::in);
if(file.fail())
{
Content = ErrorTable->GetString("W0001");
return;
}
if(file.is_open())
{
while(!file.eof())
{
file.getline(buffer,128,'\n'); // <----here
strbuf = buffer;
Content += strbuf + "\n";
}
}
file.close();
strbuf.clear();
}
It crashes on file.getline(buffer,128,'\n');
I don't understand why but it's only doing it in release build (Optimizations turned off), on debug build its working fine.
Any Ideas?
I know this is an old question, but when you encounter these sorts of issues buried deep in files such as, free.c or xmemory, you may also want to double check your project configuration. Especially when the issue pertains to only certain build configurations.
For example, in MSVC check your Project Properties > Configuration Properties > C/C++ > Code Generation > Runtime Library. Make sure it consistent for all dependencies and that it is set to a Debug/Release variant depending on the current build.
I would bet that the read prior to the read crashing the application actually failed (although I'm not quite sure why it would crash). The important thing to note is that eof() is only good for determining what caused a read failure (and typically suppressing an error message). In addition, you always want to check after the read whether it was successful. Finally, I can't see any reason why you don't read an std::string directly. In summary, try to use this loop instead:
for (std::string strbuf; std::getline(file, strbuf); ) {
Content += strbuf;
}
Asked a friend for help, we came up with this Solution:
std::string strbuf;
char buffer[256] = "\0";
FILE* f = fopen(File, "rt");
while(fgets(buffer,sizeof(buffer),f) != NULL)
{
Content += buffer;
}
fclose(f);
strbuf.clear();
Works fine, still thanks for your efforts.