1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041
|
/* GIMP - The GNU Image Manipulation Program
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
* Copyright (C) 1997 Daniel Risacher, magnus@alum.mit.edu
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/* Minor changes to support file magic */
/* 4 Oct 1997 -- Risacher */
/* compressor plug-in for GIMP */
/* based on gz.c which in turn is */
/* loosely based on url.c by */
/* Josh MacDonald, jmacd@cs.berkeley.edu */
/* and, very loosely on hrz.c by */
/* Albert Cahalan <acahalan at cs.uml.edu> */
/* LZMA compression code is based on code by Lasse Collin which was
* placed in the public-domain. */
/* This is reads and writes compressed image files for GIMP
*
* It should work with file names of the form
* filename.foo.[gz|bz2] where foo is some already-recognized extension
*
* and it also works for names of the form
* filename.xcf[gz|bz2] - which is equivalent to
* filename.xcf.[gz|bz2]
*
* I added the xcfgz bit because having a default extension of xcf.gz
* can confuse the file selection dialog box somewhat, forcing the
* user to type sometimes when they otherwise wouldn't need to.
*
* I later decided I didn't like it because I don't like to bloat
* the file-extension namespace. But I left in the recognition
* feature/bug so if people want to use files named foo.xcfgz by
* default, they can just hack their pluginrc file.
*
* to do this hack, change :
* "xcf.gz,gz,xcfgz"
* to
* "xcfgz,gz,xcf.gz"
*
*
* -Dan Risacher, 0430 CDT, 26 May 1997
*/
#include "config.h"
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#ifdef HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif
#ifdef HAVE_SYS_WAIT_H
#include <sys/wait.h>
#endif
#include <sys/stat.h>
#include <fcntl.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <glib/gstdio.h>
#ifndef _O_BINARY
#define _O_BINARY 0
#endif
#include <libgimp/gimp.h>
#include "libgimp/stdplugins-intl.h"
#include <zlib.h>
#include <bzlib.h>
#include <lzma.h>
/* Author 1: Josh MacDonald (url.c) */
/* Author 2: Daniel Risacher (gz.c) */
/* Author 3: Michael Natterer (compressor.c) */
/* According to USAF Lt Steve Werhle, US DoD software development
* contracts average about $25 USD per source line of code (SLOC). By
* that metric, I figure this plug-in is worth about $10,000 USD */
/* But you got it free. Magic of Gnu. */
typedef gboolean (* LoadFn) (GFile *infile,
GFile *outfile);
typedef gboolean (* SaveFn) (GFile *infile,
GFile *outfile);
typedef struct _CompressorEntry CompressorEntry;
struct _CompressorEntry
{
const gchar *file_type;
const gchar *mime_type;
const gchar *extensions;
const gchar *magic;
const gchar *xcf_extension;
const gchar *generic_extension;
const gchar *load_proc;
const gchar *load_blurb;
const gchar *load_help;
LoadFn load_fn;
const gchar *save_proc;
const gchar *save_blurb;
const gchar *save_help;
SaveFn save_fn;
};
typedef struct _Compressor Compressor;
typedef struct _CompressorClass CompressorClass;
struct _Compressor
{
GimpPlugIn parent_instance;
};
struct _CompressorClass
{
GimpPlugInClass parent_class;
};
#define COMPRESSOR_TYPE (compressor_get_type ())
#define COMPRESSOR(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), COMPRESSOR_TYPE, Compressor))
GType compressor_get_type (void) G_GNUC_CONST;
static GList * compressor_query_procedures (GimpPlugIn *plug_in);
static GimpProcedure * compressor_create_procedure (GimpPlugIn *plug_in,
const gchar *name);
static GimpValueArray * compressor_export (GimpProcedure *procedure,
GimpRunMode run_mode,
GimpImage *image,
GFile *file,
GimpExportOptions *options,
GimpMetadata *metadata,
GimpProcedureConfig *config,
gpointer run_data);
static GimpValueArray * compressor_load (GimpProcedure *procedure,
GimpRunMode run_mode,
GFile *file,
GimpMetadata *metadata,
GimpMetadataLoadFlags *flags,
GimpProcedureConfig *config,
gpointer run_data);
static GimpImage * load_image (const CompressorEntry *compressor,
GFile *file,
gint32 run_mode,
GimpPDBStatusType *status,
GError **error);
static GimpPDBStatusType export_image (const CompressorEntry *compressor,
GFile *file,
GimpImage *image,
GimpExportOptions *options,
gint n_drawables,
GList *drawables,
gint32 run_mode,
GError **error);
static gboolean valid_file (GFile *file);
static const gchar * find_extension (const CompressorEntry *compressor,
const gchar *filename);
static gboolean gzip_load (GFile *infile,
GFile *outfile);
static gboolean gzip_export (GFile *infile,
GFile *outfile);
static gboolean bzip2_load (GFile *infile,
GFile *outfile);
static gboolean bzip2_export (GFile *infile,
GFile *outfile);
static gboolean xz_load (GFile *infile,
GFile *outfile);
static gboolean xz_export (GFile *infile,
GFile *outfile);
static goffset get_file_info (GFile *file);
G_DEFINE_TYPE (Compressor, compressor, GIMP_TYPE_PLUG_IN)
GIMP_MAIN (COMPRESSOR_TYPE)
DEFINE_STD_SET_I18N
static const CompressorEntry compressors[] =
{
{
N_("gzip archive"),
"application/x-gzip",
"xcf.gz,xcfgz", /* FIXME "xcf.gz,gz,xcfgz" */
"0,string,\037\213",
".xcfgz",
".gz",
"file-gz-load",
"loads files compressed with gzip",
"This procedure loads files in the gzip compressed format.",
gzip_load,
"file-gz-export",
"saves files compressed with gzip",
"This procedure saves files in the gzip compressed format.",
gzip_export
},
{
N_("bzip archive"),
"application/x-bzip",
"xcf.bz2,xcfbz2", /* FIXME "xcf.bz2,bz2,xcfbz2" */
"0,string,BZh",
".xcfbz2",
".bz2",
"file-bz2-load",
"loads files compressed with bzip2",
"This procedure loads files in the bzip2 compressed format.",
bzip2_load,
"file-bz2-export",
"saves files compressed with bzip2",
"This procedure saves files in the bzip2 compressed format.",
bzip2_export
},
{
N_("xz archive"),
"application/x-xz",
"xcf.xz,xcfxz", /* FIXME "xcf.xz,xz,xcfxz" */
"0,string,\3757zXZ\x00",
".xcfxz",
".xz",
"file-xz-load",
"loads files compressed with xz",
"This procedure loads files in the xz compressed format.",
xz_load,
"file-xz-export",
"saves files compressed with xz",
"This procedure saves files in the xz compressed format.",
xz_export
}
};
static void
compressor_class_init (CompressorClass *klass)
{
GimpPlugInClass *plug_in_class = GIMP_PLUG_IN_CLASS (klass);
plug_in_class->query_procedures = compressor_query_procedures;
plug_in_class->create_procedure = compressor_create_procedure;
plug_in_class->set_i18n = STD_SET_I18N;
}
static void
compressor_init (Compressor *compressor)
{
}
static GList *
compressor_query_procedures (GimpPlugIn *plug_in)
{
GList *list = NULL;
gint i;
for (i = 0; i < G_N_ELEMENTS (compressors); i++)
{
const CompressorEntry *compressor = &compressors[i];
list = g_list_append (list, g_strdup (compressor->load_proc));
list = g_list_append (list, g_strdup (compressor->save_proc));
}
return list;
}
static GimpProcedure *
compressor_create_procedure (GimpPlugIn *plug_in,
const gchar *name)
{
GimpProcedure *procedure = NULL;
gint i;
for (i = 0; i < G_N_ELEMENTS (compressors); i++)
{
const CompressorEntry *compressor = &compressors[i];
if (! strcmp (name, compressor->load_proc))
{
procedure = gimp_load_procedure_new (plug_in, name,
GIMP_PDB_PROC_TYPE_PLUGIN,
compressor_load,
(gpointer) compressor, NULL);
gimp_procedure_set_documentation (procedure,
compressor->load_blurb,
compressor->load_help,
name);
gimp_file_procedure_set_magics (GIMP_FILE_PROCEDURE (procedure),
compressor->magic);
}
else if (! strcmp (name, compressor->save_proc))
{
procedure = gimp_export_procedure_new (plug_in, name,
GIMP_PDB_PROC_TYPE_PLUGIN,
FALSE, compressor_export,
(gpointer) compressor, NULL);
gimp_procedure_set_image_types (procedure, "RGB*, GRAY*, INDEXED*");
gimp_procedure_set_documentation (procedure,
compressor->save_blurb,
compressor->save_help,
name);
}
if (procedure)
{
gimp_procedure_set_menu_label (procedure, _(compressor->file_type));
gimp_procedure_set_attribution (procedure,
"Daniel Risacher",
"Daniel Risacher, Spencer Kimball "
"and Peter Mattis",
"1995-1997");
gimp_file_procedure_set_mime_types (GIMP_FILE_PROCEDURE (procedure),
compressor->mime_type);
gimp_file_procedure_set_extensions (GIMP_FILE_PROCEDURE (procedure),
compressor->extensions);
return procedure;
}
}
return NULL;
}
static GimpValueArray *
compressor_load (GimpProcedure *procedure,
GimpRunMode run_mode,
GFile *file,
GimpMetadata *metadata,
GimpMetadataLoadFlags *flags,
GimpProcedureConfig *config,
gpointer run_data)
{
const CompressorEntry *compressor = run_data;
GimpValueArray *return_vals;
GimpPDBStatusType status;
GimpImage *image;
GError *error = NULL;
/* We handle PDB errors by forwarding them to the caller in
* our return values.
*/
gimp_plug_in_set_pdb_error_handler (gimp_procedure_get_plug_in (procedure),
GIMP_PDB_ERROR_HANDLER_PLUGIN);
image = load_image (compressor, file, run_mode,
&status, &error);
return_vals = gimp_procedure_new_return_values (procedure, status, error);
if (image && status == GIMP_PDB_SUCCESS)
GIMP_VALUES_SET_IMAGE (return_vals, 1, image);
return return_vals;
}
static GimpValueArray *
compressor_export (GimpProcedure *procedure,
GimpRunMode run_mode,
GimpImage *image,
GFile *file,
GimpExportOptions *options,
GimpMetadata *metadata,
GimpProcedureConfig *config,
gpointer run_data)
{
const CompressorEntry *compressor = run_data;
GimpPDBStatusType status;
GList *drawables = gimp_image_list_layers (image);
gint n_drawables = g_list_length (drawables);
GError *error = NULL;
/* We handle PDB errors by forwarding them to the caller in
* our return values.
*/
gimp_plug_in_set_pdb_error_handler (gimp_procedure_get_plug_in (procedure),
GIMP_PDB_ERROR_HANDLER_PLUGIN);
status = export_image (compressor, file, image, options, n_drawables,
drawables, run_mode, &error);
g_list_free (drawables);
return gimp_procedure_new_return_values (procedure, status, error);
}
static GimpPDBStatusType
export_image (const CompressorEntry *compressor,
GFile *file,
GimpImage *image,
GimpExportOptions *options,
gint n_drawables,
GList *drawables,
gint32 run_mode,
GError **error)
{
const gchar *ext;
GFile *tmp_file;
ext = find_extension (compressor, g_file_peek_path (file));
if (! ext)
{
g_message (_("No sensible file extension, saving as compressed XCF."));
ext = ".xcf";
}
/* get a temp name with the right extension and save into it. */
tmp_file = gimp_temp_file (ext + 1);
if (! (gimp_file_save (run_mode, image, tmp_file, options) &&
valid_file (tmp_file)))
{
g_file_delete (tmp_file, NULL, NULL);
g_object_unref (tmp_file);
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
"%s", gimp_pdb_get_last_error (gimp_get_pdb ()));
return GIMP_PDB_EXECUTION_ERROR;
}
gimp_progress_init_printf (_("Compressing '%s'"),
gimp_file_get_utf8_name (file));
if (! compressor->save_fn (tmp_file, file))
{
g_file_delete (tmp_file, NULL, NULL);
g_object_unref (tmp_file);
return GIMP_PDB_EXECUTION_ERROR;
}
g_file_delete (tmp_file, NULL, NULL);
g_object_unref (tmp_file);
gimp_progress_update (1.0);
/* ask the core to save a thumbnail for compressed XCF files */
if (strcmp (ext, ".xcf") == 0)
gimp_file_create_thumbnail (image, file);
return GIMP_PDB_SUCCESS;
}
static GimpImage *
load_image (const CompressorEntry *compressor,
GFile *file,
gint32 run_mode,
GimpPDBStatusType *status,
GError **error)
{
GimpImage *image;
const gchar *ext;
GFile *tmp_file;
ext = find_extension (compressor, g_file_peek_path (file));
if (! ext)
{
g_message (_("No sensible file extension, "
"attempting to load with file magic."));
ext = ".foo";
}
/* find a temp name */
tmp_file = gimp_temp_file (ext + 1);
if (! compressor->load_fn (file, tmp_file))
{
g_object_unref (tmp_file);
*status = GIMP_PDB_EXECUTION_ERROR;
return NULL;
}
/* now that we uncompressed it, load the temp file */
image = gimp_file_load (run_mode, tmp_file);
g_file_delete (tmp_file, NULL, NULL);
g_object_unref (tmp_file);
if (image)
{
*status = GIMP_PDB_SUCCESS;
gimp_image_set_file (image, file);
}
else
{
/* Forward the return status of the underlining plug-in for the
* given format.
*/
*status = gimp_pdb_get_last_status (gimp_get_pdb ());
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
"%s", gimp_pdb_get_last_error (gimp_get_pdb ()));
}
return image;
}
static gboolean
valid_file (GFile *file)
{
GStatBuf buf;
gboolean valid;
valid = g_stat (g_file_peek_path (file), &buf) == 0 && buf.st_size > 0;
return valid;
}
static const gchar *
find_extension (const CompressorEntry *compressor,
const gchar *filename)
{
gchar *filename_copy;
gchar *ext;
/* we never free this copy - aren't we evil! */
filename_copy = g_strdup (filename);
/* find the extension, boy! */
ext = strrchr (filename_copy, '.');
while (TRUE)
{
if (!ext || ext[1] == '\0' || strchr (ext, G_DIR_SEPARATOR))
{
return NULL;
}
if (0 == g_ascii_strcasecmp (ext, compressor->xcf_extension))
{
return ".xcf"; /* we've found it */
}
if (0 != g_ascii_strcasecmp (ext, compressor->generic_extension))
{
return ext;
}
else
{
/* we found ".gz" so strip it, loop back, and look again */
*ext = '\0';
ext = strrchr (filename_copy, '.');
}
}
}
static gboolean
gzip_load (GFile *infile,
GFile *outfile)
{
gboolean ret;
int fd;
gzFile in;
FILE *out;
char buf[16384];
int len;
ret = FALSE;
in = NULL;
out = NULL;
fd = g_open (g_file_peek_path (infile), O_RDONLY | _O_BINARY, 0);
if (fd == -1)
goto out;
in = gzdopen (fd, "rb");
if (! in)
{
close (fd);
goto out;
}
out = g_fopen (g_file_peek_path (outfile), "wb");
if (! out)
goto out;
while (TRUE)
{
len = gzread (in, buf, sizeof buf);
if (len < 0)
break;
else if (len == 0)
{
ret = TRUE;
break;
}
if (fwrite(buf, 1, len, out) != len)
break;
}
out:
/* There is no need to close(fd) as it is closed by gzclose(). */
if (in)
if (gzclose (in) != Z_OK)
ret = FALSE;
if (out)
fclose (out);
return ret;
}
static gboolean
gzip_export (GFile *infile,
GFile *outfile)
{
gboolean ret;
FILE *in;
int fd;
gzFile out;
char buf[16384];
int len;
goffset tot = 0, file_size;
ret = FALSE;
in = NULL;
out = NULL;
in = g_fopen (g_file_peek_path (infile), "rb");
if (! in)
goto out;
fd = g_open (g_file_peek_path (outfile), O_CREAT | O_WRONLY | O_TRUNC | _O_BINARY, 0664);
if (fd == -1)
goto out;
out = gzdopen (fd, "wb");
if (! out)
{
close (fd);
goto out;
}
file_size = get_file_info (infile);
while (TRUE)
{
len = fread (buf, 1, sizeof buf, in);
if (ferror (in))
break;
if (len < 0)
break;
else if (len == 0)
{
ret = TRUE;
break;
}
if (gzwrite (out, buf, len) != len)
break;
gimp_progress_update ((tot += len) * 1.0 / file_size);
}
out:
if (in)
fclose (in);
/* There is no need to close(fd) as it is closed by gzclose(). */
if (out)
if (gzclose (out) != Z_OK)
ret = FALSE;
return ret;
}
static gboolean
bzip2_load (GFile *infile,
GFile *outfile)
{
gboolean ret;
int fd;
BZFILE *in;
FILE *out;
char buf[16384];
int len;
ret = FALSE;
in = NULL;
out = NULL;
fd = g_open (g_file_peek_path (infile), O_RDONLY | _O_BINARY, 0);
if (fd == -1)
goto out;
in = BZ2_bzdopen (fd, "rb");
if (!in)
{
close (fd);
goto out;
}
out = g_fopen (g_file_peek_path (outfile), "wb");
if (!out)
goto out;
while (TRUE)
{
len = BZ2_bzread (in, buf, sizeof buf);
if (len < 0)
break;
else if (len == 0)
{
ret = TRUE;
break;
}
if (fwrite(buf, 1, len, out) != len)
break;
}
out:
/* TODO: Check this in the case of BZ2_bzclose(): */
/* There is no need to close(fd) as it is closed by BZ2_bzclose(). */
if (in)
BZ2_bzclose (in);
if (out)
fclose (out);
return ret;
}
static gboolean
bzip2_export (GFile *infile,
GFile *outfile)
{
gboolean ret;
FILE *in;
int fd;
BZFILE *out;
char buf[16384];
int len;
goffset tot = 0, file_size;
ret = FALSE;
in = NULL;
out = NULL;
in = g_fopen (g_file_peek_path (infile), "rb");
if (!in)
goto out;
fd = g_open (g_file_peek_path (outfile), O_CREAT | O_WRONLY | O_TRUNC | _O_BINARY, 0664);
if (fd == -1)
goto out;
out = BZ2_bzdopen (fd, "wb");
if (!out)
{
close (fd);
goto out;
}
file_size = get_file_info (infile);
while (TRUE)
{
len = fread (buf, 1, sizeof buf, in);
if (ferror (in))
break;
if (len < 0)
break;
else if (len == 0)
{
ret = TRUE;
break;
}
if (BZ2_bzwrite (out, buf, len) != len)
break;
gimp_progress_update ((tot += len) * 1.0 / file_size);
}
out:
if (in)
fclose (in);
/* TODO: Check this in the case of BZ2_bzclose(): */
/* There is no need to close(fd) as it is closed by BZ2_bzclose(). */
if (out)
BZ2_bzclose (out);
return ret;
}
static gboolean
xz_load (GFile *infile,
GFile *outfile)
{
gboolean ret;
FILE *in;
FILE *out;
lzma_stream strm = LZMA_STREAM_INIT;
lzma_action action;
guint8 inbuf[BUFSIZ];
guint8 outbuf[BUFSIZ];
lzma_ret status;
ret = FALSE;
in = NULL;
out = NULL;
in = g_fopen (g_file_peek_path (infile), "rb");
if (!in)
goto out;
out = g_fopen (g_file_peek_path (outfile), "wb");
if (!out)
goto out;
if (lzma_stream_decoder (&strm, UINT64_MAX, 0) != LZMA_OK)
goto out;
strm.next_in = NULL;
strm.avail_in = 0;
strm.next_out = outbuf;
strm.avail_out = sizeof outbuf;
action = LZMA_RUN;
status = LZMA_OK;
while (status == LZMA_OK)
{
/* Fill the input buffer if it is empty. */
if ((strm.avail_in == 0) && (!feof(in)))
{
strm.next_in = inbuf;
strm.avail_in = fread (inbuf, 1, sizeof inbuf, in);
if (ferror (in))
goto out;
/* Once the end of the input file has been reached, we need to
tell lzma_code() that no more input will be coming and that
it should finish the encoding. */
if (feof (in))
action = LZMA_FINISH;
}
status = lzma_code (&strm, action);
if ((strm.avail_out == 0) || (status == LZMA_STREAM_END))
{
/* When lzma_code() has returned LZMA_STREAM_END, the output
buffer is likely to be only partially full. Calculate how
much new data there is to be written to the output file. */
size_t write_size = sizeof outbuf - strm.avail_out;
if (fwrite (outbuf, 1, write_size, out) != write_size)
goto out;
/* Reset next_out and avail_out. */
strm.next_out = outbuf;
strm.avail_out = sizeof outbuf;
}
}
if (status != LZMA_STREAM_END)
goto out;
lzma_end (&strm);
ret = TRUE;
out:
if (in)
fclose (in);
if (out)
fclose (out);
return ret;
}
static gboolean
xz_export (GFile *infile,
GFile *outfile)
{
gboolean ret;
FILE *in;
FILE *out;
lzma_stream strm = LZMA_STREAM_INIT;
lzma_action action;
guint8 inbuf[BUFSIZ];
guint8 outbuf[BUFSIZ];
lzma_ret status;
goffset tot = 0, file_size;
ret = FALSE;
in = NULL;
out = NULL;
in = g_fopen (g_file_peek_path (infile), "rb");
if (!in)
goto out;
file_size = get_file_info (infile);
out = g_fopen (g_file_peek_path (outfile), "wb");
if (!out)
goto out;
if (lzma_easy_encoder (&strm,
LZMA_PRESET_DEFAULT,
LZMA_CHECK_CRC64) != LZMA_OK)
goto out;
strm.next_in = NULL;
strm.avail_in = 0;
strm.next_out = outbuf;
strm.avail_out = sizeof outbuf;
action = LZMA_RUN;
status = LZMA_OK;
while (status == LZMA_OK)
{
/* Fill the input buffer if it is empty. */
if ((strm.avail_in == 0) && (!feof(in)))
{
strm.next_in = inbuf;
strm.avail_in = fread (inbuf, 1, sizeof inbuf, in);
if (ferror (in))
goto out;
/* Once the end of the input file has been reached, we need to
tell lzma_code() that no more input will be coming and that
it should finish the encoding. */
if (feof (in))
action = LZMA_FINISH;
gimp_progress_update ((tot += strm.avail_in) * 1.0 / file_size);
}
status = lzma_code (&strm, action);
if ((strm.avail_out == 0) || (status == LZMA_STREAM_END))
{
/* When lzma_code() has returned LZMA_STREAM_END, the output
buffer is likely to be only partially full. Calculate how
much new data there is to be written to the output file. */
size_t write_size = sizeof outbuf - strm.avail_out;
if (fwrite (outbuf, 1, write_size, out) != write_size)
goto out;
/* Reset next_out and avail_out. */
strm.next_out = outbuf;
strm.avail_out = sizeof outbuf;
}
}
if (status != LZMA_STREAM_END)
goto out;
lzma_end (&strm);
ret = TRUE;
out:
if (in)
fclose (in);
if (out)
fclose (out);
return ret;
}
/* get file size from a filename */
static goffset
get_file_info (GFile *file)
{
GFileInfo *info;
goffset size = 1;
info = g_file_query_info (file,
G_FILE_ATTRIBUTE_STANDARD_SIZE,
G_FILE_QUERY_INFO_NONE,
NULL, NULL);
if (info)
{
size = g_file_info_get_attribute_uint64 (info, G_FILE_ATTRIBUTE_STANDARD_SIZE);
g_object_unref (info);
}
return size;
}
|