libextractor.texi (46742B)
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 The argument of ``-l'' is a colon-separated list of plugin names, each 430 optionally preceded by a minus sign to remove the plugin instead of 431 adding it and optionally followed by a plugin configuration in 432 parentheses, as in ``-l "-pdf:pdf(fulltext)"''. Only the last ``-l'' 433 option on the command line is used, so several plugins have to be 434 named in a single list. The options understood by the individual 435 plugins are described in the chapter @emph{Existing Plugins}, under 436 @emph{Video thumbnails and audio previews} and @emph{Full text}. 437 438 Using the ``-p'' option the output of @command{extract} can be limited 439 to only certain keyword types. Similarly, using the ``-x'' option, 440 certain keyword types can be excluded. A list of all known keyword 441 types can be obtained using the ``-L'' option. 442 443 The output format of @command{extract} can be influenced with the 444 ``-V'' (more verbose, lists filenames), ``-g'' (grep-friendly, all 445 meta data on a single line per file) and ``-b'' (bibTeX style) 446 options. 447 448 @section Common usage examples for ``extract'' 449 450 The sample files used here are those of the test suite, in 451 @file{src/plugins/testdata/}. Note that the argument of @code{-l} is 452 the short name of the plugin (@code{jpeg}, not @file{jpeg.so} or 453 @file{libextractor_jpeg.so}). 454 455 @example 456 $ extract -n -l jpeg jpeg_image.jpg 457 Keywords for file jpeg_image.jpg: 458 mimetype - image/jpeg 459 image dimensions - 3x3 460 comment - (C) 2001 by Christian Grothoff, using gimp 1.2 1 461 462 $ extract -n -l jpeg -x comment jpeg_image.jpg 463 Keywords for file jpeg_image.jpg: 464 mimetype - image/jpeg 465 image dimensions - 3x3 466 467 $ extract -n -l jpeg -p comment jpeg_image.jpg 468 Keywords for file jpeg_image.jpg: 469 comment - (C) 2001 by Christian Grothoff, using gimp 1.2 1 470 471 $ extract -n -l gif -p comment jpeg_image.jpg gif_image.gif 472 Keywords for file jpeg_image.jpg: 473 Keywords for file gif_image.gif: 474 comment - Testing keyword extraction 475 @end example 476 477 478 @section Introduction to the libextractor library 479 480 Each public symbol exported by GNU libextractor has the prefix 481 @verb{|EXTRACTOR_|}. All-caps names are used for constants. For the 482 impatient, the minimal C code for using GNU libextractor (on the 483 executing binary itself) looks like this: 484 485 @verbatim 486 #include <extractor.h> 487 488 int 489 main (int argc, char ** argv) 490 { 491 struct EXTRACTOR_PluginList *plugins 492 = EXTRACTOR_plugin_add_defaults (EXTRACTOR_OPTION_DEFAULT_POLICY); 493 EXTRACTOR_extract (plugins, argv[1], 494 NULL, 0, 495 &EXTRACTOR_meta_data_print, stdout); 496 EXTRACTOR_plugin_remove_all (plugins); 497 return 0; 498 } 499 @end verbatim 500 501 The minimal API illustrated by this example is actually sufficient for 502 many applications. The full external C API of GNU libextractor is described 503 in chapter @xref{Extracting meta data}. Bindings for other languages 504 are described in chapter @xref{Language bindings}. The API for 505 writing new plugins is described in chapter @xref{Writing new Plugins}. 506 507 Note that it is possible for GNU libextractor to encounter a @code{SIGPIPE} 508 during its execution. GNU libextractor --- as it is a library and as such 509 should not interfere with your main application --- does NOT install a 510 signal handler for @code{SIGPIPE}. You thus need to install a signal 511 handler (or at least tell your system to ignore @code{SIGPIPE}) if you 512 want to avoid unexpected problems during calls to GNU libextractor. 513 @cindex SIGPIPE 514 515 @node Extracting meta data 516 @chapter Extracting meta data 517 518 In order to extract meta data with GNU libextractor you first need to 519 load the respective plugins and then call the extraction API 520 with the plugins and the data to process. This section 521 documents how to load and unload plugins, the various types 522 and formats in which meta data is returned to the application 523 and finally the extraction API itself. 524 525 @menu 526 * Plugin management:: How to load and unload plugins 527 * Meta types:: About meta types 528 * Meta formats:: About meta formats 529 * Extracting:: How to use the extraction API 530 @end menu 531 532 533 @node Plugin management 534 @section Plugin management 535 536 @cindex reentrant 537 @cindex concurrency 538 @cindex threads 539 @cindex thread-safety 540 @tindex enum EXTRACTOR_Options 541 542 Using GNU libextractor from a multi-threaded parent process requires some 543 care. The problem is that on most platforms GNU libextractor starts 544 sub-processes for the actual extraction work. This is useful to 545 isolate the parent process from potential bugs; however, it can cause 546 problems if the parent process is multi-threaded. The issue is that 547 at the time of the fork, another thread of the application may hold a 548 lock (i.e. in gettext or libc). That lock would then never be 549 released in the child process (as the other thread is not present in 550 the child process). As a result, the child process would then 551 deadlock on trying to acquire the lock and never terminate. This has 552 actually been observed with a lock in GNU gettext that is triggered by 553 the plugin startup code when it interacts with libltdl. 554 555 The problem can be solved by loading the plugins using the 556 @code{EXTRACTOR_OPTION_IN_PROCESS} option, which will run GNU libextractor 557 in-process and thus avoid the locking issue. In this case, all of the 558 functions for loading and unloading plugins, including 559 @verb{|EXTRACTOR_plugin_add_defaults|} and 560 @verb{|EXTRACTOR_plugin_remove_all|}, are thread-safe and reentrant. 561 However, using the same plugin list from multiple threads at the same 562 time is not safe. 563 564 All plugin code is expected required to be reentrant and state-less, 565 but due to the extensive use of 3rd party libraries this cannot 566 be guaranteed. 567 568 569 @deftp {C Struct} EXTRACTOR_PluginList 570 @tindex struct EXTRACTOR_PluginList 571 572 A plugin list represents a set of GNU libextractor plugins. Most of 573 the GNU libextractor API is concerned with either constructing a 574 plugin list or using it to extract meta data. The internal representation 575 of the plugin list is of no concern to users or plugin developers. 576 @end deftp 577 578 579 @deftypefun void EXTRACTOR_plugin_remove_all (struct EXTRACTOR_PluginList *plugins) 580 @findex EXTRACTOR_plugin_remove_all 581 582 Unload all of the plugins in the given list. 583 @end deftypefun 584 585 @deftypefun {struct EXTRACTOR_PluginList *} EXTRACTOR_plugin_remove (struct EXTRACTOR_PluginList *plugins, const char*name) 586 @findex EXTRACTOR_plugin_remove 587 588 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. 589 @end deftypefun 590 591 @deftypefun {struct EXTRACTOR_PluginList *} EXTRACTOR_plugin_add (struct EXTRACTOR_PluginList *plugins, const char* name,const char* options, enum EXTRACTOR_Options flags) 592 @findex EXTRACTOR_plugin_add 593 594 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. 595 @end deftypefun 596 597 @deftypefun {struct EXTRACTOR_PluginList *} EXTRACTOR_plugin_add_config (struct EXTRACTOR_PluginList *plugins, const char* config, enum EXTRACTOR_Options flags) 598 @findex EXTRACTOR_plugin_add_config 599 600 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. 601 @end deftypefun 602 603 @deftypefun {struct EXTRACTOR_PluginList *} EXTRACTOR_plugin_add_defaults (enum EXTRACTOR_Options flags) 604 @findex EXTRACTOR_plugin_add_defaults 605 606 Loads all of the plugins in the plugin directory. This function is what most GNU libextractor applications should use to setup the plugins. 607 @end deftypefun 608 609 610 611 @node Meta types 612 @section Meta types 613 614 615 @tindex enum EXTRACTOR_MetaType 616 @findex EXTRACTOR_metatype_get_max 617 618 @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|}. 619 620 @deftypefun {const char *} EXTRACTOR_metatype_to_string (enum EXTRACTOR_MetaType type) 621 @findex EXTRACTOR_metatype_to_string 622 @cindex gettext 623 @cindex internationalization 624 625 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)|}). 626 @end deftypefun 627 628 @deftypefun {const char *} EXTRACTOR_metatype_to_description (enum EXTRACTOR_MetaType type) 629 @findex EXTRACTOR_metatype_to_description 630 @cindex gettext 631 @cindex internationalization 632 633 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)|}). 634 @end deftypefun 635 636 637 638 @node Meta formats 639 @section Meta formats 640 641 @tindex enum EXTRACTOR_MetaFormat 642 643 @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. 644 645 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''. 646 647 648 649 @node Extracting 650 @section Extracting 651 652 @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) 653 @tindex EXTRACTOR_MetaDataProcessor 654 655 Type of a function that libextractor calls for each meta data item found. 656 657 @table @var 658 659 @item cls 660 closure (user-defined) 661 662 @item plugin_name 663 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); 664 665 @item type 666 libextractor-type describing the meta data; 667 668 @item format basic 669 format information about data 670 671 @item data_mime_type 672 mime-type of data (not of the original file); can be @code{NULL} (if mime-type is not known); 673 674 @item data 675 actual meta-data found 676 677 @item data_len 678 number of bytes in data 679 680 @end table 681 682 Return 0 to continue extracting, 1 to abort. 683 @end deftypefn 684 685 686 687 @deftypefun void EXTRACTOR_extract (struct EXTRACTOR_PluginList *plugins, const char *filename, const void *data, size_t size, EXTRACTOR_MetaDataProcessor proc, void *proc_cls) 688 @findex EXTRACTOR_extract 689 @cindex reentrant 690 @cindex concurrency 691 @cindex threads 692 @cindex thread-safety 693 694 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. 695 696 @cindex SIGBUS 697 @cindex bus error 698 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}). 699 700 @end deftypefun 701 702 703 @node Language bindings 704 @chapter Language bindings 705 @cindex Java 706 @cindex Mono 707 @cindex Perl 708 @cindex Python 709 @cindex PHP 710 @cindex Ruby 711 712 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. 713 714 @section Java 715 716 Compiling the GNU libextractor Java binding follows the usual process of 717 running @command{configure} and @command{make}. The result will be a 718 shared C library @file{libextractor_java.so} with the native code and 719 a JAR file (installed to @file{$PREFIX/share/java/libextractor.java}). 720 721 A minimal example for using GNU libextractor's Java binding would look 722 like this: 723 @verbatim 724 import org.gnu.libextractor.*; 725 import java.util.ArrayList; 726 727 public static void main(String[] args) { 728 Extractor ex = Extractor.getDefault(); 729 for (int i=0;i<args.length;i++) { 730 ArrayList keywords = ex.extract(args[i]); 731 System.out.println("Keywords for " + args[i] + ":"); 732 for (int j=0;j<keywords.size();j++) 733 System.out.println(keywords.get(j)); 734 } 735 } 736 @end verbatim 737 738 The GNU libextractor library and the @file{libextractor_java.so} JNI binding 739 have to be in the library search path for this to work. Furthermore, the 740 @file{libextractor.jar} file should be on the classpath. 741 742 Note that the API does not use Java 5 style generics in order to work 743 with older versions of Java. 744 745 @section Mono 746 747 his binding is undocumented at this point. 748 749 @section Perl 750 751 This binding is undocumented at this point. 752 753 @section Python 754 755 This binding is undocumented at this point. 756 757 @section PHP 758 759 This binding is undocumented at this point. 760 761 @section Ruby 762 763 This binding is undocumented at this point. 764 765 766 767 @node Utility functions 768 @chapter Utility functions 769 770 @cindex reentrant 771 @cindex concurrency 772 @cindex threads 773 @cindex thread-safety 774 This chapter describes various utility functions for GNU libextractor usage. All of the functions are reentrant. 775 776 @menu 777 * Utility Constants:: 778 * Meta data printing:: 779 @end menu 780 781 @node Utility Constants 782 @section Utility Constants 783 784 @findex EXTRACTOR_VERSION 785 The constant @verb{|EXTRACTOR_VERSION|} is a hexadecimal 786 representation of the version number of the installed libextractor 787 header. The hexadecimal format is 0xAABBCCDD where AA is the major 788 version (so far always 0), BB is the minor version, CC is the revision 789 and DD the patch number. For example, for version 0.5.18, we would 790 have AA=0, BB=5, CC=18 and DD=0. Minor releases such as 0.5.18a or 791 significant changes in unreleased versions would be marked with DD=1 792 or higher. 793 794 795 @node Meta data printing 796 @section Meta data printing 797 798 799 @findex EXTRACTOR_meta_data_print 800 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}. 801 802 803 804 @node Existing Plugins 805 @chapter Existing Plugins 806 807 @itemize @bullet 808 @item 809 APK (Android packages and Java archives, using zlib) 810 @item 811 ARCHIVE (using libarchive) 812 @item 813 DISKIMAGE (QCOW2, VMDK, VHD and VHDX virtual disk images) 814 @item 815 DVI 816 @item 817 EBOOK (EPUB and MOBI/AZW) 818 @item 819 EXIV2 (using libexiv2, 0.23 or later preferred) 820 @item 821 FLAC (using libFLAC) 822 @item 823 GEOTIFF (georeferencing tags in TIFF images) 824 @item 825 GPX (GPS tracks) 826 @item 827 GIF (using libgif) 828 @item 829 GSTREAMER (using libgstreamer v1.0 or later) 830 @item 831 HEIF (HEIC, HEIF and AVIF images) 832 @item 833 ID3 (MPEG audio with ID3v1/ID3v2 tags, Xing and LAME headers) 834 @item 835 ISO9660 (CD/DVD images) 836 @item 837 HTML (using libtidy) 838 @item 839 IT 840 @item 841 JPEG (using libjpeg v8 or later) 842 @item 843 KML (Keyhole Markup Language) 844 @item 845 LNK (Windows shell links) 846 @item 847 MAN 848 @item 849 MIDI (using libsmf) 850 @item 851 MIME (using libmagic) 852 @item 853 MBOX (Unix mailboxes and RFC 5322 messages) 854 @item 855 MPEG (using libmpeg2) 856 @item 857 MSOFFICE (OOXML: @file{.docx}, @file{.xlsx}, @file{.pptx}; and 858 Excel 2-4 BIFF streams) 859 @item 860 NSF 861 @item 862 PECOFF (Windows executables, DLLs and drivers) 863 @item 864 PLIST (Apple property lists, binary and XML) 865 @item 866 NSFE 867 @item 868 ODF 869 @item 870 OLE2 (with libgsf) 871 @item 872 OGG (with libogg) 873 @item 874 PNG 875 @item 876 PS 877 @item 878 RIFF 879 @item 880 RPM (using librpm) 881 @item 882 RTF (@file{.rtf}, including the revision table, comment authors and 883 custom document properties) 884 @item 885 S3M 886 @item 887 SQLITE (SQLite 3 databases, write-ahead logs and journals) 888 @item 889 SID 890 @item 891 PreviewGst (audio preview, using GStreamer) 892 @item 893 PreviewOpus (audio preview, using FFmpeg 5.1 or later) 894 @item 895 ThumbnailFFMPEG (video thumbnail, using libavformat, libavcodec and 896 libswscale of FFmpeg 5.1 or later) 897 @item 898 ThumbnailGst (video thumbnail, using GStreamer 1.8 or later) 899 @item 900 ThumbnailGtk (still image thumbnail, using gdk-pixbuf) 901 @item 902 TIFF (with libtiff, tested with v4) 903 @item 904 TAR (tar archives) 905 @item 906 WAV 907 @item 908 WEBP 909 @item 910 XM 911 @item 912 ZIP 913 @end itemize 914 915 @file{gzip} and @file{bzip2} compressed versions of these formats are 916 also supported (as well as meta data embedded by @file{gzip} itself) 917 if zlib or libbz2 are available. 918 919 @section Video thumbnails and audio previews 920 921 Four of the plugins in that list do not report meta data that was found 922 in the file, but meta data computed @emph{from} the file: a small image 923 taken from a video (@code{EXTRACTOR_METATYPE_THUMBNAIL}) and a short 924 audio excerpt re-encoded as Opus in an Ogg container 925 (@code{EXTRACTOR_METATYPE_AUDIO_PREVIEW}). Both are reported as 926 @code{EXTRACTOR_METAFORMAT_BINARY}. 927 928 Each feature comes in two implementations, one on top of GStreamer and 929 one on top of FFmpeg, and which of them are built depends on what 930 @command{configure} found: 931 932 @table @code 933 @item thumbnailgst 934 @itemx thumbnailffmpeg 935 Decode the video and emit a frame from about 30 seconds in, scaled to 936 fit a 128x128 box, as JPEG. Frames that are a perfectly flat surface 937 (a black leader, a fade-in) are skipped. 938 @item previewgst 939 @itemx previewopus 940 Decode about 15 seconds of audio from about 30 seconds in, fade it in 941 and out, and emit it as a mono 24 kbit/s Ogg Opus stream with the MIME 942 type @code{audio/ogg}. 943 @end table 944 945 In both cases the position is limited to a third of the duration, so a 946 short file is sampled proportionally earlier. 947 948 If both backends are installed, both plugins run and you get two 949 thumbnails (or two previews) for the same file. Which one you want is 950 an application decision; disable the other one in the configuration 951 string, as in 952 953 @example 954 pl = EXTRACTOR_plugin_add_defaults (EXTRACTOR_OPTION_DEFAULT_POLICY); 955 pl = EXTRACTOR_plugin_add_config (pl, 956 "-thumbnailffmpeg:-previewopus", 957 EXTRACTOR_OPTION_DEFAULT_POLICY); 958 @end example 959 960 All four accept a comma separated list of options in the plugin 961 configuration string: 962 963 @table @code 964 @item size=@var{pixels} 965 Edge length of the box the thumbnail must fit into (default: 128). 966 @item format=jpeg|png 967 Image format of the thumbnail (default: @code{jpeg}). 968 @item offset=@var{seconds} 969 How far into the media to sample (default: 30). 970 @item length=@var{seconds} 971 Length of the audio preview (default: 15). 972 @item bitrate=@var{bits-per-second} 973 Opus bitrate of the audio preview (default: 24000). 974 @item deadline=@var{milliseconds} 975 Wall clock budget for the whole extraction; when it runs out, the 976 plugin reports nothing rather than keeping the application waiting 977 (default: 10000, or 60000 if @code{srt} or @code{gallery} is given). 978 @end table 979 980 The two thumbnail plugins also understand: 981 982 @table @code 983 @item srt:@var{lang} 984 Additionally dump the complete subtitle track in language @var{lang} 985 as an SRT file (@code{EXTRACTOR_METATYPE_SUBTITLE_TEXT}, UTF-8, MIME 986 type @code{application/x-subrip}). @var{lang} may be given as ISO 987 639-1 (@code{de}), ISO 639-2/B (@code{ger}) or ISO 639-2/T 988 (@code{deu}) code; only the first matching track is dumped, and only 989 text subtitles (SubRip, ASS/SSA, WebVTT, timed text) can be converted. 990 Without this option the plugins merely list the language of every 991 subtitle track as @code{EXTRACTOR_METATYPE_SUBTITLE_LANGUAGE}. 992 @item gallery:@var{n} 993 Instead of a single thumbnail, emit one image showing @var{n} (at most 994 100) frames taken at roughly even intervals across the whole video, 995 arranged in a grid chosen so that the image is as close to 16:9 as 996 possible. @code{size} then applies to each tile. A frame that shows 997 basically nothing (99% of its pixels within a narrow colour range, such 998 as a black or white screen) is replaced by one taken a little earlier 999 or later. 1000 @end table 1001 1002 For example, @code{"thumbnailgst(size=256,format=png)"} asks for a 1003 256x256 PNG, and @code{"thumbnailffmpeg(gallery:12,srt:en)"} for a 1004 contact sheet of twelve frames plus the English subtitles. 1005 1006 Two caveats apply to the GStreamer variants. Their decoders and 1007 encoders are themselves GStreamer plugins, resolved at run time, so a 1008 system that lacks the @code{jpeg} or @code{opus} GStreamer plugin 1009 builds them happily and then gets no results; and because GStreamer 1010 does not survive a @code{fork()} of a process that has already 1011 initialized it, an application that runs these plugins 1012 @code{EXTRACTOR_OPTION_IN_PROCESS} should not also use out-of-process 1013 plugins in the same process afterwards. 1014 1015 @section Full text 1016 1017 The @code{html}, @code{pdf}, @code{ebook}, @code{odf}, @code{ole2} 1018 and @code{dvi} plugins can report the entire text of a document as a 1019 single @code{EXTRACTOR_METATYPE_FULL_TEXT} item in 1020 @code{EXTRACTOR_METAFORMAT_UTF8} with MIME type @code{text/plain}. As 1021 the text can be large, this is only done if the plugin is given the 1022 option @code{fulltext}, as in 1023 1024 @example 1025 pl = EXTRACTOR_plugin_add_config (NULL, 1026 "pdf(fulltext):odf(fulltext)", 1027 EXTRACTOR_OPTION_DEFAULT_POLICY); 1028 @end example 1029 1030 @noindent 1031 or, on the command line, @code{extract -n -l "pdf(fulltext)" file.pdf}. 1032 Note the @code{-n}: @code{EXTRACTOR_plugin_add} leaves the list 1033 unchanged if a plugin of that name is already in it, so adding 1034 @code{pdf(fulltext)} on top of the default plugin set --- which 1035 already contains @code{pdf}, without the option --- has no effect at 1036 all. Either drop the default set with @code{-n}, or remove the plugin 1037 before adding it back, as in 1038 @code{extract -l "-pdf:pdf(fulltext)" file.pdf}. 1039 1040 Within a paragraph, runs of whitespace (including line breaks) are 1041 reported as a single space; paragraphs are separated by exactly two 1042 newlines (@code{"\n\n"}). Where the format makes it possible, text 1043 that is not part of the running text is left out: page numbers, 1044 running headers and footers, navigation bars (HTML) and the like. The 1045 text is truncated at 16 MiB. 1046 1047 @section Forensic and bulk-scan plugins 1048 1049 Sixteen of the plugins in that list were added for a different job from 1050 the rest: identifying, and saying something useful about, every file on 1051 a large volume in a single fast pass. That purpose shapes how they 1052 behave, in ways worth knowing about before you use them. 1053 1054 They answer questions about @emph{provenance} rather than about 1055 content --- which machine, user, tool, device or account a file came 1056 from. A Windows executable reports the compiler that built it, the 1057 path its debug symbols were written to (which routinely discloses the 1058 build machine's user name) and the exploit mitigations it opts into; a 1059 shell link reports the volume serial number, the label and the NetBIOS 1060 name of the machine the target lived on, and often that machine's MAC 1061 address; a tar archive reports the user and group names of the system 1062 it was packed on; a mailbox reports the relay chain a message 1063 travelled and the addresses it passed through. 1064 1065 To keep a bulk scan fast they read headers, not files. Each rejects a 1066 file that is not its format within the first few bytes --- which is the 1067 overwhelmingly common case --- and none of them decodes payload data. 1068 The @code{sqlite} plugin in particular reads only the 100-byte header 1069 and never opens the database: opening an untrusted database would be 1070 both slow and unsafe. For the same reason none of these plugins links 1071 a third-party parsing library, so all but @code{apk} and @code{ebook}, 1072 which must inflate members of a zip container, are built 1073 unconditionally. 1074 1075 Because they characterise rather than enumerate, plugins that meet 1076 repeating structures --- archive members, mail messages, requested 1077 permissions, map placemarks --- report a bounded number of them 1078 together with an @code{EXTRACTOR_METATYPE_ENTRY_COUNT} giving the true 1079 total. Do not read the absence of a further item as the absence of 1080 further items. 1081 1082 Several of the meta types these plugins report were introduced with 1083 them, and an application that switches on 1084 @code{enum EXTRACTOR_MetaType} will not know them. Values are only 1085 ever appended, so existing values keep their meaning; use 1086 @code{EXTRACTOR_metatype_get_max()} rather than a hard-coded bound, and 1087 @code{EXTRACTOR_metatype_to_string()} to render a type you do not 1088 recognise. 1089 1090 Two habits of these formats are worth remembering when reading their 1091 output. Timestamps are reported as ISO 8601 in UTC, but several 1092 formats store local time with no zone at all --- ISO 9660 and the 1093 ISO base media format both do --- so a value may be displaced by the 1094 writer's offset from UTC. And a field being present does not make it 1095 true: every value here was written by whatever produced the file, an 1096 executable's build timestamp is trivially forged, and a disagreement 1097 between two fields is frequently more informative than either. 1098 1099 @node Writing new Plugins 1100 @chapter Writing new Plugins 1101 1102 Writing a new plugin for libextractor usually requires writing of or 1103 interfacing with an actual parser for a specific format. How this is 1104 can be accomplished depends on the format and cannot be specified in 1105 general. However, care should be taken for the code to be reentrant 1106 and highly fault-tolerant, especially with respect to malformed 1107 inputs. 1108 1109 Plugins should start by verifying that the header of the data matches 1110 the specific format and immediately return if that is not the case. 1111 Even if the header matches the expected file format, plugins must not 1112 assume that the remainder of the file is well formed. 1113 1114 The plugin library must be called libextractor_XXX.so, where XXX 1115 denotes the file format of the plugin. The library must export a 1116 method @verb{|libextractor_XXX_extract_method|}, with the following 1117 signature: 1118 @verbatim 1119 void 1120 EXTRACTOR_XXX_extract_method (struct EXTRACTOR_ExtractContext *ec); 1121 @end verbatim 1122 1123 @samp{ec} contains various information the plugin may need for its 1124 execution. Most importantly, it contains functions for reading 1125 (``read'') and seeking (``seek'') the input data and for returning 1126 extracted data (``proc''). The ``config'' member can contain 1127 additional configuration options. ``proc'' should be called on 1128 each meta data item found. If ``proc'' returns non-zero, 1129 processing should be aborted (if possible). 1130 1131 In order to test new plugins, the @file{extract} command can be run 1132 with the options ``-ni'' and ``-l XXX'' . This will run the plugin 1133 in-process (making it easier to debug) and without any of the other 1134 plugins. 1135 1136 1137 @section Example for a minimal extract method 1138 1139 The following example shows how a plugin can return the mime type of 1140 a file. 1141 @example 1142 @verbatim 1143 void 1144 EXTRACTOR_mymime_extract (struct EXTRACTOR_ExtractContext *ec) 1145 { 1146 void *data; 1147 ssize_t data_size, 1148 1149 if (-1 == (data_size = ec->read (ec->cls, &data, 4))) 1150 return; /* read error */ 1151 if (data_size < 4) 1152 return; /* file too small */ 1153 if (0 != memcmp (data, "\177ELF", 4)) 1154 return; /* not ELF */ 1155 if (0 != ec->proc (ec->cls, 1156 "mymime", 1157 EXTRACTOR_METATYPE_MIMETYPE, 1158 EXTRACTOR_METAFORMAT_UTF8, 1159 "text/plain", 1160 "application/x-executable", 1161 1 + strlen("application/x-executable"))) 1162 return; 1163 /* more calls to 'proc' here as needed */ 1164 } 1165 @end verbatim 1166 @end example 1167 1168 1169 @node Internal utility functions 1170 @chapter Internal utility functions 1171 1172 Some plugins link against the @code{libextractor_common} library which 1173 provides common abstractions needed by many plugins. This section 1174 documents this internal API for plugin developers. Note that the headers 1175 for this library are (intentionally) not installed: we do not consider 1176 this API stable and it should hence only be used by plugins that are 1177 build and shipped with GNU libextractor. Third-party plugins should 1178 not use it. 1179 1180 @file{convert_numeric.h} defines various conversion functions for 1181 numbers (in particular, byte-order conversion for floating point 1182 numbers). 1183 1184 @file{unzip.h} defines an API for accessing compressed files. 1185 1186 @file{pack.h} provides an interpreter for unpacking structs of integer 1187 numbers from streams and converting from big or little endian to host 1188 byte order at the same time. 1189 1190 @file{convert.h} provides a function for character set conversion described 1191 below. 1192 1193 @deftypefun {char *} EXTRACTOR_common_convert_to_utf8 (const char *input, size_t len, const char *charset) 1194 @cindex UTF-8 1195 @cindex character set 1196 @findex EXTRACTOR_common_convert_to_utf8 1197 Various GNU libextractor plugins make use of the internal 1198 @file{convert.h} header which defines a function 1199 1200 @verb{|EXTRACTOR_common_convert_to_utf8|} which can be used to easily convert text from 1201 any character set to UTF-8. This conversion is important since the 1202 linked list of keywords that is returned by GNU libextractor is 1203 expected to contain only UTF-8 strings. Naturally, proper conversion 1204 may not always be possible since some file formats fail to specify the 1205 character set. In that case, it is often better to not convert at 1206 all. 1207 1208 The arguments to @verb{|EXTRACTOR_common_convert_to_utf8|} are the input string (which 1209 does @emph{not} have to be zero-terminated), the length of the input 1210 string, and the character set (which @emph{must} be zero-terminated). 1211 Which character sets are supported depends on the platform, a list can 1212 generally be obtained using the @command{iconv -l} command. The 1213 return value from @verb{|EXTRACTOR_common_convert_to_utf8|} is a zero-terminated string 1214 in UTF-8 format. The responsibility to free the string is with the 1215 caller, so storing the string in the keyword list is acceptable. 1216 @end deftypefun 1217 1218 1219 1220 1221 1222 @node Reporting bugs 1223 @chapter Reporting bugs 1224 1225 @cindex bug 1226 GNU libextractor uses the @url{https://gnunet.org/bugs/,Mantis bugtracking 1227 system}. If possible, please report bugs there. You can also e-mail 1228 the GNU libextractor mailinglist at @url{libextractor@@gnu.org}. 1229 1230 1231 1232 @c ********************************************************** 1233 @c ******************* Appendices ************************* 1234 @c ********************************************************** 1235 1236 @node GNU Free Documentation License 1237 @appendix GNU Free Documentation License 1238 1239 @include fdl-1.3.texi 1240 1241 1242 @node Index 1243 @unnumbered Index 1244 1245 @printindex cp 1246 1247 @c @node Function and Data Index 1248 @c @unnumbered Function and Data Index 1249 @c @printindex fn 1250 1251 @c @node Type Index 1252 @c @unnumbered Type Index 1253 @c @printindex tp 1254 1255 @bye