brechtsanders / xlsxio

XLSX I/O - C library for reading and writing .xlsx files
MIT License
417 stars 112 forks source link

xlsxio fails if rels paths are 'absolute' #32

Closed webern closed 6 years ago

webern commented 6 years ago

I'm not sure whether or not this is allowed in the xlsx spec, but my same difficult file from #28 specified it's paths like this

<?xml version="1.0" encoding="utf-8"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
    <Relationship Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="/xl/worksheets/sheet1.xml" Id="R4f1c94c378d64409" />
    <Relationship Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="/xl/styles.xml" Id="R48c49c2535d74bcc" />
    <Relationship Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings" Target="/xl/sharedStrings.xml" Id="R5c29e56ada2e432c" />
</Relationships>

This failed in xlsxio and was fixed like this:

//join basepath and filename (caller must free result)
XML_Char* join_basepath_filename (const XML_Char* basepath, const XML_Char* filename)
{
  XML_Char* result = NULL;
  if (filename && *filename) {
    size_t basepathlen = (basepath ? XML_Char_len(basepath) : 0);
    size_t filenamelen = XML_Char_len(filename);
    if ((result = XML_Char_malloc(basepathlen + filenamelen + 1)) != NULL) {

        if(filename[0] == '/') {
            // the filename is written as an 'absolute' path, we need to
            // remove the leading slash, but otherwise return as-is
            XML_Char_poscpy(result, 0, filename + 1, filenamelen - 1);
            result[filenamelen - 1] = 0;
            return result;
        }

        if (basepathlen > 0)
        XML_Char_poscpy(result, 0, basepath, basepathlen);
      XML_Char_poscpy(result, basepathlen, filename, filenamelen);
      result[basepathlen + filenamelen] = 0;
    }
  }
  return result;
}
brechtsanders commented 6 years ago

Not sure either if it's in the XLSX specifications, but I fixed join_basepath_filename anyway just in case. Updated in subversion and will be part of next release.