libextractor

GNU libextractor
Log | Files | Refs | Submodules | README | LICENSE

libextractor.texi (43163B)


      1 \input texinfo                  @c -*- Texinfo -*-
      2 @c % The structure of this document is based on the
      3 @c % Texinfo manual from libgcrypt by Werner Koch and 
      4 @c % and Moritz Schulte.
      5 @c %**start of header
      6 @setfilename libextractor.info
      7 @include version.texi
      8 @settitle The GNU libextractor Reference Manual
      9 @c Unify all the indices into concept index.
     10 @syncodeindex fn cp
     11 @syncodeindex vr cp
     12 @syncodeindex ky cp
     13 @syncodeindex pg cp
     14 @syncodeindex tp cp
     15 @c %**end of header
     16 @copying
     17 This manual is for GNU libextractor
     18 (version @value{VERSION}, @value{UPDATED}), a library for metadata
     19 extraction.
     20 
     21 Copyright @copyright{} 2007, 2010, 2012 Christian Grothoff
     22 
     23 @quotation
     24 Permission is granted to copy, distribute and/or modify this document
     25 under the terms of the GNU Free Documentation License, Version 1.3
     26 or any later version published by the Free Software Foundation;
     27 with no Invariant Sections, no Front-Cover Texts, and no Back-Cover
     28 Texts.  A copy of the license is included in the section entitled ``GNU
     29 Free Documentation License''.
     30 @end quotation
     31 @end copying
     32 
     33 @dircategory Software libraries
     34 @direntry
     35 * Libextractor: (libextractor).    Metadata extraction library.
     36 @end direntry
     37 
     38 
     39 
     40 @c
     41 @c Titlepage
     42 @c
     43 @titlepage
     44 @title The GNU libextractor Reference Manual
     45 @subtitle Version @value{VERSION}
     46 @subtitle @value{UPDATED}
     47 @author Christian Grothoff (@email{christian@@grothoff.org})
     48 
     49 @page
     50 @vskip 0pt plus 1filll
     51 @insertcopying
     52 @end titlepage
     53 
     54 @summarycontents
     55 @contents
     56 
     57 
     58 @ifnottex
     59 @node Top
     60 @top The GNU libextractor Reference Manual
     61 @insertcopying
     62 @end ifnottex
     63 
     64 @menu
     65 * Introduction::                 What is GNU libextractor.
     66 * Preparation::                  What you should do before using the library.
     67 * Generalities::                 General library functions and data types.
     68 * Extracting meta data::         How to use GNU libextractor to obtain meta data.
     69 * Language bindings::            How to use GNU libextractor from languages other than C.
     70 * Utility functions::            Utility functions of GNU libextractor.
     71 * Existing Plugins::             What plugins are available.
     72 * Writing new Plugins::          How to write new plugins for GNU libextractor.
     73 * Internal utility functions::   Utility functions of GNU libextractor for writing plugins.
     74 * Reporting bugs::               How to report bugs or request new features.
     75 
     76 Appendices
     77 
     78 * GNU Free Documentation License::  Copying this manual.
     79 
     80 Indices
     81 
     82 * Index::                       Index
     83 @c * Function and Data Index::     Index of functions, variables and data types.
     84 @c * Type Index::                  Index of data types.
     85 
     86 @end menu
     87 
     88 
     89 
     90 @c **********************************************************
     91 @c *******************  Introduction  ***********************
     92 @c **********************************************************
     93 @node Introduction
     94 @chapter Introduction
     95 
     96 @cindex error handling
     97 GNU libextractor is GNU's library for extracting meta data from
     98 files.  Meta data includes format information (such as mime type,
     99 image dimensions, color depth, recording frequency), content
    100 descriptions (such as document title or document description) and
    101 copyright information (such as license, author and contributors).
    102 Meta data extraction is an inherently uncertain business --- a parse
    103 error can be a corrupt file, an incompatibility in the file format
    104 version, an entirely different file format or a bug in the parser.  As
    105 a result of this uncertainty, GNU libextractor deliberately
    106 avoids to ever report any errors.  Unexpected file contents simply
    107 result in less or possibly no meta data being extracted.  
    108 
    109 @cindex plugin
    110 GNU libextractor uses plugins to handle various file formats.
    111 Technically a plugin can support multiple file formats; however, most
    112 plugins only support one particular format.  By default,
    113 GNU libextractor will use all plugins that are available and found
    114 in the plugin installation directory.  Applications can
    115 request the use of only specific plugins or the exclusion of
    116 certain plugins.
    117 
    118 GNU libextractor is distributed with the @command{extract} 
    119 command@footnote{Some distributions ship @command{extract} in a
    120 seperate package.} which is a command-line tool for extracting
    121 meta data.  @command{extract} is given a list of filenames and 
    122 prints the resulting meta data to the console.  The @command{extract}
    123 source code also serves as an advanced example for how to use
    124 GNU libextractor.  
    125 
    126 This manual focuses on providing documentation for writing software
    127 with GNU libextractor.  The only relevant parts for end-users
    128 are the chapter on compiling and installing GNU libextractor
    129 (@xref{Preparation}.).  Also, the chapter on existing plugins maybe of
    130 interest (@xref{Existing Plugins}.).  Additional documentation for
    131 end-users can be find in the man page on @command{extract} (using
    132 @verb{|man extract|}).
    133 
    134 @cindex license
    135 GNU libextractor is licensed under the GNU General Public License,
    136 specifically, since version 0.7, GNU libextractor is licensed under GPLv3
    137 @emph{or any later version}.
    138 
    139 @node Preparation
    140 @chapter Preparation
    141 
    142 This chapter first describes the general build instructions that
    143 should apply to all systems.  Specific instructions for known problems
    144 for particular platforms are then described in individual sections
    145 afterwards.
    146 
    147 Compiling GNU libextractor follows the standard GNU autotools build process
    148 using @command{configure} and @command{make}.  For details on the GNU
    149 autotools build process, read the @file{INSTALL} file and query
    150 @verb{|./configure --help|} for additional options.  
    151 
    152 GNU libextractor has various dependencies, most of which are optional. 
    153 Instead of specifying the names of the software packages, we
    154 will give the list in terms of the names of the respective
    155 Debian (wheezy) packages that should be installed.
    156 
    157 You absolutely need:
    158 
    159 @itemize @bullet
    160 @item
    161 libtool
    162 @item
    163 gcc
    164 @item
    165 make
    166 @item
    167 g++ 
    168 @item
    169 libltdl7-dev
    170 @end itemize
    171 
    172 Recommended dependencies are:
    173 @itemize @bullet
    174 @item
    175 zlib1g-dev
    176 @item
    177 libbz2-dev
    178 @item
    179 libgif-dev
    180 @item
    181 libvorbis-dev
    182 @item
    183 libflac-dev
    184 @item
    185 libmpeg2-4-dev
    186 @item
    187 librpm-dev
    188 @item
    189 libgtk2.0-dev or libgtk3.0-dev
    190 @item
    191 libgsf-1-dev
    192 @item
    193 libqt4-dev
    194 @item
    195 libpoppler-dev
    196 @item
    197 libexiv2-dev
    198 @item
    199 libavformat-dev
    200 @item
    201 libswscale-dev
    202 @item
    203 libswresample-dev
    204 @item
    205 libgstreamer1.0-dev
    206 @item
    207 libgstreamer-plugins-base1.0-dev
    208 @end itemize
    209 
    210 For Subversion access and compilation one also needs:
    211 @itemize @bullet
    212 @item
    213 subversion
    214 @item
    215 autoconf
    216 @item
    217 automake
    218 @end itemize
    219 
    220 Please notify us if we missed some dependencies (note that the list is
    221 supposed to only list direct dependencies, not transitive
    222 dependencies).
    223 
    224 Once you have compiled and installed GNU libextractor, you should have a file
    225 @file{extractor.h} installed in your @file{include/} directory.  This
    226 file should be the starting point for your C and C++ development with
    227 GNU libextractor.  The build process also installs the @file{extract} binary and
    228 man pages for @file{extract} and GNU libextractor.  The @file{extract} man page
    229 documents the @file{extract} tool.  The GNU libextractor man page gives a brief
    230 summary of the C API for GNU libextractor.
    231 
    232 @cindex packageing
    233 @cindex directory structure
    234 @cindex plugin
    235 @cindex environment variables
    236 @vindex LIBEXTRACTOR_PREFIX
    237 When you install GNU libextractor, various plugins will be
    238 installed in the @file{lib/libextractor/} directory.  The main library
    239 will be installed as @file{lib/libextractor.so}.  Note that
    240 GNU libextractor will attempt to find the plugins relative to the
    241 path of the main library.  Consequently, a package manager can move
    242 the library and its plugins to a different location later --- as long
    243 as the relative path between the main library and the plugins is
    244 preserved.  As a method of last resort, the user can specify an
    245 environment variable @verb{|LIBEXTRACTOR_PREFIX|}.  If
    246 GNU libextractor cannot locate a plugin, it will look in
    247 @verb{|LIBEXTRACTOR_PREFIX/lib/libextractor/|}.
    248 
    249 
    250 @section Installation on GNU/Linux
    251 
    252 Should work using the standard instructions without problems.
    253 
    254 
    255 @section Installation on FreeBSD
    256 
    257 Should work using the standard instructions without problems.
    258 
    259 
    260 @section Installation on OpenBSD
    261 
    262 OpenBSD 3.8 also doesn't have CODESET in @file{langinfo.h}.  CODESET
    263 is used in GNU libextractor in about three places.  This causes problems
    264 during compilation.
    265 
    266 
    267 @section Installation on NetBSD
    268 
    269 No reports so far.
    270 
    271 
    272 @section Installation using MinGW
    273 
    274 Linking -lstdc++ with the provided libtool fails on Cygwin, this
    275 is a problem with libtool, there is unfortunately no flag to tell
    276 libtool how to do its job on Cygwin and it seems that it cannot be the
    277 default to set the library check to 'pass_all'.  Patching libtool may
    278 help.
    279 
    280 Note: this is a rather dated report and may no longer apply.
    281 
    282 
    283 @section Installation on OS X
    284 
    285 libextractor has two installation methods on Mac OS X: it can be
    286 installed as a Mac OS X framework or with the standard
    287 @command{./configure; make; make install} shell commands. The
    288 framework package is self-contained, but currently omits some of the
    289 extractor plugins that can be compiled in if libextractor is installed
    290 with @command{./configure; make; make install} (provided that the
    291 required dependencies exist.)
    292 
    293 @subsection Installing and uninstalling the framework
    294 
    295 The binary framework is distributed as a disk image (@file{Extractor-x.x.xx.dmg}).
    296 Installation is done by opening the disk image and clicking @file{Extractor.pkg}
    297 inside it. The Mac OS X installer application will then run. The framework
    298 is installed to the root volume's @file{/Library/Frameworks} folder and installing
    299 will require admin privileges.
    300 
    301 The framework can be uninstalled by dragging @*
    302 @file{/Library/Frameworks/Extractor.framework} to the @file{Trash}.
    303 
    304 
    305 @subsection Using the framework
    306 
    307 In the framework, the @command{extract} command line tool can be found at @*
    308 @file{/Library/Frameworks/Extractor.framework/Versions/Current/bin/extract}
    309 
    310 The framework can be used in software projects as a framework or as a dynamic
    311 library. 
    312 
    313 When using the framework as a dynamic library in projects using autotools,
    314 one would most likely want to add  @*
    315 "-I/Library/Frameworks/Extractor.framework/Versions/Current/include"
    316 to CPPFLAGS and @*
    317 "-L/Library/Frameworks/Extractor.framework/Versions/Current/lib"
    318 to LDFLAGS.
    319 
    320 
    321 @subsection Example for using the framework
    322 
    323 @example
    324 @verbatim
    325 // hello.c
    326 #include <Extractor/extractor.h>
    327 
    328 int
    329 main (int argc, char **argv)
    330 {
    331   struct EXTRACTOR_PluginList *el;
    332   el = EXTRACTOR_plugin_load_defaults (EXTRACTOR_OPTION_DEFAULT_POLICY);
    333   // ...
    334   EXTRACTOR_plugin_remove_all (el);
    335   return 0;
    336 }
    337 @end verbatim
    338 @end example
    339 
    340 You can then compile the example using
    341 
    342 @verbatim
    343 $ gcc -o hello hello.c -framework Extractor
    344 @end verbatim
    345 
    346 @subsection Example for using the dynamic library
    347 
    348 @example
    349 @verbatim
    350 // hello.c
    351 #include <extractor.h>
    352 int main()
    353 {
    354   struct EXTRACTOR_PluginList *el;
    355   el = EXTRACTOR_plugin_load_defaults (EXTRACTOR_OPTION_DEFAULT_POLICY);
    356   // ...
    357   EXTRACTOR_plugin_remove_all (el);
    358   return 0;
    359 }
    360 @end verbatim
    361 @end example
    362 
    363 You can then compile the example using
    364 
    365 @verbatim
    366 $ gcc -I/Library/Frameworks/Extractor.framework/Versions/Current/include \
    367   -o hello hello.c \
    368   -L/Library/Frameworks/Extractor.framework/Versions/Current/lib \
    369   -lextractor
    370 @end verbatim
    371 
    372 Notice the difference in the @code{#include} line.
    373 
    374 
    375 
    376 
    377 
    378 
    379 @section Note to package maintainers
    380 
    381 The suggested way to package GNU libextractor is to split it into
    382 roughly the following binary packages:
    383 
    384 @itemize @bullet
    385 @item
    386 libextractor (main library only, only hard dependency for other packages depending on GNU libextractor)
    387 @item
    388 extract (command-line tool and man page extract.1)
    389 @item
    390 libextractor-dev (extractor.h header and man page libextractor.3)
    391 @item
    392 libextractor-doc (this manual)
    393 @item
    394 libextractor-plugins (plugins without external dependencies; recommended but not required by extract and libextractor package)
    395 @item
    396 libextractor-plugin-XXX (plugin with dependency on libXXX, for example for XXX=mpeg this would be @file{libextractor_mpeg.so})
    397 @item
    398 libextractor-plugins-all (meta package that requires all plugins except experimental plugins)
    399 @end itemize
    400 
    401 This would enable minimal installations (i.e. for embedded systems) to
    402 not include any plugins, as well as moderate-size installations (that
    403 do not trigger GTK and X11) for systems that have limited resources.
    404 Right now, the MP4 plugin is experimental and does nothing and should
    405 thus never be included at all; QuickTime, MP4, M4A and 3GPP files are
    406 instead handled by the @file{libextractor_qt.so} plugin, which only
    407 depends on zlib and is part of libextractor-plugins.  The gstreamer plugin is experimental
    408 but largely works with the correct version of gstreamer and can thus
    409 be packaged (especially if the dependency is available on the target
    410 system) but should probably not be part of libextractor-plugins-all.
    411 
    412 
    413 @node Generalities
    414 @chapter Generalities
    415 
    416 @section Introduction to the ``extract'' command
    417 
    418 The @command{extract} command takes a list of file names as arguments,
    419 extracts meta data from each of those files and prints the result to
    420 the console.  By default, @command{extract} will use all available
    421 plugins and print all (non-binary) meta data that is found.
    422 
    423 The set of plugins used by @command{extract} can be controlled using
    424 the ``-l'' and ``-n'' options.  Use ``-n'' to not load all of the
    425 default plugins.  Use ``-l NAME'' to specifically load a certain
    426 plugin.  For example, specify ``-n -l mime'' to only use the MIME
    427 plugin.
    428 
    429 Using the ``-p'' option the output of @command{extract} can be limited
    430 to only certain keyword types.  Similarly, using the ``-x'' option,
    431 certain keyword types can be excluded.  A list of all known keyword
    432 types can be obtained using the ``-L'' option.
    433 
    434 The output format of @command{extract} can be influenced with the
    435 ``-V'' (more verbose, lists filenames), ``-g'' (grep-friendly, all
    436 meta data on a single line per file) and ``-b'' (bibTeX style)
    437 options.
    438 
    439 @section Common usage examples for ``extract''
    440 
    441 @example
    442 $ extract test/test.jpg
    443 comment - (C) 2001 by Christian Grothoff, using gimp 1.2 1
    444 mimetype - image/jpeg
    445 
    446 $ extract -V -x comment test/test.jpg
    447 Keywords for file test/test.jpg:
    448 mimetype - image/jpeg
    449 
    450 $ extract -p comment test/test.jpg
    451 comment - (C) 2001 by Christian Grothoff, using gimp 1.2 1
    452 
    453 $ extract -nV -l png.so -p comment test/test.jpg test/test.png
    454 Keywords for file test/test.jpg:
    455 Keywords for file test/test.png:
    456 comment - Testing keyword extraction
    457 @end example
    458 
    459 
    460 @section Introduction to the libextractor library
    461 
    462 Each public symbol exported by GNU libextractor has the prefix
    463 @verb{|EXTRACTOR_|}.  All-caps names are used for constants.  For the
    464 impatient, the minimal C code for using GNU libextractor (on the
    465 executing binary itself) looks like this:
    466 
    467 @verbatim
    468 #include <extractor.h>
    469 
    470 int 
    471 main (int argc, char ** argv) 
    472 {
    473   struct EXTRACTOR_PluginList *plugins
    474     = EXTRACTOR_plugin_add_defaults (EXTRACTOR_OPTION_DEFAULT_POLICY);
    475   EXTRACTOR_extract (plugins, argv[1],
    476                      NULL, 0, 
    477                      &EXTRACTOR_meta_data_print, stdout);
    478   EXTRACTOR_plugin_remove_all (plugins);
    479   return 0;
    480 }
    481 @end verbatim
    482 
    483 The minimal API illustrated by this example is actually sufficient for
    484 many applications.  The full external C API of GNU libextractor is described
    485 in chapter @xref{Extracting meta data}.  Bindings for other languages
    486 are described in chapter @xref{Language bindings}.  The API for
    487 writing new plugins is described in chapter @xref{Writing new Plugins}.
    488 
    489 Note that it is possible for GNU libextractor to encounter a @code{SIGPIPE}
    490 during its execution.  GNU libextractor --- as it is a library and as such
    491 should not interfere with your main application --- does NOT install a 
    492 signal handler for @code{SIGPIPE}.  You thus need to install a signal
    493 handler (or at least tell your system to ignore @code{SIGPIPE}) if you
    494 want to avoid unexpected problems during calls to GNU libextractor.  
    495 @cindex SIGPIPE
    496 
    497 @node Extracting meta data
    498 @chapter Extracting meta data
    499 
    500 In order to extract meta data with GNU libextractor you first need to
    501 load the respective plugins and then call the extraction API
    502 with the plugins and the data to process.  This section
    503 documents how to load and unload plugins, the various types
    504 and formats in which meta data is returned to the application
    505 and finally the extraction API itself.
    506 
    507 @menu
    508 * Plugin management::   How to load and unload plugins
    509 * Meta types::          About meta types
    510 * Meta formats::        About meta formats
    511 * Extracting::          How to use the extraction API
    512 @end menu
    513 
    514 
    515 @node Plugin management
    516 @section Plugin management
    517 
    518 @cindex reentrant
    519 @cindex concurrency
    520 @cindex threads
    521 @cindex thread-safety
    522 @tindex enum EXTRACTOR_Options
    523 
    524 Using GNU libextractor from a multi-threaded parent process requires some
    525 care.  The problem is that on most platforms GNU libextractor starts
    526 sub-processes for the actual extraction work.  This is useful to
    527 isolate the parent process from potential bugs; however, it can cause
    528 problems if the parent process is multi-threaded.  The issue is that
    529 at the time of the fork, another thread of the application may hold a
    530 lock (i.e. in gettext or libc).  That lock would then never be
    531 released in the child process (as the other thread is not present in
    532 the child process).  As a result, the child process would then
    533 deadlock on trying to acquire the lock and never terminate.  This has
    534 actually been observed with a lock in GNU gettext that is triggered by
    535 the plugin startup code when it interacts with libltdl.
    536 
    537 The problem can be solved by loading the plugins using the
    538 @code{EXTRACTOR_OPTION_IN_PROCESS} option, which will run GNU libextractor
    539 in-process and thus avoid the locking issue.  In this case, all of the
    540 functions for loading and unloading plugins, including
    541 @verb{|EXTRACTOR_plugin_add_defaults|} and
    542 @verb{|EXTRACTOR_plugin_remove_all|}, are thread-safe and reentrant.
    543 However, using the same plugin list from multiple threads at the same
    544 time is not safe.  
    545 
    546 All plugin code is expected required to be reentrant and state-less,
    547 but due to the extensive use of 3rd party libraries this cannot
    548 be guaranteed.
    549 
    550 
    551 @deftp {C Struct} EXTRACTOR_PluginList
    552 @tindex struct EXTRACTOR_PluginList
    553 
    554 A plugin list represents a set of GNU libextractor plugins.  Most of
    555 the GNU libextractor API is concerned with either constructing a
    556 plugin list or using it to extract meta data.  The internal representation
    557 of the plugin list is of no concern to users or plugin developers.
    558 @end deftp
    559 
    560 
    561 @deftypefun void EXTRACTOR_plugin_remove_all (struct EXTRACTOR_PluginList *plugins)
    562 @findex EXTRACTOR_plugin_remove_all
    563 
    564 Unload all of the plugins in the given list.
    565 @end deftypefun
    566 
    567 @deftypefun {struct EXTRACTOR_PluginList *} EXTRACTOR_plugin_remove (struct EXTRACTOR_PluginList *plugins, const char*name)
    568 @findex EXTRACTOR_plugin_remove
    569 
    570 Unloads a particular plugin.  The given name should be the short name of the plugin, for example ``mime'' for the mime-type extractor or ``mpeg'' for the MPEG extractor.
    571 @end deftypefun
    572 
    573 @deftypefun {struct EXTRACTOR_PluginList *} EXTRACTOR_plugin_add (struct EXTRACTOR_PluginList *plugins, const char* name,const char* options, enum EXTRACTOR_Options flags)
    574 @findex EXTRACTOR_plugin_add
    575 
    576 Loads a particular plugin.  The plugin is added to the existing list, which can be @code{NULL}.  The second argument specifies the name of the plugin (i.e. ``ogg'').  The third argument can be @code{NULL} and specifies plugin-specific options.  Finally, the last argument specifies if the plugin should be executed out-of-process (@code{EXTRACTOR_OPTION_DEFAULT_POLICY}) or not.
    577 @end deftypefun
    578 
    579 @deftypefun {struct EXTRACTOR_PluginList *} EXTRACTOR_plugin_add_config (struct EXTRACTOR_PluginList *plugins, const char* config, enum EXTRACTOR_Options flags)
    580 @findex EXTRACTOR_plugin_add_config
    581 
    582 Loads and unloads plugins based on a configuration string, modifying the existing list, which can be @code{NULL}.  The string has the format ``[-]NAME(OPTIONS)@{:[-]NAME(OPTIONS)@}*''.  Prefixing the plugin name with a ``-'' means that the plugin should be unloaded.
    583 @end deftypefun
    584 
    585 @deftypefun {struct EXTRACTOR_PluginList *} EXTRACTOR_plugin_add_defaults (enum EXTRACTOR_Options flags)
    586 @findex EXTRACTOR_plugin_add_defaults
    587 
    588 Loads all of the plugins in the plugin directory.  This function is what most GNU libextractor applications should use to setup the plugins.
    589 @end deftypefun
    590 
    591 
    592 
    593 @node Meta types
    594 @section Meta types
    595 
    596 
    597 @tindex enum EXTRACTOR_MetaType
    598 @findex EXTRACTOR_metatype_get_max
    599 
    600 @verb{|enum EXTRACTOR_MetaType|} is a C enum which defines a list of over 100 different types of meta data.  The total number can differ between different GNU libextractor releases; the maximum value for the current release can be obtained using the @verb{|EXTRACTOR_metatype_get_max|} function.  All values in this enumeration are of the form @verb{|EXTRACTOR_METATYPE_XXX|}.
    601 
    602 @deftypefun {const char *} EXTRACTOR_metatype_to_string (enum EXTRACTOR_MetaType type)
    603 @findex EXTRACTOR_metatype_to_string
    604 @cindex gettext
    605 @cindex internationalization
    606 
    607 The function @verb{|EXTRACTOR_metatype_to_string|} can be used to obtain a short English string @samp{s} describing the meta data type.  The string can be translated into other languages using GNU gettext with the domain set to GNU libextractor (@verb{|dgettext("libextractor", s)|}).  
    608 @end deftypefun
    609 
    610 @deftypefun {const char *} EXTRACTOR_metatype_to_description (enum EXTRACTOR_MetaType type)
    611 @findex EXTRACTOR_metatype_to_description
    612 @cindex gettext
    613 @cindex internationalization
    614 
    615 The function @verb{|EXTRACTOR_metatype_to_description|} can be used to obtain a longer English string @samp{s} describing the meta data type.  The description may be empty if the short description returned by @code{EXTRACTOR_metatype_to_string} is already comprehensive.  The string can be translated into other languages using GNU gettext with the domain set to GNU libextractor (@verb{|dgettext("libextractor", s)|}).  
    616 @end deftypefun
    617 
    618 
    619 
    620 @node Meta formats
    621 @section Meta formats
    622 
    623 @tindex enum EXTRACTOR_MetaFormat
    624 
    625 @verb{|enum EXTRACTOR_MetaFormat|} is a C enum which defines on a high level how the extracted meta data is represented.  Currently, the library uses three formats: UTF-8 strings, C strings and binary data.  A fourth value, @code{EXTRACTOR_METAFORMAT_UNKNOWN} is defined but not used.  UTF-8 strings are 0-terminated strings that have been converted to UTF-8.  The format code is @code{EXTRACTOR_METAFORMAT_UTF8}. Ideally, most text meta data will be of this format.  Some file formats fail to specify the encoding used for the text.  In this case, the text cannot be converted to UTF-8.  However, the meta data is still known to be 0-terminated and presumably human-readable.  In this case, the format code used is @code{EXTRACTOR_METAFORMAT_C_STRING}; however, this should not be understood to mean that the encoding is the same as that used by the C compiler.  Finally, for binary data (mostly images), the format @code{EXTRACTOR_METAFORMAT_BINARY} is used.
    626 
    627 Naturally this is not a precise description of the meta format. Plugins can provide a more precise description (if known) by providing the respective mime type of the meta data.  For example, binary image meta data could be also tagged as ``image/png'' and normal text would typically be tagged as ``text/plain''.  
    628 
    629 
    630 
    631 @node Extracting
    632 @section Extracting
    633 
    634 @deftypefn {Function Pointer} int (*EXTRACTOR_MetaDataProcessor)(void *cls, const char *plugin_name, enum EXTRACTOR_MetaType type, enum EXTRACTOR_MetaFormat format, const char *data_mime_type, const char *data, size_t data_len)
    635 @tindex EXTRACTOR_MetaDataProcessor
    636 
    637 Type of a function that libextractor calls for each meta data item found.
    638 
    639 @table @var
    640 
    641 @item cls 
    642 closure (user-defined)
    643 
    644 @item plugin_name 
    645 name of the plugin that produced this value; special values can be used (i.e. '<zlib>' for zlib being used in the main libextractor library and yielding meta data);
    646 
    647 @item type 
    648 libextractor-type describing the meta data;
    649 
    650 @item format basic 
    651 format information about data
    652 
    653 @item data_mime_type 
    654 mime-type of data (not of the original file); can be @code{NULL} (if mime-type is not known);
    655 
    656 @item data 
    657 actual meta-data found
    658 
    659 @item data_len 
    660 number of bytes in data
    661 
    662 @end table
    663 
    664 Return 0 to continue extracting, 1 to abort.
    665 @end deftypefn
    666 
    667 
    668 
    669 @deftypefun void EXTRACTOR_extract (struct EXTRACTOR_PluginList *plugins, const char *filename, const void *data, size_t size, EXTRACTOR_MetaDataProcessor proc, void *proc_cls)
    670 @findex EXTRACTOR_extract
    671 @cindex reentrant
    672 @cindex concurrency
    673 @cindex threads
    674 @cindex thread-safety
    675 
    676 This is the main function for extracting keywords with GNU libextractor.  The first argument is a plugin list which specifies the set of plugins that should be used for extracting meta data.  The @samp{filename} argument is optional and can be used to specify the name of a file to process.  If @samp{filename} is @code{NULL}, then the @samp{data} argument must point to the in-memory data to extract meta data from.  If @samp{filename} is non-@code{NULL}, @samp{data} can be @code{NULL}.  If @samp{data} is non-null, then @samp{size} is the size of @samp{data} in bytes.  Otherwise @samp{size} should be zero.  For each meta data item found, GNU libextractor will call the @samp{proc} function, passing @samp{proc_cls} as the first argument to @samp{proc}.  The other arguments to @samp{proc} depend on the specific meta data found.  
    677 
    678 @cindex SIGBUS
    679 @cindex bus error
    680 Meta data extraction should never really fail --- at worst, GNU libextractor should not call @samp{proc} with any meta data. By design, GNU libextractor should never crash or leak memory, even given corrupt files as input.  Note however, that running GNU libextractor on a corrupt file system (or incorrectly @verb{|mmap|}ed files) can result in the operating system sending a SIGBUS (bus error) to the process.  As GNU libextractor typically runs plugins out-of-process, it first maps the file into memory and then attempts to decompress it.  During decompression it is possible to encounter a SIGBUS.   GNU libextractor will @emph{not} attempt to catch this signal and your application is likely to crash.  Note again that this should only happen if the file @emph{system} is corrupt (not if individual files are corrupt).  If this is not acceptable, you might want to consider running GNU libextractor itself also out-of-process (as done, for example, by @url{http://grothoff.org/christian/doodle/,doodle}).
    681 
    682 @end deftypefun
    683 
    684 
    685 @node Language bindings
    686 @chapter Language bindings
    687 @cindex Java
    688 @cindex Mono
    689 @cindex Perl
    690 @cindex Python
    691 @cindex PHP
    692 @cindex Ruby
    693 
    694 GNU libextractor works immediately with C and C++ code. Bindings for Java, Mono, Ruby, Perl, PHP and Python are available for download from the main GNU libextractor website.  Documentation for these bindings (if available) is part of the downloads for the respective binding.  In all cases, a full installation of the C library is required before the binding can be installed.
    695 
    696 @section Java
    697 
    698 Compiling the GNU libextractor Java binding follows the usual process of
    699 running @command{configure} and @command{make}.  The result will be a
    700 shared C library @file{libextractor_java.so} with the native code and
    701 a JAR file (installed to @file{$PREFIX/share/java/libextractor.java}).
    702 
    703 A minimal example for using GNU libextractor's Java binding would look
    704 like this:
    705 @verbatim
    706 import org.gnu.libextractor.*;
    707 import java.util.ArrayList;
    708 
    709 public static void main(String[] args) {
    710   Extractor ex = Extractor.getDefault();
    711   for (int i=0;i<args.length;i++) {
    712     ArrayList keywords = ex.extract(args[i]);
    713     System.out.println("Keywords for " + args[i] + ":");
    714     for (int j=0;j<keywords.size();j++)
    715       System.out.println(keywords.get(j));
    716   }
    717 }
    718 @end verbatim
    719 
    720 The GNU libextractor library and the @file{libextractor_java.so} JNI binding
    721 have to be in the library search path for this to work.  Furthermore, the
    722 @file{libextractor.jar} file should be on the classpath.  
    723 
    724 Note that the API does not use Java 5 style generics in order to work
    725 with older versions of Java.
    726 
    727 @section Mono
    728 
    729 his binding is undocumented at this point.
    730 
    731 @section Perl
    732 
    733 This binding is undocumented at this point.
    734 
    735 @section Python
    736 
    737 This binding is undocumented at this point.
    738 
    739 @section PHP
    740 
    741 This binding is undocumented at this point.
    742 
    743 @section Ruby
    744 
    745 This binding is undocumented at this point.
    746 
    747 
    748 
    749 @node Utility functions
    750 @chapter Utility functions
    751 
    752 @cindex reentrant
    753 @cindex concurrency
    754 @cindex threads
    755 @cindex thread-safety
    756 This chapter describes various utility functions for GNU libextractor usage. All of the functions are reentrant.
    757 
    758 @menu
    759 * Utility Constants::
    760 * Meta data printing::
    761 @end menu
    762 
    763 @node Utility Constants
    764 @section Utility Constants
    765 
    766 @findex EXTRACTOR_VERSION
    767 The constant @verb{|EXTRACTOR_VERSION|} is a hexadecimal
    768 representation of the version number of the installed libextractor
    769 header.  The hexadecimal format is 0xAABBCCDD where AA is the major
    770 version (so far always 0), BB is the minor version, CC is the revision
    771 and DD the patch number.  For example, for version 0.5.18, we would
    772 have AA=0, BB=5, CC=18 and DD=0.  Minor releases such as 0.5.18a or
    773 significant changes in unreleased versions would be marked with DD=1
    774 or higher.
    775 
    776 
    777 @node Meta data printing
    778 @section Meta data printing
    779 
    780 
    781 @findex EXTRACTOR_meta_data_print
    782 The @verb{|EXTRACTOR_meta_data_print|} is a simple function which prints the meta data found with libextractor to a file.  The function is mostly useful for debugging and as an example for how to manipulate the keyword list and can be passed as the @samp{proc} argument to @code{EXTRACTOR_extract}.  The file to print to should be passed as @samp{proc_cls} (which must be of type @code{FILE *}), for example @code{stdout}.
    783 
    784 
    785 
    786 @node Existing Plugins
    787 @chapter Existing Plugins
    788 
    789 @itemize @bullet
    790 @item
    791 APK (Android packages and Java archives, using zlib)
    792 @item
    793 ARCHIVE (using libarchive)
    794 @item
    795 DISKIMAGE (QCOW2, VMDK, VHD and VHDX virtual disk images)
    796 @item
    797 DVI
    798 @item
    799 EBOOK (EPUB and MOBI/AZW)
    800 @item
    801 EXIV2 (using libexiv2, 0.23 or later preferred)
    802 @item 
    803 FLAC (using libFLAC)
    804 @item
    805 GEOTIFF (georeferencing tags in TIFF images)
    806 @item
    807 GPX (GPS tracks)
    808 @item
    809 GIF (using libgif)
    810 @item
    811 GSTREAMER (using libgstreamer v1.0 or later)
    812 @item
    813 HEIF (HEIC, HEIF and AVIF images)
    814 @item
    815 ID3 (MPEG audio with ID3v1/ID3v2 tags, Xing and LAME headers)
    816 @item
    817 ISO9660 (CD/DVD images)
    818 @item
    819 HTML (using libtidy)
    820 @item
    821 IT 
    822 @item
    823 JPEG (using libjpeg v8 or later)
    824 @item
    825 KML (Keyhole Markup Language)
    826 @item
    827 LNK (Windows shell links)
    828 @item
    829 MAN
    830 @item
    831 MIDI (using libsmf)
    832 @item
    833 MIME (using libmagic)
    834 @item
    835 MBOX (Unix mailboxes and RFC 5322 messages)
    836 @item
    837 MPEG (using libmpeg2)
    838 @item
    839 MSOFFICE (OOXML: @file{.docx}, @file{.xlsx}, @file{.pptx}; and
    840 Excel 2-4 BIFF streams)
    841 @item
    842 NSF
    843 @item
    844 PECOFF (Windows executables, DLLs and drivers)
    845 @item
    846 PLIST (Apple property lists, binary and XML)
    847 @item
    848 NSFE
    849 @item
    850 ODF
    851 @item
    852 OLE2 (with libgsf)
    853 @item
    854 OGG (with libogg)
    855 @item
    856 PNG
    857 @item
    858 PS
    859 @item
    860 RIFF
    861 @item
    862 RPM (using librpm)
    863 @item
    864 RTF (@file{.rtf}, including the revision table, comment authors and
    865 custom document properties)
    866 @item
    867 S3M
    868 @item
    869 SQLITE (SQLite 3 databases, write-ahead logs and journals)
    870 @item
    871 SID
    872 @item
    873 PreviewGst (audio preview, using GStreamer)
    874 @item
    875 PreviewOpus (audio preview, using FFmpeg 5.1 or later)
    876 @item
    877 ThumbnailFFMPEG (video thumbnail, using libavformat, libavcodec and
    878 libswscale of FFmpeg 5.1 or later)
    879 @item
    880 ThumbnailGst (video thumbnail, using GStreamer 1.8 or later)
    881 @item
    882 ThumbnailGtk (still image thumbnail, using gdk-pixbuf)
    883 @item
    884 TIFF (with libtiff, tested with v4)
    885 @item
    886 TAR (tar archives)
    887 @item
    888 WAV
    889 @item
    890 WEBP
    891 @item
    892 XM
    893 @item
    894 ZIP
    895 @end itemize
    896 
    897 @file{gzip} and @file{bzip2} compressed versions of these formats are 
    898 also supported (as well as meta data embedded by @file{gzip} itself)
    899 if zlib or libbz2 are available.
    900 
    901 @section Video thumbnails and audio previews
    902 
    903 Four of the plugins in that list do not report meta data that was found
    904 in the file, but meta data computed @emph{from} the file: a small image
    905 taken from a video (@code{EXTRACTOR_METATYPE_THUMBNAIL}) and a short
    906 audio excerpt re-encoded as Opus in an Ogg container
    907 (@code{EXTRACTOR_METATYPE_AUDIO_PREVIEW}).  Both are reported as
    908 @code{EXTRACTOR_METAFORMAT_BINARY}.
    909 
    910 Each feature comes in two implementations, one on top of GStreamer and
    911 one on top of FFmpeg, and which of them are built depends on what
    912 @command{configure} found:
    913 
    914 @table @code
    915 @item thumbnailgst
    916 @itemx thumbnailffmpeg
    917 Decode the video and emit a frame from about 30 seconds in, scaled to
    918 fit a 128x128 box, as JPEG.  Frames that are a perfectly flat surface
    919 (a black leader, a fade-in) are skipped.
    920 @item previewgst
    921 @itemx previewopus
    922 Decode about 15 seconds of audio from about 30 seconds in, fade it in
    923 and out, and emit it as a mono 24 kbit/s Ogg Opus stream with the MIME
    924 type @code{audio/ogg}.
    925 @end table
    926 
    927 In both cases the position is limited to a third of the duration, so a
    928 short file is sampled proportionally earlier.
    929 
    930 If both backends are installed, both plugins run and you get two
    931 thumbnails (or two previews) for the same file.  Which one you want is
    932 an application decision; disable the other one in the configuration
    933 string, as in
    934 
    935 @example
    936 pl = EXTRACTOR_plugin_add_defaults (EXTRACTOR_OPTION_DEFAULT_POLICY);
    937 pl = EXTRACTOR_plugin_add_config (pl,
    938                                   "-thumbnailffmpeg:-previewopus",
    939                                   EXTRACTOR_OPTION_DEFAULT_POLICY);
    940 @end example
    941 
    942 All four accept a comma separated list of options in the plugin
    943 configuration string:
    944 
    945 @table @code
    946 @item size=@var{pixels}
    947 Edge length of the box the thumbnail must fit into (default: 128).
    948 @item format=jpeg|png
    949 Image format of the thumbnail (default: @code{jpeg}).
    950 @item offset=@var{seconds}
    951 How far into the media to sample (default: 30).
    952 @item length=@var{seconds}
    953 Length of the audio preview (default: 15).
    954 @item bitrate=@var{bits-per-second}
    955 Opus bitrate of the audio preview (default: 24000).
    956 @item deadline=@var{milliseconds}
    957 Wall clock budget for the whole extraction; when it runs out, the
    958 plugin reports nothing rather than keeping the application waiting
    959 (default: 10000).
    960 @end table
    961 
    962 For example, @code{"thumbnailgst(size=256,format=png)"} asks for a
    963 256x256 PNG.
    964 
    965 Two caveats apply to the GStreamer variants.  Their decoders and
    966 encoders are themselves GStreamer plugins, resolved at run time, so a
    967 system that lacks the @code{jpeg} or @code{opus} GStreamer plugin
    968 builds them happily and then gets no results; and because GStreamer
    969 does not survive a @code{fork()} of a process that has already
    970 initialized it, an application that runs these plugins
    971 @code{EXTRACTOR_OPTION_IN_PROCESS} should not also use out-of-process
    972 plugins in the same process afterwards.
    973 
    974 @section Forensic and bulk-scan plugins
    975 
    976 Sixteen of the plugins in that list were added for a different job from
    977 the rest: identifying, and saying something useful about, every file on
    978 a large volume in a single fast pass.  That purpose shapes how they
    979 behave, in ways worth knowing about before you use them.
    980 
    981 They answer questions about @emph{provenance} rather than about
    982 content --- which machine, user, tool, device or account a file came
    983 from.  A Windows executable reports the compiler that built it, the
    984 path its debug symbols were written to (which routinely discloses the
    985 build machine's user name) and the exploit mitigations it opts into; a
    986 shell link reports the volume serial number, the label and the NetBIOS
    987 name of the machine the target lived on, and often that machine's MAC
    988 address; a tar archive reports the user and group names of the system
    989 it was packed on; a mailbox reports the relay chain a message
    990 travelled and the addresses it passed through.
    991 
    992 To keep a bulk scan fast they read headers, not files.  Each rejects a
    993 file that is not its format within the first few bytes --- which is the
    994 overwhelmingly common case --- and none of them decodes payload data.
    995 The @code{sqlite} plugin in particular reads only the 100-byte header
    996 and never opens the database: opening an untrusted database would be
    997 both slow and unsafe.  For the same reason none of these plugins links
    998 a third-party parsing library, so all but @code{apk} and @code{ebook},
    999 which must inflate members of a zip container, are built
   1000 unconditionally.
   1001 
   1002 Because they characterise rather than enumerate, plugins that meet
   1003 repeating structures --- archive members, mail messages, requested
   1004 permissions, map placemarks --- report a bounded number of them
   1005 together with an @code{EXTRACTOR_METATYPE_ENTRY_COUNT} giving the true
   1006 total.  Do not read the absence of a further item as the absence of
   1007 further items.
   1008 
   1009 Several of the meta types these plugins report were introduced with
   1010 them, and an application that switches on
   1011 @code{enum EXTRACTOR_MetaType} will not know them.  Values are only
   1012 ever appended, so existing values keep their meaning; use
   1013 @code{EXTRACTOR_metatype_get_max()} rather than a hard-coded bound, and
   1014 @code{EXTRACTOR_metatype_to_string()} to render a type you do not
   1015 recognise.
   1016 
   1017 Two habits of these formats are worth remembering when reading their
   1018 output.  Timestamps are reported as ISO 8601 in UTC, but several
   1019 formats store local time with no zone at all --- ISO 9660 and the
   1020 ISO base media format both do --- so a value may be displaced by the
   1021 writer's offset from UTC.  And a field being present does not make it
   1022 true: every value here was written by whatever produced the file, an
   1023 executable's build timestamp is trivially forged, and a disagreement
   1024 between two fields is frequently more informative than either.
   1025 
   1026 @node Writing new Plugins
   1027 @chapter Writing new Plugins
   1028 
   1029 Writing a new plugin for libextractor usually requires writing of or
   1030 interfacing with an actual parser for a specific format.  How this is
   1031 can be accomplished depends on the format and cannot be specified in
   1032 general.  However, care should be taken for the code to be reentrant
   1033 and highly fault-tolerant, especially with respect to malformed
   1034 inputs.
   1035 
   1036 Plugins should start by verifying that the header of the data matches
   1037 the specific format and immediately return if that is not the case.
   1038 Even if the header matches the expected file format, plugins must not
   1039 assume that the remainder of the file is well formed.
   1040 
   1041 The plugin library must be called libextractor_XXX.so, where XXX 
   1042 denotes the file format of the plugin. The library must export a 
   1043 method @verb{|libextractor_XXX_extract_method|}, with the following 
   1044 signature:
   1045 @verbatim
   1046 void
   1047 EXTRACTOR_XXX_extract_method (struct EXTRACTOR_ExtractContext *ec);
   1048 @end verbatim
   1049 
   1050 @samp{ec} contains various information the plugin may need for its
   1051 execution.  Most importantly, it contains functions for reading
   1052 (``read'') and seeking (``seek'') the input data and for returning
   1053 extracted data (``proc'').  The ``config'' member can contain
   1054 additional configuration options.  ``proc'' should be called on
   1055 each meta data item found.  If ``proc'' returns non-zero,
   1056 processing should be aborted (if possible).
   1057 
   1058 In order to test new plugins, the @file{extract} command can be run
   1059 with the options ``-ni'' and ``-l XXX'' .  This will run the plugin
   1060 in-process (making it easier to debug) and without any of the other
   1061 plugins.
   1062 
   1063 
   1064 @section Example for a minimal extract method
   1065 
   1066 The following example shows how a plugin can return the mime type of
   1067 a file.
   1068 @example
   1069 @verbatim
   1070 void
   1071 EXTRACTOR_mymime_extract (struct EXTRACTOR_ExtractContext *ec)
   1072 {
   1073   void *data;
   1074   ssize_t data_size,
   1075 
   1076   if (-1 == (data_size = ec->read (ec->cls, &data, 4)))
   1077     return; /* read error */
   1078   if (data_size < 4)
   1079     return; /* file too small */
   1080   if (0 != memcmp (data, "\177ELF", 4))
   1081     return; /* not ELF */
   1082   if (0 != ec->proc (ec->cls, 
   1083                      "mymime",
   1084                      EXTRACTOR_METATYPE_MIMETYPE,
   1085                      EXTRACTOR_METAFORMAT_UTF8,
   1086                      "text/plain",
   1087                      "application/x-executable",
   1088                      1 + strlen("application/x-executable")))
   1089     return;
   1090   /* more calls to 'proc' here as needed */
   1091 }
   1092 @end verbatim
   1093 @end example
   1094 
   1095 
   1096 @node Internal utility functions
   1097 @chapter Internal utility functions
   1098 
   1099 Some plugins link against the @code{libextractor_common} library which
   1100 provides common abstractions needed by many plugins.  This section
   1101 documents this internal API for plugin developers.  Note that the headers
   1102 for this library are (intentionally) not installed: we do not consider
   1103 this API stable and it should hence only be used by plugins that are 
   1104 build and shipped with GNU libextractor.  Third-party plugins should
   1105 not use it.
   1106 
   1107 @file{convert_numeric.h} defines various conversion functions for
   1108 numbers (in particular, byte-order conversion for floating point
   1109 numbers).  
   1110 
   1111 @file{unzip.h} defines an API for accessing compressed files.
   1112 
   1113 @file{pack.h} provides an interpreter for unpacking structs of integer
   1114 numbers from streams and converting from big or little endian to host
   1115 byte order at the same time.
   1116 
   1117 @file{convert.h} provides a function for character set conversion described
   1118 below.
   1119 
   1120 @deftypefun {char *} EXTRACTOR_common_convert_to_utf8 (const char *input, size_t len, const char *charset)
   1121 @cindex UTF-8
   1122 @cindex character set
   1123 @findex EXTRACTOR_common_convert_to_utf8
   1124 Various GNU libextractor plugins make use of the internal
   1125 @file{convert.h} header which defines a function
   1126 
   1127 @verb{|EXTRACTOR_common_convert_to_utf8|} which can be used to easily convert text from
   1128 any character set to UTF-8.  This conversion is important since the
   1129 linked list of keywords that is returned by GNU libextractor is
   1130 expected to contain only UTF-8 strings.  Naturally, proper conversion
   1131 may not always be possible since some file formats fail to specify the
   1132 character set.  In that case, it is often better to not convert at
   1133 all.
   1134 
   1135 The arguments to @verb{|EXTRACTOR_common_convert_to_utf8|} are the input string (which
   1136 does @emph{not} have to be zero-terminated), the length of the input
   1137 string, and the character set (which @emph{must} be zero-terminated).
   1138 Which character sets are supported depends on the platform, a list can
   1139 generally be obtained using the @command{iconv -l} command.  The
   1140 return value from @verb{|EXTRACTOR_common_convert_to_utf8|} is a zero-terminated string
   1141 in UTF-8 format.  The responsibility to free the string is with the
   1142 caller, so storing the string in the keyword list is acceptable.
   1143 @end deftypefun
   1144 
   1145 
   1146 
   1147 
   1148 
   1149 @node Reporting bugs
   1150 @chapter Reporting bugs
   1151 
   1152 @cindex bug
   1153 GNU libextractor uses the @url{https://gnunet.org/bugs/,Mantis bugtracking
   1154 system}.  If possible, please report bugs there.  You can also e-mail
   1155 the GNU libextractor mailinglist at @url{libextractor@@gnu.org}.
   1156 
   1157 
   1158 
   1159 @c **********************************************************
   1160 @c *******************  Appendices  *************************
   1161 @c **********************************************************
   1162 
   1163 @node GNU Free Documentation License
   1164 @appendix GNU Free Documentation License
   1165 
   1166 @include fdl-1.3.texi
   1167 
   1168 
   1169 @node Index
   1170 @unnumbered Index
   1171 
   1172 @printindex cp
   1173 
   1174 @c @node Function and Data Index
   1175 @c @unnumbered Function and Data Index
   1176 @c @printindex fn
   1177 
   1178 @c @node Type Index
   1179 @c @unnumbered Type Index
   1180 @c @printindex tp
   1181 
   1182 @bye