[gdal-dev] Call for discussion on RFC 70: Guessing output format from output file name extension

Mark Johnson mj10777 at googlemail.com
Fri Nov 10 04:40:32 PST 2017


>
> ogr2ogr output.gpkg input.shp Where would you read the Magic-Headers in
> this case?

Before the input file is opened.
Read the first 100 bytes, compare with known signatures


if ((memcmp (blob, tiff_signature_big, 4) == 0) &&
    (memcmp (blob, tiff_signature_little, 4) == 0))
{
  return type_tiff;
}
else if (strncmp ((char *) blob, "%PDF-", 5) == 0)
{
  return type_pdf;
}
else if (strncmp ((char *) blob, "SQLite format 3", 14) == 0)
{
  return type_sqlite3; // even if the extension is called '.db' or '.blombo'
}
else if (strncmp ((char *) blob, "** This file contains an SQLite 2", 14)
== 0)
{
  return type_sqlite2; // even if the extension is called '.db'
}
else if ((memcmp (blob, jp2_big, 12) == 0) &&
    (memcmp (blob, jp2_little, 12) == 0))
{
  return type_jp2;
}
else if ((memcmp (blob,jpeg1_signature, 2) == 0) &&
    (memcmp (blob, blob + size - 2, jpeg2_signature, 2) == 0))
{
  return type_jp2;
}
else // for other signatures
{
  return type_other;
}
else // for file extensions
{
  return type_kmz;
}

A Sqlite3 Database can have any type of extension.
Sometimes even '.sdb' is used, which normally is reserved for a
Sybase-Database - which is also a file based Database with its own Magic
Number.

To reliably determine this you use 'Magic Numbers', so that you know trough
knowledge instead of guessing.

'Magic Numbers' were introduced to avoid this problem, since a user could
rename the file to anything they want.

The above code is based on the used spatialite code, which read a BLOB,
where the file is stored and thus has no file-name or extension to read.

Reading the Magic-Header signatures first and then guessing from the
file extension may be a better approach.
>
> Even if the title uses the word 'Guessing', that is not a reason not so
suggest a more practical approach that, is less error prone.

Mark
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20171110/84944683/attachment-0001.html>


More information about the gdal-dev mailing list