Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

saving structure in v7 format with compression #251

Closed
SciCed opened this issue Jun 13, 2024 · 9 comments
Closed

saving structure in v7 format with compression #251

SciCed opened this issue Jun 13, 2024 · 9 comments
Labels

Comments

@SciCed
Copy link

SciCed commented Jun 13, 2024

Saving a structure in v7 format with compression creates a corrupted file.
After some investigation, the problem seems to be present since matio 1.5.12.

File generated with matio 1.5.9 (not corrupted):
structS-matio-1.5.9.zip

File generated with matio 1.5.27 (corrupted):
structS-matio-1.5.27.zip

The structure saved contains two fields type and age with values "logiciel" and 30.

@tbeu
Copy link
Owner

tbeu commented Jun 13, 2024

Thanks for reporting. That's rather unexpected given the age of v1.5.12. Can you share the code how you created the MAT files? Did you run git bisect to find the bad commit?

@SciCed
Copy link
Author

SciCed commented Jun 14, 2024

I did not use git bisect, I rebuilt matio on different tags to find the last working version.
The Scilab code used to generate files:

structS = struct("type", "logiciel", "age", 30);
savematfile("structS-matio-1.5.9.mat", "structS", "-v7")

Files generated using matio 1.5.9 can be read back in 1.5.9 or 1.5.27 but files generated using 1.5.27 cannot be read in 1.5.9 nor 1.5.27, that is why I incriminated the save.

Without compression, I can save and load files without trouble.
I hope this helps.

@tbeu
Copy link
Owner

tbeu commented Jun 15, 2024

Thanks for the details! Do you mind to run git bisect to find the bad commit as in your other issue? I do hope that it will be easier for you than guessing by me because of the many changes between 1.5.9 and 1.5.12. Thank you!

@tbeu
Copy link
Owner

tbeu commented Jul 16, 2024

I am afraid I cannot reproduce without the driving Scilab code. The following vanilla Matio test code produces a 100% binary identical MAT file to your valid structS-matio-1.5.9.mat - tested with current abe00b8 on Win (little endian) with MSVC.

mat_t *mat;
char output_name[256];
int version[3];

Mat_GetLibraryVersion(version, version + 1, version + 2);
snprintf(output_name, 256, "structS-matio-%d.%d.%d.mat", version[0], version[1], version[2]);

mat = Mat_CreateVer(output_name, "MATLAB 5.0 MAT-file, Platform: x86_64-pc-windows, Created by: libmatio v1.5.9 on Thu Jun 13 16:06:30 2024\x0A", MAT_FT_MAT5);
if (mat) {
  const wchar_t *str = L"logiciel";
  const double age[1] = { 30.0 };
  const size_t num_fields = 2;
  const char *fieldnames[2] = { "type", "age" };
  size_t dims[2];
  matvar_t *matvar, *struct_matvar;
  
  dims[0] = 1; dims[1] = 1;
  struct_matvar = Mat_VarCreateStruct("structS", 2, dims, fieldnames, num_fields);
  dims[0] = 1; dims[1] = 8;
  matvar = Mat_VarCreate(fieldnames[0], MAT_C_CHAR, MAT_T_UINT16, 2, dims, (void *)str, 0);
  Mat_VarSetStructFieldByName(struct_matvar, fieldnames[0], 0, matvar);
  dims[0] = 1; dims[1] = 1;
  matvar = Mat_VarCreate(fieldnames[1], MAT_C_DOUBLE, MAT_T_DOUBLE, 2, dims, (void *)age, 0);
  Mat_VarSetStructFieldByName(struct_matvar, fieldnames[1], 0, matvar);
  Mat_VarWrite(mat, struct_matvar, MAT_COMPRESSION_ZLIB);
  Mat_VarFree(struct_matvar);
  Mat_Close(mat);
}

@SciCed
Copy link
Author

SciCed commented Aug 28, 2024

Thanks for having a look.
I've built your code and the generated file can be loaded in Scilab.
I'll check what's the difference between your code and what we are doing in Scilab.

@SciCed
Copy link
Author

SciCed commented Aug 29, 2024

Here is a code, based on yours, that reproduces what we are doing in Scilab.
The problem occurred when I changed the string logiciel from wchar_t* to char* and MAT_T_UINT8 to MAT_T_UINT16.

    mat_t *mat;
    char output_name[256];
    int version[3];

    Mat_GetLibraryVersion(version, version + 1, version + 2);
    snprintf(output_name, 256, "structS-matio-%d.%d.%d.mat", version[0], version[1], version[2]);

    mat = Mat_CreateVer(output_name, "MATLAB 5.0 MAT-file, Platform: x86_64-pc-windows, Created by: libmatio v1.5.9 on Thu Jun 13 16:06:30 2024\x0A", MAT_FT_MAT5);
    if (mat) {
        const char* str = "logiciel";
        const double age[1] = { 30.0 };
        const size_t num_fields = 2;
        const char *fieldnames[2] = { "type", "age" };
        size_t dims[2];
        matvar_t *matvar, *struct_matvar;
        matvar_t* structEntries[3] = {NULL, NULL, NULL};

        dims[0] = 1; dims[1] = 8;
        structEntries[0] = Mat_VarCreate(fieldnames[0], MAT_C_CHAR, MAT_T_UINT8, 2, dims, (void *)str, 0);
        dims[0] = 1; dims[1] = 1;
        structEntries[1] = Mat_VarCreate(fieldnames[1], MAT_C_DOUBLE, MAT_T_DOUBLE, 2, dims, (void *)age, 0);
        dims[0] = 1; dims[1] = 1;
        struct_matvar = Mat_VarCreate("structS", MAT_C_STRUCT, MAT_T_STRUCT, 2, dims, structEntries, 0);

        Mat_VarWrite(mat, struct_matvar, MAT_COMPRESSION_ZLIB);
        Mat_VarFree(struct_matvar);
        Mat_Close(mat);
    }

@tbeu
Copy link
Owner

tbeu commented Aug 29, 2024

Thanks for the test. 6321cda (on mat5.c) seems to be the suspicious commit. Need to investigate in more detail.

@tbeu tbeu added bug and removed input needed labels Sep 1, 2024
tbeu added a commit that referenced this issue Sep 4, 2024
tbeu added a commit that referenced this issue Sep 4, 2024
tbeu added a commit that referenced this issue Sep 4, 2024
@tbeu
Copy link
Owner

tbeu commented Sep 5, 2024

@SciCed Please confirm that 8407b4e fixes the issue.

@SciCed
Copy link
Author

SciCed commented Sep 11, 2024

@tbeu The fix is working well, thanks!

@tbeu tbeu closed this as completed Sep 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants