ref: e740c549e59a3a58dca615478dc5dcee3fb915d8
parent: 243b91b224b414dae03d260c646f29774585a75f
author: Tor Andersson <[email protected]>
date: Fri Apr 8 07:59:16 EDT 2016
Reindent jbig2dec source to follow gs coding style. First a pass through gnu indent: indent \ -bad -nbap -nsob -br -ce -cli0 \ -npcs -ncs -i4 -di0 -psl -lp -lps -nut -l160 \ *.c *.h Followed by astyle to patch over some of the indentation bugs in gnu indent: astyle --style=kr -H -U -k3 *.c *.h
--- a/config_win32.h
+++ b/config_win32.h
@@ -17,7 +17,6 @@
jbig2dec
*/
-
/* configuration header file for compiling under Microsoft Windows */
/* update package version here */
@@ -25,24 +24,25 @@
#define VERSION "0.13"
#if defined(_MSC_VER) || (defined(__BORLANDC__) && defined(__WIN32__))
- /* Microsoft Visual C++ or Borland C++ */
- typedef signed char int8_t;
- typedef short int int16_t;
- typedef int int32_t;
- typedef __int64 int64_t;
+/* Microsoft Visual C++ or Borland C++ */
+typedef signed char int8_t;
+typedef short int int16_t;
+typedef int int32_t;
+typedef __int64 int64_t;
- typedef unsigned char uint8_t;
- typedef unsigned short int uint16_t;
- typedef unsigned int uint32_t;
- /* no uint64_t */
+typedef unsigned char uint8_t;
+typedef unsigned short int uint16_t;
+typedef unsigned int uint32_t;
+/* no uint64_t */
+
# if defined(_MSC_VER)
-# if _MSC_VER < 1500 /* VS 2008 has vsnprintf */
+# if _MSC_VER < 1500 /* VS 2008 has vsnprintf */
# define vsnprintf _vsnprintf
# endif
# endif
-# if defined(_MSC_VER) && _MSC_VER>=1900 /* VS 2014 and later have (finally) snprintf */
+# if defined(_MSC_VER) && _MSC_VER>=1900 /* VS 2014 and later have (finally) snprintf */
# define STDC99
# else
# define snprintf _snprintf
--- a/getopt.c
+++ b/getopt.c
@@ -3,7 +3,7 @@
"Keep this file name-space clean" means, talk to [email protected]
before changing it!
Copyright (C) 1987,88,89,90,91,92,93,94,95,96,98,99,2000,2001
- Free Software Foundation, Inc.
+ Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
@@ -59,7 +59,6 @@
#ifndef ELIDE_CODE
-
/* This needs to come after some library #include
to get __GNU_LIBRARY__ defined. */
#ifdef __GNU_LIBRARY__
@@ -67,7 +66,7 @@
contain conflicting prototypes for getopt. */
# include <stdlib.h>
# include <unistd.h>
-#endif /* GNU C library. */
+#endif /* GNU C library. */
#ifdef VMS
# include <unixlib.h>
@@ -182,9 +181,8 @@
of the value of `ordering'. In the case of RETURN_IN_ORDER, only
`--' can cause `getopt' to return -1 with `optind' != ARGC. */
-static enum
-{
- REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER
+static enum {
+ REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER
} ordering;
/* Value of POSIXLY_CORRECT environment variable. */
@@ -209,21 +207,20 @@
whose names are inconsistent. */
#ifndef getenv
-extern char *getenv ();
+extern char *getenv();
#endif
static char *
-my_index (str, chr)
- const char *str;
- int chr;
+my_index(str, chr)
+const char *str;
+int chr;
{
- while (*str)
- {
- if (*str == chr)
- return (char *) str;
- str++;
+ while (*str) {
+ if (*str == chr)
+ return (char *)str;
+ str++;
}
- return 0;
+ return 0;
}
/* If using GCC, we can safely declare strlen this way.
@@ -234,7 +231,7 @@
# if (!defined __STDC__ || !__STDC__) && !defined strlen
/* gcc with -traditional declares the built-in strlen to return int,
and has done so at least since version 2.4.5. -- rms. */
-extern int strlen (const char *);
+extern int strlen(const char *);
# endif /* not __STDC__ */
#endif /* __GNUC__ */
@@ -278,9 +275,9 @@
# else
# define SWAP_FLAGS(ch1, ch2)
# endif
-#else /* !_LIBC */
+#else /* !_LIBC */
# define SWAP_FLAGS(ch1, ch2)
-#endif /* _LIBC */
+#endif /* _LIBC */
/* Exchange two adjacent subsequences of ARGV.
One subsequence is elements [first_nonopt,last_nonopt)
@@ -292,158 +289,138 @@
the new indices of the non-options in ARGV after they are moved. */
#if defined __STDC__ && __STDC__
-static void exchange (char **);
+static void exchange(char **);
#endif
static void
-exchange (argv)
- char **argv;
+exchange(argv)
+char **argv;
{
- int bottom = first_nonopt;
- int middle = last_nonopt;
- int top = optind;
- char *tem;
+ int bottom = first_nonopt;
+ int middle = last_nonopt;
+ int top = optind;
+ char *tem;
- /* Exchange the shorter segment with the far end of the longer segment.
- That puts the shorter segment into the right place.
- It leaves the longer segment in the right place overall,
- but it consists of two parts that need to be swapped next. */
+ /* Exchange the shorter segment with the far end of the longer segment.
+ That puts the shorter segment into the right place.
+ It leaves the longer segment in the right place overall,
+ but it consists of two parts that need to be swapped next. */
#if defined _LIBC && defined USE_NONOPTION_FLAGS
- /* First make sure the handling of the `__getopt_nonoption_flags'
- string can work normally. Our top argument must be in the range
- of the string. */
- if (nonoption_flags_len > 0 && top >= nonoption_flags_max_len)
- {
- /* We must extend the array. The user plays games with us and
- presents new arguments. */
- char *new_str = malloc (top + 1);
- if (new_str == NULL)
- nonoption_flags_len = nonoption_flags_max_len = 0;
- else
- {
- memset (__mempcpy (new_str, __getopt_nonoption_flags,
- nonoption_flags_max_len),
- '\0', top + 1 - nonoption_flags_max_len);
- nonoption_flags_max_len = top + 1;
- __getopt_nonoption_flags = new_str;
- }
+ /* First make sure the handling of the `__getopt_nonoption_flags'
+ string can work normally. Our top argument must be in the range
+ of the string. */
+ if (nonoption_flags_len > 0 && top >= nonoption_flags_max_len) {
+ /* We must extend the array. The user plays games with us and
+ presents new arguments. */
+ char *new_str = malloc(top + 1);
+
+ if (new_str == NULL)
+ nonoption_flags_len = nonoption_flags_max_len = 0;
+ else {
+ memset(__mempcpy(new_str, __getopt_nonoption_flags, nonoption_flags_max_len), '\0', top + 1 - nonoption_flags_max_len);
+ nonoption_flags_max_len = top + 1;
+ __getopt_nonoption_flags = new_str;
+ }
}
#endif
- while (top > middle && middle > bottom)
- {
- if (top - middle > middle - bottom)
- {
- /* Bottom segment is the short one. */
- int len = middle - bottom;
- register int i;
+ while (top > middle && middle > bottom) {
+ if (top - middle > middle - bottom) {
+ /* Bottom segment is the short one. */
+ int len = middle - bottom;
+ register int i;
- /* Swap it with the top part of the top segment. */
- for (i = 0; i < len; i++)
- {
- tem = argv[bottom + i];
- argv[bottom + i] = argv[top - (middle - bottom) + i];
- argv[top - (middle - bottom) + i] = tem;
- SWAP_FLAGS (bottom + i, top - (middle - bottom) + i);
- }
- /* Exclude the moved bottom segment from further swapping. */
- top -= len;
- }
- else
- {
- /* Top segment is the short one. */
- int len = top - middle;
- register int i;
+ /* Swap it with the top part of the top segment. */
+ for (i = 0; i < len; i++) {
+ tem = argv[bottom + i];
+ argv[bottom + i] = argv[top - (middle - bottom) + i];
+ argv[top - (middle - bottom) + i] = tem;
+ SWAP_FLAGS(bottom + i, top - (middle - bottom) + i);
+ }
+ /* Exclude the moved bottom segment from further swapping. */
+ top -= len;
+ } else {
+ /* Top segment is the short one. */
+ int len = top - middle;
+ register int i;
- /* Swap it with the bottom part of the bottom segment. */
- for (i = 0; i < len; i++)
- {
- tem = argv[bottom + i];
- argv[bottom + i] = argv[middle + i];
- argv[middle + i] = tem;
- SWAP_FLAGS (bottom + i, middle + i);
- }
- /* Exclude the moved top segment from further swapping. */
- bottom += len;
- }
+ /* Swap it with the bottom part of the bottom segment. */
+ for (i = 0; i < len; i++) {
+ tem = argv[bottom + i];
+ argv[bottom + i] = argv[middle + i];
+ argv[middle + i] = tem;
+ SWAP_FLAGS(bottom + i, middle + i);
+ }
+ /* Exclude the moved top segment from further swapping. */
+ bottom += len;
+ }
}
- /* Update records for the slots the non-options now occupy. */
+ /* Update records for the slots the non-options now occupy. */
- first_nonopt += (optind - last_nonopt);
- last_nonopt = optind;
+ first_nonopt += (optind - last_nonopt);
+ last_nonopt = optind;
}
/* Initialize the internal data when the first call is made. */
#if defined __STDC__ && __STDC__
-static const char *_getopt_initialize (int, char *const *, const char *);
+static const char *_getopt_initialize(int, char *const *, const char *);
#endif
static const char *
-_getopt_initialize (argc, argv, optstring)
- int argc;
- char *const *argv;
- const char *optstring;
+_getopt_initialize(argc, argv, optstring)
+int argc;
+char *const *argv;
+const char *optstring;
{
- /* Start processing options with ARGV-element 1 (since ARGV-element 0
- is the program name); the sequence of previously skipped
- non-option ARGV-elements is empty. */
+ /* Start processing options with ARGV-element 1 (since ARGV-element 0
+ is the program name); the sequence of previously skipped
+ non-option ARGV-elements is empty. */
- first_nonopt = last_nonopt = optind;
+ first_nonopt = last_nonopt = optind;
- nextchar = NULL;
+ nextchar = NULL;
- posixly_correct = getenv ("POSIXLY_CORRECT");
+ posixly_correct = getenv("POSIXLY_CORRECT");
- /* Determine how to handle the ordering of options and nonoptions. */
+ /* Determine how to handle the ordering of options and nonoptions. */
- if (optstring[0] == '-')
- {
- ordering = RETURN_IN_ORDER;
- ++optstring;
- }
- else if (optstring[0] == '+')
- {
- ordering = REQUIRE_ORDER;
- ++optstring;
- }
- else if (posixly_correct != NULL)
- ordering = REQUIRE_ORDER;
- else
- ordering = PERMUTE;
+ if (optstring[0] == '-') {
+ ordering = RETURN_IN_ORDER;
+ ++optstring;
+ } else if (optstring[0] == '+') {
+ ordering = REQUIRE_ORDER;
+ ++optstring;
+ } else if (posixly_correct != NULL)
+ ordering = REQUIRE_ORDER;
+ else
+ ordering = PERMUTE;
#if defined _LIBC && defined USE_NONOPTION_FLAGS
- if (posixly_correct == NULL
- && argc == __libc_argc && argv == __libc_argv)
- {
- if (nonoption_flags_max_len == 0)
- {
- if (__getopt_nonoption_flags == NULL
- || __getopt_nonoption_flags[0] == '\0')
- nonoption_flags_max_len = -1;
- else
- {
- const char *orig_str = __getopt_nonoption_flags;
- int len = nonoption_flags_max_len = strlen (orig_str);
- if (nonoption_flags_max_len < argc)
- nonoption_flags_max_len = argc;
- __getopt_nonoption_flags =
- (char *) malloc (nonoption_flags_max_len);
- if (__getopt_nonoption_flags == NULL)
- nonoption_flags_max_len = -1;
- else
- memset (__mempcpy (__getopt_nonoption_flags, orig_str, len),
- '\0', nonoption_flags_max_len - len);
- }
- }
- nonoption_flags_len = nonoption_flags_max_len;
- }
- else
- nonoption_flags_len = 0;
+ if (posixly_correct == NULL && argc == __libc_argc && argv == __libc_argv) {
+ if (nonoption_flags_max_len == 0) {
+ if (__getopt_nonoption_flags == NULL || __getopt_nonoption_flags[0] == '\0')
+ nonoption_flags_max_len = -1;
+ else {
+ const char *orig_str = __getopt_nonoption_flags;
+ int len = nonoption_flags_max_len = strlen(orig_str);
+
+ if (nonoption_flags_max_len < argc)
+ nonoption_flags_max_len = argc;
+ __getopt_nonoption_flags = (char *)malloc(nonoption_flags_max_len);
+ if (__getopt_nonoption_flags == NULL)
+ nonoption_flags_max_len = -1;
+ else
+ memset(__mempcpy(__getopt_nonoption_flags, orig_str, len), '\0', nonoption_flags_max_len - len);
+ }
+ }
+ nonoption_flags_len = nonoption_flags_max_len;
+ } else
+ nonoption_flags_len = 0;
#endif
- return optstring;
+ return optstring;
}
/* Scan elements of ARGV (whose length is ARGC) for option characters
@@ -503,35 +480,35 @@
long-named options. */
int
-_getopt_internal (argc, argv, optstring, longopts, longind, long_only)
- int argc;
- char *const *argv;
- const char *optstring;
- const struct option *longopts;
- int *longind;
- int long_only;
+_getopt_internal(argc, argv, optstring, longopts, longind, long_only)
+int argc;
+char *const *argv;
+const char *optstring;
+const struct option *longopts;
+int *longind;
+int long_only;
{
- int print_errors = opterr;
- if (optstring[0] == ':')
- print_errors = 0;
+ int print_errors = opterr;
- if (argc < 1)
- return -1;
+ if (optstring[0] == ':')
+ print_errors = 0;
- optarg = NULL;
+ if (argc < 1)
+ return -1;
- if (optind == 0 || !__getopt_initialized)
- {
- if (optind == 0)
- optind = 1; /* Don't scan ARGV[0], the program name. */
- optstring = _getopt_initialize (argc, argv, optstring);
- __getopt_initialized = 1;
+ optarg = NULL;
+
+ if (optind == 0 || !__getopt_initialized) {
+ if (optind == 0)
+ optind = 1; /* Don't scan ARGV[0], the program name. */
+ optstring = _getopt_initialize(argc, argv, optstring);
+ __getopt_initialized = 1;
}
- /* Test whether ARGV[optind] points to a non-option argument.
- Either it does not have option syntax, or there is an environment flag
- from the shell indicating it is not an option. The later information
- is only used when the used in the GNU libc. */
+ /* Test whether ARGV[optind] points to a non-option argument.
+ Either it does not have option syntax, or there is an environment flag
+ from the shell indicating it is not an option. The later information
+ is only used when the used in the GNU libc. */
#if defined _LIBC && defined USE_NONOPTION_FLAGS
# define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0' \
|| (optind < nonoption_flags_len \
@@ -540,447 +517,365 @@
# define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0')
#endif
- if (nextchar == NULL || *nextchar == '\0')
- {
- /* Advance to the next ARGV-element. */
+ if (nextchar == NULL || *nextchar == '\0') {
+ /* Advance to the next ARGV-element. */
- /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been
- moved back by the user (who may also have changed the arguments). */
- if (last_nonopt > optind)
- last_nonopt = optind;
- if (first_nonopt > optind)
- first_nonopt = optind;
+ /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been
+ moved back by the user (who may also have changed the arguments). */
+ if (last_nonopt > optind)
+ last_nonopt = optind;
+ if (first_nonopt > optind)
+ first_nonopt = optind;
- if (ordering == PERMUTE)
- {
- /* If we have just processed some options following some non-options,
- exchange them so that the options come first. */
+ if (ordering == PERMUTE) {
+ /* If we have just processed some options following some non-options,
+ exchange them so that the options come first. */
- if (first_nonopt != last_nonopt && last_nonopt != optind)
- exchange ((char **) argv);
- else if (last_nonopt != optind)
- first_nonopt = optind;
+ if (first_nonopt != last_nonopt && last_nonopt != optind)
+ exchange((char **)argv);
+ else if (last_nonopt != optind)
+ first_nonopt = optind;
- /* Skip any additional non-options
- and extend the range of non-options previously skipped. */
+ /* Skip any additional non-options
+ and extend the range of non-options previously skipped. */
- while (optind < argc && NONOPTION_P)
- optind++;
- last_nonopt = optind;
- }
+ while (optind < argc && NONOPTION_P)
+ optind++;
+ last_nonopt = optind;
+ }
- /* The special ARGV-element `--' means premature end of options.
- Skip it like a null option,
- then exchange with previous non-options as if it were an option,
- then skip everything else like a non-option. */
+ /* The special ARGV-element `--' means premature end of options.
+ Skip it like a null option,
+ then exchange with previous non-options as if it were an option,
+ then skip everything else like a non-option. */
- if (optind != argc && !strcmp (argv[optind], "--"))
- {
- optind++;
+ if (optind != argc && !strcmp(argv[optind], "--")) {
+ optind++;
- if (first_nonopt != last_nonopt && last_nonopt != optind)
- exchange ((char **) argv);
- else if (first_nonopt == last_nonopt)
- first_nonopt = optind;
- last_nonopt = argc;
+ if (first_nonopt != last_nonopt && last_nonopt != optind)
+ exchange((char **)argv);
+ else if (first_nonopt == last_nonopt)
+ first_nonopt = optind;
+ last_nonopt = argc;
- optind = argc;
- }
+ optind = argc;
+ }
- /* If we have done all the ARGV-elements, stop the scan
- and back over any non-options that we skipped and permuted. */
+ /* If we have done all the ARGV-elements, stop the scan
+ and back over any non-options that we skipped and permuted. */
- if (optind == argc)
- {
- /* Set the next-arg-index to point at the non-options
- that we previously skipped, so the caller will digest them. */
- if (first_nonopt != last_nonopt)
- optind = first_nonopt;
- return -1;
- }
+ if (optind == argc) {
+ /* Set the next-arg-index to point at the non-options
+ that we previously skipped, so the caller will digest them. */
+ if (first_nonopt != last_nonopt)
+ optind = first_nonopt;
+ return -1;
+ }
- /* If we have come to a non-option and did not permute it,
- either stop the scan or describe it to the caller and pass it by. */
+ /* If we have come to a non-option and did not permute it,
+ either stop the scan or describe it to the caller and pass it by. */
- if (NONOPTION_P)
- {
- if (ordering == REQUIRE_ORDER)
- return -1;
- optarg = argv[optind++];
- return 1;
- }
+ if (NONOPTION_P) {
+ if (ordering == REQUIRE_ORDER)
+ return -1;
+ optarg = argv[optind++];
+ return 1;
+ }
- /* We have found another option-ARGV-element.
- Skip the initial punctuation. */
+ /* We have found another option-ARGV-element.
+ Skip the initial punctuation. */
- nextchar = (argv[optind] + 1
- + (longopts != NULL && argv[optind][1] == '-'));
+ nextchar = (argv[optind] + 1 + (longopts != NULL && argv[optind][1] == '-'));
}
- /* Decode the current option-ARGV-element. */
+ /* Decode the current option-ARGV-element. */
- /* Check whether the ARGV-element is a long option.
+ /* Check whether the ARGV-element is a long option.
- If long_only and the ARGV-element has the form "-f", where f is
- a valid short option, don't consider it an abbreviated form of
- a long option that starts with f. Otherwise there would be no
- way to give the -f short option.
+ If long_only and the ARGV-element has the form "-f", where f is
+ a valid short option, don't consider it an abbreviated form of
+ a long option that starts with f. Otherwise there would be no
+ way to give the -f short option.
- On the other hand, if there's a long option "fubar" and
- the ARGV-element is "-fu", do consider that an abbreviation of
- the long option, just like "--fu", and not "-f" with arg "u".
+ On the other hand, if there's a long option "fubar" and
+ the ARGV-element is "-fu", do consider that an abbreviation of
+ the long option, just like "--fu", and not "-f" with arg "u".
- This distinction seems to be the most useful approach. */
+ This distinction seems to be the most useful approach. */
- if (longopts != NULL
- && (argv[optind][1] == '-'
- || (long_only && (argv[optind][2] || !my_index (optstring, argv[optind][1])))))
- {
- char *nameend;
- const struct option *p;
- const struct option *pfound = NULL;
- int exact = 0;
- int ambig = 0;
- int indfound = -1;
- int option_index;
+ if (longopts != NULL && (argv[optind][1] == '-' || (long_only && (argv[optind][2] || !my_index(optstring, argv[optind][1]))))) {
+ char *nameend;
+ const struct option *p;
+ const struct option *pfound = NULL;
+ int exact = 0;
+ int ambig = 0;
+ int indfound = -1;
+ int option_index;
- for (nameend = nextchar; *nameend && *nameend != '='; nameend++)
- /* Do nothing. */ ;
+ for (nameend = nextchar; *nameend && *nameend != '='; nameend++)
+ /* Do nothing. */ ;
- /* Test all long options for either exact match
- or abbreviated matches. */
- for (p = longopts, option_index = 0; p->name; p++, option_index++)
- if (!strncmp (p->name, nextchar, nameend - nextchar))
- {
- if ((unsigned int) (nameend - nextchar)
- == (unsigned int) strlen (p->name))
- {
- /* Exact match found. */
- pfound = p;
- indfound = option_index;
- exact = 1;
- break;
- }
- else if (pfound == NULL)
- {
- /* First nonexact match found. */
- pfound = p;
- indfound = option_index;
- }
- else if (long_only
- || pfound->has_arg != p->has_arg
- || pfound->flag != p->flag
- || pfound->val != p->val)
- /* Second or later nonexact match found. */
- ambig = 1;
- }
+ /* Test all long options for either exact match
+ or abbreviated matches. */
+ for (p = longopts, option_index = 0; p->name; p++, option_index++)
+ if (!strncmp(p->name, nextchar, nameend - nextchar)) {
+ if ((unsigned int)(nameend - nextchar)
+ == (unsigned int)strlen(p->name)) {
+ /* Exact match found. */
+ pfound = p;
+ indfound = option_index;
+ exact = 1;
+ break;
+ } else if (pfound == NULL) {
+ /* First nonexact match found. */
+ pfound = p;
+ indfound = option_index;
+ } else if (long_only || pfound->has_arg != p->has_arg || pfound->flag != p->flag || pfound->val != p->val)
+ /* Second or later nonexact match found. */
+ ambig = 1;
+ }
- if (ambig && !exact)
- {
- if (print_errors)
- fprintf (stderr, _("%s: option `%s' is ambiguous\n"),
- argv[0], argv[optind]);
- nextchar += strlen (nextchar);
- optind++;
- optopt = 0;
- return '?';
- }
+ if (ambig && !exact) {
+ if (print_errors)
+ fprintf(stderr, _("%s: option `%s' is ambiguous\n"), argv[0], argv[optind]);
+ nextchar += strlen(nextchar);
+ optind++;
+ optopt = 0;
+ return '?';
+ }
- if (pfound != NULL)
- {
- option_index = indfound;
- optind++;
- if (*nameend)
- {
- /* Don't test has_arg with >, because some C compilers don't
- allow it to be used on enums. */
- if (pfound->has_arg)
- optarg = nameend + 1;
- else
- {
- if (print_errors)
- {
- if (argv[optind - 1][1] == '-')
- /* --option */
- fprintf (stderr,
- _("%s: option `--%s' doesn't allow an argument\n"),
- argv[0], pfound->name);
- else
- /* +option or -option */
- fprintf (stderr,
- _("%s: option `%c%s' doesn't allow an argument\n"),
- argv[0], argv[optind - 1][0], pfound->name);
- }
+ if (pfound != NULL) {
+ option_index = indfound;
+ optind++;
+ if (*nameend) {
+ /* Don't test has_arg with >, because some C compilers don't
+ allow it to be used on enums. */
+ if (pfound->has_arg)
+ optarg = nameend + 1;
+ else {
+ if (print_errors) {
+ if (argv[optind - 1][1] == '-')
+ /* --option */
+ fprintf(stderr, _("%s: option `--%s' doesn't allow an argument\n"), argv[0], pfound->name);
+ else
+ /* +option or -option */
+ fprintf(stderr, _("%s: option `%c%s' doesn't allow an argument\n"), argv[0], argv[optind - 1][0], pfound->name);
+ }
- nextchar += strlen (nextchar);
+ nextchar += strlen(nextchar);
- optopt = pfound->val;
- return '?';
- }
- }
- else if (pfound->has_arg == 1)
- {
- if (optind < argc)
- optarg = argv[optind++];
- else
- {
- if (print_errors)
- fprintf (stderr,
- _("%s: option `%s' requires an argument\n"),
- argv[0], argv[optind - 1]);
- nextchar += strlen (nextchar);
- optopt = pfound->val;
- return optstring[0] == ':' ? ':' : '?';
- }
- }
- nextchar += strlen (nextchar);
- if (longind != NULL)
- *longind = option_index;
- if (pfound->flag)
- {
- *(pfound->flag) = pfound->val;
- return 0;
- }
- return pfound->val;
- }
+ optopt = pfound->val;
+ return '?';
+ }
+ } else if (pfound->has_arg == 1) {
+ if (optind < argc)
+ optarg = argv[optind++];
+ else {
+ if (print_errors)
+ fprintf(stderr, _("%s: option `%s' requires an argument\n"), argv[0], argv[optind - 1]);
+ nextchar += strlen(nextchar);
+ optopt = pfound->val;
+ return optstring[0] == ':' ? ':' : '?';
+ }
+ }
+ nextchar += strlen(nextchar);
+ if (longind != NULL)
+ *longind = option_index;
+ if (pfound->flag) {
+ *(pfound->flag) = pfound->val;
+ return 0;
+ }
+ return pfound->val;
+ }
- /* Can't find it as a long option. If this is not getopt_long_only,
- or the option starts with '--' or is not a valid short
- option, then it's an error.
- Otherwise interpret it as a short option. */
- if (!long_only || argv[optind][1] == '-'
- || my_index (optstring, *nextchar) == NULL)
- {
- if (print_errors)
- {
- if (argv[optind][1] == '-')
- /* --option */
- fprintf (stderr, _("%s: unrecognized option `--%s'\n"),
- argv[0], nextchar);
- else
- /* +option or -option */
- fprintf (stderr, _("%s: unrecognized option `%c%s'\n"),
- argv[0], argv[optind][0], nextchar);
- }
- nextchar = (char *) "";
- optind++;
- optopt = 0;
- return '?';
- }
+ /* Can't find it as a long option. If this is not getopt_long_only,
+ or the option starts with '--' or is not a valid short
+ option, then it's an error.
+ Otherwise interpret it as a short option. */
+ if (!long_only || argv[optind][1] == '-' || my_index(optstring, *nextchar) == NULL) {
+ if (print_errors) {
+ if (argv[optind][1] == '-')
+ /* --option */
+ fprintf(stderr, _("%s: unrecognized option `--%s'\n"), argv[0], nextchar);
+ else
+ /* +option or -option */
+ fprintf(stderr, _("%s: unrecognized option `%c%s'\n"), argv[0], argv[optind][0], nextchar);
+ }
+ nextchar = (char *)"";
+ optind++;
+ optopt = 0;
+ return '?';
+ }
}
- /* Look at and handle the next short option-character. */
+ /* Look at and handle the next short option-character. */
- {
- char c = *nextchar++;
- char *temp = my_index (optstring, c);
+ {
+ char c = *nextchar++;
+ char *temp = my_index(optstring, c);
- /* Increment `optind' when we start to process its last character. */
- if (*nextchar == '\0')
- ++optind;
+ /* Increment `optind' when we start to process its last character. */
+ if (*nextchar == '\0')
+ ++optind;
- if (temp == NULL || c == ':')
- {
- if (print_errors)
- {
- if (posixly_correct)
- /* 1003.2 specifies the format of this message. */
- fprintf (stderr, _("%s: illegal option -- %c\n"),
- argv[0], c);
- else
- fprintf (stderr, _("%s: invalid option -- %c\n"),
- argv[0], c);
- }
- optopt = c;
- return '?';
- }
- /* Convenience. Treat POSIX -W foo same as long option --foo */
- if (temp[0] == 'W' && temp[1] == ';')
- {
- char *nameend;
- const struct option *p;
- const struct option *pfound = NULL;
- int exact = 0;
- int ambig = 0;
- int indfound = 0;
- int option_index;
+ if (temp == NULL || c == ':') {
+ if (print_errors) {
+ if (posixly_correct)
+ /* 1003.2 specifies the format of this message. */
+ fprintf(stderr, _("%s: illegal option -- %c\n"), argv[0], c);
+ else
+ fprintf(stderr, _("%s: invalid option -- %c\n"), argv[0], c);
+ }
+ optopt = c;
+ return '?';
+ }
+ /* Convenience. Treat POSIX -W foo same as long option --foo */
+ if (temp[0] == 'W' && temp[1] == ';') {
+ char *nameend;
+ const struct option *p;
+ const struct option *pfound = NULL;
+ int exact = 0;
+ int ambig = 0;
+ int indfound = 0;
+ int option_index;
- /* This is an option that requires an argument. */
- if (*nextchar != '\0')
- {
- optarg = nextchar;
- /* If we end this ARGV-element by taking the rest as an arg,
- we must advance to the next element now. */
- optind++;
- }
- else if (optind == argc)
- {
- if (print_errors)
- {
- /* 1003.2 specifies the format of this message. */
- fprintf (stderr, _("%s: option requires an argument -- %c\n"),
- argv[0], c);
- }
- optopt = c;
- if (optstring[0] == ':')
- c = ':';
- else
- c = '?';
- return c;
- }
- else
- /* We already incremented `optind' once;
- increment it again when taking next ARGV-elt as argument. */
- optarg = argv[optind++];
+ /* This is an option that requires an argument. */
+ if (*nextchar != '\0') {
+ optarg = nextchar;
+ /* If we end this ARGV-element by taking the rest as an arg,
+ we must advance to the next element now. */
+ optind++;
+ } else if (optind == argc) {
+ if (print_errors) {
+ /* 1003.2 specifies the format of this message. */
+ fprintf(stderr, _("%s: option requires an argument -- %c\n"), argv[0], c);
+ }
+ optopt = c;
+ if (optstring[0] == ':')
+ c = ':';
+ else
+ c = '?';
+ return c;
+ } else
+ /* We already incremented `optind' once;
+ increment it again when taking next ARGV-elt as argument. */
+ optarg = argv[optind++];
- /* optarg is now the argument, see if it's in the
- table of longopts. */
+ /* optarg is now the argument, see if it's in the
+ table of longopts. */
- for (nextchar = nameend = optarg; *nameend && *nameend != '='; nameend++)
- /* Do nothing. */ ;
+ for (nextchar = nameend = optarg; *nameend && *nameend != '='; nameend++)
+ /* Do nothing. */ ;
- /* Test all long options for either exact match
- or abbreviated matches. */
- for (p = longopts, option_index = 0; p->name; p++, option_index++)
- if (!strncmp (p->name, nextchar, nameend - nextchar))
- {
- if ((unsigned int) (nameend - nextchar) == strlen (p->name))
- {
- /* Exact match found. */
- pfound = p;
- indfound = option_index;
- exact = 1;
- break;
- }
- else if (pfound == NULL)
- {
- /* First nonexact match found. */
- pfound = p;
- indfound = option_index;
- }
- else
- /* Second or later nonexact match found. */
- ambig = 1;
- }
- if (ambig && !exact)
- {
- if (print_errors)
- fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"),
- argv[0], argv[optind]);
- nextchar += strlen (nextchar);
- optind++;
- return '?';
- }
- if (pfound != NULL)
- {
- option_index = indfound;
- if (*nameend)
- {
- /* Don't test has_arg with >, because some C compilers don't
- allow it to be used on enums. */
- if (pfound->has_arg)
- optarg = nameend + 1;
- else
- {
- if (print_errors)
- fprintf (stderr, _("\
-%s: option `-W %s' doesn't allow an argument\n"),
- argv[0], pfound->name);
+ /* Test all long options for either exact match
+ or abbreviated matches. */
+ for (p = longopts, option_index = 0; p->name; p++, option_index++)
+ if (!strncmp(p->name, nextchar, nameend - nextchar)) {
+ if ((unsigned int)(nameend - nextchar) == strlen(p->name)) {
+ /* Exact match found. */
+ pfound = p;
+ indfound = option_index;
+ exact = 1;
+ break;
+ } else if (pfound == NULL) {
+ /* First nonexact match found. */
+ pfound = p;
+ indfound = option_index;
+ } else
+ /* Second or later nonexact match found. */
+ ambig = 1;
+ }
+ if (ambig && !exact) {
+ if (print_errors)
+ fprintf(stderr, _("%s: option `-W %s' is ambiguous\n"), argv[0], argv[optind]);
+ nextchar += strlen(nextchar);
+ optind++;
+ return '?';
+ }
+ if (pfound != NULL) {
+ option_index = indfound;
+ if (*nameend) {
+ /* Don't test has_arg with >, because some C compilers don't
+ allow it to be used on enums. */
+ if (pfound->has_arg)
+ optarg = nameend + 1;
+ else {
+ if (print_errors)
+ fprintf(stderr, _("\
+%s: option `-W %s' doesn't allow an argument\n"), argv[0], pfound->name);
- nextchar += strlen (nextchar);
- return '?';
- }
- }
- else if (pfound->has_arg == 1)
- {
- if (optind < argc)
- optarg = argv[optind++];
- else
- {
- if (print_errors)
- fprintf (stderr,
- _("%s: option `%s' requires an argument\n"),
- argv[0], argv[optind - 1]);
- nextchar += strlen (nextchar);
- return optstring[0] == ':' ? ':' : '?';
- }
- }
- nextchar += strlen (nextchar);
- if (longind != NULL)
- *longind = option_index;
- if (pfound->flag)
- {
- *(pfound->flag) = pfound->val;
- return 0;
- }
- return pfound->val;
- }
- nextchar = NULL;
- return 'W'; /* Let the application handle it. */
- }
- if (temp[1] == ':')
- {
- if (temp[2] == ':')
- {
- /* This is an option that accepts an argument optionally. */
- if (*nextchar != '\0')
- {
- optarg = nextchar;
- optind++;
- }
- else
- optarg = NULL;
- nextchar = NULL;
- }
- else
- {
- /* This is an option that requires an argument. */
- if (*nextchar != '\0')
- {
- optarg = nextchar;
- /* If we end this ARGV-element by taking the rest as an arg,
- we must advance to the next element now. */
- optind++;
- }
- else if (optind == argc)
- {
- if (print_errors)
- {
- /* 1003.2 specifies the format of this message. */
- fprintf (stderr,
- _("%s: option requires an argument -- %c\n"),
- argv[0], c);
- }
- optopt = c;
- if (optstring[0] == ':')
- c = ':';
- else
- c = '?';
- }
- else
- /* We already incremented `optind' once;
- increment it again when taking next ARGV-elt as argument. */
- optarg = argv[optind++];
- nextchar = NULL;
- }
- }
- return c;
- }
-}
+ nextchar += strlen(nextchar);
+ return '?';
+ }
+ } else if (pfound->has_arg == 1) {
+ if (optind < argc)
+ optarg = argv[optind++];
+ else {
+ if (print_errors)
+ fprintf(stderr, _("%s: option `%s' requires an argument\n"), argv[0], argv[optind - 1]);
+ nextchar += strlen(nextchar);
+ return optstring[0] == ':' ? ':' : '?';
+ }
+ }
+ nextchar += strlen(nextchar);
+ if (longind != NULL)
+ *longind = option_index;
+ if (pfound->flag) {
+ *(pfound->flag) = pfound->val;
+ return 0;
+ }
+ return pfound->val;
+ }
+ nextchar = NULL;
+ return 'W'; /* Let the application handle it. */
+ }
+ if (temp[1] == ':') {
+ if (temp[2] == ':') {
+ /* This is an option that accepts an argument optionally. */
+ if (*nextchar != '\0') {
+ optarg = nextchar;
+ optind++;
+ } else
+ optarg = NULL;
+ nextchar = NULL;
+ } else {
+ /* This is an option that requires an argument. */
+ if (*nextchar != '\0') {
+ optarg = nextchar;
+ /* If we end this ARGV-element by taking the rest as an arg,
+ we must advance to the next element now. */
+ optind++;
+ } else if (optind == argc) {
+ if (print_errors) {
+ /* 1003.2 specifies the format of this message. */
+ fprintf(stderr, _("%s: option requires an argument -- %c\n"), argv[0], c);
+ }
+ optopt = c;
+ if (optstring[0] == ':')
+ c = ':';
+ else
+ c = '?';
+ } else
+ /* We already incremented `optind' once;
+ increment it again when taking next ARGV-elt as argument. */
+ optarg = argv[optind++];
+ nextchar = NULL;
+ }
+ }
+ return c;
+ }
+}
int
-getopt (argc, argv, optstring)
- int argc;
- char *const *argv;
- const char *optstring;
+getopt(argc, argv, optstring)
+int argc;
+char *const *argv;
+const char *optstring;
{
- return _getopt_internal (argc, argv, optstring,
- (const struct option *) 0,
- (int *) 0,
- 0);
+ return _getopt_internal(argc, argv, optstring, (const struct option *)0, (int *)0, 0);
}
-#endif /* Not ELIDE_CODE. */
+#endif /* Not ELIDE_CODE. */
#ifdef TEST
@@ -988,68 +883,65 @@
the above definition of `getopt'. */
int
-main (argc, argv)
- int argc;
- char **argv;
+main(argc, argv)
+int argc;
+char **argv;
{
- int c;
- int digit_optind = 0;
+ int c;
+ int digit_optind = 0;
- while (1)
- {
- int this_option_optind = optind ? optind : 1;
+ while (1) {
+ int this_option_optind = optind ? optind : 1;
- c = getopt (argc, argv, "abc:d:0123456789");
- if (c == -1)
- break;
+ c = getopt(argc, argv, "abc:d:0123456789");
+ if (c == -1)
+ break;
- switch (c)
- {
- case '0':
- case '1':
- case '2':
- case '3':
- case '4':
- case '5':
- case '6':
- case '7':
- case '8':
- case '9':
- if (digit_optind != 0 && digit_optind != this_option_optind)
- printf ("digits occur in two different argv-elements.\n");
- digit_optind = this_option_optind;
- printf ("option %c\n", c);
- break;
+ switch (c) {
+ case '0':
+ case '1':
+ case '2':
+ case '3':
+ case '4':
+ case '5':
+ case '6':
+ case '7':
+ case '8':
+ case '9':
+ if (digit_optind != 0 && digit_optind != this_option_optind)
+ printf("digits occur in two different argv-elements.\n");
+ digit_optind = this_option_optind;
+ printf("option %c\n", c);
+ break;
- case 'a':
- printf ("option a\n");
- break;
+ case 'a':
+ printf("option a\n");
+ break;
- case 'b':
- printf ("option b\n");
- break;
+ case 'b':
+ printf("option b\n");
+ break;
- case 'c':
- printf ("option c with value `%s'\n", optarg);
- break;
+ case 'c':
+ printf("option c with value `%s'\n", optarg);
+ break;
- case '?':
- break;
+ case '?':
+ break;
- default:
- printf ("?? getopt returned character code 0%o ??\n", c);
- }
+ default:
+ printf("?? getopt returned character code 0%o ??\n", c);
+ }
}
- if (optind < argc)
- {
- printf ("non-option ARGV-elements: ");
- while (optind < argc)
- printf ("%s ", argv[optind++]);
- printf ("\n");
+ if (optind < argc) {
+ printf("non-option ARGV-elements: ");
+ while (optind < argc)
+ printf("%s ", argv[optind++]);
+ printf("\n");
}
- exit (0);
+ exit(0);
}
#endif /* TEST */
--- a/getopt.h
+++ b/getopt.h
@@ -35,7 +35,8 @@
#endif
#ifdef __cplusplus
-extern "C" {
+extern "C"
+{
#endif
/* For communication from `getopt' to the caller.
@@ -91,18 +92,17 @@
one). For long options that have a zero `flag' field, `getopt'
returns the contents of the `val' field. */
-struct option
-{
+struct option {
# if (defined __STDC__ && __STDC__) || defined __cplusplus
- const char *name;
+ const char *name;
# else
- char *name;
+ char *name;
# endif
- /* has_arg can't be an enum because some compilers complain about
- type mismatches in all the code that assumes it is an int. */
- int has_arg;
- int *flag;
- int val;
+ /* has_arg can't be an enum because some compilers complain about
+ type mismatches in all the code that assumes it is an int. */
+ int has_arg;
+ int *flag;
+ int val;
};
/* Names for the values of the `has_arg' field of `struct option'. */
@@ -110,9 +110,8 @@
# define no_argument 0
# define required_argument 1
# define optional_argument 2
-#endif /* need getopt */
+#endif /* need getopt */
-
/* Get definitions and prototypes for functions to process the
arguments in ARGV (ARGC of them, minus the program name) for
options given in OPTS.
@@ -142,33 +141,27 @@
/* Many other libraries have conflicting prototypes for getopt, with
differences in the consts, in stdlib.h. To avoid compilation
errors, only prototype getopt for the GNU C library. */
-extern int getopt (int __argc, char *const *__argv, const char *__shortopts);
-# else /* not __GNU_LIBRARY__ */
-extern int getopt ();
-# endif /* __GNU_LIBRARY__ */
+extern int getopt(int __argc, char *const *__argv, const char *__shortopts);
+# else /* not __GNU_LIBRARY__ */
+extern int getopt();
+# endif /* __GNU_LIBRARY__ */
# ifndef __need_getopt
-extern int getopt_long (int __argc, char *const *__argv, const char *__shortopts,
- const struct option *__longopts, int *__longind);
-extern int getopt_long_only (int __argc, char *const *__argv,
- const char *__shortopts,
- const struct option *__longopts, int *__longind);
+extern int getopt_long(int __argc, char *const *__argv, const char *__shortopts, const struct option *__longopts, int *__longind);
+extern int getopt_long_only(int __argc, char *const *__argv, const char *__shortopts, const struct option *__longopts, int *__longind);
/* Internal only. Users should not call this directly. */
-extern int _getopt_internal (int __argc, char *const *__argv,
- const char *__shortopts,
- const struct option *__longopts, int *__longind,
- int __long_only);
+extern int _getopt_internal(int __argc, char *const *__argv, const char *__shortopts, const struct option *__longopts, int *__longind, int __long_only);
# endif
-#else /* not __STDC__ */
-extern int getopt ();
+#else /* not __STDC__ */
+extern int getopt();
# ifndef __need_getopt
-extern int getopt_long ();
-extern int getopt_long_only ();
+extern int getopt_long();
+extern int getopt_long_only();
-extern int _getopt_internal ();
+extern int _getopt_internal();
# endif
-#endif /* __STDC__ */
+#endif /* __STDC__ */
#ifdef __cplusplus
}
@@ -177,4 +170,4 @@
/* Make sure we later can get all the definitions and declarations. */
#undef __need_getopt
-#endif /* getopt.h */
+#endif /* getopt.h */
--- a/getopt1.c
+++ b/getopt1.c
@@ -52,7 +52,6 @@
#ifndef ELIDE_CODE
-
/* This needs to come after some library #include
to get __GNU_LIBRARY__ defined. */
#ifdef __GNU_LIBRARY__
@@ -64,14 +63,14 @@
#endif
int
-getopt_long (argc, argv, options, long_options, opt_index)
- int argc;
- char *const *argv;
- const char *options;
- const struct option *long_options;
- int *opt_index;
+getopt_long(argc, argv, options, long_options, opt_index)
+int argc;
+char *const *argv;
+const char *options;
+const struct option *long_options;
+int *opt_index;
{
- return _getopt_internal (argc, argv, options, long_options, opt_index, 0);
+ return _getopt_internal(argc, argv, options, long_options, opt_index, 0);
}
/* Like getopt_long, but '-' as well as '--' can indicate a long option.
@@ -80,18 +79,17 @@
instead. */
int
-getopt_long_only (argc, argv, options, long_options, opt_index)
- int argc;
- char *const *argv;
- const char *options;
- const struct option *long_options;
- int *opt_index;
+getopt_long_only(argc, argv, options, long_options, opt_index)
+int argc;
+char *const *argv;
+const char *options;
+const struct option *long_options;
+int *opt_index;
{
- return _getopt_internal (argc, argv, options, long_options, opt_index, 1);
+ return _getopt_internal(argc, argv, options, long_options, opt_index, 1);
}
-
-#endif /* Not ELIDE_CODE. */
+#endif /* Not ELIDE_CODE. */
#ifdef TEST
@@ -98,91 +96,87 @@
#include <stdio.h>
int
-main (argc, argv)
- int argc;
- char **argv;
+main(argc, argv)
+int argc;
+char **argv;
{
- int c;
- int digit_optind = 0;
+ int c;
+ int digit_optind = 0;
- while (1)
- {
- int this_option_optind = optind ? optind : 1;
- int option_index = 0;
- static struct option long_options[] =
- {
- {"add", 1, 0, 0},
- {"append", 0, 0, 0},
- {"delete", 1, 0, 0},
- {"verbose", 0, 0, 0},
- {"create", 0, 0, 0},
- {"file", 1, 0, 0},
- {0, 0, 0, 0}
- };
+ while (1) {
+ int this_option_optind = optind ? optind : 1;
+ int option_index = 0;
- c = getopt_long (argc, argv, "abc:d:0123456789",
- long_options, &option_index);
- if (c == -1)
- break;
+ static struct option long_options[] = {
+ {"add", 1, 0, 0},
+ {"append", 0, 0, 0},
+ {"delete", 1, 0, 0},
+ {"verbose", 0, 0, 0},
+ {"create", 0, 0, 0},
+ {"file", 1, 0, 0},
+ {0, 0, 0, 0}
+ };
- switch (c)
- {
- case 0:
- printf ("option %s", long_options[option_index].name);
- if (optarg)
- printf (" with arg %s", optarg);
- printf ("\n");
- break;
+ c = getopt_long(argc, argv, "abc:d:0123456789", long_options, &option_index);
+ if (c == -1)
+ break;
- case '0':
- case '1':
- case '2':
- case '3':
- case '4':
- case '5':
- case '6':
- case '7':
- case '8':
- case '9':
- if (digit_optind != 0 && digit_optind != this_option_optind)
- printf ("digits occur in two different argv-elements.\n");
- digit_optind = this_option_optind;
- printf ("option %c\n", c);
- break;
+ switch (c) {
+ case 0:
+ printf("option %s", long_options[option_index].name);
+ if (optarg)
+ printf(" with arg %s", optarg);
+ printf("\n");
+ break;
- case 'a':
- printf ("option a\n");
- break;
+ case '0':
+ case '1':
+ case '2':
+ case '3':
+ case '4':
+ case '5':
+ case '6':
+ case '7':
+ case '8':
+ case '9':
+ if (digit_optind != 0 && digit_optind != this_option_optind)
+ printf("digits occur in two different argv-elements.\n");
+ digit_optind = this_option_optind;
+ printf("option %c\n", c);
+ break;
- case 'b':
- printf ("option b\n");
- break;
+ case 'a':
+ printf("option a\n");
+ break;
- case 'c':
- printf ("option c with value `%s'\n", optarg);
- break;
+ case 'b':
+ printf("option b\n");
+ break;
- case 'd':
- printf ("option d with value `%s'\n", optarg);
- break;
+ case 'c':
+ printf("option c with value `%s'\n", optarg);
+ break;
- case '?':
- break;
+ case 'd':
+ printf("option d with value `%s'\n", optarg);
+ break;
- default:
- printf ("?? getopt returned character code 0%o ??\n", c);
- }
+ case '?':
+ break;
+
+ default:
+ printf("?? getopt returned character code 0%o ??\n", c);
+ }
}
- if (optind < argc)
- {
- printf ("non-option ARGV-elements: ");
- while (optind < argc)
- printf ("%s ", argv[optind++]);
- printf ("\n");
+ if (optind < argc) {
+ printf("non-option ARGV-elements: ");
+ while (optind < argc)
+ printf("%s ", argv[optind++]);
+ printf("\n");
}
- exit (0);
+ exit(0);
}
#endif /* TEST */
--- a/jbig2.c
+++ b/jbig2.c
@@ -17,7 +17,6 @@
jbig2dec
*/
-
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
@@ -32,49 +31,48 @@
#include "jbig2_priv.h"
static void *
-jbig2_default_alloc (Jbig2Allocator *allocator, size_t size)
+jbig2_default_alloc(Jbig2Allocator *allocator, size_t size)
{
- return malloc (size);
+ return malloc(size);
}
static void
-jbig2_default_free (Jbig2Allocator *allocator, void *p)
+jbig2_default_free(Jbig2Allocator *allocator, void *p)
{
- free (p);
+ free(p);
}
static void *
-jbig2_default_realloc (Jbig2Allocator *allocator, void *p, size_t size)
+jbig2_default_realloc(Jbig2Allocator *allocator, void *p, size_t size)
{
- return realloc (p, size);
+ return realloc(p, size);
}
-static Jbig2Allocator jbig2_default_allocator =
-{
- jbig2_default_alloc,
- jbig2_default_free,
- jbig2_default_realloc
+static Jbig2Allocator jbig2_default_allocator = {
+ jbig2_default_alloc,
+ jbig2_default_free,
+ jbig2_default_realloc
};
void *
-jbig2_alloc (Jbig2Allocator *allocator, size_t size, size_t num)
+jbig2_alloc(Jbig2Allocator *allocator, size_t size, size_t num)
{
- /* check for integer multiplication overflow */
- if (num > 0 && size >= (size_t)-0x100 / num)
- return NULL;
- return allocator->alloc(allocator, size * num);
+ /* check for integer multiplication overflow */
+ if (num > 0 && size >= (size_t) - 0x100 / num)
+ return NULL;
+ return allocator->alloc(allocator, size * num);
}
/* jbig2_free and jbig2_realloc moved to the bottom of this file */
static int
-jbig2_default_error(void *data, const char *msg,
- Jbig2Severity severity, int32_t seg_idx)
+jbig2_default_error(void *data, const char *msg, Jbig2Severity severity, int32_t seg_idx)
{
/* report only fatal errors by default */
if (severity == JBIG2_SEVERITY_FATAL) {
fprintf(stderr, "jbig2 decoder FATAL ERROR: %s", msg);
- if (seg_idx != -1) fprintf(stderr, " (segment 0x%02x)", seg_idx);
+ if (seg_idx != -1)
+ fprintf(stderr, " (segment 0x%02x)", seg_idx);
fprintf(stderr, "\n");
fflush(stderr);
}
@@ -83,89 +81,80 @@
}
int
-jbig2_error(Jbig2Ctx *ctx, Jbig2Severity severity, int32_t segment_number,
- const char *fmt, ...)
+jbig2_error(Jbig2Ctx *ctx, Jbig2Severity severity, int32_t segment_number, const char *fmt, ...)
{
- char buf[1024];
- va_list ap;
- int n;
- int code;
+ char buf[1024];
+ va_list ap;
+ int n;
+ int code;
- va_start (ap, fmt);
- n = vsnprintf (buf, sizeof(buf), fmt, ap);
- va_end (ap);
- if (n < 0 || n == sizeof(buf))
- strncpy (buf, "jbig2_error: error in generating error string", sizeof(buf));
- code = ctx->error_callback (ctx->error_callback_data, buf, severity, segment_number);
- if (severity == JBIG2_SEVERITY_FATAL)
- code = -1;
- return code;
+ va_start(ap, fmt);
+ n = vsnprintf(buf, sizeof(buf), fmt, ap);
+ va_end(ap);
+ if (n < 0 || n == sizeof(buf))
+ strncpy(buf, "jbig2_error: error in generating error string", sizeof(buf));
+ code = ctx->error_callback(ctx->error_callback_data, buf, severity, segment_number);
+ if (severity == JBIG2_SEVERITY_FATAL)
+ code = -1;
+ return code;
}
Jbig2Ctx *
-jbig2_ctx_new (Jbig2Allocator *allocator,
- Jbig2Options options,
- Jbig2GlobalCtx *global_ctx,
- Jbig2ErrorCallback error_callback,
- void *error_callback_data)
+jbig2_ctx_new(Jbig2Allocator *allocator, Jbig2Options options, Jbig2GlobalCtx *global_ctx, Jbig2ErrorCallback error_callback, void *error_callback_data)
{
- Jbig2Ctx *result;
+ Jbig2Ctx *result;
- if (allocator == NULL)
- allocator = &jbig2_default_allocator;
- if (error_callback == NULL)
- error_callback = &jbig2_default_error;
+ if (allocator == NULL)
+ allocator = &jbig2_default_allocator;
+ if (error_callback == NULL)
+ error_callback = &jbig2_default_error;
- result = (Jbig2Ctx*)jbig2_alloc(allocator, sizeof(Jbig2Ctx), 1);
- if (result == NULL) {
- error_callback(error_callback_data, "initial context allocation failed!",
- JBIG2_SEVERITY_FATAL, -1);
- return result;
- }
+ result = (Jbig2Ctx *) jbig2_alloc(allocator, sizeof(Jbig2Ctx), 1);
+ if (result == NULL) {
+ error_callback(error_callback_data, "initial context allocation failed!", JBIG2_SEVERITY_FATAL, -1);
+ return result;
+ }
- result->allocator = allocator;
- result->options = options;
- result->global_ctx = (const Jbig2Ctx *)global_ctx;
- result->error_callback = error_callback;
- result->error_callback_data = error_callback_data;
+ result->allocator = allocator;
+ result->options = options;
+ result->global_ctx = (const Jbig2Ctx *)global_ctx;
+ result->error_callback = error_callback;
+ result->error_callback_data = error_callback_data;
- result->state = (options & JBIG2_OPTIONS_EMBEDDED) ?
- JBIG2_FILE_SEQUENTIAL_HEADER :
- JBIG2_FILE_HEADER;
+ result->state = (options & JBIG2_OPTIONS_EMBEDDED) ? JBIG2_FILE_SEQUENTIAL_HEADER : JBIG2_FILE_HEADER;
- result->buf = NULL;
+ result->buf = NULL;
- result->n_segments = 0;
- result->n_segments_max = 16;
- result->segments = jbig2_new(result, Jbig2Segment*, result->n_segments_max);
- if (result->segments == NULL) {
- error_callback(error_callback_data, "initial segments allocation failed!",
- JBIG2_SEVERITY_FATAL, -1);
- jbig2_free(allocator, result);
- return result;
- }
- result->segment_index = 0;
+ result->n_segments = 0;
+ result->n_segments_max = 16;
+ result->segments = jbig2_new(result, Jbig2Segment *, result->n_segments_max);
+ if (result->segments == NULL) {
+ error_callback(error_callback_data, "initial segments allocation failed!", JBIG2_SEVERITY_FATAL, -1);
+ jbig2_free(allocator, result);
+ return result;
+ }
+ result->segment_index = 0;
- result->current_page = 0;
- result->max_page_index = 4;
- result->pages = jbig2_new(result, Jbig2Page, result->max_page_index);
- if (result->pages == NULL) {
- error_callback(error_callback_data, "initial pages allocation failed!",
- JBIG2_SEVERITY_FATAL, -1);
- jbig2_free(allocator, result->segments);
- jbig2_free(allocator, result);
- return result;
- }
- {
- int index;
- for (index = 0; index < result->max_page_index; index++) {
- result->pages[index].state = JBIG2_PAGE_FREE;
- result->pages[index].number = 0;
- result->pages[index].image = NULL;
+ result->current_page = 0;
+ result->max_page_index = 4;
+ result->pages = jbig2_new(result, Jbig2Page, result->max_page_index);
+ if (result->pages == NULL) {
+ error_callback(error_callback_data, "initial pages allocation failed!", JBIG2_SEVERITY_FATAL, -1);
+ jbig2_free(allocator, result->segments);
+ jbig2_free(allocator, result);
+ return result;
}
- }
+ {
+ int index;
- return result;
+ for (index = 0; index < result->max_page_index; index++) {
+ result->pages[index].state = JBIG2_PAGE_FREE;
+ result->pages[index].number = 0;
+ result->pages[index].image = NULL;
+ }
+ }
+
+ return result;
}
#define get_uint16(bptr)\
@@ -175,29 +164,28 @@
int16_t
jbig2_get_int16(const byte *bptr)
-{
+{
return get_int16(bptr);
}
uint16_t
jbig2_get_uint16(const byte *bptr)
-{
+{
return get_uint16(bptr);
}
int32_t
jbig2_get_int32(const byte *bptr)
-{
- return ((int32_t)get_int16(bptr) << 16) | get_uint16(bptr + 2);
+{
+ return ((int32_t) get_int16(bptr) << 16) | get_uint16(bptr + 2);
}
uint32_t
jbig2_get_uint32(const byte *bptr)
-{
- return ((uint32_t)get_uint16(bptr) << 16) | get_uint16(bptr + 2);
+{
+ return ((uint32_t) get_uint16(bptr) << 16) | get_uint16(bptr + 2);
}
-
/**
* jbig2_data_in: submit data for decoding
* @ctx: The jbig2dec decoder context
@@ -212,203 +200,173 @@
* the error handling callback returns
**/
int
-jbig2_data_in (Jbig2Ctx *ctx, const unsigned char *data, size_t size)
+jbig2_data_in(Jbig2Ctx *ctx, const unsigned char *data, size_t size)
{
- const size_t initial_buf_size = 1024;
+ const size_t initial_buf_size = 1024;
- if (ctx->buf == NULL)
- {
- size_t buf_size = initial_buf_size;
+ if (ctx->buf == NULL) {
+ size_t buf_size = initial_buf_size;
- do
- buf_size <<= 1;
- while (buf_size < size);
- ctx->buf = jbig2_new(ctx, byte, buf_size);
- if (ctx->buf == NULL)
- {
- return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1,
- "failed to allocate ctx->buf in jbig2_data_in");
- }
- ctx->buf_size = buf_size;
- ctx->buf_rd_ix = 0;
- ctx->buf_wr_ix = 0;
- }
- else if (ctx->buf_wr_ix + size > ctx->buf_size)
- {
- if (ctx->buf_rd_ix <= (ctx->buf_size >> 1) &&
- ctx->buf_wr_ix - ctx->buf_rd_ix + size <= ctx->buf_size)
- {
- memmove(ctx->buf, ctx->buf + ctx->buf_rd_ix,
- ctx->buf_wr_ix - ctx->buf_rd_ix);
- }
- else
- {
- byte *buf;
- size_t buf_size = initial_buf_size;
+ do
+ buf_size <<= 1;
+ while (buf_size < size);
+ ctx->buf = jbig2_new(ctx, byte, buf_size);
+ if (ctx->buf == NULL) {
+ return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "failed to allocate ctx->buf in jbig2_data_in");
+ }
+ ctx->buf_size = buf_size;
+ ctx->buf_rd_ix = 0;
+ ctx->buf_wr_ix = 0;
+ } else if (ctx->buf_wr_ix + size > ctx->buf_size) {
+ if (ctx->buf_rd_ix <= (ctx->buf_size >> 1) && ctx->buf_wr_ix - ctx->buf_rd_ix + size <= ctx->buf_size) {
+ memmove(ctx->buf, ctx->buf + ctx->buf_rd_ix, ctx->buf_wr_ix - ctx->buf_rd_ix);
+ } else {
+ byte *buf;
+ size_t buf_size = initial_buf_size;
- do
- buf_size <<= 1;
- while (buf_size < ctx->buf_wr_ix - ctx->buf_rd_ix + size);
- buf = jbig2_new(ctx, byte, buf_size);
- if (buf == NULL)
- {
- return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1,
- "failed to allocate buf in jbig2_data_in");
- }
- memcpy(buf, ctx->buf + ctx->buf_rd_ix,
- ctx->buf_wr_ix - ctx->buf_rd_ix);
- jbig2_free(ctx->allocator, ctx->buf);
- ctx->buf = buf;
- ctx->buf_size = buf_size;
- }
- ctx->buf_wr_ix -= ctx->buf_rd_ix;
- ctx->buf_rd_ix = 0;
+ do
+ buf_size <<= 1;
+ while (buf_size < ctx->buf_wr_ix - ctx->buf_rd_ix + size);
+ buf = jbig2_new(ctx, byte, buf_size);
+ if (buf == NULL) {
+ return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "failed to allocate buf in jbig2_data_in");
+ }
+ memcpy(buf, ctx->buf + ctx->buf_rd_ix, ctx->buf_wr_ix - ctx->buf_rd_ix);
+ jbig2_free(ctx->allocator, ctx->buf);
+ ctx->buf = buf;
+ ctx->buf_size = buf_size;
+ }
+ ctx->buf_wr_ix -= ctx->buf_rd_ix;
+ ctx->buf_rd_ix = 0;
}
- memcpy(ctx->buf + ctx->buf_wr_ix, data, size);
- ctx->buf_wr_ix += size;
+ memcpy(ctx->buf + ctx->buf_wr_ix, data, size);
+ ctx->buf_wr_ix += size;
- /* data has now been added to buffer */
+ /* data has now been added to buffer */
- for (;;)
- {
- const byte jbig2_id_string[8] = { 0x97, 0x4a, 0x42, 0x32, 0x0d, 0x0a, 0x1a, 0x0a };
- Jbig2Segment *segment;
- size_t header_size;
- int code;
+ for (;;) {
+ const byte jbig2_id_string[8] = { 0x97, 0x4a, 0x42, 0x32, 0x0d, 0x0a, 0x1a, 0x0a };
+ Jbig2Segment *segment;
+ size_t header_size;
+ int code;
- switch (ctx->state)
- {
- case JBIG2_FILE_HEADER:
- /* D.4.1 */
- if (ctx->buf_wr_ix - ctx->buf_rd_ix < 9)
- return 0;
- if (memcmp(ctx->buf + ctx->buf_rd_ix, jbig2_id_string, 8))
- return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1,
- "Not a JBIG2 file header");
- /* D.4.2 */
- ctx->file_header_flags = ctx->buf[ctx->buf_rd_ix + 8];
- if (ctx->file_header_flags & 0xFC) {
- jbig2_error(ctx, JBIG2_SEVERITY_WARNING, -1,
- "reserved bits (2-7) of file header flags are not zero (0x%02x)", ctx->file_header_flags);
- }
- /* D.4.3 */
- if (!(ctx->file_header_flags & 2)) /* number of pages is known */
- {
- if (ctx->buf_wr_ix - ctx->buf_rd_ix < 13)
- return 0;
- ctx->n_pages = jbig2_get_uint32(ctx->buf + ctx->buf_rd_ix + 9);
- ctx->buf_rd_ix += 13;
- if (ctx->n_pages == 1)
- jbig2_error(ctx, JBIG2_SEVERITY_INFO, -1, "file header indicates a single page document");
- else
- jbig2_error(ctx, JBIG2_SEVERITY_INFO, -1, "file header indicates a %d page document", ctx->n_pages);
- }
- else /* number of pages not known */
- {
- ctx->n_pages=0;
- ctx->buf_rd_ix += 9;
+ switch (ctx->state) {
+ case JBIG2_FILE_HEADER:
+ /* D.4.1 */
+ if (ctx->buf_wr_ix - ctx->buf_rd_ix < 9)
+ return 0;
+ if (memcmp(ctx->buf + ctx->buf_rd_ix, jbig2_id_string, 8))
+ return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "Not a JBIG2 file header");
+ /* D.4.2 */
+ ctx->file_header_flags = ctx->buf[ctx->buf_rd_ix + 8];
+ if (ctx->file_header_flags & 0xFC) {
+ jbig2_error(ctx, JBIG2_SEVERITY_WARNING, -1, "reserved bits (2-7) of file header flags are not zero (0x%02x)", ctx->file_header_flags);
}
- /* determine the file organization based on the flags - D.4.2 again */
- if (ctx->file_header_flags & 1)
- {
- ctx->state = JBIG2_FILE_SEQUENTIAL_HEADER;
- jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, -1, "file header indicates sequential organization");
- }
- else
- {
- ctx->state = JBIG2_FILE_RANDOM_HEADERS;
- jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, -1, "file header indicates random-access organization");
+ /* D.4.3 */
+ if (!(ctx->file_header_flags & 2)) { /* number of pages is known */
+ if (ctx->buf_wr_ix - ctx->buf_rd_ix < 13)
+ return 0;
+ ctx->n_pages = jbig2_get_uint32(ctx->buf + ctx->buf_rd_ix + 9);
+ ctx->buf_rd_ix += 13;
+ if (ctx->n_pages == 1)
+ jbig2_error(ctx, JBIG2_SEVERITY_INFO, -1, "file header indicates a single page document");
+ else
+ jbig2_error(ctx, JBIG2_SEVERITY_INFO, -1, "file header indicates a %d page document", ctx->n_pages);
+ } else { /* number of pages not known */
+ ctx->n_pages = 0;
+ ctx->buf_rd_ix += 9;
}
- break;
- case JBIG2_FILE_SEQUENTIAL_HEADER:
- case JBIG2_FILE_RANDOM_HEADERS:
- segment = jbig2_parse_segment_header(ctx, ctx->buf + ctx->buf_rd_ix,
- ctx->buf_wr_ix - ctx->buf_rd_ix,
- &header_size);
- if (segment == NULL)
- return 0; /* need more data */
- ctx->buf_rd_ix += header_size;
+ /* determine the file organization based on the flags - D.4.2 again */
+ if (ctx->file_header_flags & 1) {
+ ctx->state = JBIG2_FILE_SEQUENTIAL_HEADER;
+ jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, -1, "file header indicates sequential organization");
+ } else {
+ ctx->state = JBIG2_FILE_RANDOM_HEADERS;
+ jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, -1, "file header indicates random-access organization");
- if (ctx->n_segments == ctx->n_segments_max)
- ctx->segments = jbig2_renew(ctx, ctx->segments, Jbig2Segment*,
- (ctx->n_segments_max <<= 2));
+ }
+ break;
+ case JBIG2_FILE_SEQUENTIAL_HEADER:
+ case JBIG2_FILE_RANDOM_HEADERS:
+ segment = jbig2_parse_segment_header(ctx, ctx->buf + ctx->buf_rd_ix, ctx->buf_wr_ix - ctx->buf_rd_ix, &header_size);
+ if (segment == NULL)
+ return 0; /* need more data */
+ ctx->buf_rd_ix += header_size;
- ctx->segments[ctx->n_segments++] = segment;
- if (ctx->state == JBIG2_FILE_RANDOM_HEADERS)
- {
- if ((segment->flags & 63) == 51) /* end of file */
- ctx->state = JBIG2_FILE_RANDOM_BODIES;
- }
- else /* JBIG2_FILE_SEQUENTIAL_HEADER */
- ctx->state = JBIG2_FILE_SEQUENTIAL_BODY;
- break;
- case JBIG2_FILE_SEQUENTIAL_BODY:
- case JBIG2_FILE_RANDOM_BODIES:
- segment = ctx->segments[ctx->segment_index];
- if (segment->data_length > ctx->buf_wr_ix - ctx->buf_rd_ix)
- return 0; /* need more data */
- code = jbig2_parse_segment(ctx, segment, ctx->buf + ctx->buf_rd_ix);
- ctx->buf_rd_ix += segment->data_length;
- ctx->segment_index++;
- if (ctx->state == JBIG2_FILE_RANDOM_BODIES)
- {
- if (ctx->segment_index == ctx->n_segments)
- ctx->state = JBIG2_FILE_EOF;
- }
- else /* JBIG2_FILE_SEQUENCIAL_BODY */
- {
- ctx->state = JBIG2_FILE_SEQUENTIAL_HEADER;
- }
- if (code < 0)
- {
- ctx->state = JBIG2_FILE_EOF;
- return code;
- }
- break;
- case JBIG2_FILE_EOF:
- if (ctx->buf_rd_ix == ctx->buf_wr_ix)
- return 0;
- return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, -1,
- "Garbage beyond end of file");
- }
+ if (ctx->n_segments == ctx->n_segments_max)
+ ctx->segments = jbig2_renew(ctx, ctx->segments, Jbig2Segment *, (ctx->n_segments_max <<= 2));
+
+ ctx->segments[ctx->n_segments++] = segment;
+ if (ctx->state == JBIG2_FILE_RANDOM_HEADERS) {
+ if ((segment->flags & 63) == 51) /* end of file */
+ ctx->state = JBIG2_FILE_RANDOM_BODIES;
+ } else /* JBIG2_FILE_SEQUENTIAL_HEADER */
+ ctx->state = JBIG2_FILE_SEQUENTIAL_BODY;
+ break;
+ case JBIG2_FILE_SEQUENTIAL_BODY:
+ case JBIG2_FILE_RANDOM_BODIES:
+ segment = ctx->segments[ctx->segment_index];
+ if (segment->data_length > ctx->buf_wr_ix - ctx->buf_rd_ix)
+ return 0; /* need more data */
+ code = jbig2_parse_segment(ctx, segment, ctx->buf + ctx->buf_rd_ix);
+ ctx->buf_rd_ix += segment->data_length;
+ ctx->segment_index++;
+ if (ctx->state == JBIG2_FILE_RANDOM_BODIES) {
+ if (ctx->segment_index == ctx->n_segments)
+ ctx->state = JBIG2_FILE_EOF;
+ } else { /* JBIG2_FILE_SEQUENCIAL_BODY */
+
+ ctx->state = JBIG2_FILE_SEQUENTIAL_HEADER;
+ }
+ if (code < 0) {
+ ctx->state = JBIG2_FILE_EOF;
+ return code;
+ }
+ break;
+ case JBIG2_FILE_EOF:
+ if (ctx->buf_rd_ix == ctx->buf_wr_ix)
+ return 0;
+ return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, -1, "Garbage beyond end of file");
+ }
}
}
void
-jbig2_ctx_free (Jbig2Ctx *ctx)
+jbig2_ctx_free(Jbig2Ctx *ctx)
{
- Jbig2Allocator *ca = ctx->allocator;
- int i;
+ Jbig2Allocator *ca = ctx->allocator;
+ int i;
- jbig2_free(ca, ctx->buf);
- if (ctx->segments != NULL) {
- for (i = 0; i < ctx->n_segments; i++)
- jbig2_free_segment(ctx, ctx->segments[i]);
- jbig2_free(ca, ctx->segments);
- }
+ jbig2_free(ca, ctx->buf);
+ if (ctx->segments != NULL) {
+ for (i = 0; i < ctx->n_segments; i++)
+ jbig2_free_segment(ctx, ctx->segments[i]);
+ jbig2_free(ca, ctx->segments);
+ }
- if (ctx->pages != NULL) {
- for (i = 0; i <= ctx->current_page; i++)
- if (ctx->pages[i].image != NULL)
- jbig2_image_release(ctx, ctx->pages[i].image);
- jbig2_free(ca, ctx->pages);
- }
+ if (ctx->pages != NULL) {
+ for (i = 0; i <= ctx->current_page; i++)
+ if (ctx->pages[i].image != NULL)
+ jbig2_image_release(ctx, ctx->pages[i].image);
+ jbig2_free(ca, ctx->pages);
+ }
- jbig2_free(ca, ctx);
+ jbig2_free(ca, ctx);
}
-Jbig2GlobalCtx *jbig2_make_global_ctx (Jbig2Ctx *ctx)
+Jbig2GlobalCtx *
+jbig2_make_global_ctx(Jbig2Ctx *ctx)
{
- return (Jbig2GlobalCtx *)ctx;
+ return (Jbig2GlobalCtx *) ctx;
}
-void jbig2_global_ctx_free(Jbig2GlobalCtx *global_ctx)
+void
+jbig2_global_ctx_free(Jbig2GlobalCtx *global_ctx)
{
- jbig2_ctx_free((Jbig2Ctx *)global_ctx);
+ jbig2_ctx_free((Jbig2Ctx *) global_ctx);
}
-
/* I'm not committed to keeping the word stream interface. It's handy
when you think you may be streaming your input, but if you're not
(as is currently the case), it just adds complexity.
@@ -415,58 +373,54 @@
*/
typedef struct {
- Jbig2WordStream super;
- const byte *data;
- size_t size;
+ Jbig2WordStream super;
+ const byte *data;
+ size_t size;
} Jbig2WordStreamBuf;
static int
-jbig2_word_stream_buf_get_next_word(Jbig2WordStream *self, int offset,
- uint32_t *word)
+jbig2_word_stream_buf_get_next_word(Jbig2WordStream *self, int offset, uint32_t *word)
{
- Jbig2WordStreamBuf *z = (Jbig2WordStreamBuf *)self;
- const byte *data = z->data;
- uint32_t result;
+ Jbig2WordStreamBuf *z = (Jbig2WordStreamBuf *) self;
+ const byte *data = z->data;
+ uint32_t result;
- if (offset + 4 < z->size)
- result = (data[offset] << 24) | (data[offset + 1] << 16) |
- (data[offset + 2] << 8) | data[offset + 3];
- else if (offset >= z->size)
- return -1;
- else
- {
- int i;
+ if (offset + 4 < z->size)
+ result = (data[offset] << 24) | (data[offset + 1] << 16) | (data[offset + 2] << 8) | data[offset + 3];
+ else if (offset >= z->size)
+ return -1;
+ else {
+ int i;
- result = 0;
- for (i = 0; i < z->size - offset; i++)
- result |= data[offset + i] << ((3 - i) << 3);
+ result = 0;
+ for (i = 0; i < z->size - offset; i++)
+ result |= data[offset + i] << ((3 - i) << 3);
}
- *word = result;
- return 0;
+ *word = result;
+ return 0;
}
Jbig2WordStream *
jbig2_word_stream_buf_new(Jbig2Ctx *ctx, const byte *data, size_t size)
{
- Jbig2WordStreamBuf *result = jbig2_new(ctx, Jbig2WordStreamBuf, 1);
- if (result == NULL)
- {
- jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1,
- "failed to allocate Jbig2WordStreamBuf in jbig2_word_stream_buf_new");
- return NULL;
- }
+ Jbig2WordStreamBuf *result = jbig2_new(ctx, Jbig2WordStreamBuf, 1);
- result->super.get_next_word = jbig2_word_stream_buf_get_next_word;
- result->data = data;
- result->size = size;
+ if (result == NULL) {
+ jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "failed to allocate Jbig2WordStreamBuf in jbig2_word_stream_buf_new");
+ return NULL;
+ }
- return &result->super;
+ result->super.get_next_word = jbig2_word_stream_buf_get_next_word;
+ result->data = data;
+ result->size = size;
+
+ return &result->super;
}
void
jbig2_word_stream_buf_free(Jbig2Ctx *ctx, Jbig2WordStream *ws)
{
- jbig2_free(ctx->allocator, ws);
+ jbig2_free(ctx->allocator, ws);
}
/* When Memento is in use, the ->free and ->realloc calls get
@@ -479,17 +433,16 @@
#endif
void
-jbig2_free (Jbig2Allocator *allocator, void *p)
+jbig2_free(Jbig2Allocator *allocator, void *p)
{
- allocator->free (allocator, p);
+ allocator->free(allocator, p);
}
void *
-jbig2_realloc (Jbig2Allocator *allocator, void *p, size_t size, size_t num)
+jbig2_realloc(Jbig2Allocator *allocator, void *p, size_t size, size_t num)
{
- /* check for integer multiplication overflow */
- if (num > 0 && size >= (size_t)-0x100 / num)
- return NULL;
- return allocator->realloc(allocator, p, size * num);
+ /* check for integer multiplication overflow */
+ if (num > 0 && size >= (size_t) - 0x100 / num)
+ return NULL;
+ return allocator->realloc(allocator, p, size * num);
}
-
--- a/jbig2.h
+++ b/jbig2.h
@@ -17,9 +17,9 @@
jbig2dec
*/
-
#ifdef __cplusplus
-extern "C" {
+extern "C"
+{
#endif
#ifndef _JBIG2_H
@@ -27,14 +27,14 @@
/* warning levels */
typedef enum {
- JBIG2_SEVERITY_DEBUG,
- JBIG2_SEVERITY_INFO,
- JBIG2_SEVERITY_WARNING,
- JBIG2_SEVERITY_FATAL
+ JBIG2_SEVERITY_DEBUG,
+ JBIG2_SEVERITY_INFO,
+ JBIG2_SEVERITY_WARNING,
+ JBIG2_SEVERITY_FATAL
} Jbig2Severity;
typedef enum {
- JBIG2_OPTIONS_EMBEDDED = 1
+ JBIG2_OPTIONS_EMBEDDED = 1
} Jbig2Options;
/* forward public structure declarations */
@@ -56,18 +56,17 @@
*/
struct _Jbig2Image {
- int width, height, stride;
- uint8_t *data;
- int refcount;
+ int width, height, stride;
+ uint8_t *data;
+ int refcount;
};
-Jbig2Image* jbig2_image_new(Jbig2Ctx *ctx, int width, int height);
-Jbig2Image* jbig2_image_clone(Jbig2Ctx *ctx, Jbig2Image *image);
-void jbig2_image_release(Jbig2Ctx *ctx, Jbig2Image *image);
-void jbig2_image_free(Jbig2Ctx *ctx, Jbig2Image *image);
-void jbig2_image_clear(Jbig2Ctx *ctx, Jbig2Image *image, int value);
-Jbig2Image *jbig2_image_resize(Jbig2Ctx *ctx, Jbig2Image *image,
- int width, int height);
+Jbig2Image *jbig2_image_new(Jbig2Ctx *ctx, int width, int height);
+Jbig2Image *jbig2_image_clone(Jbig2Ctx *ctx, Jbig2Image *image);
+void jbig2_image_release(Jbig2Ctx *ctx, Jbig2Image *image);
+void jbig2_image_free(Jbig2Ctx *ctx, Jbig2Image *image);
+void jbig2_image_clear(Jbig2Ctx *ctx, Jbig2Image *image, int value);
+Jbig2Image *jbig2_image_resize(Jbig2Ctx *ctx, Jbig2Image *image, int width, int height);
/* errors are returned from the library via a callback. If no callback
is provided (a NULL argument is passed ot jbig2_ctx_new) a default
@@ -74,9 +73,7 @@
handler is used which prints fatal errors to the stderr stream. */
/* error callback */
-typedef int (*Jbig2ErrorCallback) (void *data,
- const char *msg, Jbig2Severity severity,
- int32_t seg_idx);
+typedef int (*Jbig2ErrorCallback)(void *data, const char *msg, Jbig2Severity severity, int32_t seg_idx);
/* memory allocation is likewise done via a set of callbacks so that
clients can better control memory usage. If a NULL is passed for
@@ -85,55 +82,49 @@
/* dynamic memory callbacks */
struct _Jbig2Allocator {
- void *(*alloc) (Jbig2Allocator *allocator, size_t size);
- void (*free) (Jbig2Allocator *allocator, void *p);
- void *(*realloc) (Jbig2Allocator *allocator, void *p, size_t size);
+ void *(*alloc)(Jbig2Allocator *allocator, size_t size);
+ void (*free)(Jbig2Allocator *allocator, void *p);
+ void *(*realloc)(Jbig2Allocator *allocator, void *p, size_t size);
};
/* decoder context */
-Jbig2Ctx *jbig2_ctx_new (Jbig2Allocator *allocator,
- Jbig2Options options,
- Jbig2GlobalCtx *global_ctx,
- Jbig2ErrorCallback error_callback,
- void *error_callback_data);
-void jbig2_ctx_free (Jbig2Ctx *ctx);
+Jbig2Ctx *jbig2_ctx_new(Jbig2Allocator *allocator,
+ Jbig2Options options, Jbig2GlobalCtx *global_ctx, Jbig2ErrorCallback error_callback, void *error_callback_data);
+void jbig2_ctx_free(Jbig2Ctx *ctx);
/* global context for embedded streams */
-Jbig2GlobalCtx *jbig2_make_global_ctx (Jbig2Ctx *ctx);
-void jbig2_global_ctx_free (Jbig2GlobalCtx *global_ctx);
+Jbig2GlobalCtx *jbig2_make_global_ctx(Jbig2Ctx *ctx);
+void jbig2_global_ctx_free(Jbig2GlobalCtx *global_ctx);
/* submit data to the decoder */
-int jbig2_data_in (Jbig2Ctx *ctx, const unsigned char *data, size_t size);
+int jbig2_data_in(Jbig2Ctx *ctx, const unsigned char *data, size_t size);
/* get the next available decoded page image. NULL means there isn't one. */
-Jbig2Image *jbig2_page_out (Jbig2Ctx *ctx);
+Jbig2Image *jbig2_page_out(Jbig2Ctx *ctx);
/* mark a returned page image as no longer needed. */
-int jbig2_release_page (Jbig2Ctx *ctx, Jbig2Image *image);
+int jbig2_release_page(Jbig2Ctx *ctx, Jbig2Image *image);
/* mark the current page as complete, simulating an end-of-page segment (for broken streams) */
-int jbig2_complete_page (Jbig2Ctx *ctx);
+int jbig2_complete_page(Jbig2Ctx *ctx);
-
/* segment header routines */
struct _Jbig2Segment {
- uint32_t number;
- uint8_t flags;
- uint32_t page_association;
- size_t data_length;
- int referred_to_segment_count;
- uint32_t *referred_to_segments;
- void *result;
+ uint32_t number;
+ uint8_t flags;
+ uint32_t page_association;
+ size_t data_length;
+ int referred_to_segment_count;
+ uint32_t *referred_to_segments;
+ void *result;
};
-Jbig2Segment *jbig2_parse_segment_header (Jbig2Ctx *ctx, uint8_t *buf, size_t buf_size,
- size_t *p_header_size);
-int jbig2_parse_segment (Jbig2Ctx *ctx, Jbig2Segment *segment,
- const uint8_t *segment_data);
-void jbig2_free_segment (Jbig2Ctx *ctx, Jbig2Segment *segment);
+Jbig2Segment *jbig2_parse_segment_header(Jbig2Ctx *ctx, uint8_t *buf, size_t buf_size, size_t *p_header_size);
+int jbig2_parse_segment(Jbig2Ctx *ctx, Jbig2Segment *segment, const uint8_t *segment_data);
+void jbig2_free_segment(Jbig2Ctx *ctx, Jbig2Segment *segment);
Jbig2Segment *jbig2_find_segment(Jbig2Ctx *ctx, uint32_t number);
-#endif /* _JBIG2_H */
+#endif /* _JBIG2_H */
#ifdef __cplusplus
}
--- a/jbig2_arith.c
+++ b/jbig2_arith.c
@@ -17,7 +17,6 @@
jbig2dec
*/
-
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
@@ -31,18 +30,18 @@
#include "jbig2_arith.h"
struct _Jbig2ArithState {
- uint32_t C;
- int A;
+ uint32_t C;
+ int A;
- int CT;
+ int CT;
- uint32_t next_word;
- int next_word_bytes;
+ uint32_t next_word;
+ int next_word_bytes;
- Jbig2WordStream *ws;
- int offset;
+ Jbig2WordStream *ws;
+ int offset;
- Jbig2Ctx *ctx;
+ Jbig2Ctx *ctx;
};
#undef SOFTWARE_CONVENTION
@@ -62,110 +61,95 @@
*/
static int
-jbig2_arith_bytein (Jbig2ArithState *as)
+jbig2_arith_bytein(Jbig2ArithState *as)
{
- byte B;
+ byte B;
- /* invariant: as->next_word_bytes > 0 */
+ /* invariant: as->next_word_bytes > 0 */
- /* Figure G.3 */
- B = (byte)((as->next_word >> 24) & 0xFF);
- if (B == 0xFF)
- {
- byte B1;
- if (as->next_word_bytes == 1)
- {
- Jbig2WordStream *ws = as->ws;
- if (ws->get_next_word (ws, as->offset, &as->next_word))
- {
- jbig2_error(as->ctx, JBIG2_SEVERITY_FATAL, -1,
- "end of jbig2 buffer reached at offset %d", as->offset);
- return -1;
- }
- as->offset += 4;
- B1 = (byte)((as->next_word >> 24) & 0xFF);
- if (B1 > 0x8F)
- {
+ /* Figure G.3 */
+ B = (byte)((as->next_word >> 24) & 0xFF);
+ if (B == 0xFF) {
+ byte B1;
+
+ if (as->next_word_bytes == 1) {
+ Jbig2WordStream *ws = as->ws;
+
+ if (ws->get_next_word(ws, as->offset, &as->next_word)) {
+ jbig2_error(as->ctx, JBIG2_SEVERITY_FATAL, -1, "end of jbig2 buffer reached at offset %d", as->offset);
+ return -1;
+ }
+ as->offset += 4;
+ B1 = (byte)((as->next_word >> 24) & 0xFF);
+ if (B1 > 0x8F) {
#ifdef JBIG2_DEBUG_ARITH
- fprintf(stderr, "read %02x (aa)\n", B);
+ fprintf(stderr, "read %02x (aa)\n", B);
#endif
#ifndef SOFTWARE_CONVENTION
- as->C += 0xFF00;
+ as->C += 0xFF00;
#endif
- as->CT = 8;
- as->next_word = (0xFF00 | B1) << 16;
- as->next_word_bytes = 2;
- }
- else
- {
+ as->CT = 8;
+ as->next_word = (0xFF00 | B1) << 16;
+ as->next_word_bytes = 2;
+ } else {
#ifdef JBIG2_DEBUG_ARITH
- fprintf(stderr, "read %02x (a)\n", B);
+ fprintf(stderr, "read %02x (a)\n", B);
#endif
#ifdef SOFTWARE_CONVENTION
- as->C += 0xFE00 - (B1 << 9);
+ as->C += 0xFE00 - (B1 << 9);
#else
- as->C += B1 << 9;
+ as->C += B1 << 9;
#endif
- as->CT = 7;
- as->next_word_bytes = 4;
- }
- }
- else
- {
- B1 = (byte)((as->next_word >> 16) & 0xFF);
- if (B1 > 0x8F)
- {
+ as->CT = 7;
+ as->next_word_bytes = 4;
+ }
+ } else {
+ B1 = (byte)((as->next_word >> 16) & 0xFF);
+ if (B1 > 0x8F) {
#ifdef JBIG2_DEBUG_ARITH
- fprintf(stderr, "read %02x (ba)\n", B);
+ fprintf(stderr, "read %02x (ba)\n", B);
#endif
#ifndef SOFTWARE_CONVENTION
- as->C += 0xFF00;
+ as->C += 0xFF00;
#endif
- as->CT = 8;
- }
- else
- {
- as->next_word_bytes--;
- as->next_word <<= 8;
+ as->CT = 8;
+ } else {
+ as->next_word_bytes--;
+ as->next_word <<= 8;
#ifdef JBIG2_DEBUG_ARITH
- fprintf(stderr, "read %02x (b)\n", B);
+ fprintf(stderr, "read %02x (b)\n", B);
#endif
#ifdef SOFTWARE_CONVENTION
- as->C += 0xFE00 - (B1 << 9);
+ as->C += 0xFE00 - (B1 << 9);
#else
- as->C += (B1 << 9);
+ as->C += (B1 << 9);
#endif
- as->CT = 7;
- }
- }
- }
- else
- {
+ as->CT = 7;
+ }
+ }
+ } else {
#ifdef JBIG2_DEBUG_ARITH
- fprintf(stderr, "read %02x\n", B);
+ fprintf(stderr, "read %02x\n", B);
#endif
- as->CT = 8;
- as->next_word <<= 8;
- as->next_word_bytes--;
- if (as->next_word_bytes == 0)
- {
- Jbig2WordStream *ws = as->ws;
+ as->CT = 8;
+ as->next_word <<= 8;
+ as->next_word_bytes--;
+ if (as->next_word_bytes == 0) {
+ Jbig2WordStream *ws = as->ws;
- if (ws->get_next_word (ws, as->offset, &as->next_word))
- {
- jbig2_error(as->ctx, JBIG2_SEVERITY_FATAL, -1,
- "end of jbig2 buffer reached at offset %d", as->offset);
- return -1;
- }
- as->offset += 4;
- as->next_word_bytes = 4;
- }
- B = (byte)((as->next_word >> 24) & 0xFF);
+ if (ws->get_next_word(ws, as->offset, &as->next_word)) {
+ jbig2_error(as->ctx, JBIG2_SEVERITY_FATAL, -1, "end of jbig2 buffer reached at offset %d", as->offset);
+ return -1;
+ }
+ as->offset += 4;
+ as->next_word_bytes = 4;
+ }
+ B = (byte)((as->next_word >> 24) & 0xFF);
#ifdef SOFTWARE_CONVENTION
- as->C += 0xFF00 - (B << 8);
+ as->C += 0xFF00 - (B << 8);
#else
- as->C += (B << 8);
+ as->C += (B << 8);
#endif
}
return 0;
@@ -173,10 +157,9 @@
#if defined(JBIG2_DEBUG) || defined(JBIG2_DEBUG_ARITH)
static void
-jbig2_arith_trace (Jbig2ArithState *as, Jbig2ArithCx cx)
+jbig2_arith_trace(Jbig2ArithState *as, Jbig2ArithCx cx)
{
- fprintf(stderr, "I = %2d, MPS = %d, A = %04x, CT = %2d, C = %08x\n",
- cx & 0x7f, cx >> 7, as->A, as->CT, as->C);
+ fprintf(stderr, "I = %2d, MPS = %d, A = %04x, CT = %2d, C = %08x\n", cx & 0x7f, cx >> 7, as->A, as->CT, as->C);
}
#endif
@@ -185,48 +168,43 @@
* not affect the associated Jbig2WordStream.
*/
Jbig2ArithState *
-jbig2_arith_new (Jbig2Ctx *ctx, Jbig2WordStream *ws)
+jbig2_arith_new(Jbig2Ctx *ctx, Jbig2WordStream *ws)
{
- Jbig2ArithState *result;
+ Jbig2ArithState *result;
- result = jbig2_new(ctx, Jbig2ArithState, 1);
- if (result == NULL)
- {
- jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1,
- "failed to allocate Jbig2ArithState in jbig2_arith_new");
- return result;
- }
+ result = jbig2_new(ctx, Jbig2ArithState, 1);
+ if (result == NULL) {
+ jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "failed to allocate Jbig2ArithState in jbig2_arith_new");
+ return result;
+ }
- result->ws = ws;
- result->ctx = ctx;
+ result->ws = ws;
+ result->ctx = ctx;
- if (ws->get_next_word (ws, 0, &result->next_word))
- {
- jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1,
- "unable to get first word in jbig2_arith_new");
- jbig2_free(ctx->allocator, result);
- return NULL;
- }
- result->next_word_bytes = 4;
- result->offset = 4;
+ if (ws->get_next_word(ws, 0, &result->next_word)) {
+ jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "unable to get first word in jbig2_arith_new");
+ jbig2_free(ctx->allocator, result);
+ return NULL;
+ }
+ result->next_word_bytes = 4;
+ result->offset = 4;
- /* Figure E.20 */
+ /* Figure E.20 */
#ifdef SOFTWARE_CONVENTION
- result->C = (~(result->next_word >> 8)) & 0xFF0000;
+ result->C = (~(result->next_word >> 8)) & 0xFF0000;
#else
- result->C = (result->next_word >> 8) & 0xFF0000;
+ result->C = (result->next_word >> 8) & 0xFF0000;
#endif
- if (jbig2_arith_bytein (result))
- {
- jbig2_free(ctx->allocator, result);
- return NULL;
- }
- result->C <<= 7;
- result->CT -= 7;
- result->A = 0x8000;
+ if (jbig2_arith_bytein(result)) {
+ jbig2_free(ctx->allocator, result);
+ return NULL;
+ }
+ result->C <<= 7;
+ result->CT -= 7;
+ result->A = 0x8000;
- return result;
+ return result;
}
#define MAX_QE_ARRAY_SIZE 47
@@ -233,149 +211,133 @@
/* could put bit fields in to minimize memory usage */
typedef struct {
- unsigned short Qe;
- byte mps_xor; /* mps_xor = index ^ NMPS */
- byte lps_xor; /* lps_xor = index ^ NLPS ^ (SWITCH << 7) */
+ unsigned short Qe;
+ byte mps_xor; /* mps_xor = index ^ NMPS */
+ byte lps_xor; /* lps_xor = index ^ NLPS ^ (SWITCH << 7) */
} Jbig2ArithQe;
const Jbig2ArithQe jbig2_arith_Qe[MAX_QE_ARRAY_SIZE] = {
- { 0x5601, 1 ^ 0, 1 ^ 0 ^ 0x80 },
- { 0x3401, 2 ^ 1, 6 ^ 1 },
- { 0x1801, 3 ^ 2, 9 ^ 2 },
- { 0x0AC1, 4 ^ 3, 12 ^ 3 },
- { 0x0521, 5 ^ 4, 29 ^ 4 },
- { 0x0221, 38 ^ 5, 33 ^ 5 },
- { 0x5601, 7 ^ 6, 6 ^ 6 ^ 0x80 },
- { 0x5401, 8 ^ 7, 14 ^ 7 },
- { 0x4801, 9 ^ 8, 14 ^ 8 },
- { 0x3801, 10 ^ 9, 14 ^ 9 },
- { 0x3001, 11 ^ 10, 17 ^ 10 },
- { 0x2401, 12 ^ 11, 18 ^ 11 },
- { 0x1C01, 13 ^ 12, 20 ^ 12 },
- { 0x1601, 29 ^ 13, 21 ^ 13 },
- { 0x5601, 15 ^ 14, 14 ^ 14 ^ 0x80 },
- { 0x5401, 16 ^ 15, 14 ^ 15 },
- { 0x5101, 17 ^ 16, 15 ^ 16 },
- { 0x4801, 18 ^ 17, 16 ^ 17 },
- { 0x3801, 19 ^ 18, 17 ^ 18 },
- { 0x3401, 20 ^ 19, 18 ^ 19 },
- { 0x3001, 21 ^ 20, 19 ^ 20 },
- { 0x2801, 22 ^ 21, 19 ^ 21 },
- { 0x2401, 23 ^ 22, 20 ^ 22 },
- { 0x2201, 24 ^ 23, 21 ^ 23 },
- { 0x1C01, 25 ^ 24, 22 ^ 24 },
- { 0x1801, 26 ^ 25, 23 ^ 25 },
- { 0x1601, 27 ^ 26, 24 ^ 26 },
- { 0x1401, 28 ^ 27, 25 ^ 27 },
- { 0x1201, 29 ^ 28, 26 ^ 28 },
- { 0x1101, 30 ^ 29, 27 ^ 29 },
- { 0x0AC1, 31 ^ 30, 28 ^ 30 },
- { 0x09C1, 32 ^ 31, 29 ^ 31 },
- { 0x08A1, 33 ^ 32, 30 ^ 32 },
- { 0x0521, 34 ^ 33, 31 ^ 33 },
- { 0x0441, 35 ^ 34, 32 ^ 34 },
- { 0x02A1, 36 ^ 35, 33 ^ 35 },
- { 0x0221, 37 ^ 36, 34 ^ 36 },
- { 0x0141, 38 ^ 37, 35 ^ 37 },
- { 0x0111, 39 ^ 38, 36 ^ 38 },
- { 0x0085, 40 ^ 39, 37 ^ 39 },
- { 0x0049, 41 ^ 40, 38 ^ 40 },
- { 0x0025, 42 ^ 41, 39 ^ 41 },
- { 0x0015, 43 ^ 42, 40 ^ 42 },
- { 0x0009, 44 ^ 43, 41 ^ 43 },
- { 0x0005, 45 ^ 44, 42 ^ 44 },
- { 0x0001, 45 ^ 45, 43 ^ 45 },
- { 0x5601, 46 ^ 46, 46 ^ 46 }
+ {0x5601, 1 ^ 0, 1 ^ 0 ^ 0x80},
+ {0x3401, 2 ^ 1, 6 ^ 1},
+ {0x1801, 3 ^ 2, 9 ^ 2},
+ {0x0AC1, 4 ^ 3, 12 ^ 3},
+ {0x0521, 5 ^ 4, 29 ^ 4},
+ {0x0221, 38 ^ 5, 33 ^ 5},
+ {0x5601, 7 ^ 6, 6 ^ 6 ^ 0x80},
+ {0x5401, 8 ^ 7, 14 ^ 7},
+ {0x4801, 9 ^ 8, 14 ^ 8},
+ {0x3801, 10 ^ 9, 14 ^ 9},
+ {0x3001, 11 ^ 10, 17 ^ 10},
+ {0x2401, 12 ^ 11, 18 ^ 11},
+ {0x1C01, 13 ^ 12, 20 ^ 12},
+ {0x1601, 29 ^ 13, 21 ^ 13},
+ {0x5601, 15 ^ 14, 14 ^ 14 ^ 0x80},
+ {0x5401, 16 ^ 15, 14 ^ 15},
+ {0x5101, 17 ^ 16, 15 ^ 16},
+ {0x4801, 18 ^ 17, 16 ^ 17},
+ {0x3801, 19 ^ 18, 17 ^ 18},
+ {0x3401, 20 ^ 19, 18 ^ 19},
+ {0x3001, 21 ^ 20, 19 ^ 20},
+ {0x2801, 22 ^ 21, 19 ^ 21},
+ {0x2401, 23 ^ 22, 20 ^ 22},
+ {0x2201, 24 ^ 23, 21 ^ 23},
+ {0x1C01, 25 ^ 24, 22 ^ 24},
+ {0x1801, 26 ^ 25, 23 ^ 25},
+ {0x1601, 27 ^ 26, 24 ^ 26},
+ {0x1401, 28 ^ 27, 25 ^ 27},
+ {0x1201, 29 ^ 28, 26 ^ 28},
+ {0x1101, 30 ^ 29, 27 ^ 29},
+ {0x0AC1, 31 ^ 30, 28 ^ 30},
+ {0x09C1, 32 ^ 31, 29 ^ 31},
+ {0x08A1, 33 ^ 32, 30 ^ 32},
+ {0x0521, 34 ^ 33, 31 ^ 33},
+ {0x0441, 35 ^ 34, 32 ^ 34},
+ {0x02A1, 36 ^ 35, 33 ^ 35},
+ {0x0221, 37 ^ 36, 34 ^ 36},
+ {0x0141, 38 ^ 37, 35 ^ 37},
+ {0x0111, 39 ^ 38, 36 ^ 38},
+ {0x0085, 40 ^ 39, 37 ^ 39},
+ {0x0049, 41 ^ 40, 38 ^ 40},
+ {0x0025, 42 ^ 41, 39 ^ 41},
+ {0x0015, 43 ^ 42, 40 ^ 42},
+ {0x0009, 44 ^ 43, 41 ^ 43},
+ {0x0005, 45 ^ 44, 42 ^ 44},
+ {0x0001, 45 ^ 45, 43 ^ 45},
+ {0x5601, 46 ^ 46, 46 ^ 46}
};
static int
-jbig2_arith_renormd (Jbig2ArithState *as)
+jbig2_arith_renormd(Jbig2ArithState *as)
{
- /* Figure E.18 */
- do
- {
- if ((as->CT == 0) && (jbig2_arith_bytein (as) < 0))
- return -1;
- as->A <<= 1;
- as->C <<= 1;
- as->CT--;
- }
- while ((as->A & 0x8000) == 0);
- return 0;
+ /* Figure E.18 */
+ do {
+ if ((as->CT == 0) && (jbig2_arith_bytein(as) < 0))
+ return -1;
+ as->A <<= 1;
+ as->C <<= 1;
+ as->CT--;
+ } while ((as->A & 0x8000) == 0);
+ return 0;
}
bool
-jbig2_arith_decode (Jbig2ArithState *as, Jbig2ArithCx *pcx)
+jbig2_arith_decode(Jbig2ArithState *as, Jbig2ArithCx *pcx)
{
- Jbig2ArithCx cx = *pcx;
- const Jbig2ArithQe *pqe;
- unsigned int index = cx & 0x7f;
- bool D;
+ Jbig2ArithCx cx = *pcx;
+ const Jbig2ArithQe *pqe;
+ unsigned int index = cx & 0x7f;
+ bool D;
- if (index >= MAX_QE_ARRAY_SIZE)
- {
- return -1;
- }
- else
- {
- pqe = &jbig2_arith_Qe[index];
- }
+ if (index >= MAX_QE_ARRAY_SIZE) {
+ return -1;
+ } else {
+ pqe = &jbig2_arith_Qe[index];
+ }
- /* Figure E.15 */
- as->A -= pqe->Qe;
- if (
+ /* Figure E.15 */
+ as->A -= pqe->Qe;
+ if (
#ifdef SOFTWARE_CONVENTION
- /* Note: I do not think this is correct. See above. */
- (as->C >> 16) < as->A
+ /* Note: I do not think this is correct. See above. */
+ (as->C >> 16) < as->A
#else
- !((as->C >> 16) < pqe->Qe)
+ !((as->C >> 16) < pqe->Qe)
#endif
- )
- {
+ ) {
#ifndef SOFTWARE_CONVENTION
- as->C -= pqe->Qe << 16;
+ as->C -= pqe->Qe << 16;
#endif
- if ((as->A & 0x8000) == 0)
- {
- /* MPS_EXCHANGE, Figure E.16 */
- if (as->A < pqe->Qe)
- {
- D = 1 - (cx >> 7);
- *pcx ^= pqe->lps_xor;
- }
- else
- {
- D = cx >> 7;
- *pcx ^= pqe->mps_xor;
- }
- if (jbig2_arith_renormd (as))
- return -1;
- return D;
- }
- else
- return cx >> 7;
- }
- else
- {
+ if ((as->A & 0x8000) == 0) {
+ /* MPS_EXCHANGE, Figure E.16 */
+ if (as->A < pqe->Qe) {
+ D = 1 - (cx >> 7);
+ *pcx ^= pqe->lps_xor;
+ } else {
+ D = cx >> 7;
+ *pcx ^= pqe->mps_xor;
+ }
+ if (jbig2_arith_renormd(as))
+ return -1;
+ return D;
+ } else
+ return cx >> 7;
+ } else {
#ifdef SOFTWARE_CONVENTION
- as->C -= (as->A) << 16;
+ as->C -= (as->A) << 16;
#endif
- /* LPS_EXCHANGE, Figure E.17 */
- if (as->A < pqe->Qe)
- {
- as->A = pqe->Qe;
- D = cx >> 7;
- *pcx ^= pqe->mps_xor;
- }
- else
- {
- as->A = pqe->Qe;
- D = 1 - (cx >> 7);
- *pcx ^= pqe->lps_xor;
- }
- if (jbig2_arith_renormd (as))
- return -1;
- return D;
+ /* LPS_EXCHANGE, Figure E.17 */
+ if (as->A < pqe->Qe) {
+ as->A = pqe->Qe;
+ D = cx >> 7;
+ *pcx ^= pqe->mps_xor;
+ } else {
+ as->A = pqe->Qe;
+ D = 1 - (cx >> 7);
+ *pcx ^= pqe->lps_xor;
+ }
+ if (jbig2_arith_renormd(as))
+ return -1;
+ return D;
}
}
@@ -382,64 +344,61 @@
bool
jbig2_arith_has_reached_marker(Jbig2ArithState *as)
{
- return as->next_word_bytes == 2 && (as->next_word >> 16) > 0xFF8F;
+ return as->next_word_bytes == 2 && (as->next_word >> 16) > 0xFF8F;
}
#ifdef TEST
static int
-test_get_word (Jbig2WordStream *self, int offset, uint32_t *word)
+test_get_word(Jbig2WordStream *self, int offset, uint32_t *word)
{
- byte stream[] = {
- 0x84, 0xC7, 0x3B, 0xFC, 0xE1, 0xA1, 0x43, 0x04, 0x02, 0x20, 0x00, 0x00,
- 0x41, 0x0D, 0xBB, 0x86, 0xF4, 0x31, 0x7F, 0xFF, 0x88, 0xFF, 0x37, 0x47,
- 0x1A, 0xDB, 0x6A, 0xDF, 0xFF, 0xAC,
- 0x00, 0x00
- };
- if (offset >= sizeof(stream))
- return -1;
- *word = (stream[offset] << 24) | (stream[offset + 1] << 16) |
- (stream[offset + 2] << 8) | stream[offset + 3];
- return 0;
+ byte stream[] = {
+ 0x84, 0xC7, 0x3B, 0xFC, 0xE1, 0xA1, 0x43, 0x04, 0x02, 0x20, 0x00, 0x00,
+ 0x41, 0x0D, 0xBB, 0x86, 0xF4, 0x31, 0x7F, 0xFF, 0x88, 0xFF, 0x37, 0x47,
+ 0x1A, 0xDB, 0x6A, 0xDF, 0xFF, 0xAC,
+ 0x00, 0x00
+ };
+ if (offset >= sizeof(stream))
+ return -1;
+ *word = (stream[offset] << 24) | (stream[offset + 1] << 16) | (stream[offset + 2] << 8) | stream[offset + 3];
+ return 0;
}
int
-main (int argc, char **argv)
+main(int argc, char **argv)
{
- Jbig2Ctx *ctx;
- Jbig2WordStream ws;
- Jbig2ArithState *as;
- int i;
- Jbig2ArithCx cx = 0;
+ Jbig2Ctx *ctx;
+ Jbig2WordStream ws;
+ Jbig2ArithState *as;
+ int i;
+ Jbig2ArithCx cx = 0;
- ctx = jbig2_ctx_new(NULL, 0, NULL, NULL, NULL);
+ ctx = jbig2_ctx_new(NULL, 0, NULL, NULL, NULL);
- ws.get_next_word = test_get_word;
- as = jbig2_arith_new (ctx, &ws);
+ ws.get_next_word = test_get_word;
+ as = jbig2_arith_new(ctx, &ws);
#ifdef JBIG2_DEBUG_ARITH
- jbig2_arith_trace (as, cx);
+ jbig2_arith_trace(as, cx);
#endif
- for (i = 0; i < 256; i++)
- {
+ for (i = 0; i < 256; i++) {
#ifdef JBIG2_DEBUG_ARITH
- bool D =
+ bool D =
#else
- (void)
+ (void)
#endif
+ jbig2_arith_decode(as, &cx);
- jbig2_arith_decode (as, &cx);
-
#ifdef JBIG2_DEBUG_ARITH
- fprintf(stderr, "%3d: D = %d, ", i, D);
- jbig2_arith_trace (as, cx);
+ fprintf(stderr, "%3d: D = %d, ", i, D);
+ jbig2_arith_trace(as, cx);
#endif
}
- jbig2_free(ctx->allocator, as);
+ jbig2_free(ctx->allocator, as);
- jbig2_ctx_free(ctx);
+ jbig2_ctx_free(ctx);
- return 0;
+ return 0;
}
#endif
--- a/jbig2_arith.h
+++ b/jbig2_arith.h
@@ -17,7 +17,6 @@
jbig2dec
*/
-
typedef struct _Jbig2ArithState Jbig2ArithState;
/* An arithmetic coding context is stored as a single byte, with the
@@ -26,13 +25,10 @@
typedef unsigned char Jbig2ArithCx;
/* allocate and initialize a new arithmetic coding state */
-Jbig2ArithState *
-jbig2_arith_new (Jbig2Ctx *ctx, Jbig2WordStream *ws);
+Jbig2ArithState *jbig2_arith_new(Jbig2Ctx *ctx, Jbig2WordStream *ws);
/* decode a bit */
-bool
-jbig2_arith_decode (Jbig2ArithState *as, Jbig2ArithCx *pcx);
+bool jbig2_arith_decode(Jbig2ArithState *as, Jbig2ArithCx *pcx);
/* returns true if the end of the data stream has been reached (for sanity checks) */
-bool
-jbig2_arith_has_reached_marker(Jbig2ArithState *as);
+bool jbig2_arith_has_reached_marker(Jbig2ArithState *as);
--- a/jbig2_arith_iaid.c
+++ b/jbig2_arith_iaid.c
@@ -17,7 +17,6 @@
jbig2dec
*/
-
/* Annex A.3 */
#ifdef HAVE_CONFIG_H
@@ -26,10 +25,10 @@
#include "os_types.h"
#include <stddef.h>
-#include <string.h> /* memset() */
+#include <string.h> /* memset() */
#ifdef VERBOSE
-#include <stdio.h> /* for debug printing only */
+#include <stdio.h> /* for debug printing only */
#endif
#include "jbig2.h"
@@ -38,75 +37,66 @@
#include "jbig2_arith_iaid.h"
struct _Jbig2ArithIaidCtx {
- int SBSYMCODELEN;
- Jbig2ArithCx *IAIDx;
+ int SBSYMCODELEN;
+ Jbig2ArithCx *IAIDx;
};
Jbig2ArithIaidCtx *
jbig2_arith_iaid_ctx_new(Jbig2Ctx *ctx, int SBSYMCODELEN)
{
- Jbig2ArithIaidCtx *result = jbig2_new(ctx, Jbig2ArithIaidCtx, 1);
- int ctx_size = 1 << SBSYMCODELEN;
+ Jbig2ArithIaidCtx *result = jbig2_new(ctx, Jbig2ArithIaidCtx, 1);
+ int ctx_size = 1 << SBSYMCODELEN;
- if (result == NULL)
- {
- jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1,
- "failed to allocate storage in jbig2_arith_iaid_ctx_new");
- return result;
- }
+ if (result == NULL) {
+ jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "failed to allocate storage in jbig2_arith_iaid_ctx_new");
+ return result;
+ }
- result->SBSYMCODELEN = SBSYMCODELEN;
- result->IAIDx = jbig2_new(ctx, Jbig2ArithCx, ctx_size);
- if (result->IAIDx != NULL)
- {
- memset(result->IAIDx, 0, ctx_size);
- }
- else
- {
- jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1,
- "failed to allocate symbol ID storage in jbig2_arith_iaid_ctx_new");
- }
+ result->SBSYMCODELEN = SBSYMCODELEN;
+ result->IAIDx = jbig2_new(ctx, Jbig2ArithCx, ctx_size);
+ if (result->IAIDx != NULL) {
+ memset(result->IAIDx, 0, ctx_size);
+ } else {
+ jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "failed to allocate symbol ID storage in jbig2_arith_iaid_ctx_new");
+ }
- return result;
+ return result;
}
/* A.3 */
/* Return value: -1 on error, 0 on normal value */
int
-jbig2_arith_iaid_decode(Jbig2ArithIaidCtx *ctx, Jbig2ArithState *as,
- int32_t *p_result)
+jbig2_arith_iaid_decode(Jbig2ArithIaidCtx *ctx, Jbig2ArithState *as, int32_t *p_result)
{
- Jbig2ArithCx *IAIDx = ctx->IAIDx;
- int SBSYMCODELEN = ctx->SBSYMCODELEN;
- int PREV = 1;
- int D;
- int i;
+ Jbig2ArithCx *IAIDx = ctx->IAIDx;
+ int SBSYMCODELEN = ctx->SBSYMCODELEN;
+ int PREV = 1;
+ int D;
+ int i;
- /* A.3 (2) */
- for (i = 0; i < SBSYMCODELEN; i++)
- {
- D = jbig2_arith_decode(as, &IAIDx[PREV]);
- if (D < 0)
- return -1;
+ /* A.3 (2) */
+ for (i = 0; i < SBSYMCODELEN; i++) {
+ D = jbig2_arith_decode(as, &IAIDx[PREV]);
+ if (D < 0)
+ return -1;
#ifdef VERBOSE
- fprintf(stderr, "IAID%x: D = %d\n", PREV, D);
+ fprintf(stderr, "IAID%x: D = %d\n", PREV, D);
#endif
- PREV = (PREV << 1) | D;
+ PREV = (PREV << 1) | D;
}
- /* A.3 (3) */
- PREV -= 1 << SBSYMCODELEN;
+ /* A.3 (3) */
+ PREV -= 1 << SBSYMCODELEN;
#ifdef VERBOSE
- fprintf(stderr, "IAID result: %d\n", PREV);
+ fprintf(stderr, "IAID result: %d\n", PREV);
#endif
- *p_result = PREV;
- return 0;
+ *p_result = PREV;
+ return 0;
}
void
jbig2_arith_iaid_ctx_free(Jbig2Ctx *ctx, Jbig2ArithIaidCtx *iax)
{
- if (iax != NULL)
- {
+ if (iax != NULL) {
jbig2_free(ctx->allocator, iax->IAIDx);
jbig2_free(ctx->allocator, iax);
}
--- a/jbig2_arith_iaid.h
+++ b/jbig2_arith_iaid.h
@@ -17,15 +17,10 @@
jbig2dec
*/
-
typedef struct _Jbig2ArithIaidCtx Jbig2ArithIaidCtx;
-Jbig2ArithIaidCtx *
-jbig2_arith_iaid_ctx_new(Jbig2Ctx *ctx, int SBSYMCODELEN);
+Jbig2ArithIaidCtx *jbig2_arith_iaid_ctx_new(Jbig2Ctx *ctx, int SBSYMCODELEN);
-int
-jbig2_arith_iaid_decode(Jbig2ArithIaidCtx *ctx, Jbig2ArithState *as,
- int32_t *p_result);
+int jbig2_arith_iaid_decode(Jbig2ArithIaidCtx *ctx, Jbig2ArithState *as, int32_t *p_result);
-void
-jbig2_arith_iaid_ctx_free(Jbig2Ctx *ctx, Jbig2ArithIaidCtx *iax);
+void jbig2_arith_iaid_ctx_free(Jbig2Ctx *ctx, Jbig2ArithIaidCtx *iax);
--- a/jbig2_arith_int.c
+++ b/jbig2_arith_int.c
@@ -17,7 +17,6 @@
jbig2dec
*/
-
/* Annex A */
#ifdef HAVE_CONFIG_H
@@ -26,7 +25,7 @@
#include "os_types.h"
#include <stddef.h>
-#include <string.h> /* memset() */
+#include <string.h> /* memset() */
#include "jbig2.h"
#include "jbig2_priv.h"
@@ -34,130 +33,109 @@
#include "jbig2_arith_int.h"
struct _Jbig2ArithIntCtx {
- Jbig2ArithCx IAx[512];
+ Jbig2ArithCx IAx[512];
};
Jbig2ArithIntCtx *
jbig2_arith_int_ctx_new(Jbig2Ctx *ctx)
{
- Jbig2ArithIntCtx *result = jbig2_new(ctx, Jbig2ArithIntCtx, 1);
+ Jbig2ArithIntCtx *result = jbig2_new(ctx, Jbig2ArithIntCtx, 1);
- if (result == NULL)
- {
- jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1,
- "failed to allocate Jbig2ArithIntCtx in jbig2_arith_int_ctx_new");
- }
- else
- {
- memset(result->IAx, 0, sizeof(result->IAx));
- }
+ if (result == NULL) {
+ jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "failed to allocate Jbig2ArithIntCtx in jbig2_arith_int_ctx_new");
+ } else {
+ memset(result->IAx, 0, sizeof(result->IAx));
+ }
- return result;
+ return result;
}
/* A.2 */
/* Return value: -1 on error, 0 on normal value, 1 on OOB return. */
int
-jbig2_arith_int_decode(Jbig2ArithIntCtx *ctx, Jbig2ArithState *as,
- int32_t *p_result)
+jbig2_arith_int_decode(Jbig2ArithIntCtx *ctx, Jbig2ArithState *as, int32_t *p_result)
{
- Jbig2ArithCx *IAx = ctx->IAx;
- int PREV = 1;
- int S, V;
- int bit;
- int n_tail, offset;
- int i;
+ Jbig2ArithCx *IAx = ctx->IAx;
+ int PREV = 1;
+ int S, V;
+ int bit;
+ int n_tail, offset;
+ int i;
- S = jbig2_arith_decode(as, &IAx[PREV]);
- if (S < 0)
- return -1;
- PREV = (PREV << 1) | S;
+ S = jbig2_arith_decode(as, &IAx[PREV]);
+ if (S < 0)
+ return -1;
+ PREV = (PREV << 1) | S;
- bit = jbig2_arith_decode(as, &IAx[PREV]);
- if (bit < 0)
- return -1;
- PREV = (PREV << 1) | bit;
- if (bit)
- {
- bit = jbig2_arith_decode(as, &IAx[PREV]);
- if (bit < 0)
- return -1;
- PREV = (PREV << 1) | bit;
+ bit = jbig2_arith_decode(as, &IAx[PREV]);
+ if (bit < 0)
+ return -1;
+ PREV = (PREV << 1) | bit;
+ if (bit) {
+ bit = jbig2_arith_decode(as, &IAx[PREV]);
+ if (bit < 0)
+ return -1;
+ PREV = (PREV << 1) | bit;
- if (bit)
- {
- bit = jbig2_arith_decode(as, &IAx[PREV]);
- if (bit < 0)
- return -1;
- PREV = (PREV << 1) | bit;
+ if (bit) {
+ bit = jbig2_arith_decode(as, &IAx[PREV]);
+ if (bit < 0)
+ return -1;
+ PREV = (PREV << 1) | bit;
- if (bit)
- {
- bit = jbig2_arith_decode(as, &IAx[PREV]);
- if (bit < 0)
- return -1;
- PREV = (PREV << 1) | bit;
+ if (bit) {
+ bit = jbig2_arith_decode(as, &IAx[PREV]);
+ if (bit < 0)
+ return -1;
+ PREV = (PREV << 1) | bit;
- if (bit)
- {
- bit = jbig2_arith_decode(as, &IAx[PREV]);
- if (bit < 0)
- return -1;
- PREV = (PREV << 1) | bit;
+ if (bit) {
+ bit = jbig2_arith_decode(as, &IAx[PREV]);
+ if (bit < 0)
+ return -1;
+ PREV = (PREV << 1) | bit;
- if (bit)
- {
- n_tail = 32;
- offset = 4436;
- }
- else
- {
- n_tail = 12;
- offset = 340;
- }
- }
- else
- {
- n_tail = 8;
- offset = 84;
- }
- }
- else
- {
- n_tail = 6;
- offset = 20;
- }
- }
- else
- {
- n_tail = 4;
- offset = 4;
- }
- }
- else
- {
- n_tail = 2;
- offset = 0;
+ if (bit) {
+ n_tail = 32;
+ offset = 4436;
+ } else {
+ n_tail = 12;
+ offset = 340;
+ }
+ } else {
+ n_tail = 8;
+ offset = 84;
+ }
+ } else {
+ n_tail = 6;
+ offset = 20;
+ }
+ } else {
+ n_tail = 4;
+ offset = 4;
+ }
+ } else {
+ n_tail = 2;
+ offset = 0;
}
- V = 0;
- for (i = 0; i < n_tail; i++)
- {
- bit = jbig2_arith_decode(as, &IAx[PREV]);
- if (bit < 0)
- return -1;
- PREV = ((PREV << 1) & 511) | (PREV & 256) | bit;
- V = (V << 1) | bit;
+ V = 0;
+ for (i = 0; i < n_tail; i++) {
+ bit = jbig2_arith_decode(as, &IAx[PREV]);
+ if (bit < 0)
+ return -1;
+ PREV = ((PREV << 1) & 511) | (PREV & 256) | bit;
+ V = (V << 1) | bit;
}
- V += offset;
- V = S ? -V : V;
- *p_result = V;
- return S && V == 0 ? 1 : 0;
+ V += offset;
+ V = S ? -V : V;
+ *p_result = V;
+ return S && V == 0 ? 1 : 0;
}
void
jbig2_arith_int_ctx_free(Jbig2Ctx *ctx, Jbig2ArithIntCtx *iax)
{
- jbig2_free(ctx->allocator, iax);
+ jbig2_free(ctx->allocator, iax);
}
--- a/jbig2_arith_int.h
+++ b/jbig2_arith_int.h
@@ -17,15 +17,10 @@
jbig2dec
*/
-
typedef struct _Jbig2ArithIntCtx Jbig2ArithIntCtx;
-Jbig2ArithIntCtx *
-jbig2_arith_int_ctx_new(Jbig2Ctx *ctx);
+Jbig2ArithIntCtx *jbig2_arith_int_ctx_new(Jbig2Ctx *ctx);
-int
-jbig2_arith_int_decode(Jbig2ArithIntCtx *ctx, Jbig2ArithState *as,
- int32_t *p_result);
+int jbig2_arith_int_decode(Jbig2ArithIntCtx *ctx, Jbig2ArithState *as, int32_t *p_result);
-void
-jbig2_arith_int_ctx_free(Jbig2Ctx *ctx, Jbig2ArithIntCtx *iax);
+void jbig2_arith_int_ctx_free(Jbig2Ctx *ctx, Jbig2ArithIntCtx *iax);
--- a/jbig2_generic.c
+++ b/jbig2_generic.c
@@ -17,7 +17,6 @@
jbig2dec
*/
-
/**
* Generic region handlers.
**/
@@ -28,7 +27,7 @@
#include "os_types.h"
#include <stddef.h>
-#include <string.h> /* memcpy(), memset() */
+#include <string.h> /* memcpy(), memset() */
#ifdef OUTPUT_PBM
#include <stdio.h>
@@ -45,694 +44,608 @@
int
jbig2_generic_stats_size(Jbig2Ctx *ctx, int template)
{
- int stats_size = template == 0 ? 1 << 16 :
- template == 1 ? 1 << 1 << 13 : 1 << 10;
- return stats_size;
+ int stats_size = template == 0 ? 1 << 16 : template == 1 ? 1 << 1 << 13 : 1 << 10;
+
+ return stats_size;
}
-
-static int
-jbig2_decode_generic_template0(Jbig2Ctx *ctx,
- Jbig2Segment *segment,
- const Jbig2GenericRegionParams *params,
- Jbig2ArithState *as,
- Jbig2Image *image,
- Jbig2ArithCx *GB_stats)
+ static int
+ jbig2_decode_generic_template0(Jbig2Ctx *ctx,
+ Jbig2Segment *segment,
+ const Jbig2GenericRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GB_stats)
{
- const int GBW = image->width;
- const int GBH = image->height;
- const int rowstride = image->stride;
- int x, y;
- byte *gbreg_line = (byte *)image->data;
+ const int GBW = image->width;
+ const int GBH = image->height;
+ const int rowstride = image->stride;
+ int x, y;
+ byte *gbreg_line = (byte *) image->data;
- /* todo: currently we only handle the nominal gbat location */
+ /* todo: currently we only handle the nominal gbat location */
#ifdef OUTPUT_PBM
- printf("P4\n%d %d\n", GBW, GBH);
+ printf("P4\n%d %d\n", GBW, GBH);
#endif
- if (GBW <= 0)
- return 0;
+ if (GBW <= 0)
+ return 0;
- for (y = 0; y < GBH; y++)
- {
- uint32_t CONTEXT;
- uint32_t line_m1;
- uint32_t line_m2;
- int padded_width = (GBW + 7) & -8;
+ for (y = 0; y < GBH; y++) {
+ uint32_t CONTEXT;
+ uint32_t line_m1;
+ uint32_t line_m2;
+ int padded_width = (GBW + 7) & -8;
- line_m1 = (y >= 1) ? gbreg_line[-rowstride] : 0;
- line_m2 = (y >= 2) ? gbreg_line[-(rowstride << 1)] << 6 : 0;
- CONTEXT = (line_m1 & 0x7f0) | (line_m2 & 0xf800);
+ line_m1 = (y >= 1) ? gbreg_line[-rowstride] : 0;
+ line_m2 = (y >= 2) ? gbreg_line[-(rowstride << 1)] << 6 : 0;
+ CONTEXT = (line_m1 & 0x7f0) | (line_m2 & 0xf800);
- /* 6.2.5.7 3d */
- for (x = 0; x < padded_width; x += 8)
- {
- byte result = 0;
- int x_minor;
- int minor_width = GBW - x > 8 ? 8 : GBW - x;
+ /* 6.2.5.7 3d */
+ for (x = 0; x < padded_width; x += 8) {
+ byte result = 0;
+ int x_minor;
+ int minor_width = GBW - x > 8 ? 8 : GBW - x;
- if (y >= 1)
- line_m1 = (line_m1 << 8) |
- (x + 8 < GBW ? gbreg_line[-rowstride + (x >> 3) + 1] : 0);
+ if (y >= 1)
+ line_m1 = (line_m1 << 8) | (x + 8 < GBW ? gbreg_line[-rowstride + (x >> 3) + 1] : 0);
- if (y >= 2)
- line_m2 = (line_m2 << 8) |
- (x + 8 < GBW ? gbreg_line[-(rowstride << 1) + (x >> 3) + 1] << 6: 0);
+ if (y >= 2)
+ line_m2 = (line_m2 << 8) | (x + 8 < GBW ? gbreg_line[-(rowstride << 1) + (x >> 3) + 1] << 6 : 0);
- /* This is the speed-critical inner loop. */
- for (x_minor = 0; x_minor < minor_width; x_minor++)
- {
- bool bit;
+ /* This is the speed-critical inner loop. */
+ for (x_minor = 0; x_minor < minor_width; x_minor++) {
+ bool bit;
- bit = jbig2_arith_decode(as, &GB_stats[CONTEXT]);
- if (bit < 0)
- return -1;
- result |= bit << (7 - x_minor);
- CONTEXT = ((CONTEXT & 0x7bf7) << 1) | bit |
- ((line_m1 >> (7 - x_minor)) & 0x10) |
- ((line_m2 >> (7 - x_minor)) & 0x800);
- }
- gbreg_line[x >> 3] = result;
- }
+ bit = jbig2_arith_decode(as, &GB_stats[CONTEXT]);
+ if (bit < 0)
+ return -1;
+ result |= bit << (7 - x_minor);
+ CONTEXT = ((CONTEXT & 0x7bf7) << 1) | bit | ((line_m1 >> (7 - x_minor)) & 0x10) | ((line_m2 >> (7 - x_minor)) & 0x800);
+ }
+ gbreg_line[x >> 3] = result;
+ }
#ifdef OUTPUT_PBM
- fwrite(gbreg_line, 1, rowstride, stdout);
+ fwrite(gbreg_line, 1, rowstride, stdout);
#endif
- gbreg_line += rowstride;
+ gbreg_line += rowstride;
}
- return 0;
+ return 0;
}
static int
jbig2_decode_generic_template0_unopt(Jbig2Ctx *ctx,
- Jbig2Segment *segment,
- const Jbig2GenericRegionParams *params,
- Jbig2ArithState *as,
- Jbig2Image *image,
- Jbig2ArithCx *GB_stats)
+ Jbig2Segment *segment,
+ const Jbig2GenericRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GB_stats)
{
- const int GBW = image->width;
- const int GBH = image->height;
- uint32_t CONTEXT;
- int x,y;
- bool bit;
+ const int GBW = image->width;
+ const int GBH = image->height;
+ uint32_t CONTEXT;
+ int x, y;
+ bool bit;
- /* this version is generic and easy to understand, but very slow */
+ /* this version is generic and easy to understand, but very slow */
- for (y = 0; y < GBH; y++) {
- for (x = 0; x < GBW; x++) {
- CONTEXT = 0;
- CONTEXT |= jbig2_image_get_pixel(image, x - 1, y) << 0;
- CONTEXT |= jbig2_image_get_pixel(image, x - 2, y) << 1;
- CONTEXT |= jbig2_image_get_pixel(image, x - 3, y) << 2;
- CONTEXT |= jbig2_image_get_pixel(image, x - 4, y) << 3;
- CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0],
- y + params->gbat[1]) << 4;
- CONTEXT |= jbig2_image_get_pixel(image, x + 2, y - 1) << 5;
- CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 1) << 6;
- CONTEXT |= jbig2_image_get_pixel(image, x + 0, y - 1) << 7;
- CONTEXT |= jbig2_image_get_pixel(image, x - 1, y - 1) << 8;
- CONTEXT |= jbig2_image_get_pixel(image, x - 2, y - 1) << 9;
- CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[2],
- y + params->gbat[3]) << 10;
- CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[4],
- y + params->gbat[5]) << 11;
- CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 2) << 12;
- CONTEXT |= jbig2_image_get_pixel(image, x + 0, y - 2) << 13;
- CONTEXT |= jbig2_image_get_pixel(image, x - 1, y - 2) << 14;
- CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[6],
- y + params->gbat[7]) << 15;
- bit = jbig2_arith_decode(as, &GB_stats[CONTEXT]);
- if (bit < 0)
- return -1;
- jbig2_image_set_pixel(image, x, y, bit);
+ for (y = 0; y < GBH; y++) {
+ for (x = 0; x < GBW; x++) {
+ CONTEXT = 0;
+ CONTEXT |= jbig2_image_get_pixel(image, x - 1, y) << 0;
+ CONTEXT |= jbig2_image_get_pixel(image, x - 2, y) << 1;
+ CONTEXT |= jbig2_image_get_pixel(image, x - 3, y) << 2;
+ CONTEXT |= jbig2_image_get_pixel(image, x - 4, y) << 3;
+ CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0], y + params->gbat[1]) << 4;
+ CONTEXT |= jbig2_image_get_pixel(image, x + 2, y - 1) << 5;
+ CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 1) << 6;
+ CONTEXT |= jbig2_image_get_pixel(image, x + 0, y - 1) << 7;
+ CONTEXT |= jbig2_image_get_pixel(image, x - 1, y - 1) << 8;
+ CONTEXT |= jbig2_image_get_pixel(image, x - 2, y - 1) << 9;
+ CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[2], y + params->gbat[3]) << 10;
+ CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[4], y + params->gbat[5]) << 11;
+ CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 2) << 12;
+ CONTEXT |= jbig2_image_get_pixel(image, x + 0, y - 2) << 13;
+ CONTEXT |= jbig2_image_get_pixel(image, x - 1, y - 2) << 14;
+ CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[6], y + params->gbat[7]) << 15;
+ bit = jbig2_arith_decode(as, &GB_stats[CONTEXT]);
+ if (bit < 0)
+ return -1;
+ jbig2_image_set_pixel(image, x, y, bit);
+ }
}
- }
- return 0;
+ return 0;
}
static int
jbig2_decode_generic_template1(Jbig2Ctx *ctx,
- Jbig2Segment *segment,
- const Jbig2GenericRegionParams *params,
- Jbig2ArithState *as,
- Jbig2Image *image,
- Jbig2ArithCx *GB_stats)
+ Jbig2Segment *segment,
+ const Jbig2GenericRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GB_stats)
{
- const int GBW = image->width;
- const int GBH = image->height;
- const int rowstride = image->stride;
- int x, y;
- byte *gbreg_line = (byte *)image->data;
+ const int GBW = image->width;
+ const int GBH = image->height;
+ const int rowstride = image->stride;
+ int x, y;
+ byte *gbreg_line = (byte *) image->data;
- /* todo: currently we only handle the nominal gbat location */
+ /* todo: currently we only handle the nominal gbat location */
#ifdef OUTPUT_PBM
- printf("P4\n%d %d\n", GBW, GBH);
+ printf("P4\n%d %d\n", GBW, GBH);
#endif
- if (GBW <= 0)
- return 0;
+ if (GBW <= 0)
+ return 0;
- for (y = 0; y < GBH; y++)
- {
- uint32_t CONTEXT;
- uint32_t line_m1;
- uint32_t line_m2;
- int padded_width = (GBW + 7) & -8;
+ for (y = 0; y < GBH; y++) {
+ uint32_t CONTEXT;
+ uint32_t line_m1;
+ uint32_t line_m2;
+ int padded_width = (GBW + 7) & -8;
- line_m1 = (y >= 1) ? gbreg_line[-rowstride] : 0;
- line_m2 = (y >= 2) ? gbreg_line[-(rowstride << 1)] << 5 : 0;
- CONTEXT = ((line_m1 >> 1) & 0x1f8) | ((line_m2 >> 1) & 0x1e00);
+ line_m1 = (y >= 1) ? gbreg_line[-rowstride] : 0;
+ line_m2 = (y >= 2) ? gbreg_line[-(rowstride << 1)] << 5 : 0;
+ CONTEXT = ((line_m1 >> 1) & 0x1f8) | ((line_m2 >> 1) & 0x1e00);
- /* 6.2.5.7 3d */
- for (x = 0; x < padded_width; x += 8)
- {
- byte result = 0;
- int x_minor;
- int minor_width = GBW - x > 8 ? 8 : GBW - x;
+ /* 6.2.5.7 3d */
+ for (x = 0; x < padded_width; x += 8) {
+ byte result = 0;
+ int x_minor;
+ int minor_width = GBW - x > 8 ? 8 : GBW - x;
- if (y >= 1)
- line_m1 = (line_m1 << 8) |
- (x + 8 < GBW ? gbreg_line[-rowstride + (x >> 3) + 1] : 0);
+ if (y >= 1)
+ line_m1 = (line_m1 << 8) | (x + 8 < GBW ? gbreg_line[-rowstride + (x >> 3) + 1] : 0);
- if (y >= 2)
- line_m2 = (line_m2 << 8) |
- (x + 8 < GBW ? gbreg_line[-(rowstride << 1) + (x >> 3) + 1] << 5: 0);
+ if (y >= 2)
+ line_m2 = (line_m2 << 8) | (x + 8 < GBW ? gbreg_line[-(rowstride << 1) + (x >> 3) + 1] << 5 : 0);
- /* This is the speed-critical inner loop. */
- for (x_minor = 0; x_minor < minor_width; x_minor++)
- {
- bool bit;
+ /* This is the speed-critical inner loop. */
+ for (x_minor = 0; x_minor < minor_width; x_minor++) {
+ bool bit;
- bit = jbig2_arith_decode(as, &GB_stats[CONTEXT]);
- if (bit < 0)
- return -1;
- result |= bit << (7 - x_minor);
- CONTEXT = ((CONTEXT & 0xefb) << 1) | bit |
- ((line_m1 >> (8 - x_minor)) & 0x8) |
- ((line_m2 >> (8 - x_minor)) & 0x200);
- }
- gbreg_line[x >> 3] = result;
- }
+ bit = jbig2_arith_decode(as, &GB_stats[CONTEXT]);
+ if (bit < 0)
+ return -1;
+ result |= bit << (7 - x_minor);
+ CONTEXT = ((CONTEXT & 0xefb) << 1) | bit | ((line_m1 >> (8 - x_minor)) & 0x8) | ((line_m2 >> (8 - x_minor)) & 0x200);
+ }
+ gbreg_line[x >> 3] = result;
+ }
#ifdef OUTPUT_PBM
- fwrite(gbreg_line, 1, rowstride, stdout);
+ fwrite(gbreg_line, 1, rowstride, stdout);
#endif
- gbreg_line += rowstride;
+ gbreg_line += rowstride;
}
- return 0;
+ return 0;
}
static int
jbig2_decode_generic_template2(Jbig2Ctx *ctx,
- Jbig2Segment *segment,
- const Jbig2GenericRegionParams *params,
- Jbig2ArithState *as,
- Jbig2Image *image,
- Jbig2ArithCx *GB_stats)
+ Jbig2Segment *segment,
+ const Jbig2GenericRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GB_stats)
{
- const int GBW = image->width;
- const int GBH = image->height;
- const int rowstride = image->stride;
- int x, y;
- byte *gbreg_line = (byte *)image->data;
+ const int GBW = image->width;
+ const int GBH = image->height;
+ const int rowstride = image->stride;
+ int x, y;
+ byte *gbreg_line = (byte *) image->data;
- /* todo: currently we only handle the nominal gbat location */
+ /* todo: currently we only handle the nominal gbat location */
#ifdef OUTPUT_PBM
- printf("P4\n%d %d\n", GBW, GBH);
+ printf("P4\n%d %d\n", GBW, GBH);
#endif
- if (GBW <= 0)
- return 0;
+ if (GBW <= 0)
+ return 0;
- for (y = 0; y < GBH; y++)
- {
- uint32_t CONTEXT;
- uint32_t line_m1;
- uint32_t line_m2;
- int padded_width = (GBW + 7) & -8;
+ for (y = 0; y < GBH; y++) {
+ uint32_t CONTEXT;
+ uint32_t line_m1;
+ uint32_t line_m2;
+ int padded_width = (GBW + 7) & -8;
- line_m1 = (y >= 1) ? gbreg_line[-rowstride] : 0;
- line_m2 = (y >= 2) ? gbreg_line[-(rowstride << 1)] << 4 : 0;
- CONTEXT = ((line_m1 >> 3) & 0x7c) | ((line_m2 >> 3) & 0x380);
+ line_m1 = (y >= 1) ? gbreg_line[-rowstride] : 0;
+ line_m2 = (y >= 2) ? gbreg_line[-(rowstride << 1)] << 4 : 0;
+ CONTEXT = ((line_m1 >> 3) & 0x7c) | ((line_m2 >> 3) & 0x380);
- /* 6.2.5.7 3d */
- for (x = 0; x < padded_width; x += 8)
- {
- byte result = 0;
- int x_minor;
- int minor_width = GBW - x > 8 ? 8 : GBW - x;
+ /* 6.2.5.7 3d */
+ for (x = 0; x < padded_width; x += 8) {
+ byte result = 0;
+ int x_minor;
+ int minor_width = GBW - x > 8 ? 8 : GBW - x;
- if (y >= 1)
- line_m1 = (line_m1 << 8) |
- (x + 8 < GBW ? gbreg_line[-rowstride + (x >> 3) + 1] : 0);
+ if (y >= 1)
+ line_m1 = (line_m1 << 8) | (x + 8 < GBW ? gbreg_line[-rowstride + (x >> 3) + 1] : 0);
- if (y >= 2)
- line_m2 = (line_m2 << 8) |
- (x + 8 < GBW ? gbreg_line[-(rowstride << 1) + (x >> 3) + 1] << 4: 0);
+ if (y >= 2)
+ line_m2 = (line_m2 << 8) | (x + 8 < GBW ? gbreg_line[-(rowstride << 1) + (x >> 3) + 1] << 4 : 0);
- /* This is the speed-critical inner loop. */
- for (x_minor = 0; x_minor < minor_width; x_minor++)
- {
- bool bit;
+ /* This is the speed-critical inner loop. */
+ for (x_minor = 0; x_minor < minor_width; x_minor++) {
+ bool bit;
- bit = jbig2_arith_decode(as, &GB_stats[CONTEXT]);
- if (bit < 0)
- return -1;
- result |= bit << (7 - x_minor);
- CONTEXT = ((CONTEXT & 0x1bd) << 1) | bit |
- ((line_m1 >> (10 - x_minor)) & 0x4) |
- ((line_m2 >> (10 - x_minor)) & 0x80);
- }
- gbreg_line[x >> 3] = result;
- }
+ bit = jbig2_arith_decode(as, &GB_stats[CONTEXT]);
+ if (bit < 0)
+ return -1;
+ result |= bit << (7 - x_minor);
+ CONTEXT = ((CONTEXT & 0x1bd) << 1) | bit | ((line_m1 >> (10 - x_minor)) & 0x4) | ((line_m2 >> (10 - x_minor)) & 0x80);
+ }
+ gbreg_line[x >> 3] = result;
+ }
#ifdef OUTPUT_PBM
- fwrite(gbreg_line, 1, rowstride, stdout);
+ fwrite(gbreg_line, 1, rowstride, stdout);
#endif
- gbreg_line += rowstride;
+ gbreg_line += rowstride;
}
- return 0;
+ return 0;
}
static int
jbig2_decode_generic_template2a(Jbig2Ctx *ctx,
- Jbig2Segment *segment,
- const Jbig2GenericRegionParams *params,
- Jbig2ArithState *as,
- Jbig2Image *image,
- Jbig2ArithCx *GB_stats)
+ Jbig2Segment *segment,
+ const Jbig2GenericRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GB_stats)
{
- const int GBW = image->width;
- const int GBH = image->height;
- const int rowstride = image->stride;
- int x, y;
- byte *gbreg_line = (byte *)image->data;
+ const int GBW = image->width;
+ const int GBH = image->height;
+ const int rowstride = image->stride;
+ int x, y;
+ byte *gbreg_line = (byte *) image->data;
- /* This is a special case for GBATX1 = 3, GBATY1 = -1 */
+ /* This is a special case for GBATX1 = 3, GBATY1 = -1 */
#ifdef OUTPUT_PBM
- printf("P4\n%d %d\n", GBW, GBH);
+ printf("P4\n%d %d\n", GBW, GBH);
#endif
- if (GBW <= 0)
- return 0;
+ if (GBW <= 0)
+ return 0;
- for (y = 0; y < GBH; y++)
- {
- uint32_t CONTEXT;
- uint32_t line_m1;
- uint32_t line_m2;
- int padded_width = (GBW + 7) & -8;
+ for (y = 0; y < GBH; y++) {
+ uint32_t CONTEXT;
+ uint32_t line_m1;
+ uint32_t line_m2;
+ int padded_width = (GBW + 7) & -8;
- line_m1 = (y >= 1) ? gbreg_line[-rowstride] : 0;
- line_m2 = (y >= 2) ? gbreg_line[-(rowstride << 1)] << 4 : 0;
- CONTEXT = ((line_m1 >> 3) & 0x78) | ((line_m1 >> 2) & 0x4) | ((line_m2 >> 3) & 0x380);
+ line_m1 = (y >= 1) ? gbreg_line[-rowstride] : 0;
+ line_m2 = (y >= 2) ? gbreg_line[-(rowstride << 1)] << 4 : 0;
+ CONTEXT = ((line_m1 >> 3) & 0x78) | ((line_m1 >> 2) & 0x4) | ((line_m2 >> 3) & 0x380);
- /* 6.2.5.7 3d */
- for (x = 0; x < padded_width; x += 8)
- {
- byte result = 0;
- int x_minor;
- int minor_width = GBW - x > 8 ? 8 : GBW - x;
+ /* 6.2.5.7 3d */
+ for (x = 0; x < padded_width; x += 8) {
+ byte result = 0;
+ int x_minor;
+ int minor_width = GBW - x > 8 ? 8 : GBW - x;
- if (y >= 1)
- line_m1 = (line_m1 << 8) |
- (x + 8 < GBW ? gbreg_line[-rowstride + (x >> 3) + 1] : 0);
+ if (y >= 1)
+ line_m1 = (line_m1 << 8) | (x + 8 < GBW ? gbreg_line[-rowstride + (x >> 3) + 1] : 0);
- if (y >= 2)
- line_m2 = (line_m2 << 8) |
- (x + 8 < GBW ? gbreg_line[-(rowstride << 1) + (x >> 3) + 1] << 4: 0);
+ if (y >= 2)
+ line_m2 = (line_m2 << 8) | (x + 8 < GBW ? gbreg_line[-(rowstride << 1) + (x >> 3) + 1] << 4 : 0);
- /* This is the speed-critical inner loop. */
- for (x_minor = 0; x_minor < minor_width; x_minor++)
- {
- bool bit;
+ /* This is the speed-critical inner loop. */
+ for (x_minor = 0; x_minor < minor_width; x_minor++) {
+ bool bit;
- bit = jbig2_arith_decode(as, &GB_stats[CONTEXT]);
- if (bit < 0)
- return -1;
- result |= bit << (7 - x_minor);
- CONTEXT = ((CONTEXT & 0x1b9) << 1) | bit |
- ((line_m1 >> (10 - x_minor)) & 0x8) |
- ((line_m1 >> (9 - x_minor)) & 0x4) |
- ((line_m2 >> (10 - x_minor)) & 0x80);
- }
- gbreg_line[x >> 3] = result;
- }
+ bit = jbig2_arith_decode(as, &GB_stats[CONTEXT]);
+ if (bit < 0)
+ return -1;
+ result |= bit << (7 - x_minor);
+ CONTEXT = ((CONTEXT & 0x1b9) << 1) | bit |
+ ((line_m1 >> (10 - x_minor)) & 0x8) | ((line_m1 >> (9 - x_minor)) & 0x4) | ((line_m2 >> (10 - x_minor)) & 0x80);
+ }
+ gbreg_line[x >> 3] = result;
+ }
#ifdef OUTPUT_PBM
- fwrite(gbreg_line, 1, rowstride, stdout);
+ fwrite(gbreg_line, 1, rowstride, stdout);
#endif
- gbreg_line += rowstride;
+ gbreg_line += rowstride;
}
- return 0;
+ return 0;
}
#ifdef UNUSED
static int
jbig2_decode_generic_template3(Jbig2Ctx *ctx,
- Jbig2Segment *segment,
- const Jbig2GenericRegionParams *params,
- Jbig2ArithState *as,
- Jbig2Image *image,
- Jbig2ArithCx *GB_stats)
+ Jbig2Segment *segment,
+ const Jbig2GenericRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GB_stats)
{
- const int GBW = image->width;
- const int GBH = image->height;
- const int rowstride = image->stride;
- byte *gbreg_line = (byte *)image->data;
- int x, y;
+ const int GBW = image->width;
+ const int GBH = image->height;
+ const int rowstride = image->stride;
+ byte *gbreg_line = (byte *) image->data;
+ int x, y;
- /* this routine only handles the nominal AT location */
+ /* this routine only handles the nominal AT location */
#ifdef OUTPUT_PBM
- printf("P4\n%d %d\n", GBW, GBH);
+ printf("P4\n%d %d\n", GBW, GBH);
#endif
- if (GBW <= 0)
- return 0;
+ if (GBW <= 0)
+ return 0;
- for (y = 0; y < GBH; y++)
- {
- uint32_t CONTEXT;
- uint32_t line_m1;
- int padded_width = (GBW + 7) & -8;
+ for (y = 0; y < GBH; y++) {
+ uint32_t CONTEXT;
+ uint32_t line_m1;
+ int padded_width = (GBW + 7) & -8;
- line_m1 = (y >= 1) ? gbreg_line[-rowstride] : 0;
- CONTEXT = (line_m1 >> 1) & 0x3f0;
+ line_m1 = (y >= 1) ? gbreg_line[-rowstride] : 0;
+ CONTEXT = (line_m1 >> 1) & 0x3f0;
- /* 6.2.5.7 3d */
- for (x = 0; x < padded_width; x += 8)
- {
- byte result = 0;
- int x_minor;
- int minor_width = GBW - x > 8 ? 8 : GBW - x;
+ /* 6.2.5.7 3d */
+ for (x = 0; x < padded_width; x += 8) {
+ byte result = 0;
+ int x_minor;
+ int minor_width = GBW - x > 8 ? 8 : GBW - x;
- if (y >= 1)
- line_m1 = (line_m1 << 8) |
- (x + 8 < GBW ? gbreg_line[-rowstride + (x >> 3) + 1] : 0);
+ if (y >= 1)
+ line_m1 = (line_m1 << 8) | (x + 8 < GBW ? gbreg_line[-rowstride + (x >> 3) + 1] : 0);
- /* This is the speed-critical inner loop. */
- for (x_minor = 0; x_minor < minor_width; x_minor++)
- {
- bool bit;
+ /* This is the speed-critical inner loop. */
+ for (x_minor = 0; x_minor < minor_width; x_minor++) {
+ bool bit;
- bit = jbig2_arith_decode(as, &GB_stats[CONTEXT]);
- if (bit < 0)
- return -1;
- result |= bit << (7 - x_minor);
- CONTEXT = ((CONTEXT & 0x1f7) << 1) | bit |
- ((line_m1 >> (10 - x_minor)) & 0x010);
- }
- gbreg_line[x >> 3] = result;
- }
+ bit = jbig2_arith_decode(as, &GB_stats[CONTEXT]);
+ if (bit < 0)
+ return -1;
+ result |= bit << (7 - x_minor);
+ CONTEXT = ((CONTEXT & 0x1f7) << 1) | bit | ((line_m1 >> (10 - x_minor)) & 0x010);
+ }
+ gbreg_line[x >> 3] = result;
+ }
#ifdef OUTPUT_PBM
- fwrite(gbreg_line, 1, rowstride, stdout);
+ fwrite(gbreg_line, 1, rowstride, stdout);
#endif
- gbreg_line += rowstride;
+ gbreg_line += rowstride;
}
- return 0;
+ return 0;
}
#endif
static int
jbig2_decode_generic_template3_unopt(Jbig2Ctx *ctx,
- Jbig2Segment *segment,
- const Jbig2GenericRegionParams *params,
- Jbig2ArithState *as,
- Jbig2Image *image,
- Jbig2ArithCx *GB_stats)
+ Jbig2Segment *segment,
+ const Jbig2GenericRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GB_stats)
{
- const int GBW = image->width;
- const int GBH = image->height;
- uint32_t CONTEXT;
- int x,y;
- bool bit;
+ const int GBW = image->width;
+ const int GBH = image->height;
+ uint32_t CONTEXT;
+ int x, y;
+ bool bit;
- /* this version is generic and easy to understand, but very slow */
+ /* this version is generic and easy to understand, but very slow */
- for (y = 0; y < GBH; y++) {
- for (x = 0; x < GBW; x++) {
- CONTEXT = 0;
- CONTEXT |= jbig2_image_get_pixel(image, x - 1, y) << 0;
- CONTEXT |= jbig2_image_get_pixel(image, x - 2, y) << 1;
- CONTEXT |= jbig2_image_get_pixel(image, x - 3, y) << 2;
- CONTEXT |= jbig2_image_get_pixel(image, x - 4, y) << 3;
- CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0],
- y + params->gbat[1]) << 4;
- CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 1) << 5;
- CONTEXT |= jbig2_image_get_pixel(image, x + 0, y - 1) << 6;
- CONTEXT |= jbig2_image_get_pixel(image, x - 1, y - 1) << 7;
- CONTEXT |= jbig2_image_get_pixel(image, x - 2, y - 1) << 8;
- CONTEXT |= jbig2_image_get_pixel(image, x - 3, y - 1) << 9;
- bit = jbig2_arith_decode(as, &GB_stats[CONTEXT]);
- if (bit < 0)
- return -1;
- jbig2_image_set_pixel(image, x, y, bit);
+ for (y = 0; y < GBH; y++) {
+ for (x = 0; x < GBW; x++) {
+ CONTEXT = 0;
+ CONTEXT |= jbig2_image_get_pixel(image, x - 1, y) << 0;
+ CONTEXT |= jbig2_image_get_pixel(image, x - 2, y) << 1;
+ CONTEXT |= jbig2_image_get_pixel(image, x - 3, y) << 2;
+ CONTEXT |= jbig2_image_get_pixel(image, x - 4, y) << 3;
+ CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0], y + params->gbat[1]) << 4;
+ CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 1) << 5;
+ CONTEXT |= jbig2_image_get_pixel(image, x + 0, y - 1) << 6;
+ CONTEXT |= jbig2_image_get_pixel(image, x - 1, y - 1) << 7;
+ CONTEXT |= jbig2_image_get_pixel(image, x - 2, y - 1) << 8;
+ CONTEXT |= jbig2_image_get_pixel(image, x - 3, y - 1) << 9;
+ bit = jbig2_arith_decode(as, &GB_stats[CONTEXT]);
+ if (bit < 0)
+ return -1;
+ jbig2_image_set_pixel(image, x, y, bit);
+ }
}
- }
- return 0;
+ return 0;
}
static void
copy_prev_row(Jbig2Image *image, int row)
{
- if (!row) {
- /* no previous row */
- memset( image->data, 0, image->stride );
- } else {
- /* duplicate data from the previous row */
- uint8_t *src = image->data + (row - 1) * image->stride;
- memcpy( src + image->stride, src, image->stride );
- }
+ if (!row) {
+ /* no previous row */
+ memset(image->data, 0, image->stride);
+ } else {
+ /* duplicate data from the previous row */
+ uint8_t *src = image->data + (row - 1) * image->stride;
+
+ memcpy(src + image->stride, src, image->stride);
+ }
}
static int
jbig2_decode_generic_template0_TPGDON(Jbig2Ctx *ctx,
- Jbig2Segment *segment,
- const Jbig2GenericRegionParams *params,
- Jbig2ArithState *as,
- Jbig2Image *image,
- Jbig2ArithCx *GB_stats)
+ Jbig2Segment *segment,
+ const Jbig2GenericRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GB_stats)
{
- const int GBW = image->width;
- const int GBH = image->height;
- uint32_t CONTEXT;
- int x, y;
- bool bit;
- int LTP = 0;
+ const int GBW = image->width;
+ const int GBH = image->height;
+ uint32_t CONTEXT;
+ int x, y;
+ bool bit;
+ int LTP = 0;
- for (y = 0; y < GBH; y++)
- {
- bit = jbig2_arith_decode(as, &GB_stats[0x9B25]);
- if (bit < 0)
- return -1;
- LTP ^= bit;
- if (!LTP) {
- for (x = 0; x < GBW; x++) {
- CONTEXT = jbig2_image_get_pixel(image, x - 1, y);
- CONTEXT |= jbig2_image_get_pixel(image, x - 2, y) << 1;
- CONTEXT |= jbig2_image_get_pixel(image, x - 3, y) << 2;
- CONTEXT |= jbig2_image_get_pixel(image, x - 4, y) << 3;
- CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0],
- y + params->gbat[1]) << 4;
- CONTEXT |= jbig2_image_get_pixel(image, x + 2, y - 1) << 5;
- CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 1) << 6;
- CONTEXT |= jbig2_image_get_pixel(image, x , y - 1) << 7;
- CONTEXT |= jbig2_image_get_pixel(image, x - 1, y - 1) << 8;
- CONTEXT |= jbig2_image_get_pixel(image, x - 2, y - 1) << 9;
- CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[2],
- y + params->gbat[3]) << 10;
- CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[4],
- y + params->gbat[5]) << 11;
- CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 2) << 12;
- CONTEXT |= jbig2_image_get_pixel(image, x , y - 2) << 13;
- CONTEXT |= jbig2_image_get_pixel(image, x - 1, y - 2) << 14;
- CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[6],
- y + params->gbat[7]) << 15;
- bit = jbig2_arith_decode(as, &GB_stats[CONTEXT]);
+ for (y = 0; y < GBH; y++) {
+ bit = jbig2_arith_decode(as, &GB_stats[0x9B25]);
if (bit < 0)
- return -1;
- jbig2_image_set_pixel(image, x, y, bit);
- }
- } else {
- copy_prev_row(image, y);
+ return -1;
+ LTP ^= bit;
+ if (!LTP) {
+ for (x = 0; x < GBW; x++) {
+ CONTEXT = jbig2_image_get_pixel(image, x - 1, y);
+ CONTEXT |= jbig2_image_get_pixel(image, x - 2, y) << 1;
+ CONTEXT |= jbig2_image_get_pixel(image, x - 3, y) << 2;
+ CONTEXT |= jbig2_image_get_pixel(image, x - 4, y) << 3;
+ CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0], y + params->gbat[1]) << 4;
+ CONTEXT |= jbig2_image_get_pixel(image, x + 2, y - 1) << 5;
+ CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 1) << 6;
+ CONTEXT |= jbig2_image_get_pixel(image, x, y - 1) << 7;
+ CONTEXT |= jbig2_image_get_pixel(image, x - 1, y - 1) << 8;
+ CONTEXT |= jbig2_image_get_pixel(image, x - 2, y - 1) << 9;
+ CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[2], y + params->gbat[3]) << 10;
+ CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[4], y + params->gbat[5]) << 11;
+ CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 2) << 12;
+ CONTEXT |= jbig2_image_get_pixel(image, x, y - 2) << 13;
+ CONTEXT |= jbig2_image_get_pixel(image, x - 1, y - 2) << 14;
+ CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[6], y + params->gbat[7]) << 15;
+ bit = jbig2_arith_decode(as, &GB_stats[CONTEXT]);
+ if (bit < 0)
+ return -1;
+ jbig2_image_set_pixel(image, x, y, bit);
+ }
+ } else {
+ copy_prev_row(image, y);
+ }
}
- }
- return 0;
+ return 0;
}
static int
-jbig2_decode_generic_template1_TPGDON(Jbig2Ctx *ctx,
- Jbig2Segment *segment,
- const Jbig2GenericRegionParams *params,
- Jbig2ArithState *as,
- Jbig2Image *image,
- Jbig2ArithCx *GB_stats)
+jbig2_decode_generic_template1_TPGDON(Jbig2Ctx *ctx,
+ Jbig2Segment *segment,
+ const Jbig2GenericRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GB_stats)
{
- const int GBW = image->width;
- const int GBH = image->height;
- uint32_t CONTEXT;
- int x, y;
- bool bit;
- int LTP = 0;
+ const int GBW = image->width;
+ const int GBH = image->height;
+ uint32_t CONTEXT;
+ int x, y;
+ bool bit;
+ int LTP = 0;
- for (y = 0; y < GBH; y++) {
- bit = jbig2_arith_decode(as, &GB_stats[0x0795]);
- if (bit < 0)
- return -1;
- LTP ^= bit;
- if (!LTP) {
- for (x = 0; x < GBW; x++) {
- CONTEXT = jbig2_image_get_pixel(image, x - 1, y);
- CONTEXT |= jbig2_image_get_pixel(image, x - 2, y) << 1;
- CONTEXT |= jbig2_image_get_pixel(image, x - 3, y) << 2;
- CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0],
- y + params->gbat[1]) << 3;
- CONTEXT |= jbig2_image_get_pixel(image, x + 2, y - 1) << 4;
- CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 1) << 5;
- CONTEXT |= jbig2_image_get_pixel(image, x , y - 1) << 6;
- CONTEXT |= jbig2_image_get_pixel(image, x - 1, y - 1) << 7;
- CONTEXT |= jbig2_image_get_pixel(image, x - 2, y - 1) << 8;
- CONTEXT |= jbig2_image_get_pixel(image, x + 2, y - 2) << 9;
- CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 2) << 10;
- CONTEXT |= jbig2_image_get_pixel(image, x , y - 2) << 11;
- CONTEXT |= jbig2_image_get_pixel(image, x - 1, y - 2) << 12;
- bit = jbig2_arith_decode(as, &GB_stats[CONTEXT]);
+ for (y = 0; y < GBH; y++) {
+ bit = jbig2_arith_decode(as, &GB_stats[0x0795]);
if (bit < 0)
- return -1;
- jbig2_image_set_pixel(image, x, y, bit);
- }
- } else {
- copy_prev_row(image, y);
+ return -1;
+ LTP ^= bit;
+ if (!LTP) {
+ for (x = 0; x < GBW; x++) {
+ CONTEXT = jbig2_image_get_pixel(image, x - 1, y);
+ CONTEXT |= jbig2_image_get_pixel(image, x - 2, y) << 1;
+ CONTEXT |= jbig2_image_get_pixel(image, x - 3, y) << 2;
+ CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0], y + params->gbat[1]) << 3;
+ CONTEXT |= jbig2_image_get_pixel(image, x + 2, y - 1) << 4;
+ CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 1) << 5;
+ CONTEXT |= jbig2_image_get_pixel(image, x, y - 1) << 6;
+ CONTEXT |= jbig2_image_get_pixel(image, x - 1, y - 1) << 7;
+ CONTEXT |= jbig2_image_get_pixel(image, x - 2, y - 1) << 8;
+ CONTEXT |= jbig2_image_get_pixel(image, x + 2, y - 2) << 9;
+ CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 2) << 10;
+ CONTEXT |= jbig2_image_get_pixel(image, x, y - 2) << 11;
+ CONTEXT |= jbig2_image_get_pixel(image, x - 1, y - 2) << 12;
+ bit = jbig2_arith_decode(as, &GB_stats[CONTEXT]);
+ if (bit < 0)
+ return -1;
+ jbig2_image_set_pixel(image, x, y, bit);
+ }
+ } else {
+ copy_prev_row(image, y);
+ }
}
- }
- return 0;
+ return 0;
}
static int
-jbig2_decode_generic_template2_TPGDON(Jbig2Ctx *ctx,
- Jbig2Segment *segment,
- const Jbig2GenericRegionParams *params,
- Jbig2ArithState *as,
- Jbig2Image *image,
- Jbig2ArithCx *GB_stats)
+jbig2_decode_generic_template2_TPGDON(Jbig2Ctx *ctx,
+ Jbig2Segment *segment,
+ const Jbig2GenericRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GB_stats)
{
- const int GBW = image->width;
- const int GBH = image->height;
- uint32_t CONTEXT;
- int x, y;
- bool bit;
- int LTP = 0;
+ const int GBW = image->width;
+ const int GBH = image->height;
+ uint32_t CONTEXT;
+ int x, y;
+ bool bit;
+ int LTP = 0;
- for (y = 0; y < GBH; y++) {
- bit = jbig2_arith_decode(as, &GB_stats[0xE5]);
- if (bit < 0)
- return -1;
- LTP ^= bit;
- if (!LTP) {
- for (x = 0; x < GBW; x++) {
- CONTEXT = jbig2_image_get_pixel(image, x - 1, y);
- CONTEXT |= jbig2_image_get_pixel(image, x - 2, y) << 1;
- CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0],
- y + params->gbat[1]) << 2;
- CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 1) << 3;
- CONTEXT |= jbig2_image_get_pixel(image, x , y - 1) << 4;
- CONTEXT |= jbig2_image_get_pixel(image, x - 1, y - 1) << 5;
- CONTEXT |= jbig2_image_get_pixel(image, x - 2, y - 1) << 6;
- CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 2) << 7;
- CONTEXT |= jbig2_image_get_pixel(image, x , y - 2) << 8;
- CONTEXT |= jbig2_image_get_pixel(image, x - 1, y - 2) << 9;
- bit = jbig2_arith_decode(as, &GB_stats[CONTEXT]);
+ for (y = 0; y < GBH; y++) {
+ bit = jbig2_arith_decode(as, &GB_stats[0xE5]);
if (bit < 0)
- return -1;
- jbig2_image_set_pixel(image, x, y, bit);
- }
- } else {
- copy_prev_row(image, y);
+ return -1;
+ LTP ^= bit;
+ if (!LTP) {
+ for (x = 0; x < GBW; x++) {
+ CONTEXT = jbig2_image_get_pixel(image, x - 1, y);
+ CONTEXT |= jbig2_image_get_pixel(image, x - 2, y) << 1;
+ CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0], y + params->gbat[1]) << 2;
+ CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 1) << 3;
+ CONTEXT |= jbig2_image_get_pixel(image, x, y - 1) << 4;
+ CONTEXT |= jbig2_image_get_pixel(image, x - 1, y - 1) << 5;
+ CONTEXT |= jbig2_image_get_pixel(image, x - 2, y - 1) << 6;
+ CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 2) << 7;
+ CONTEXT |= jbig2_image_get_pixel(image, x, y - 2) << 8;
+ CONTEXT |= jbig2_image_get_pixel(image, x - 1, y - 2) << 9;
+ bit = jbig2_arith_decode(as, &GB_stats[CONTEXT]);
+ if (bit < 0)
+ return -1;
+ jbig2_image_set_pixel(image, x, y, bit);
+ }
+ } else {
+ copy_prev_row(image, y);
+ }
}
- }
- return 0;
+ return 0;
}
static int
-jbig2_decode_generic_template3_TPGDON(Jbig2Ctx *ctx,
- Jbig2Segment *segment,
- const Jbig2GenericRegionParams *params,
- Jbig2ArithState *as,
- Jbig2Image *image,
- Jbig2ArithCx *GB_stats)
+jbig2_decode_generic_template3_TPGDON(Jbig2Ctx *ctx,
+ Jbig2Segment *segment,
+ const Jbig2GenericRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GB_stats)
{
- const int GBW = image->width;
- const int GBH = image->height;
- uint32_t CONTEXT;
- int x, y;
- bool bit;
- int LTP = 0;
+ const int GBW = image->width;
+ const int GBH = image->height;
+ uint32_t CONTEXT;
+ int x, y;
+ bool bit;
+ int LTP = 0;
- for (y = 0; y < GBH; y++) {
- bit = jbig2_arith_decode(as, &GB_stats[0x0195]);
- if (bit < 0)
- return -1;
- LTP ^= bit;
- if (!LTP) {
- for (x = 0; x < GBW; x++) {
- CONTEXT = jbig2_image_get_pixel(image, x - 1, y);
- CONTEXT |= jbig2_image_get_pixel(image, x - 2, y) << 1;
- CONTEXT |= jbig2_image_get_pixel(image, x - 3, y) << 2;
- CONTEXT |= jbig2_image_get_pixel(image, x - 4, y) << 3;
- CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0],
- y + params->gbat[1]) << 4;
- CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 1) << 5;
- CONTEXT |= jbig2_image_get_pixel(image, x , y - 1) << 6;
- CONTEXT |= jbig2_image_get_pixel(image, x - 1, y - 1) << 7;
- CONTEXT |= jbig2_image_get_pixel(image, x - 2, y - 1) << 8;
- CONTEXT |= jbig2_image_get_pixel(image, x - 3, y - 1) << 9;
- bit = jbig2_arith_decode(as, &GB_stats[CONTEXT]);
+ for (y = 0; y < GBH; y++) {
+ bit = jbig2_arith_decode(as, &GB_stats[0x0195]);
if (bit < 0)
- return -1;
- jbig2_image_set_pixel(image, x, y, bit);
- }
- } else {
- copy_prev_row(image, y);
+ return -1;
+ LTP ^= bit;
+ if (!LTP) {
+ for (x = 0; x < GBW; x++) {
+ CONTEXT = jbig2_image_get_pixel(image, x - 1, y);
+ CONTEXT |= jbig2_image_get_pixel(image, x - 2, y) << 1;
+ CONTEXT |= jbig2_image_get_pixel(image, x - 3, y) << 2;
+ CONTEXT |= jbig2_image_get_pixel(image, x - 4, y) << 3;
+ CONTEXT |= jbig2_image_get_pixel(image, x + params->gbat[0], y + params->gbat[1]) << 4;
+ CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 1) << 5;
+ CONTEXT |= jbig2_image_get_pixel(image, x, y - 1) << 6;
+ CONTEXT |= jbig2_image_get_pixel(image, x - 1, y - 1) << 7;
+ CONTEXT |= jbig2_image_get_pixel(image, x - 2, y - 1) << 8;
+ CONTEXT |= jbig2_image_get_pixel(image, x - 3, y - 1) << 9;
+ bit = jbig2_arith_decode(as, &GB_stats[CONTEXT]);
+ if (bit < 0)
+ return -1;
+ jbig2_image_set_pixel(image, x, y, bit);
+ }
+ } else {
+ copy_prev_row(image, y);
+ }
}
- }
- return 0;
+ return 0;
}
static int
jbig2_decode_generic_region_TPGDON(Jbig2Ctx *ctx,
- Jbig2Segment *segment,
- const Jbig2GenericRegionParams *params,
- Jbig2ArithState *as,
- Jbig2Image *image,
- Jbig2ArithCx *GB_stats)
+ Jbig2Segment *segment,
+ const Jbig2GenericRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GB_stats)
{
- switch (params->GBTEMPLATE) {
+ switch (params->GBTEMPLATE) {
case 0:
- return jbig2_decode_generic_template0_TPGDON(ctx, segment,
- params, as, image, GB_stats);
+ return jbig2_decode_generic_template0_TPGDON(ctx, segment, params, as, image, GB_stats);
case 1:
- return jbig2_decode_generic_template1_TPGDON(ctx, segment,
- params, as, image, GB_stats);
+ return jbig2_decode_generic_template1_TPGDON(ctx, segment, params, as, image, GB_stats);
case 2:
- return jbig2_decode_generic_template2_TPGDON(ctx, segment,
- params, as, image, GB_stats);
+ return jbig2_decode_generic_template2_TPGDON(ctx, segment, params, as, image, GB_stats);
case 3:
- return jbig2_decode_generic_template3_TPGDON(ctx, segment,
- params, as, image, GB_stats);
- }
+ return jbig2_decode_generic_template3_TPGDON(ctx, segment, params, as, image, GB_stats);
+ }
- return -1;
+ return -1;
}
/**
@@ -754,65 +667,45 @@
**/
int
jbig2_decode_generic_region(Jbig2Ctx *ctx,
- Jbig2Segment *segment,
- const Jbig2GenericRegionParams *params,
- Jbig2ArithState *as,
- Jbig2Image *image,
- Jbig2ArithCx *GB_stats)
+ Jbig2Segment *segment, const Jbig2GenericRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GB_stats)
{
- const int8_t *gbat = params->gbat;
+ const int8_t *gbat = params->gbat;
- if (image->stride * image->height > (1 << 24) && segment->data_length < image->stride * image->height / 256) {
- return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "region is far larger than data provided (%d << %d), aborting to prevent DOS",
- segment->data_length, image->stride * image->height);
- }
+ if (image->stride * image->height > (1 << 24) && segment->data_length < image->stride * image->height / 256) {
+ return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
+ "region is far larger than data provided (%d << %d), aborting to prevent DOS", segment->data_length, image->stride * image->height);
+ }
- if (!params->MMR && params->TPGDON)
- return jbig2_decode_generic_region_TPGDON(ctx, segment, params,
- as, image, GB_stats);
+ if (!params->MMR && params->TPGDON)
+ return jbig2_decode_generic_region_TPGDON(ctx, segment, params, as, image, GB_stats);
- if (!params->MMR && params->GBTEMPLATE == 0) {
- if (gbat[0] == +3 && gbat[1] == -1 &&
- gbat[2] == -3 && gbat[3] == -1 &&
- gbat[4] == +2 && gbat[5] == -2 &&
- gbat[6] == -2 && gbat[7] == -2)
- return jbig2_decode_generic_template0(ctx, segment, params,
- as, image, GB_stats);
- else
- return jbig2_decode_generic_template0_unopt(ctx, segment, params,
- as, image, GB_stats);
- } else if (!params->MMR && params->GBTEMPLATE == 1)
- return jbig2_decode_generic_template1(ctx, segment, params,
- as, image, GB_stats);
- else if (!params->MMR && params->GBTEMPLATE == 2)
- {
- if (gbat[0] == 3 && gbat[1] == -1)
- return jbig2_decode_generic_template2a(ctx, segment, params,
- as, image, GB_stats);
- else
- return jbig2_decode_generic_template2(ctx, segment, params,
- as, image, GB_stats);
+ if (!params->MMR && params->GBTEMPLATE == 0) {
+ if (gbat[0] == +3 && gbat[1] == -1 && gbat[2] == -3 && gbat[3] == -1 && gbat[4] == +2 && gbat[5] == -2 && gbat[6] == -2 && gbat[7] == -2)
+ return jbig2_decode_generic_template0(ctx, segment, params, as, image, GB_stats);
+ else
+ return jbig2_decode_generic_template0_unopt(ctx, segment, params, as, image, GB_stats);
+ } else if (!params->MMR && params->GBTEMPLATE == 1)
+ return jbig2_decode_generic_template1(ctx, segment, params, as, image, GB_stats);
+ else if (!params->MMR && params->GBTEMPLATE == 2) {
+ if (gbat[0] == 3 && gbat[1] == -1)
+ return jbig2_decode_generic_template2a(ctx, segment, params, as, image, GB_stats);
+ else
+ return jbig2_decode_generic_template2(ctx, segment, params, as, image, GB_stats);
+ } else if (!params->MMR && params->GBTEMPLATE == 3) {
+ if (gbat[0] == 2 && gbat[1] == -1)
+ return jbig2_decode_generic_template3_unopt(ctx, segment, params, as, image, GB_stats);
+ else
+ return jbig2_decode_generic_template3_unopt(ctx, segment, params, as, image, GB_stats);
}
- else if (!params->MMR && params->GBTEMPLATE == 3) {
- if (gbat[0] == 2 && gbat[1] == -1)
- return jbig2_decode_generic_template3_unopt(ctx, segment, params,
- as, image, GB_stats);
- else
- return jbig2_decode_generic_template3_unopt(ctx, segment, params,
- as, image, GB_stats);
- }
- {
- int i;
- for (i = 0; i < 8; i++)
- jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
- "gbat[%d] = %d", i, params->gbat[i]);
- }
- jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number,
- "decode_generic_region: MMR=%d, GBTEMPLATE=%d NYI",
- params->MMR, params->GBTEMPLATE);
- return -1;
+ {
+ int i;
+
+ for (i = 0; i < 8; i++)
+ jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "gbat[%d] = %d", i, params->gbat[i]);
+ }
+ jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "decode_generic_region: MMR=%d, GBTEMPLATE=%d NYI", params->MMR, params->GBTEMPLATE);
+ return -1;
}
/**
@@ -819,116 +712,91 @@
* Handler for immediate generic region segments
*/
int
-jbig2_immediate_generic_region(Jbig2Ctx *ctx, Jbig2Segment *segment,
- const byte *segment_data)
+jbig2_immediate_generic_region(Jbig2Ctx *ctx, Jbig2Segment *segment, const byte *segment_data)
{
- Jbig2RegionSegmentInfo rsi;
- byte seg_flags;
- int8_t gbat[8];
- int offset;
- int gbat_bytes = 0;
- Jbig2GenericRegionParams params;
- int code = 0;
- Jbig2Image *image = NULL;
- Jbig2WordStream *ws = NULL;
- Jbig2ArithState *as = NULL;
- Jbig2ArithCx *GB_stats = NULL;
+ Jbig2RegionSegmentInfo rsi;
+ byte seg_flags;
+ int8_t gbat[8];
+ int offset;
+ int gbat_bytes = 0;
+ Jbig2GenericRegionParams params;
+ int code = 0;
+ Jbig2Image *image = NULL;
+ Jbig2WordStream *ws = NULL;
+ Jbig2ArithState *as = NULL;
+ Jbig2ArithCx *GB_stats = NULL;
- /* 7.4.6 */
- if (segment->data_length < 18)
- return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "Segment too short");
+ /* 7.4.6 */
+ if (segment->data_length < 18)
+ return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "Segment too short");
- jbig2_get_region_segment_info(&rsi, segment_data);
- jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number,
- "generic region: %d x %d @ (%d, %d), flags = %02x",
- rsi.width, rsi.height, rsi.x, rsi.y, rsi.flags);
+ jbig2_get_region_segment_info(&rsi, segment_data);
+ jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "generic region: %d x %d @ (%d, %d), flags = %02x", rsi.width, rsi.height, rsi.x, rsi.y, rsi.flags);
- /* 7.4.6.2 */
- seg_flags = segment_data[17];
- jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number,
- "segment flags = %02x", seg_flags);
- if ((seg_flags & 1) && (seg_flags & 6))
- jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number,
- "MMR is 1, but GBTEMPLATE is not 0");
+ /* 7.4.6.2 */
+ seg_flags = segment_data[17];
+ jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "segment flags = %02x", seg_flags);
+ if ((seg_flags & 1) && (seg_flags & 6))
+ jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "MMR is 1, but GBTEMPLATE is not 0");
- /* 7.4.6.3 */
- if (!(seg_flags & 1))
- {
- gbat_bytes = (seg_flags & 6) ? 2 : 8;
- if (18 + gbat_bytes > segment->data_length)
- return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "Segment too short");
- memcpy(gbat, segment_data + 18, gbat_bytes);
- jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number,
- "gbat: %d, %d", gbat[0], gbat[1]);
+ /* 7.4.6.3 */
+ if (!(seg_flags & 1)) {
+ gbat_bytes = (seg_flags & 6) ? 2 : 8;
+ if (18 + gbat_bytes > segment->data_length)
+ return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "Segment too short");
+ memcpy(gbat, segment_data + 18, gbat_bytes);
+ jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "gbat: %d, %d", gbat[0], gbat[1]);
}
- offset = 18 + gbat_bytes;
+ offset = 18 + gbat_bytes;
- /* Table 34 */
- params.MMR = seg_flags & 1;
- params.GBTEMPLATE = (seg_flags & 6) >> 1;
- params.TPGDON = (seg_flags & 8) >> 3;
- params.USESKIP = 0;
- memcpy (params.gbat, gbat, gbat_bytes);
+ /* Table 34 */
+ params.MMR = seg_flags & 1;
+ params.GBTEMPLATE = (seg_flags & 6) >> 1;
+ params.TPGDON = (seg_flags & 8) >> 3;
+ params.USESKIP = 0;
+ memcpy(params.gbat, gbat, gbat_bytes);
- image = jbig2_image_new(ctx, rsi.width, rsi.height);
- if (image == NULL)
- return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "unable to allocate generic image");
- jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
- "allocated %d x %d image buffer for region decode results",
- rsi.width, rsi.height);
+ image = jbig2_image_new(ctx, rsi.width, rsi.height);
+ if (image == NULL)
+ return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "unable to allocate generic image");
+ jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "allocated %d x %d image buffer for region decode results", rsi.width, rsi.height);
- if (params.MMR)
- {
- code = jbig2_decode_generic_mmr(ctx, segment, ¶ms,
- segment_data + offset, segment->data_length - offset, image);
- }
- else
- {
- int stats_size = jbig2_generic_stats_size(ctx, params.GBTEMPLATE);
- GB_stats = jbig2_new(ctx, Jbig2ArithCx, stats_size);
- if (GB_stats == NULL)
- {
- code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "unable to allocate GB_stats in jbig2_immediate_generic_region");
- goto cleanup;
- }
- memset(GB_stats, 0, stats_size);
+ if (params.MMR) {
+ code = jbig2_decode_generic_mmr(ctx, segment, ¶ms, segment_data + offset, segment->data_length - offset, image);
+ } else {
+ int stats_size = jbig2_generic_stats_size(ctx, params.GBTEMPLATE);
- ws = jbig2_word_stream_buf_new(ctx, segment_data + offset,
- segment->data_length - offset);
- if (ws == NULL)
- {
- code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "unable to allocate ws in jbig2_immediate_generic_region");
- goto cleanup;
- }
- as = jbig2_arith_new(ctx, ws);
- if (as == NULL)
- {
- code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "unable to allocate as in jbig2_immediate_generic_region");
- goto cleanup;
- }
- code = jbig2_decode_generic_region(ctx, segment, ¶ms,
- as, image, GB_stats);
+ GB_stats = jbig2_new(ctx, Jbig2ArithCx, stats_size);
+ if (GB_stats == NULL) {
+ code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "unable to allocate GB_stats in jbig2_immediate_generic_region");
+ goto cleanup;
+ }
+ memset(GB_stats, 0, stats_size);
+
+ ws = jbig2_word_stream_buf_new(ctx, segment_data + offset, segment->data_length - offset);
+ if (ws == NULL) {
+ code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "unable to allocate ws in jbig2_immediate_generic_region");
+ goto cleanup;
+ }
+ as = jbig2_arith_new(ctx, ws);
+ if (as == NULL) {
+ code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "unable to allocate as in jbig2_immediate_generic_region");
+ goto cleanup;
+ }
+ code = jbig2_decode_generic_region(ctx, segment, ¶ms, as, image, GB_stats);
}
- if (code >= 0)
- jbig2_page_add_result(ctx, &ctx->pages[ctx->current_page],
- image, rsi.x, rsi.y, rsi.op);
- else
- jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "error while decoding immediate_generic_region");
+ if (code >= 0)
+ jbig2_page_add_result(ctx, &ctx->pages[ctx->current_page], image, rsi.x, rsi.y, rsi.op);
+ else
+ jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "error while decoding immediate_generic_region");
cleanup:
- jbig2_free(ctx->allocator, as);
- jbig2_word_stream_buf_free(ctx, ws);
- jbig2_free(ctx->allocator, GB_stats);
- jbig2_image_release(ctx, image);
+ jbig2_free(ctx->allocator, as);
+ jbig2_word_stream_buf_free(ctx, ws);
+ jbig2_free(ctx->allocator, GB_stats);
+ jbig2_image_release(ctx, image);
- return code;
+ return code;
}
--- a/jbig2_generic.h
+++ b/jbig2_generic.h
@@ -17,7 +17,6 @@
jbig2dec
*/
-
/**
* Headers for Generic and Generic Refinement region handling
**/
@@ -24,44 +23,35 @@
/* 6.4 Table 2 */
typedef struct {
- bool MMR;
- /* GBW */
- /* GBH */
- int GBTEMPLATE;
- bool TPGDON;
- bool USESKIP;
- /* SKIP */
- int8_t gbat[8];
+ bool MMR;
+ /* GBW */
+ /* GBH */
+ int GBTEMPLATE;
+ bool TPGDON;
+ bool USESKIP;
+ /* SKIP */
+ int8_t gbat[8];
} Jbig2GenericRegionParams;
/* return the appropriate context size for the given template */
-int
-jbig2_generic_stats_size(Jbig2Ctx *ctx, int template);
+int jbig2_generic_stats_size(Jbig2Ctx *ctx, int template);
int
jbig2_decode_generic_region(Jbig2Ctx *ctx,
- Jbig2Segment *segment,
- const Jbig2GenericRegionParams *params,
- Jbig2ArithState *as,
- Jbig2Image *image,
- Jbig2ArithCx *GB_stats);
+ Jbig2Segment *segment, const Jbig2GenericRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GB_stats);
-
/* 6.3 Table 6 */
typedef struct {
- /* GRW */
- /* GRH */
- bool GRTEMPLATE;
- Jbig2Image *reference;
- int32_t DX, DY;
- bool TPGRON;
- int8_t grat[4];
+ /* GRW */
+ /* GRH */
+ bool GRTEMPLATE;
+ Jbig2Image *reference;
+ int32_t DX, DY;
+ bool TPGRON;
+ int8_t grat[4];
} Jbig2RefinementRegionParams;
int
jbig2_decode_refinement_region(Jbig2Ctx *ctx,
- Jbig2Segment *segment,
- const Jbig2RefinementRegionParams *params,
- Jbig2ArithState *as,
- Jbig2Image *image,
- Jbig2ArithCx *GB_stats);
+ Jbig2Segment *segment,
+ const Jbig2RefinementRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GB_stats);
--- a/jbig2_halftone.c
+++ b/jbig2_halftone.c
@@ -17,7 +17,6 @@
jbig2dec
*/
-
/* JBIG2 Pattern Dictionary and Halftone Region decoding */
#ifdef HAVE_CONFIG_H
@@ -25,7 +24,7 @@
#endif
#include "os_types.h"
-#include <string.h> /* memset() */
+#include <string.h> /* memset() */
#include "jbig2.h"
#include "jbig2_priv.h"
@@ -39,56 +38,49 @@
* jbig2_hd_new: create a new dictionary from a collective bitmap
*/
Jbig2PatternDict *
-jbig2_hd_new(Jbig2Ctx *ctx,
- const Jbig2PatternDictParams *params,
- Jbig2Image *image)
+jbig2_hd_new(Jbig2Ctx *ctx, const Jbig2PatternDictParams *params, Jbig2Image *image)
{
- Jbig2PatternDict *new;
- const int N = params->GRAYMAX + 1;
- const int HPW = params->HDPW;
- const int HPH = params->HDPH;
- int i;
+ Jbig2PatternDict *new;
+ const int N = params->GRAYMAX + 1;
+ const int HPW = params->HDPW;
+ const int HPH = params->HDPH;
+ int i;
- /* allocate a new struct */
- new = jbig2_new(ctx, Jbig2PatternDict, 1);
- if (new != NULL) {
- new->patterns = jbig2_new(ctx, Jbig2Image*, N);
- if (new->patterns == NULL) {
- jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1,
- "failed to allocate pattern in collective bitmap dictionary");
- jbig2_free(ctx->allocator, new);
- return NULL;
- }
- new->n_patterns = N;
- new->HPW = HPW;
- new->HPH = HPH;
+ /* allocate a new struct */
+ new = jbig2_new(ctx, Jbig2PatternDict, 1);
+ if (new != NULL) {
+ new->patterns = jbig2_new(ctx, Jbig2Image *, N);
+ if (new->patterns == NULL) {
+ jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "failed to allocate pattern in collective bitmap dictionary");
+ jbig2_free(ctx->allocator, new);
+ return NULL;
+ }
+ new->n_patterns = N;
+ new->HPW = HPW;
+ new->HPH = HPH;
- /* 6.7.5(4) - copy out the individual pattern images */
- for (i = 0; i < N; i++) {
- new->patterns[i] = jbig2_image_new(ctx, HPW, HPH);
- if (new->patterns[i] == NULL) {
- int j;
- jbig2_error(ctx, JBIG2_SEVERITY_WARNING, -1,
- "failed to allocate pattern element image");
- for (j = 0; j < i; j++)
- jbig2_free(ctx->allocator, new->patterns[j]);
- jbig2_free(ctx->allocator, new);
- return NULL;
- }
- /* compose with the REPLACE operator; the source
- will be clipped to the destintion, selecting the
- proper sub image */
- jbig2_image_compose(ctx, new->patterns[i], image,
- -i * HPW, 0, JBIG2_COMPOSE_REPLACE);
+ /* 6.7.5(4) - copy out the individual pattern images */
+ for (i = 0; i < N; i++) {
+ new->patterns[i] = jbig2_image_new(ctx, HPW, HPH);
+ if (new->patterns[i] == NULL) {
+ int j;
+
+ jbig2_error(ctx, JBIG2_SEVERITY_WARNING, -1, "failed to allocate pattern element image");
+ for (j = 0; j < i; j++)
+ jbig2_free(ctx->allocator, new->patterns[j]);
+ jbig2_free(ctx->allocator, new);
+ return NULL;
+ }
+ /* compose with the REPLACE operator; the source
+ will be clipped to the destintion, selecting the
+ proper sub image */
+ jbig2_image_compose(ctx, new->patterns[i], image, -i * HPW, 0, JBIG2_COMPOSE_REPLACE);
+ }
+ } else {
+ jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "failed to allocate collective bitmap dictionary");
}
- }
- else
- {
- jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1,
- "failed to allocate collective bitmap dictionary");
- }
- return new;
+ return new;
}
/**
@@ -97,13 +89,15 @@
void
jbig2_hd_release(Jbig2Ctx *ctx, Jbig2PatternDict *dict)
{
- int i;
+ int i;
- if (dict == NULL) return;
- for (i = 0; i < dict->n_patterns; i++)
- if (dict->patterns[i]) jbig2_image_release(ctx, dict->patterns[i]);
- jbig2_free(ctx->allocator, dict->patterns);
- jbig2_free(ctx->allocator, dict);
+ if (dict == NULL)
+ return;
+ for (i = 0; i < dict->n_patterns; i++)
+ if (dict->patterns[i])
+ jbig2_image_release(ctx, dict->patterns[i]);
+ jbig2_free(ctx->allocator, dict->patterns);
+ jbig2_free(ctx->allocator, dict);
}
/**
@@ -124,133 +118,114 @@
**/
static Jbig2PatternDict *
jbig2_decode_pattern_dict(Jbig2Ctx *ctx, Jbig2Segment *segment,
- const Jbig2PatternDictParams *params,
- const byte *data, const size_t size,
- Jbig2ArithCx *GB_stats)
+ const Jbig2PatternDictParams *params, const byte *data, const size_t size, Jbig2ArithCx *GB_stats)
{
- Jbig2PatternDict *hd = NULL;
- Jbig2Image *image = NULL;
- Jbig2GenericRegionParams rparams;
- int code = 0;
+ Jbig2PatternDict *hd = NULL;
+ Jbig2Image *image = NULL;
+ Jbig2GenericRegionParams rparams;
+ int code = 0;
- /* allocate the collective image */
- image = jbig2_image_new(ctx,
- params->HDPW * (params->GRAYMAX + 1), params->HDPH);
- if (image == NULL) {
- jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number,
- "failed to allocate collective bitmap for halftone dict!");
- return NULL;
- }
+ /* allocate the collective image */
+ image = jbig2_image_new(ctx, params->HDPW * (params->GRAYMAX + 1), params->HDPH);
+ if (image == NULL) {
+ jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate collective bitmap for halftone dict!");
+ return NULL;
+ }
- /* fill out the generic region decoder parameters */
- rparams.MMR = params->HDMMR;
- rparams.GBTEMPLATE = params->HDTEMPLATE;
- rparams.TPGDON = 0; /* not used if HDMMR = 1 */
- rparams.USESKIP = 0;
- rparams.gbat[0] = -(int8_t)params->HDPW;
- rparams.gbat[1] = 0;
- rparams.gbat[2] = -3;
- rparams.gbat[3] = -1;
- rparams.gbat[4] = 2;
- rparams.gbat[5] = -2;
- rparams.gbat[6] = -2;
- rparams.gbat[7] = -2;
+ /* fill out the generic region decoder parameters */
+ rparams.MMR = params->HDMMR;
+ rparams.GBTEMPLATE = params->HDTEMPLATE;
+ rparams.TPGDON = 0; /* not used if HDMMR = 1 */
+ rparams.USESKIP = 0;
+ rparams.gbat[0] = -(int8_t) params->HDPW;
+ rparams.gbat[1] = 0;
+ rparams.gbat[2] = -3;
+ rparams.gbat[3] = -1;
+ rparams.gbat[4] = 2;
+ rparams.gbat[5] = -2;
+ rparams.gbat[6] = -2;
+ rparams.gbat[7] = -2;
- if (params->HDMMR) {
- code = jbig2_decode_generic_mmr(ctx, segment, &rparams, data, size, image);
- } else {
- Jbig2WordStream *ws = jbig2_word_stream_buf_new(ctx, data, size);
- if (ws != NULL)
- {
- Jbig2ArithState *as = jbig2_arith_new(ctx, ws);
- if (as != NULL)
- {
- code = jbig2_decode_generic_region(ctx, segment, &rparams,
- as, image, GB_stats);
- }
- else
- {
- code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number,
- "failed to allocate storage for as in halftone dict!");
- }
+ if (params->HDMMR) {
+ code = jbig2_decode_generic_mmr(ctx, segment, &rparams, data, size, image);
+ } else {
+ Jbig2WordStream *ws = jbig2_word_stream_buf_new(ctx, data, size);
- jbig2_free(ctx->allocator, as);
- jbig2_word_stream_buf_free(ctx, ws);
+ if (ws != NULL) {
+ Jbig2ArithState *as = jbig2_arith_new(ctx, ws);
+
+ if (as != NULL) {
+ code = jbig2_decode_generic_region(ctx, segment, &rparams, as, image, GB_stats);
+ } else {
+ code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate storage for as in halftone dict!");
+ }
+
+ jbig2_free(ctx->allocator, as);
+ jbig2_word_stream_buf_free(ctx, ws);
+ } else {
+ code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate storage for ws in halftone dict!");
+ }
}
- else
- {
- code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number,
- "failed to allocate storage for ws in halftone dict!");
- }
- }
- if (code == 0) hd = jbig2_hd_new(ctx, params, image);
- jbig2_image_release(ctx, image);
+ if (code == 0)
+ hd = jbig2_hd_new(ctx, params, image);
+ jbig2_image_release(ctx, image);
- return hd;
+ return hd;
}
/* 7.4.4 */
int
-jbig2_pattern_dictionary(Jbig2Ctx *ctx, Jbig2Segment *segment,
- const byte *segment_data)
+jbig2_pattern_dictionary(Jbig2Ctx *ctx, Jbig2Segment *segment, const byte *segment_data)
{
- Jbig2PatternDictParams params;
- Jbig2ArithCx *GB_stats = NULL;
- byte flags;
- int offset = 0;
+ Jbig2PatternDictParams params;
+ Jbig2ArithCx *GB_stats = NULL;
+ byte flags;
+ int offset = 0;
- /* 7.4.4.1 - Data header */
- if (segment->data_length < 7) {
- return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "Segment too short");
- }
- flags = segment_data[0];
- params.HDMMR = flags & 1;
- params.HDTEMPLATE = (flags & 6) >> 1;
- params.HDPW = segment_data[1];
- params.HDPH = segment_data[2];
- params.GRAYMAX = jbig2_get_uint32(segment_data + 3);
- offset += 7;
+ /* 7.4.4.1 - Data header */
+ if (segment->data_length < 7) {
+ return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "Segment too short");
+ }
+ flags = segment_data[0];
+ params.HDMMR = flags & 1;
+ params.HDTEMPLATE = (flags & 6) >> 1;
+ params.HDPW = segment_data[1];
+ params.HDPH = segment_data[2];
+ params.GRAYMAX = jbig2_get_uint32(segment_data + 3);
+ offset += 7;
- jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number,
- "pattern dictionary, flags=%02x, %d grays (%dx%d cell)",
- flags, params.GRAYMAX + 1, params.HDPW, params.HDPH);
+ jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number,
+ "pattern dictionary, flags=%02x, %d grays (%dx%d cell)", flags, params.GRAYMAX + 1, params.HDPW, params.HDPH);
- if (params.HDMMR && params.HDTEMPLATE) {
- jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number,
- "HDTEMPLATE is %d when HDMMR is %d, contrary to spec",
- params.HDTEMPLATE, params.HDMMR);
- }
- if (flags & 0xf8) {
- jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number,
- "Reserved flag bits non-zero");
- }
+ if (params.HDMMR && params.HDTEMPLATE) {
+ jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "HDTEMPLATE is %d when HDMMR is %d, contrary to spec", params.HDTEMPLATE, params.HDMMR);
+ }
+ if (flags & 0xf8) {
+ jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "Reserved flag bits non-zero");
+ }
- /* 7.4.4.2 */
- if (!params.HDMMR) {
- /* allocate and zero arithmetic coding stats */
- int stats_size = jbig2_generic_stats_size(ctx, params.HDTEMPLATE);
- GB_stats = jbig2_new(ctx, Jbig2ArithCx, stats_size);
- if (GB_stats == NULL)
- {
- jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "failed to allocate GB_stats in pattern dictionary");
- return 0;
+ /* 7.4.4.2 */
+ if (!params.HDMMR) {
+ /* allocate and zero arithmetic coding stats */
+ int stats_size = jbig2_generic_stats_size(ctx, params.HDTEMPLATE);
+
+ GB_stats = jbig2_new(ctx, Jbig2ArithCx, stats_size);
+ if (GB_stats == NULL) {
+ jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to allocate GB_stats in pattern dictionary");
+ return 0;
+ }
+ memset(GB_stats, 0, stats_size);
}
- memset(GB_stats, 0, stats_size);
- }
- segment->result = jbig2_decode_pattern_dict(ctx, segment, ¶ms,
- segment_data + offset,
- segment->data_length - offset, GB_stats);
+ segment->result = jbig2_decode_pattern_dict(ctx, segment, ¶ms, segment_data + offset, segment->data_length - offset, GB_stats);
- /* todo: retain GB_stats? */
- if (!params.HDMMR) {
- jbig2_free(ctx->allocator, GB_stats);
- }
+ /* todo: retain GB_stats? */
+ if (!params.HDMMR) {
+ jbig2_free(ctx->allocator, GB_stats);
+ }
- return (segment->result != NULL) ? 0 : -1;
+ return (segment->result != NULL) ? 0 : -1;
}
/**
@@ -263,7 +238,7 @@
* @GSMMR: if MMR is used
* @GSW: width of gray-scale image
* @GSH: height of gray-scale image
- * @GSBPP: number of bitplanes/Jbig2Images to use
+ * @GSBPP: number of bitplanes/Jbig2Images to use
* @GSKIP: mask indicating which values should be skipped
* @GSTEMPLATE: template used to code the gray-scale bitplanes
* @GB_stats: artimetic coding context to use
@@ -275,196 +250,179 @@
* 0 on failure
**/
uint8_t **
-jbig2_decode_gray_scale_image(Jbig2Ctx *ctx, Jbig2Segment* segment,
+jbig2_decode_gray_scale_image(Jbig2Ctx *ctx, Jbig2Segment *segment,
const byte *data, const size_t size,
- bool GSMMR, uint32_t GSW, uint32_t GSH,
- uint32_t GSBPP, bool GSUSESKIP,
- Jbig2Image *GSKIP, int GSTEMPLATE,
- Jbig2ArithCx *GB_stats)
+ bool GSMMR, uint32_t GSW, uint32_t GSH,
+ uint32_t GSBPP, bool GSUSESKIP, Jbig2Image *GSKIP, int GSTEMPLATE, Jbig2ArithCx *GB_stats)
{
- uint8_t **GSVALS = NULL;
- size_t consumed_bytes = 0;
- int i, j, code, stride;
- int x, y;
- Jbig2Image **GSPLANES;
- Jbig2GenericRegionParams rparams;
- Jbig2WordStream *ws = NULL;
- Jbig2ArithState *as = NULL;
+ uint8_t **GSVALS = NULL;
+ size_t consumed_bytes = 0;
+ int i, j, code, stride;
+ int x, y;
+ Jbig2Image **GSPLANES;
+ Jbig2GenericRegionParams rparams;
+ Jbig2WordStream *ws = NULL;
+ Jbig2ArithState *as = NULL;
- /* allocate GSPLANES */
- GSPLANES = jbig2_new(ctx, Jbig2Image*, GSBPP);
- if (GSPLANES == NULL) {
- jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "failed to allocate %d bytes for GSPLANES", GSBPP);
- return NULL;
- }
-
- for (i = 0; i < GSBPP; ++i) {
- GSPLANES[i] = jbig2_image_new(ctx, GSW, GSH);
- if (GSPLANES[i] == NULL) {
- jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "failed to allocate %dx%d image for GSPLANES", GSW, GSH);
- /* free already allocated */
- for (j = i-1; j >= 0; --j) {
- jbig2_image_release(ctx, GSPLANES[j]);
- }
- jbig2_free(ctx->allocator, GSPLANES);
- return NULL;
+ /* allocate GSPLANES */
+ GSPLANES = jbig2_new(ctx, Jbig2Image *, GSBPP);
+ if (GSPLANES == NULL) {
+ jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to allocate %d bytes for GSPLANES", GSBPP);
+ return NULL;
}
- }
- /* C.5 step 1. Decode GSPLANES[GSBPP-1] */
- /* fill generic region decoder parameters */
- rparams.MMR = GSMMR;
- rparams.GBTEMPLATE = GSTEMPLATE;
- rparams.TPGDON = 0;
- rparams.USESKIP = GSUSESKIP;
- rparams.gbat[0] = (GSTEMPLATE <= 1? 3 : 2);
- rparams.gbat[1] = -1;
- rparams.gbat[2] = -3;
- rparams.gbat[3] = -1;
- rparams.gbat[4] = 2;
- rparams.gbat[5] = -2;
- rparams.gbat[6] = -2;
- rparams.gbat[7] = -2;
-
- if (GSMMR) {
- code = jbig2_decode_halftone_mmr(ctx, &rparams, data, size,
- GSPLANES[GSBPP-1], &consumed_bytes);
- } else {
- ws = jbig2_word_stream_buf_new(ctx, data, size);
- if (ws == NULL)
- {
- jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "failed to allocate ws in jbig2_decode_gray_scale_image");
- goto cleanup;
+ for (i = 0; i < GSBPP; ++i) {
+ GSPLANES[i] = jbig2_image_new(ctx, GSW, GSH);
+ if (GSPLANES[i] == NULL) {
+ jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to allocate %dx%d image for GSPLANES", GSW, GSH);
+ /* free already allocated */
+ for (j = i - 1; j >= 0; --j) {
+ jbig2_image_release(ctx, GSPLANES[j]);
+ }
+ jbig2_free(ctx->allocator, GSPLANES);
+ return NULL;
+ }
}
- as = jbig2_arith_new(ctx, ws);
- if (as == NULL)
- {
- jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "failed to allocate as in jbig2_decode_gray_scale_image");
- goto cleanup;
- }
+ /* C.5 step 1. Decode GSPLANES[GSBPP-1] */
+ /* fill generic region decoder parameters */
+ rparams.MMR = GSMMR;
+ rparams.GBTEMPLATE = GSTEMPLATE;
+ rparams.TPGDON = 0;
+ rparams.USESKIP = GSUSESKIP;
+ rparams.gbat[0] = (GSTEMPLATE <= 1 ? 3 : 2);
+ rparams.gbat[1] = -1;
+ rparams.gbat[2] = -3;
+ rparams.gbat[3] = -1;
+ rparams.gbat[4] = 2;
+ rparams.gbat[5] = -2;
+ rparams.gbat[6] = -2;
+ rparams.gbat[7] = -2;
- code = jbig2_decode_generic_region(ctx, segment, &rparams, as,
- GSPLANES[GSBPP-1], GB_stats);
-
- }
- if (code != 0) {
- jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "error decoding GSPLANES for halftone image");
- goto cleanup;
- }
-
- /* C.5 step 2. Set j = GSBPP-2 */
- j = GSBPP - 2;
- /* C.5 step 3. decode loop */
- while(j >= 0) {
- /* C.5 step 3. (a) */
if (GSMMR) {
- code = jbig2_decode_halftone_mmr(ctx, &rparams, data + consumed_bytes,
- size - consumed_bytes, GSPLANES[j],
- &consumed_bytes);
+ code = jbig2_decode_halftone_mmr(ctx, &rparams, data, size, GSPLANES[GSBPP - 1], &consumed_bytes);
} else {
- code = jbig2_decode_generic_region(ctx, segment, &rparams, as,
- GSPLANES[j], GB_stats);
+ ws = jbig2_word_stream_buf_new(ctx, data, size);
+ if (ws == NULL) {
+ jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to allocate ws in jbig2_decode_gray_scale_image");
+ goto cleanup;
+ }
+
+ as = jbig2_arith_new(ctx, ws);
+ if (as == NULL) {
+ jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to allocate as in jbig2_decode_gray_scale_image");
+ goto cleanup;
+ }
+
+ code = jbig2_decode_generic_region(ctx, segment, &rparams, as, GSPLANES[GSBPP - 1], GB_stats);
+
}
if (code != 0) {
- jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "error decoding GSPLANES for halftone image");
- goto cleanup;
+ jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "error decoding GSPLANES for halftone image");
+ goto cleanup;
}
- /* C.5 step 3. (b):
- * for each [x,y]
- * GSPLANES[j][x][y] = GSPLANES[j+1][x][y] XOR GSPLANES[j][x][y] */
- stride = GSPLANES[0]->stride;
- for (i=0; i < stride * GSH; ++i)
- GSPLANES[j]->data[i] ^= GSPLANES[j+1]->data[i];
+ /* C.5 step 2. Set j = GSBPP-2 */
+ j = GSBPP - 2;
+ /* C.5 step 3. decode loop */
+ while (j >= 0) {
+ /* C.5 step 3. (a) */
+ if (GSMMR) {
+ code = jbig2_decode_halftone_mmr(ctx, &rparams, data + consumed_bytes, size - consumed_bytes, GSPLANES[j], &consumed_bytes);
+ } else {
+ code = jbig2_decode_generic_region(ctx, segment, &rparams, as, GSPLANES[j], GB_stats);
+ }
+ if (code != 0) {
+ jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "error decoding GSPLANES for halftone image");
+ goto cleanup;
+ }
- /* C.5 step 3. (c) */
- --j;
- }
+ /* C.5 step 3. (b):
+ * for each [x,y]
+ * GSPLANES[j][x][y] = GSPLANES[j+1][x][y] XOR GSPLANES[j][x][y] */
+ stride = GSPLANES[0]->stride;
+ for (i = 0; i < stride * GSH; ++i)
+ GSPLANES[j]->data[i] ^= GSPLANES[j + 1]->data[i];
- /* allocate GSVALS */
- GSVALS = jbig2_new(ctx, uint8_t* , GSW);
- if (GSVALS == NULL) {
- jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "failed to allocate GSVALS: %d bytes", GSW);
- goto cleanup;
- }
- for (i=0; i<GSW; ++i) {
- GSVALS[i] = jbig2_new(ctx, uint8_t , GSH);
- if (GSVALS[i] == NULL) {
- jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "failed to allocate GSVALS: %d bytes", GSH * GSW);
- /* free already allocated */
- for (j = i-1; j >= 0; --j) {
- jbig2_free(ctx->allocator, GSVALS[j]);
- }
- jbig2_free(ctx->allocator, GSVALS);
- GSVALS = NULL;
- goto cleanup;
+ /* C.5 step 3. (c) */
+ --j;
}
- }
- /* C.5 step 4. */
- for (x = 0; x < GSW; ++x) {
- for (y = 0; y < GSH; ++y) {
- GSVALS[x][y] = 0;
+ /* allocate GSVALS */
+ GSVALS = jbig2_new(ctx, uint8_t *, GSW);
+ if (GSVALS == NULL) {
+ jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to allocate GSVALS: %d bytes", GSW);
+ goto cleanup;
+ }
+ for (i = 0; i < GSW; ++i) {
+ GSVALS[i] = jbig2_new(ctx, uint8_t, GSH);
+ if (GSVALS[i] == NULL) {
+ jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to allocate GSVALS: %d bytes", GSH * GSW);
+ /* free already allocated */
+ for (j = i - 1; j >= 0; --j) {
+ jbig2_free(ctx->allocator, GSVALS[j]);
+ }
+ jbig2_free(ctx->allocator, GSVALS);
+ GSVALS = NULL;
+ goto cleanup;
+ }
+ }
- for (j = 0; j < GSBPP; ++j)
- GSVALS[x][y] += jbig2_image_get_pixel(GSPLANES[j], x, y) << j;
+ /* C.5 step 4. */
+ for (x = 0; x < GSW; ++x) {
+ for (y = 0; y < GSH; ++y) {
+ GSVALS[x][y] = 0;
+
+ for (j = 0; j < GSBPP; ++j)
+ GSVALS[x][y] += jbig2_image_get_pixel(GSPLANES[j], x, y) << j;
+ }
}
- }
cleanup:
- /* free memory */
- if (!GSMMR) {
- jbig2_free(ctx->allocator, as);
- jbig2_word_stream_buf_free(ctx, ws);
- }
- for (i=0; i< GSBPP; ++i)
- jbig2_image_release(ctx, GSPLANES[i]);
+ /* free memory */
+ if (!GSMMR) {
+ jbig2_free(ctx->allocator, as);
+ jbig2_word_stream_buf_free(ctx, ws);
+ }
+ for (i = 0; i < GSBPP; ++i)
+ jbig2_image_release(ctx, GSPLANES[i]);
- jbig2_free(ctx->allocator, GSPLANES);
+ jbig2_free(ctx->allocator, GSPLANES);
- return GSVALS;
+ return GSVALS;
}
/**
- * jbig2_decode_ht_region_get_hpats: get pattern dictionary
+ * jbig2_decode_ht_region_get_hpats: get pattern dictionary
*
* @ctx: jbig2 decoder context
- * @segment: jbig2 halftone region segment
+ * @segment: jbig2 halftone region segment
*
- * Returns the first referred pattern dictionary of segment
+ * Returns the first referred pattern dictionary of segment
*
* returns: pattern dictionary
* 0 if search failed
**/
-Jbig2PatternDict *
+Jbig2PatternDict *
jbig2_decode_ht_region_get_hpats(Jbig2Ctx *ctx, Jbig2Segment *segment)
{
- int index = 0;
- Jbig2PatternDict *pattern_dict = NULL;
- Jbig2Segment *rsegment = NULL;
+ int index = 0;
+ Jbig2PatternDict *pattern_dict = NULL;
+ Jbig2Segment *rsegment = NULL;
- /* loop through all referred segments */
- while (!pattern_dict && segment->referred_to_segment_count > index) {
- rsegment = jbig2_find_segment(ctx, segment->referred_to_segments[index]);
- if (rsegment) {
- /* segment type is pattern dictionary and result is not empty */
- if ((rsegment->flags & 0x3f) == 16 && rsegment->result) {
- pattern_dict = (Jbig2PatternDict *) rsegment->result;
- return pattern_dict;
- }
+ /* loop through all referred segments */
+ while (!pattern_dict && segment->referred_to_segment_count > index) {
+ rsegment = jbig2_find_segment(ctx, segment->referred_to_segments[index]);
+ if (rsegment) {
+ /* segment type is pattern dictionary and result is not empty */
+ if ((rsegment->flags & 0x3f) == 16 && rsegment->result) {
+ pattern_dict = (Jbig2PatternDict *) rsegment->result;
+ return pattern_dict;
+ }
+ }
+ index++;
}
- index++;
- }
- return pattern_dict;
+ return pattern_dict;
}
/**
@@ -471,8 +429,8 @@
* jbig2_decode_halftone_region: decode a halftone region
*
* @ctx: jbig2 decoder context
- * @segment: jbig2 halftone region segment
- * @params: parameters
+ * @segment: jbig2 halftone region segment
+ * @params: parameters
* @data: pointer to halftone region data to be decoded
* @size: length of halftone region data
* @GB_stats: artimetic coding context to use
@@ -485,85 +443,74 @@
**/
int
jbig2_decode_halftone_region(Jbig2Ctx *ctx, Jbig2Segment *segment,
- Jbig2HalftoneRegionParams *params,
- const byte *data, const size_t size,
- Jbig2Image *image,
- Jbig2ArithCx *GB_stats)
+ Jbig2HalftoneRegionParams *params, const byte *data, const size_t size, Jbig2Image *image, Jbig2ArithCx *GB_stats)
{
- uint32_t HBPP;
- uint32_t HNUMPATS;
- uint8_t **GI;
- Jbig2Image *HSKIP = NULL;
- Jbig2PatternDict * HPATS;
- int i;
- uint32_t mg, ng;
- int32_t x, y;
- uint8_t gray_val;
+ uint32_t HBPP;
+ uint32_t HNUMPATS;
+ uint8_t **GI;
+ Jbig2Image *HSKIP = NULL;
+ Jbig2PatternDict *HPATS;
+ int i;
+ uint32_t mg, ng;
+ int32_t x, y;
+ uint8_t gray_val;
- /* 6.6.5 point 1. Fill bitmap with HDEFPIXEL */
- memset(image->data, params->HDEFPIXEL, image->stride * image->height);
+ /* 6.6.5 point 1. Fill bitmap with HDEFPIXEL */
+ memset(image->data, params->HDEFPIXEL, image->stride * image->height);
- /* 6.6.5 point 2. compute HSKIP */
- if (params->HENABLESKIP == 1) {
- jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number,
- "unhandled option HENABLESKIP");
- }
+ /* 6.6.5 point 2. compute HSKIP */
+ if (params->HENABLESKIP == 1) {
+ jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "unhandled option HENABLESKIP");
+ }
- /* 6.6.5 point 3. set HBPP to ceil(log2(HNUMPATS)):
- * we need the number of patterns used in this region (HNUMPATS)
- * get it from referred pattern dictionary */
+ /* 6.6.5 point 3. set HBPP to ceil(log2(HNUMPATS)):
+ * we need the number of patterns used in this region (HNUMPATS)
+ * get it from referred pattern dictionary */
- HPATS = jbig2_decode_ht_region_get_hpats(ctx, segment);
- if (!HPATS) {
- jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number,
- "no pattern dictionary found, skipping halftone image");
- return -1;
- }
- HNUMPATS = HPATS->n_patterns;
+ HPATS = jbig2_decode_ht_region_get_hpats(ctx, segment);
+ if (!HPATS) {
+ jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "no pattern dictionary found, skipping halftone image");
+ return -1;
+ }
+ HNUMPATS = HPATS->n_patterns;
- /* calculate ceil(log2(HNUMPATS)) */
- HBPP = 0;
- while(HNUMPATS > (1 << ++HBPP));
+ /* calculate ceil(log2(HNUMPATS)) */
+ HBPP = 0;
+ while (HNUMPATS > (1 << ++HBPP));
- /* 6.6.5 point 4. decode gray-scale image as mentioned in annex C */
- GI = jbig2_decode_gray_scale_image(ctx, segment, data, size,
- params->HMMR, params->HGW,
- params->HGH, HBPP, params->HENABLESKIP,
- HSKIP, params->HTEMPLATE,
- GB_stats);
+ /* 6.6.5 point 4. decode gray-scale image as mentioned in annex C */
+ GI = jbig2_decode_gray_scale_image(ctx, segment, data, size,
+ params->HMMR, params->HGW, params->HGH, HBPP, params->HENABLESKIP, HSKIP, params->HTEMPLATE, GB_stats);
- if (!GI) {
- jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number,
- "unable to acquire gray-scale image, skipping halftone image");
- return -1;
- }
+ if (!GI) {
+ jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "unable to acquire gray-scale image, skipping halftone image");
+ return -1;
+ }
- /* 6.6.5 point 5. place patterns with procedure mentioned in 6.6.5.2 */
- for (mg = 0 ; mg < params->HGH ; ++mg) {
- for (ng = 0 ; ng < params->HGW ; ++ng ) {
- x = (params->HGX + mg * params->HRY + ng * params->HRX) >> 8;
- y = (params->HGY + mg * params->HRX - ng * params->HRY) >> 8;
+ /* 6.6.5 point 5. place patterns with procedure mentioned in 6.6.5.2 */
+ for (mg = 0; mg < params->HGH; ++mg) {
+ for (ng = 0; ng < params->HGW; ++ng) {
+ x = (params->HGX + mg * params->HRY + ng * params->HRX) >> 8;
+ y = (params->HGY + mg * params->HRX - ng * params->HRY) >> 8;
- /* prevent pattern index >= HNUMPATS */
- gray_val = GI[ng][mg];
- if (gray_val >= HNUMPATS) {
- jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number,
- "gray-scale image uses value %d which larger than pattern dictionary",
- gray_val);
- /* use highest aviable pattern */
- gray_val = HNUMPATS - 1;
- }
- jbig2_image_compose(ctx, image, HPATS->patterns[gray_val], x, y, params->op);
+ /* prevent pattern index >= HNUMPATS */
+ gray_val = GI[ng][mg];
+ if (gray_val >= HNUMPATS) {
+ jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "gray-scale image uses value %d which larger than pattern dictionary", gray_val);
+ /* use highest aviable pattern */
+ gray_val = HNUMPATS - 1;
+ }
+ jbig2_image_compose(ctx, image, HPATS->patterns[gray_val], x, y, params->op);
+ }
}
- }
- /* free GI */
- for (i = 0; i < params->HGW; ++i) {
- jbig2_free(ctx->allocator, GI[i]);
- }
- jbig2_free(ctx->allocator, GI);
+ /* free GI */
+ for (i = 0; i < params->HGW; ++i) {
+ jbig2_free(ctx->allocator, GI[i]);
+ }
+ jbig2_free(ctx->allocator, GI);
- return 0;
+ return 0;
}
/**
@@ -572,104 +519,92 @@
int
jbig2_halftone_region(Jbig2Ctx *ctx, Jbig2Segment *segment, const byte *segment_data)
{
- int offset = 0;
- Jbig2RegionSegmentInfo region_info;
- Jbig2HalftoneRegionParams params;
- Jbig2Image *image = NULL;
- Jbig2ArithCx *GB_stats = NULL;
- int code = 0;
+ int offset = 0;
+ Jbig2RegionSegmentInfo region_info;
+ Jbig2HalftoneRegionParams params;
+ Jbig2Image *image = NULL;
+ Jbig2ArithCx *GB_stats = NULL;
+ int code = 0;
- /* 7.4.5.1 */
- if (segment->data_length < 17) goto too_short;
- jbig2_get_region_segment_info(®ion_info, segment_data);
- offset += 17;
+ /* 7.4.5.1 */
+ if (segment->data_length < 17)
+ goto too_short;
+ jbig2_get_region_segment_info(®ion_info, segment_data);
+ offset += 17;
- if (segment->data_length < 18) goto too_short;
+ if (segment->data_length < 18)
+ goto too_short;
- /* 7.4.5.1.1 */
- params.flags = segment_data[offset];
- params.HMMR = params.flags & 1;
- params.HTEMPLATE = (params.flags & 6) >> 1;
- params.HENABLESKIP = (params.flags & 8) >> 3;
- params.op = (Jbig2ComposeOp)((params.flags & 0x70) >> 4);
- params.HDEFPIXEL = (params.flags &0x80) >> 7;
- offset += 1;
+ /* 7.4.5.1.1 */
+ params.flags = segment_data[offset];
+ params.HMMR = params.flags & 1;
+ params.HTEMPLATE = (params.flags & 6) >> 1;
+ params.HENABLESKIP = (params.flags & 8) >> 3;
+ params.op = (Jbig2ComposeOp)((params.flags & 0x70) >> 4);
+ params.HDEFPIXEL = (params.flags & 0x80) >> 7;
+ offset += 1;
- jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number,
- "halftone region: %d x %d @ (%x,%d) flags=%02x",
- region_info.width, region_info.height,
- region_info.x, region_info.y, params.flags);
+ jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number,
+ "halftone region: %d x %d @ (%x,%d) flags=%02x", region_info.width, region_info.height, region_info.x, region_info.y, params.flags);
- if (params.HMMR && params.HTEMPLATE) {
- jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number,
- "HTEMPLATE is %d when HMMR is %d, contrary to spec",
- params.HTEMPLATE, params.HMMR);
- }
- if (params.HMMR && params.HENABLESKIP) {
- jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number,
- "HENABLESKIP is %d when HMMR is %d, contrary to spec",
- params.HENABLESKIP, params.HMMR);
- }
+ if (params.HMMR && params.HTEMPLATE) {
+ jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "HTEMPLATE is %d when HMMR is %d, contrary to spec", params.HTEMPLATE, params.HMMR);
+ }
+ if (params.HMMR && params.HENABLESKIP) {
+ jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "HENABLESKIP is %d when HMMR is %d, contrary to spec", params.HENABLESKIP, params.HMMR);
+ }
- /* Figure 43 */
- if (segment->data_length - offset < 16) goto too_short;
- params.HGW = jbig2_get_uint32(segment_data + offset);
- params.HGH = jbig2_get_uint32(segment_data + offset + 4);
- params.HGX = jbig2_get_int32(segment_data + offset + 8);
- params.HGY = jbig2_get_int32(segment_data + offset + 12);
- offset += 16;
+ /* Figure 43 */
+ if (segment->data_length - offset < 16)
+ goto too_short;
+ params.HGW = jbig2_get_uint32(segment_data + offset);
+ params.HGH = jbig2_get_uint32(segment_data + offset + 4);
+ params.HGX = jbig2_get_int32(segment_data + offset + 8);
+ params.HGY = jbig2_get_int32(segment_data + offset + 12);
+ offset += 16;
- /* Figure 44 */
- if (segment->data_length - offset < 4) goto too_short;
- params.HRX = jbig2_get_uint16(segment_data + offset);
- params.HRY = jbig2_get_uint16(segment_data + offset + 2);
- offset += 4;
+ /* Figure 44 */
+ if (segment->data_length - offset < 4)
+ goto too_short;
+ params.HRX = jbig2_get_uint16(segment_data + offset);
+ params.HRY = jbig2_get_uint16(segment_data + offset + 2);
+ offset += 4;
- jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number,
- " grid %d x %d @ (%d.%d,%d.%d) vector (%d.%d,%d.%d)",
- params.HGW, params.HGH,
- params.HGX >> 8, params.HGX & 0xff,
- params.HGY >> 8, params.HGY & 0xff,
- params.HRX >> 8, params.HRX & 0xff,
- params.HRY >> 8, params.HRY & 0xff);
+ jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number,
+ " grid %d x %d @ (%d.%d,%d.%d) vector (%d.%d,%d.%d)",
+ params.HGW, params.HGH,
+ params.HGX >> 8, params.HGX & 0xff, params.HGY >> 8, params.HGY & 0xff, params.HRX >> 8, params.HRX & 0xff, params.HRY >> 8, params.HRY & 0xff);
- /* 7.4.5.2.2 */
- if (!params.HMMR) {
- /* allocate and zero arithmetic coding stats */
- int stats_size = jbig2_generic_stats_size(ctx, params.HTEMPLATE);
- GB_stats = jbig2_new(ctx, Jbig2ArithCx, stats_size);
- if (GB_stats == NULL)
- {
- return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "failed to allocate GB_stats in halftone region");
+ /* 7.4.5.2.2 */
+ if (!params.HMMR) {
+ /* allocate and zero arithmetic coding stats */
+ int stats_size = jbig2_generic_stats_size(ctx, params.HTEMPLATE);
+
+ GB_stats = jbig2_new(ctx, Jbig2ArithCx, stats_size);
+ if (GB_stats == NULL) {
+ return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to allocate GB_stats in halftone region");
+ }
+ memset(GB_stats, 0, stats_size);
}
- memset(GB_stats, 0, stats_size);
- }
- image = jbig2_image_new(ctx, region_info.width, region_info.height);
- if (image == NULL)
- {
- jbig2_free(ctx->allocator, GB_stats);
- return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number,
- "unable to allocate halftone image");
- }
+ image = jbig2_image_new(ctx, region_info.width, region_info.height);
+ if (image == NULL) {
+ jbig2_free(ctx->allocator, GB_stats);
+ return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "unable to allocate halftone image");
+ }
- code = jbig2_decode_halftone_region(ctx, segment, ¶ms,
- segment_data + offset, segment->data_length - offset,
- image, GB_stats);
+ code = jbig2_decode_halftone_region(ctx, segment, ¶ms, segment_data + offset, segment->data_length - offset, image, GB_stats);
- /* todo: retain GB_stats? */
- if (!params.HMMR) {
- jbig2_free(ctx->allocator, GB_stats);
- }
+ /* todo: retain GB_stats? */
+ if (!params.HMMR) {
+ jbig2_free(ctx->allocator, GB_stats);
+ }
- jbig2_page_add_result(ctx, &ctx->pages[ctx->current_page],
- image, region_info.x, region_info.y, region_info.op);
- jbig2_image_release(ctx, image);
+ jbig2_page_add_result(ctx, &ctx->pages[ctx->current_page], image, region_info.x, region_info.y, region_info.op);
+ jbig2_image_release(ctx, image);
- return code;
+ return code;
too_short:
- return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "Segment too short");
+ return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "Segment too short");
}
--- a/jbig2_halftone.h
+++ b/jbig2_halftone.h
@@ -17,65 +17,53 @@
jbig2dec
*/
-
#ifndef _JBIG2_HALFTONE_H
#define _JBIG2_HALFTONE_H
typedef struct {
- int n_patterns;
- Jbig2Image **patterns;
- int HPW, HPH;
+ int n_patterns;
+ Jbig2Image **patterns;
+ int HPW, HPH;
} Jbig2PatternDict;
-
/* Table 24 */
typedef struct {
- bool HDMMR;
- uint32_t HDPW;
- uint32_t HDPH;
- uint32_t GRAYMAX;
- int HDTEMPLATE;
+ bool HDMMR;
+ uint32_t HDPW;
+ uint32_t HDPH;
+ uint32_t GRAYMAX;
+ int HDTEMPLATE;
} Jbig2PatternDictParams;
/* Table 33 */
typedef struct {
- byte flags;
- uint32_t HGW;
- uint32_t HGH;
- int32_t HGX;
- int32_t HGY;
- uint16_t HRX;
- uint16_t HRY;
- bool HMMR;
- int HTEMPLATE;
- bool HENABLESKIP;
- Jbig2ComposeOp op;
- bool HDEFPIXEL;
+ byte flags;
+ uint32_t HGW;
+ uint32_t HGH;
+ int32_t HGX;
+ int32_t HGY;
+ uint16_t HRX;
+ uint16_t HRY;
+ bool HMMR;
+ int HTEMPLATE;
+ bool HENABLESKIP;
+ Jbig2ComposeOp op;
+ bool HDEFPIXEL;
} Jbig2HalftoneRegionParams;
-Jbig2PatternDict*
-jbig2_hd_new(Jbig2Ctx *ctx, const Jbig2PatternDictParams *params,
- Jbig2Image *image);
+Jbig2PatternDict *jbig2_hd_new(Jbig2Ctx *ctx, const Jbig2PatternDictParams *params, Jbig2Image *image);
-void
-jbig2_hd_release(Jbig2Ctx *ctx, Jbig2PatternDict *dict);
+void jbig2_hd_release(Jbig2Ctx *ctx, Jbig2PatternDict *dict);
-uint8_t **
-jbig2_decode_gray_scale_image(Jbig2Ctx *ctx, Jbig2Segment* segment,
- const byte *data, const size_t size,
- bool GSMMR, uint32_t GSW, uint32_t GSH,
- uint32_t GSBPP, bool GSUSESKIP,
- Jbig2Image *GSKIP, int GSTEMPLATE,
- Jbig2ArithCx *GB_stats);
+uint8_t **jbig2_decode_gray_scale_image(Jbig2Ctx *ctx, Jbig2Segment *segment,
+ const byte *data, const size_t size,
+ bool GSMMR, uint32_t GSW, uint32_t GSH,
+ uint32_t GSBPP, bool GSUSESKIP, Jbig2Image *GSKIP, int GSTEMPLATE, Jbig2ArithCx *GB_stats);
-Jbig2PatternDict *
-jbig2_decode_ht_region_get_hpats(Jbig2Ctx *ctx, Jbig2Segment *segment);
+Jbig2PatternDict *jbig2_decode_ht_region_get_hpats(Jbig2Ctx *ctx, Jbig2Segment *segment);
int
jbig2_decode_halftone_region(Jbig2Ctx *ctx, Jbig2Segment *segment,
- Jbig2HalftoneRegionParams *params,
- const byte *data, const size_t size,
- Jbig2Image *image,
- Jbig2ArithCx *GB_stats);
+ Jbig2HalftoneRegionParams *params, const byte *data, const size_t size, Jbig2Image *image, Jbig2ArithCx *GB_stats);
#endif /* _JBIG2_HALFTONE_H */
--- a/jbig2_huffman.c
+++ b/jbig2_huffman.c
@@ -17,7 +17,6 @@
jbig2dec
*/
-
/* Huffman table decoding procedures
-- See Annex B of the JBIG2 specification */
@@ -42,60 +41,55 @@
#define JBIG2_HUFFMAN_FLAGS_ISLOW 2
#define JBIG2_HUFFMAN_FLAGS_ISEXT 4
-
-
struct _Jbig2HuffmanState {
- /* The current bit offset is equal to (offset * 8) + offset_bits.
- The MSB of this_word is the current bit offset. The MSB of next_word
- is (offset + 4) * 8. */
- uint32_t this_word;
- uint32_t next_word;
- int offset_bits;
- int offset;
- int offset_limit;
+ /* The current bit offset is equal to (offset * 8) + offset_bits.
+ The MSB of this_word is the current bit offset. The MSB of next_word
+ is (offset + 4) * 8. */
+ uint32_t this_word;
+ uint32_t next_word;
+ int offset_bits;
+ int offset;
+ int offset_limit;
- Jbig2WordStream *ws;
- Jbig2Ctx *ctx;
+ Jbig2WordStream *ws;
+ Jbig2Ctx *ctx;
};
-
static uint32_t
huff_get_next_word(Jbig2HuffmanState *hs, int offset)
{
- uint32_t word = 0;
- Jbig2WordStream *ws = hs->ws;
- if ((ws->get_next_word (ws, offset, &word)) &&
- ((hs->offset_limit == 0) || (offset < hs->offset_limit)))
- hs->offset_limit = offset;
- return word;
+ uint32_t word = 0;
+ Jbig2WordStream *ws = hs->ws;
+
+ if ((ws->get_next_word(ws, offset, &word)) && ((hs->offset_limit == 0) || (offset < hs->offset_limit)))
+ hs->offset_limit = offset;
+ return word;
}
-
/** Allocate and initialize a new huffman coding state
* the returned pointer can simply be freed; this does
* not affect the associated Jbig2WordStream.
*/
Jbig2HuffmanState *
-jbig2_huffman_new (Jbig2Ctx *ctx, Jbig2WordStream *ws)
+jbig2_huffman_new(Jbig2Ctx *ctx, Jbig2WordStream *ws)
{
- Jbig2HuffmanState *result = NULL;
+ Jbig2HuffmanState *result = NULL;
- result = jbig2_new(ctx, Jbig2HuffmanState, 1);
+ result = jbig2_new(ctx, Jbig2HuffmanState, 1);
- if (result != NULL) {
- result->offset = 0;
- result->offset_bits = 0;
- result->offset_limit = 0;
- result->ws = ws;
- result->ctx = ctx;
- result->this_word = huff_get_next_word(result, 0);
- result->next_word = huff_get_next_word(result, 4);
- } else {
- jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1,
- "failed to allocate new huffman coding state");
- }
+ if (result != NULL) {
+ result->offset = 0;
+ result->offset_bits = 0;
+ result->offset_limit = 0;
+ result->ws = ws;
+ result->ctx = ctx;
+ result->this_word = huff_get_next_word(result, 0);
+ result->next_word = huff_get_next_word(result, 4);
+ } else {
+ jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "failed to allocate new huffman coding state");
+ }
- return result;
+ return result;
}
/** Free an allocated huffman coding state.
@@ -102,10 +96,11 @@
* This just calls jbig2_free() if the pointer is not NULL
*/
void
-jbig2_huffman_free (Jbig2Ctx *ctx, Jbig2HuffmanState *hs)
+jbig2_huffman_free(Jbig2Ctx *ctx, Jbig2HuffmanState *hs)
{
- if (hs != NULL) jbig2_free(ctx->allocator, hs);
- return;
+ if (hs != NULL)
+ jbig2_free(ctx->allocator, hs);
+ return;
}
/** debug routines **/
@@ -112,51 +107,56 @@
#ifdef JBIG2_DEBUG
/** print current huffman state */
-void jbig2_dump_huffman_state(Jbig2HuffmanState *hs) {
- fprintf(stderr, "huffman state %08x %08x offset %d.%d\n",
- hs->this_word, hs->next_word, hs->offset, hs->offset_bits);
+void
+jbig2_dump_huffman_state(Jbig2HuffmanState *hs)
+{
+ fprintf(stderr, "huffman state %08x %08x offset %d.%d\n", hs->this_word, hs->next_word, hs->offset, hs->offset_bits);
}
/** print the binary string we're reading from */
-void jbig2_dump_huffman_binary(Jbig2HuffmanState *hs)
+void
+jbig2_dump_huffman_binary(Jbig2HuffmanState *hs)
{
- const uint32_t word = hs->this_word;
- int i;
+ const uint32_t word = hs->this_word;
+ int i;
- fprintf(stderr, "huffman binary ");
- for (i = 31; i >= 0; i--)
- fprintf(stderr, ((word >> i) & 1) ? "1" : "0");
- fprintf(stderr, "\n");
+ fprintf(stderr, "huffman binary ");
+ for (i = 31; i >= 0; i--)
+ fprintf(stderr, ((word >> i) & 1) ? "1" : "0");
+ fprintf(stderr, "\n");
}
/** print huffman table */
-void jbig2_dump_huffman_table(const Jbig2HuffmanTable *table)
+void
+jbig2_dump_huffman_table(const Jbig2HuffmanTable *table)
{
int i;
int table_size = (1 << table->log_table_size);
+
fprintf(stderr, "huffman table %p (log_table_size=%d, %d entries, entryies=%p):\n", table, table->log_table_size, table_size, table->entries);
for (i = 0; i < table_size; i++) {
fprintf(stderr, "%6d: PREFLEN=%d, RANGELEN=%d, ", i, table->entries[i].PREFLEN, table->entries[i].RANGELEN);
- if ( table->entries[i].flags & JBIG2_HUFFMAN_FLAGS_ISEXT ) {
+ if (table->entries[i].flags & JBIG2_HUFFMAN_FLAGS_ISEXT) {
fprintf(stderr, "ext=%p", table->entries[i].u.ext_table);
} else {
fprintf(stderr, "RANGELOW=%d", table->entries[i].u.RANGELOW);
}
- if ( table->entries[i].flags ) {
+ if (table->entries[i].flags) {
int need_comma = 0;
+
fprintf(stderr, ", flags=0x%x(", table->entries[i].flags);
- if ( table->entries[i].flags & JBIG2_HUFFMAN_FLAGS_ISOOB ) {
+ if (table->entries[i].flags & JBIG2_HUFFMAN_FLAGS_ISOOB) {
fprintf(stderr, "OOB");
need_comma = 1;
}
- if ( table->entries[i].flags & JBIG2_HUFFMAN_FLAGS_ISLOW ) {
- if ( need_comma )
+ if (table->entries[i].flags & JBIG2_HUFFMAN_FLAGS_ISLOW) {
+ if (need_comma)
fprintf(stderr, ",");
fprintf(stderr, "LOW");
need_comma = 1;
}
- if ( table->entries[i].flags & JBIG2_HUFFMAN_FLAGS_ISEXT ) {
- if ( need_comma )
+ if (table->entries[i].flags & JBIG2_HUFFMAN_FLAGS_ISEXT) {
+ if (need_comma)
fprintf(stderr, ",");
fprintf(stderr, "EXT");
}
@@ -174,42 +174,40 @@
void
jbig2_huffman_skip(Jbig2HuffmanState *hs)
{
- int bits = hs->offset_bits & 7;
+ int bits = hs->offset_bits & 7;
- if (bits) {
- bits = 8 - bits;
- hs->offset_bits += bits;
- hs->this_word = (hs->this_word << bits) |
- (hs->next_word >> (32 - hs->offset_bits));
- }
+ if (bits) {
+ bits = 8 - bits;
+ hs->offset_bits += bits;
+ hs->this_word = (hs->this_word << bits) | (hs->next_word >> (32 - hs->offset_bits));
+ }
- if (hs->offset_bits >= 32) {
- hs->this_word = hs->next_word;
- hs->offset += 4;
- hs->next_word = huff_get_next_word(hs, hs->offset + 4);
- hs->offset_bits -= 32;
- if (hs->offset_bits) {
- hs->this_word = (hs->this_word << hs->offset_bits) |
- (hs->next_word >> (32 - hs->offset_bits));
+ if (hs->offset_bits >= 32) {
+ hs->this_word = hs->next_word;
+ hs->offset += 4;
+ hs->next_word = huff_get_next_word(hs, hs->offset + 4);
+ hs->offset_bits -= 32;
+ if (hs->offset_bits) {
+ hs->this_word = (hs->this_word << hs->offset_bits) | (hs->next_word >> (32 - hs->offset_bits));
+ }
}
- }
}
/* skip ahead a specified number of bytes in the word stream
*/
-void jbig2_huffman_advance(Jbig2HuffmanState *hs, int offset)
+void
+jbig2_huffman_advance(Jbig2HuffmanState *hs, int offset)
{
- hs->offset += offset & ~3;
- hs->offset_bits += (offset & 3) << 3;
- if (hs->offset_bits >= 32) {
- hs->offset += 4;
- hs->offset_bits -= 32;
- }
- hs->this_word = huff_get_next_word(hs, hs->offset);
- hs->next_word = huff_get_next_word(hs, hs->offset + 4);
- if (hs->offset_bits > 0)
- hs->this_word = (hs->this_word << hs->offset_bits) |
- (hs->next_word >> (32 - hs->offset_bits));
+ hs->offset += offset & ~3;
+ hs->offset_bits += (offset & 3) << 3;
+ if (hs->offset_bits >= 32) {
+ hs->offset += 4;
+ hs->offset_bits -= 32;
+ }
+ hs->this_word = huff_get_next_word(hs, hs->offset);
+ hs->next_word = huff_get_next_word(hs, hs->offset + 4);
+ if (hs->offset_bits > 0)
+ hs->this_word = (hs->this_word << hs->offset_bits) | (hs->next_word >> (32 - hs->offset_bits));
}
/* return the offset of the huffman decode pointer (in bytes)
@@ -218,7 +216,7 @@
int
jbig2_huffman_offset(Jbig2HuffmanState *hs)
{
- return hs->offset + (hs->offset_bits >> 3);
+ return hs->offset + (hs->offset_bits >> 3);
}
/* read a number of bits directly from the huffman state
@@ -225,130 +223,116 @@
* without decoding against a table
*/
int32_t
-jbig2_huffman_get_bits (Jbig2HuffmanState *hs, const int bits, int *err)
+jbig2_huffman_get_bits(Jbig2HuffmanState *hs, const int bits, int *err)
{
- uint32_t this_word = hs->this_word;
- int32_t result;
+ uint32_t this_word = hs->this_word;
+ int32_t result;
- if (hs->offset_limit && hs->offset >= hs->offset_limit) {
- jbig2_error(hs->ctx, JBIG2_SEVERITY_FATAL, -1,
- "end of jbig2 buffer reached at offset %d", hs->offset);
- *err = -1;
- return -1;
- }
+ if (hs->offset_limit && hs->offset >= hs->offset_limit) {
+ jbig2_error(hs->ctx, JBIG2_SEVERITY_FATAL, -1, "end of jbig2 buffer reached at offset %d", hs->offset);
+ *err = -1;
+ return -1;
+ }
- result = this_word >> (32 - bits);
- hs->offset_bits += bits;
- if (hs->offset_bits >= 32) {
- hs->offset += 4;
- hs->offset_bits -= 32;
- hs->this_word = hs->next_word;
- hs->next_word = huff_get_next_word(hs, hs->offset + 4);
- if (hs->offset_bits) {
- hs->this_word = (hs->this_word << hs->offset_bits) |
- (hs->next_word >> (32 - hs->offset_bits));
+ result = this_word >> (32 - bits);
+ hs->offset_bits += bits;
+ if (hs->offset_bits >= 32) {
+ hs->offset += 4;
+ hs->offset_bits -= 32;
+ hs->this_word = hs->next_word;
+ hs->next_word = huff_get_next_word(hs, hs->offset + 4);
+ if (hs->offset_bits) {
+ hs->this_word = (hs->this_word << hs->offset_bits) | (hs->next_word >> (32 - hs->offset_bits));
+ } else {
+ hs->this_word = (hs->this_word << hs->offset_bits);
+ }
} else {
- hs->this_word = (hs->this_word << hs->offset_bits);
+ hs->this_word = (this_word << bits) | (hs->next_word >> (32 - hs->offset_bits));
}
- } else {
- hs->this_word = (this_word << bits) |
- (hs->next_word >> (32 - hs->offset_bits));
- }
- return result;
+ return result;
}
int32_t
-jbig2_huffman_get (Jbig2HuffmanState *hs,
- const Jbig2HuffmanTable *table, bool *oob)
+jbig2_huffman_get(Jbig2HuffmanState *hs, const Jbig2HuffmanTable *table, bool *oob)
{
- Jbig2HuffmanEntry *entry;
- byte flags;
- int offset_bits = hs->offset_bits;
- uint32_t this_word = hs->this_word;
- uint32_t next_word;
- int RANGELEN;
- int32_t result;
+ Jbig2HuffmanEntry *entry;
+ byte flags;
+ int offset_bits = hs->offset_bits;
+ uint32_t this_word = hs->this_word;
+ uint32_t next_word;
+ int RANGELEN;
+ int32_t result;
- if (hs->offset_limit && hs->offset >= hs->offset_limit) {
- jbig2_error(hs->ctx, JBIG2_SEVERITY_FATAL, -1,
- "end of Jbig2WordStream reached at offset %d", hs->offset);
- if (oob)
- *oob = -1;
- return -1;
- }
+ if (hs->offset_limit && hs->offset >= hs->offset_limit) {
+ jbig2_error(hs->ctx, JBIG2_SEVERITY_FATAL, -1, "end of Jbig2WordStream reached at offset %d", hs->offset);
+ if (oob)
+ *oob = -1;
+ return -1;
+ }
- for (;;)
- {
- int log_table_size = table->log_table_size;
- int PREFLEN;
+ for (;;) {
+ int log_table_size = table->log_table_size;
+ int PREFLEN;
- /* SumatraPDF: shifting by the size of the operand is undefined */
- entry = &table->entries[log_table_size > 0 ? this_word >> (32 - log_table_size) : 0];
- flags = entry->flags;
- PREFLEN = entry->PREFLEN;
- if ((flags == (byte)-1) && (PREFLEN == (byte)-1) && (entry->u.RANGELOW == -1))
- {
- if (oob)
- *oob = -1;
- return -1;
- }
+ /* SumatraPDF: shifting by the size of the operand is undefined */
+ entry = &table->entries[log_table_size > 0 ? this_word >> (32 - log_table_size) : 0];
+ flags = entry->flags;
+ PREFLEN = entry->PREFLEN;
+ if ((flags == (byte) - 1) && (PREFLEN == (byte) - 1) && (entry->u.RANGELOW == -1)) {
+ if (oob)
+ *oob = -1;
+ return -1;
+ }
- next_word = hs->next_word;
- offset_bits += PREFLEN;
- if (offset_bits >= 32)
- {
- this_word = next_word;
- hs->offset += 4;
- next_word = huff_get_next_word(hs, hs->offset + 4);
- offset_bits -= 32;
- hs->next_word = next_word;
- PREFLEN = offset_bits;
- }
- if (PREFLEN)
- this_word = (this_word << PREFLEN) |
- (next_word >> (32 - offset_bits));
- if (flags & JBIG2_HUFFMAN_FLAGS_ISEXT)
- {
- table = entry->u.ext_table;
- }
- else
- break;
+ next_word = hs->next_word;
+ offset_bits += PREFLEN;
+ if (offset_bits >= 32) {
+ this_word = next_word;
+ hs->offset += 4;
+ next_word = huff_get_next_word(hs, hs->offset + 4);
+ offset_bits -= 32;
+ hs->next_word = next_word;
+ PREFLEN = offset_bits;
+ }
+ if (PREFLEN)
+ this_word = (this_word << PREFLEN) | (next_word >> (32 - offset_bits));
+ if (flags & JBIG2_HUFFMAN_FLAGS_ISEXT) {
+ table = entry->u.ext_table;
+ } else
+ break;
}
- result = entry->u.RANGELOW;
- RANGELEN = entry->RANGELEN;
- if (RANGELEN > 0)
- {
- int32_t HTOFFSET;
+ result = entry->u.RANGELOW;
+ RANGELEN = entry->RANGELEN;
+ if (RANGELEN > 0) {
+ int32_t HTOFFSET;
- HTOFFSET = this_word >> (32 - RANGELEN);
- if (flags & JBIG2_HUFFMAN_FLAGS_ISLOW)
- result -= HTOFFSET;
- else
- result += HTOFFSET;
+ HTOFFSET = this_word >> (32 - RANGELEN);
+ if (flags & JBIG2_HUFFMAN_FLAGS_ISLOW)
+ result -= HTOFFSET;
+ else
+ result += HTOFFSET;
- offset_bits += RANGELEN;
- if (offset_bits >= 32)
- {
- this_word = next_word;
- hs->offset += 4;
- next_word = huff_get_next_word(hs, hs->offset + 4);
- offset_bits -= 32;
- hs->next_word = next_word;
- RANGELEN = offset_bits;
- }
-if (RANGELEN)
- this_word = (this_word << RANGELEN) |
- (next_word >> (32 - offset_bits));
+ offset_bits += RANGELEN;
+ if (offset_bits >= 32) {
+ this_word = next_word;
+ hs->offset += 4;
+ next_word = huff_get_next_word(hs, hs->offset + 4);
+ offset_bits -= 32;
+ hs->next_word = next_word;
+ RANGELEN = offset_bits;
+ }
+ if (RANGELEN)
+ this_word = (this_word << RANGELEN) | (next_word >> (32 - offset_bits));
}
- hs->this_word = this_word;
- hs->offset_bits = offset_bits;
+ hs->this_word = this_word;
+ hs->offset_bits = offset_bits;
- if (oob != NULL)
- *oob = (flags & JBIG2_HUFFMAN_FLAGS_ISOOB);
+ if (oob != NULL)
+ *oob = (flags & JBIG2_HUFFMAN_FLAGS_ISOOB);
- return result;
+ return result;
}
/* TODO: more than 8 bits here is wasteful of memory. We have support
@@ -360,152 +344,139 @@
* set of template params provided by the spec or a table segment
*/
Jbig2HuffmanTable *
-jbig2_build_huffman_table (Jbig2Ctx *ctx, const Jbig2HuffmanParams *params)
+jbig2_build_huffman_table(Jbig2Ctx *ctx, const Jbig2HuffmanParams *params)
{
- int *LENCOUNT;
- int LENMAX = -1;
- const int lencountcount = 256;
- const Jbig2HuffmanLine *lines = params->lines;
- int n_lines = params->n_lines;
- int i, j;
- int max_j;
- int log_table_size = 0;
- Jbig2HuffmanTable *result;
- Jbig2HuffmanEntry *entries;
- int CURLEN;
- int firstcode = 0;
- int CURCODE;
- int CURTEMP;
+ int *LENCOUNT;
+ int LENMAX = -1;
+ const int lencountcount = 256;
+ const Jbig2HuffmanLine *lines = params->lines;
+ int n_lines = params->n_lines;
+ int i, j;
+ int max_j;
+ int log_table_size = 0;
+ Jbig2HuffmanTable *result;
+ Jbig2HuffmanEntry *entries;
+ int CURLEN;
+ int firstcode = 0;
+ int CURCODE;
+ int CURTEMP;
- LENCOUNT = jbig2_new(ctx, int, lencountcount);
- if (LENCOUNT == NULL) {
- jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1,
- "couldn't allocate storage for huffman histogram");
- return NULL;
- }
- memset(LENCOUNT, 0, sizeof(int) * lencountcount);
+ LENCOUNT = jbig2_new(ctx, int, lencountcount);
- /* B.3, 1. */
- for (i = 0; i < params->n_lines; i++)
- {
- int PREFLEN = lines[i].PREFLEN;
- int lts;
+ if (LENCOUNT == NULL) {
+ jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "couldn't allocate storage for huffman histogram");
+ return NULL;
+ }
+ memset(LENCOUNT, 0, sizeof(int) * lencountcount);
- if (PREFLEN > LENMAX)
- {
- for (j = LENMAX + 1; j < PREFLEN + 1; j++)
- LENCOUNT[j] = 0;
- LENMAX = PREFLEN;
- }
- LENCOUNT[PREFLEN]++;
+ /* B.3, 1. */
+ for (i = 0; i < params->n_lines; i++) {
+ int PREFLEN = lines[i].PREFLEN;
+ int lts;
- lts = PREFLEN + lines[i].RANGELEN;
- if (lts > LOG_TABLE_SIZE_MAX)
- lts = PREFLEN;
- if (lts <= LOG_TABLE_SIZE_MAX && log_table_size < lts)
- log_table_size = lts;
+ if (PREFLEN > LENMAX) {
+ for (j = LENMAX + 1; j < PREFLEN + 1; j++)
+ LENCOUNT[j] = 0;
+ LENMAX = PREFLEN;
+ }
+ LENCOUNT[PREFLEN]++;
+
+ lts = PREFLEN + lines[i].RANGELEN;
+ if (lts > LOG_TABLE_SIZE_MAX)
+ lts = PREFLEN;
+ if (lts <= LOG_TABLE_SIZE_MAX && log_table_size < lts)
+ log_table_size = lts;
}
- jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, -1,
- "constructing huffman table log size %d", log_table_size);
- max_j = 1 << log_table_size;
+ jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, -1, "constructing huffman table log size %d", log_table_size);
+ max_j = 1 << log_table_size;
- result = jbig2_new(ctx, Jbig2HuffmanTable, 1);
- if (result == NULL)
- {
- jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1,
- "couldn't allocate result storage in jbig2_build_huffman_table");
- jbig2_free(ctx->allocator, LENCOUNT);
- return NULL;
- }
- result->log_table_size = log_table_size;
- entries = jbig2_new(ctx, Jbig2HuffmanEntry, max_j);
- if (entries == NULL)
- {
- jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1,
- "couldn't allocate entries storage in jbig2_build_huffman_table");
- jbig2_free(ctx->allocator, result);
- jbig2_free(ctx->allocator, LENCOUNT);
- return NULL;
- }
- /* fill now to catch missing JBIG2Globals later */
- memset(entries, 0xFF, sizeof(Jbig2HuffmanEntry)*max_j);
- result->entries = entries;
+ result = jbig2_new(ctx, Jbig2HuffmanTable, 1);
+ if (result == NULL) {
+ jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "couldn't allocate result storage in jbig2_build_huffman_table");
+ jbig2_free(ctx->allocator, LENCOUNT);
+ return NULL;
+ }
+ result->log_table_size = log_table_size;
+ entries = jbig2_new(ctx, Jbig2HuffmanEntry, max_j);
+ if (entries == NULL) {
+ jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "couldn't allocate entries storage in jbig2_build_huffman_table");
+ jbig2_free(ctx->allocator, result);
+ jbig2_free(ctx->allocator, LENCOUNT);
+ return NULL;
+ }
+ /* fill now to catch missing JBIG2Globals later */
+ memset(entries, 0xFF, sizeof(Jbig2HuffmanEntry) * max_j);
+ result->entries = entries;
- LENCOUNT[0] = 0;
+ LENCOUNT[0] = 0;
- for (CURLEN = 1; CURLEN <= LENMAX; CURLEN++)
- {
- int shift = log_table_size - CURLEN;
+ for (CURLEN = 1; CURLEN <= LENMAX; CURLEN++) {
+ int shift = log_table_size - CURLEN;
- /* B.3 3.(a) */
- firstcode = (firstcode + LENCOUNT[CURLEN - 1]) << 1;
- CURCODE = firstcode;
- /* B.3 3.(b) */
- for (CURTEMP = 0; CURTEMP < n_lines; CURTEMP++)
- {
- int PREFLEN = lines[CURTEMP].PREFLEN;
- if (PREFLEN == CURLEN)
- {
- int RANGELEN = lines[CURTEMP].RANGELEN;
- int start_j = CURCODE << shift;
- int end_j = (CURCODE + 1) << shift;
- byte eflags = 0;
+ /* B.3 3.(a) */
+ firstcode = (firstcode + LENCOUNT[CURLEN - 1]) << 1;
+ CURCODE = firstcode;
+ /* B.3 3.(b) */
+ for (CURTEMP = 0; CURTEMP < n_lines; CURTEMP++) {
+ int PREFLEN = lines[CURTEMP].PREFLEN;
- if (end_j > max_j) {
- jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1,
- "ran off the end of the entries table! (%d >= %d)",
- end_j, max_j);
- jbig2_free(ctx->allocator, result->entries);
- jbig2_free(ctx->allocator, result);
- jbig2_free(ctx->allocator, LENCOUNT);
- return NULL;
- }
- /* todo: build extension tables */
- if (params->HTOOB && CURTEMP == n_lines - 1)
- eflags |= JBIG2_HUFFMAN_FLAGS_ISOOB;
- if (CURTEMP == n_lines - (params->HTOOB ? 3 : 2))
- eflags |= JBIG2_HUFFMAN_FLAGS_ISLOW;
- if (PREFLEN + RANGELEN > LOG_TABLE_SIZE_MAX) {
- for (j = start_j; j < end_j; j++) {
- entries[j].u.RANGELOW = lines[CURTEMP].RANGELOW;
- entries[j].PREFLEN = PREFLEN;
- entries[j].RANGELEN = RANGELEN;
- entries[j].flags = eflags;
- }
- } else {
- for (j = start_j; j < end_j; j++) {
- int32_t HTOFFSET = (j >> (shift - RANGELEN)) &
- ((1 << RANGELEN) - 1);
- if (eflags & JBIG2_HUFFMAN_FLAGS_ISLOW)
- entries[j].u.RANGELOW = lines[CURTEMP].RANGELOW -
- HTOFFSET;
- else
- entries[j].u.RANGELOW = lines[CURTEMP].RANGELOW +
- HTOFFSET;
- entries[j].PREFLEN = PREFLEN + RANGELEN;
- entries[j].RANGELEN = 0;
- entries[j].flags = eflags;
- }
- }
- CURCODE++;
- }
- }
- }
+ if (PREFLEN == CURLEN) {
+ int RANGELEN = lines[CURTEMP].RANGELEN;
+ int start_j = CURCODE << shift;
+ int end_j = (CURCODE + 1) << shift;
+ byte eflags = 0;
+
+ if (end_j > max_j) {
+ jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "ran off the end of the entries table! (%d >= %d)", end_j, max_j);
+ jbig2_free(ctx->allocator, result->entries);
+ jbig2_free(ctx->allocator, result);
+ jbig2_free(ctx->allocator, LENCOUNT);
+ return NULL;
+ }
+ /* todo: build extension tables */
+ if (params->HTOOB && CURTEMP == n_lines - 1)
+ eflags |= JBIG2_HUFFMAN_FLAGS_ISOOB;
+ if (CURTEMP == n_lines - (params->HTOOB ? 3 : 2))
+ eflags |= JBIG2_HUFFMAN_FLAGS_ISLOW;
+ if (PREFLEN + RANGELEN > LOG_TABLE_SIZE_MAX) {
+ for (j = start_j; j < end_j; j++) {
+ entries[j].u.RANGELOW = lines[CURTEMP].RANGELOW;
+ entries[j].PREFLEN = PREFLEN;
+ entries[j].RANGELEN = RANGELEN;
+ entries[j].flags = eflags;
+ }
+ } else {
+ for (j = start_j; j < end_j; j++) {
+ int32_t HTOFFSET = (j >> (shift - RANGELEN)) & ((1 << RANGELEN) - 1);
- jbig2_free(ctx->allocator, LENCOUNT);
+ if (eflags & JBIG2_HUFFMAN_FLAGS_ISLOW)
+ entries[j].u.RANGELOW = lines[CURTEMP].RANGELOW - HTOFFSET;
+ else
+ entries[j].u.RANGELOW = lines[CURTEMP].RANGELOW + HTOFFSET;
+ entries[j].PREFLEN = PREFLEN + RANGELEN;
+ entries[j].RANGELEN = 0;
+ entries[j].flags = eflags;
+ }
+ }
+ CURCODE++;
+ }
+ }
+ }
- return result;
+ jbig2_free(ctx->allocator, LENCOUNT);
+
+ return result;
}
/** Free the memory associated with the representation of table */
void
-jbig2_release_huffman_table (Jbig2Ctx *ctx, Jbig2HuffmanTable *table)
+jbig2_release_huffman_table(Jbig2Ctx *ctx, Jbig2HuffmanTable *table)
{
- if (table != NULL) {
- jbig2_free(ctx->allocator, table->entries);
- jbig2_free(ctx->allocator, table);
- }
- return;
+ if (table != NULL) {
+ jbig2_free(ctx->allocator, table->entries);
+ jbig2_free(ctx->allocator, table);
+ }
+ return;
}
/* Routines to handle "code table segment (53)" */
@@ -520,9 +491,11 @@
const int n_proc_bytes = (endbit + 7) / 8;
const int rshift = n_proc_bytes * 8 - endbit;
int i;
+
for (i = n_proc_bytes - 1; i >= 0; i--) {
uint32_t d = data[byte_offset++];
const int nshift = i * 8 - rshift;
+
if (nshift > 0)
d <<= nshift;
else if (nshift < 0)
@@ -548,45 +521,50 @@
{
/* B.2 1) (B.2.1) Code table flags */
const int code_table_flags = segment_data[0];
- const int HTOOB = code_table_flags & 0x01; /* Bit 0: HTOOB */
+ const int HTOOB = code_table_flags & 0x01; /* Bit 0: HTOOB */
+
/* Bits 1-3: Number of bits used in code table line prefix size fields */
- const int HTPS = (code_table_flags >> 1 & 0x07) + 1;
+ const int HTPS = (code_table_flags >> 1 & 0x07) + 1;
+
/* Bits 4-6: Number of bits used in code table line range size fields */
- const int HTRS = (code_table_flags >> 4 & 0x07) + 1;
+ const int HTRS = (code_table_flags >> 4 & 0x07) + 1;
+
/* B.2 2) (B.2.2) The lower bound of the first table line in the encoded table */
- const int32_t HTLOW = jbig2_get_int32(segment_data + 1);
+ const int32_t HTLOW = jbig2_get_int32(segment_data + 1);
+
/* B.2 3) (B.2.3) One larger than the upeer bound of
the last normal table line in the encoded table */
const int32_t HTHIGH = jbig2_get_int32(segment_data + 5);
+
/* estimated number of lines int this table, used for alloacting memory for lines */
- const size_t lines_max = (segment->data_length * 8 - HTPS * (HTOOB ? 3 : 2)) /
- (HTPS + HTRS) + (HTOOB ? 3 : 2);
+ const size_t lines_max = (segment->data_length * 8 - HTPS * (HTOOB ? 3 : 2)) / (HTPS + HTRS) + (HTOOB ? 3 : 2);
+
/* points to a first table line data */
const byte *lines_data = segment_data + 9;
- const size_t lines_data_bitlen = (segment->data_length - 9) * 8; /* length in bit */
+ const size_t lines_data_bitlen = (segment->data_length - 9) * 8; /* length in bit */
+
/* bit offset: controls bit reading */
size_t boffset = 0;
+
/* B.2 4) */
int32_t CURRANGELOW = HTLOW;
size_t NTEMP = 0;
#ifdef JBIG2_DEBUG
- jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
- "DECODING USER TABLE... Flags: %d, HTOOB: %d, HTPS: %d, HTRS: %d, HTLOW: %d, HTHIGH: %d",
- code_table_flags, HTOOB, HTPS, HTRS, HTLOW, HTHIGH);
+ jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
+ "DECODING USER TABLE... Flags: %d, HTOOB: %d, HTPS: %d, HTRS: %d, HTLOW: %d, HTHIGH: %d",
+ code_table_flags, HTOOB, HTPS, HTRS, HTLOW, HTHIGH);
#endif
/* allocate HuffmanParams & HuffmanLine */
params = jbig2_new(ctx, Jbig2HuffmanParams, 1);
if (params == NULL) {
- jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "Could not allocate Huffman Table Parameter");
+ jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "Could not allocate Huffman Table Parameter");
goto error_exit;
}
line = jbig2_new(ctx, Jbig2HuffmanLine, lines_max);
if (line == NULL) {
- jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "Could not allocate Huffman Table Lines");
+ jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "Could not allocate Huffman Table Lines");
goto error_exit;
}
/* B.2 5) */
@@ -594,7 +572,7 @@
/* B.2 5) a) */
if (boffset + HTPS >= lines_data_bitlen)
goto too_short;
- line[NTEMP].PREFLEN = jbig2_table_read_bits(lines_data, &boffset, HTPS);
+ line[NTEMP].PREFLEN = jbig2_table_read_bits(lines_data, &boffset, HTPS);
/* B.2 5) b) */
if (boffset + HTRS >= lines_data_bitlen)
goto too_short;
@@ -607,7 +585,7 @@
/* B.2 6), B.2 7) lower range table line */
if (boffset + HTPS >= lines_data_bitlen)
goto too_short;
- line[NTEMP].PREFLEN = jbig2_table_read_bits(lines_data, &boffset, HTPS);
+ line[NTEMP].PREFLEN = jbig2_table_read_bits(lines_data, &boffset, HTPS);
line[NTEMP].RANGELEN = 32;
line[NTEMP].RANGELOW = HTLOW - 1;
NTEMP++;
@@ -614,7 +592,7 @@
/* B.2 8), B.2 9) upper range table line */
if (boffset + HTPS >= lines_data_bitlen)
goto too_short;
- line[NTEMP].PREFLEN = jbig2_table_read_bits(lines_data, &boffset, HTPS);
+ line[NTEMP].PREFLEN = jbig2_table_read_bits(lines_data, &boffset, HTPS);
line[NTEMP].RANGELEN = 32;
line[NTEMP].RANGELOW = HTHIGH;
NTEMP++;
@@ -623,7 +601,7 @@
/* B.2 10) a), B.2 10) b) out-of-bound table line */
if (boffset + HTPS >= lines_data_bitlen)
goto too_short;
- line[NTEMP].PREFLEN = jbig2_table_read_bits(lines_data, &boffset, HTPS);
+ line[NTEMP].PREFLEN = jbig2_table_read_bits(lines_data, &boffset, HTPS);
line[NTEMP].RANGELEN = 0;
line[NTEMP].RANGELOW = 0;
NTEMP++;
@@ -630,26 +608,27 @@
}
if (NTEMP != lines_max) {
Jbig2HuffmanLine *new_line = jbig2_renew(ctx, line,
- Jbig2HuffmanLine, NTEMP);
- if ( new_line == NULL ) {
- jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "Could not reallocate Huffman Table Lines");
+ Jbig2HuffmanLine, NTEMP);
+
+ if (new_line == NULL) {
+ jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "Could not reallocate Huffman Table Lines");
goto error_exit;
}
line = new_line;
}
- params->HTOOB = HTOOB;
+ params->HTOOB = HTOOB;
params->n_lines = NTEMP;
- params->lines = line;
+ params->lines = line;
segment->result = params;
#ifdef JBIG2_DEBUG
{
int i;
+
for (i = 0; i < NTEMP; i++) {
- jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
- "Line: %d, PREFLEN: %d, RANGELEN: %d, RANGELOW: %d",
- i, params->lines[i].PREFLEN, params->lines[i].RANGELEN, params->lines[i].RANGELOW);
+ jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
+ "Line: %d, PREFLEN: %d, RANGELEN: %d, RANGELOW: %d",
+ i, params->lines[i].PREFLEN, params->lines[i].RANGELEN, params->lines[i].RANGELOW);
}
}
#endif
@@ -686,8 +665,8 @@
int i, table_index = 0;
for (i = 0; i < segment->referred_to_segment_count; i++) {
- const Jbig2Segment * const rsegment =
- jbig2_find_segment(ctx, segment->referred_to_segments[i]);
+ const Jbig2Segment *const rsegment = jbig2_find_segment(ctx, segment->referred_to_segments[i]);
+
if (rsegment && (rsegment->flags & 63) == 53) {
if (table_index == index)
return (const Jbig2HuffmanParams *)rsegment->result;
@@ -697,7 +676,6 @@
return NULL;
}
-
#ifdef TEST
#include <stdio.h>
@@ -707,61 +685,60 @@
to use in decoding it. 1 = table B.1 (A), 2 = table B.2 (B), and so on */
/* this test stream should decode to { 8, 5, oob, 8 } */
-const byte test_stream[] = { 0xe9, 0xcb, 0xf4, 0x00 };
-const byte test_tabindex[] = { 4, 2, 2, 1 };
+const byte test_stream[] = { 0xe9, 0xcb, 0xf4, 0x00 };
+const byte test_tabindex[] = { 4, 2, 2, 1 };
static int
-test_get_word (Jbig2WordStream *self, int offset, uint32_t *word)
+test_get_word(Jbig2WordStream *self, int offset, uint32_t *word)
{
- /* assume test_stream[] is at least 4 bytes */
- if (offset+3 > sizeof(test_stream))
- return -1;
- *word = ( (test_stream[offset] << 24) |
- (test_stream[offset+1] << 16) |
- (test_stream[offset+2] << 8) |
- (test_stream[offset+3]) );
- return 0;
+ /* assume test_stream[] is at least 4 bytes */
+ if (offset + 3 > sizeof(test_stream))
+ return -1;
+ *word = ((test_stream[offset] << 24) | (test_stream[offset + 1] << 16) | (test_stream[offset + 2] << 8) | (test_stream[offset + 3]));
+ return 0;
}
int
-main (int argc, char **argv)
+main(int argc, char **argv)
{
- Jbig2Ctx *ctx;
- Jbig2HuffmanTable *tables[5];
- Jbig2HuffmanState *hs;
- Jbig2WordStream ws;
- bool oob;
- int32_t code;
+ Jbig2Ctx *ctx;
+ Jbig2HuffmanTable *tables[5];
+ Jbig2HuffmanState *hs;
+ Jbig2WordStream ws;
+ bool oob;
+ int32_t code;
- ctx = jbig2_ctx_new(NULL, 0, NULL, NULL, NULL);
+ ctx = jbig2_ctx_new(NULL, 0, NULL, NULL, NULL);
- tables[0] = NULL;
- tables[1] = jbig2_build_huffman_table (ctx, &jbig2_huffman_params_A);
- tables[2] = jbig2_build_huffman_table (ctx, &jbig2_huffman_params_B);
- tables[3] = NULL;
- tables[4] = jbig2_build_huffman_table (ctx, &jbig2_huffman_params_D);
- ws.get_next_word = test_get_word;
- hs = jbig2_huffman_new (ctx, &ws);
+ tables[0] = NULL;
+ tables[1] = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_A);
+ tables[2] = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_B);
+ tables[3] = NULL;
+ tables[4] = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_D);
+ ws.get_next_word = test_get_word;
+ hs = jbig2_huffman_new(ctx, &ws);
- printf("testing jbig2 huffmann decoding...");
- printf("\t(should be 8 5 (oob) 8)\n");
+ printf("testing jbig2 huffmann decoding...");
+ printf("\t(should be 8 5 (oob) 8)\n");
- {
- int i;
- int sequence_length = sizeof(test_tabindex);
+ {
+ int i;
+ int sequence_length = sizeof(test_tabindex);
- for (i = 0; i < sequence_length; i++) {
- code = jbig2_huffman_get (hs, tables[test_tabindex[i]], &oob);
- if (oob) printf("(oob) ");
- else printf("%d ", code);
- }
- }
+ for (i = 0; i < sequence_length; i++) {
+ code = jbig2_huffman_get(hs, tables[test_tabindex[i]], &oob);
+ if (oob)
+ printf("(oob) ");
+ else
+ printf("%d ", code);
+ }
+ }
- printf("\n");
+ printf("\n");
- jbig2_ctx_free(ctx);
+ jbig2_ctx_free(ctx);
- return 0;
+ return 0;
}
#endif
@@ -807,10 +784,9 @@
/* 0000 0000 0001 1100 0000 0000 0000 0000 */
0x00, 0x1c, 0x00, 0x00,
/* 0000 0000 0000 01 */
- 0x00, 0x04,
+ 0x00, 0x04,
};
-
/* test code for Table B.2 - Standard Huffman table B */
const int32_t test_output_B[] = {
/* line 0, PREFLEN=1, RANGELEN=0, VAL=0, 0 */
@@ -1962,7 +1938,7 @@
test_output_##x, countof(test_output_##x), \
}
-test_huffmancodes_t tests[] = {
+test_huffmancodes_t tests[] = {
DEF_TEST_HUFFMANCODES(A),
DEF_TEST_HUFFMANCODES(B),
DEF_TEST_HUFFMANCODES(C),
@@ -1989,22 +1965,23 @@
test_get_word(Jbig2WordStream *self, int offset, uint32_t *word)
{
uint32_t val = 0;
- test_stream_t *st = (test_stream_t *)self;
+ test_stream_t *st = (test_stream_t *) self;
+
if (st != NULL) {
if (st->h != NULL) {
if (offset >= st->h->input_len)
return -1;
- if (offset < st->h->input_len) {
- val |= (st->h->input[offset] << 24);
+ if (offset < st->h->input_len) {
+ val |= (st->h->input[offset] << 24);
}
- if (offset+1 < st->h->input_len) {
- val |= (st->h->input[offset+1] << 16);
+ if (offset + 1 < st->h->input_len) {
+ val |= (st->h->input[offset + 1] << 16);
}
- if (offset+2 < st->h->input_len) {
- val |= (st->h->input[offset+2] << 8);
+ if (offset + 2 < st->h->input_len) {
+ val |= (st->h->input[offset + 2] << 8);
}
- if (offset+3 < st->h->input_len) {
- val |= st->h->input[offset+3];
+ if (offset + 3 < st->h->input_len) {
+ val |= st->h->input[offset + 3];
}
}
}
@@ -2013,7 +1990,7 @@
}
int
-main (int argc, char **argv)
+main(int argc, char **argv)
{
Jbig2Ctx *ctx = jbig2_ctx_new(NULL, 0, NULL, NULL, NULL);
int i;
@@ -2035,7 +2012,7 @@
} else {
/* jbig2_dump_huffman_table(table); */
hs = jbig2_huffman_new(ctx, &st.ws);
- if ( hs == NULL ) {
+ if (hs == NULL) {
printf("jbig2_huffman_new() returned NULL!\n");
} else {
for (j = 0; j < st.h->output_len; j++) {
@@ -2045,6 +2022,7 @@
printf("ok, ");
} else {
int need_comma = 0;
+
printf("NG(");
if (code != st.h->output[j]) {
printf("%d", code);
--- a/jbig2_huffman.h
+++ b/jbig2_huffman.h
@@ -17,7 +17,6 @@
jbig2dec
*/
-
#ifndef JBIG2_HUFFMAN_H
#define JBIG2_HUFFMAN_H
@@ -29,54 +28,47 @@
typedef struct _Jbig2HuffmanParams Jbig2HuffmanParams;
struct _Jbig2HuffmanEntry {
- union {
- int32_t RANGELOW;
- Jbig2HuffmanTable *ext_table;
- } u;
- byte PREFLEN;
- byte RANGELEN;
- byte flags;
+ union {
+ int32_t RANGELOW;
+ Jbig2HuffmanTable *ext_table;
+ } u;
+ byte PREFLEN;
+ byte RANGELEN;
+ byte flags;
};
struct _Jbig2HuffmanTable {
- int log_table_size;
- Jbig2HuffmanEntry *entries;
+ int log_table_size;
+ Jbig2HuffmanEntry *entries;
};
typedef struct _Jbig2HuffmanLine Jbig2HuffmanLine;
struct _Jbig2HuffmanLine {
- int PREFLEN;
- int RANGELEN;
- int RANGELOW;
+ int PREFLEN;
+ int RANGELEN;
+ int RANGELOW;
};
struct _Jbig2HuffmanParams {
- bool HTOOB;
- int n_lines;
- const Jbig2HuffmanLine *lines;
+ bool HTOOB;
+ int n_lines;
+ const Jbig2HuffmanLine *lines;
};
-Jbig2HuffmanState *
-jbig2_huffman_new (Jbig2Ctx *ctx, Jbig2WordStream *ws);
+Jbig2HuffmanState *jbig2_huffman_new(Jbig2Ctx *ctx, Jbig2WordStream *ws);
-void
-jbig2_huffman_free (Jbig2Ctx *ctx, Jbig2HuffmanState *hs);
+void jbig2_huffman_free(Jbig2Ctx *ctx, Jbig2HuffmanState *hs);
-void
-jbig2_huffman_skip(Jbig2HuffmanState *hs);
+void jbig2_huffman_skip(Jbig2HuffmanState *hs);
void jbig2_huffman_advance(Jbig2HuffmanState *hs, int offset);
-int
-jbig2_huffman_offset(Jbig2HuffmanState *hs);
+int jbig2_huffman_offset(Jbig2HuffmanState *hs);
-int32_t
-jbig2_huffman_get (Jbig2HuffmanState *hs,
- const Jbig2HuffmanTable *table, bool *oob);
+int32_t jbig2_huffman_get(Jbig2HuffmanState *hs, const Jbig2HuffmanTable *table, bool *oob);
-int32_t
-jbig2_huffman_get_bits (Jbig2HuffmanState *hs, const int bits, int *err);
+int32_t jbig2_huffman_get_bits(Jbig2HuffmanState *hs, const int bits, int *err);
#ifdef JBIG2_DEBUG
void jbig2_dump_huffman_state(Jbig2HuffmanState *hs);
@@ -83,11 +75,9 @@
void jbig2_dump_huffman_binary(Jbig2HuffmanState *hs);
#endif
-Jbig2HuffmanTable *
-jbig2_build_huffman_table (Jbig2Ctx *ctx, const Jbig2HuffmanParams *params);
+Jbig2HuffmanTable *jbig2_build_huffman_table(Jbig2Ctx *ctx, const Jbig2HuffmanParams *params);
-void
-jbig2_release_huffman_table (Jbig2Ctx *ctx, Jbig2HuffmanTable *table);
+void jbig2_release_huffman_table(Jbig2Ctx *ctx, Jbig2HuffmanTable *table);
/* standard Huffman templates defined by the specification */
extern const Jbig2HuffmanParams jbig2_huffman_params_A; /* Table B.1 */
@@ -109,15 +99,12 @@
/* Routines to handle "code table segment (53)" */
/* Parse a code table segment, store Jbig2HuffmanParams in segment->result */
-int
-jbig2_table(Jbig2Ctx *ctx, Jbig2Segment *segment, const byte *segment_data);
+int jbig2_table(Jbig2Ctx *ctx, Jbig2Segment *segment, const byte *segment_data);
/* free Jbig2HuffmanParams allocated by jbig2_huffman_table() */
-void
-jbig2_table_free(Jbig2Ctx *ctx, Jbig2HuffmanParams *params);
+void jbig2_table_free(Jbig2Ctx *ctx, Jbig2HuffmanParams *params);
/* find a user supplied table used by 'segment' and by 'index' */
-const Jbig2HuffmanParams *
-jbig2_find_table(Jbig2Ctx *ctx, Jbig2Segment *segment, int index);
+const Jbig2HuffmanParams *jbig2_find_table(Jbig2Ctx *ctx, Jbig2Segment *segment, int index);
#endif /* JBIG2_HUFFMAN_H */
--- a/jbig2_hufftab.h
+++ b/jbig2_hufftab.h
@@ -17,7 +17,6 @@
jbig2dec
*/
-
/* predefined Huffman table definitions
-- See Annex B of the JBIG2 specification */
@@ -29,318 +28,287 @@
#define JBIG2_COUNTOF(x) (sizeof((x)) / sizeof((x)[0]))
/* Table B.1 */
-const Jbig2HuffmanLine
-jbig2_huffman_lines_A[] = {
- { 1, 4, 0 },
- { 2, 8, 16 },
- { 3, 16, 272 },
- { 0, 32, -1 }, /* low */
- { 3, 32, 65808 } /* high */
+const Jbig2HuffmanLine jbig2_huffman_lines_A[] = {
+ {1, 4, 0},
+ {2, 8, 16},
+ {3, 16, 272},
+ {0, 32, -1}, /* low */
+ {3, 32, 65808} /* high */
};
-const Jbig2HuffmanParams
-jbig2_huffman_params_A = { FALSE, JBIG2_COUNTOF(jbig2_huffman_lines_A), jbig2_huffman_lines_A };
+const Jbig2HuffmanParams jbig2_huffman_params_A = { FALSE, JBIG2_COUNTOF(jbig2_huffman_lines_A), jbig2_huffman_lines_A };
/* Table B.2 */
-const Jbig2HuffmanLine
-jbig2_huffman_lines_B[] = {
- { 1, 0, 0 },
- { 2, 0, 1 },
- { 3, 0, 2 },
- { 4, 3, 3 },
- { 5, 6, 11 },
- { 0, 32, -1 }, /* low */
- { 6, 32, 75 }, /* high */
- { 6, 0, 0 } /* OOB */
+const Jbig2HuffmanLine jbig2_huffman_lines_B[] = {
+ {1, 0, 0},
+ {2, 0, 1},
+ {3, 0, 2},
+ {4, 3, 3},
+ {5, 6, 11},
+ {0, 32, -1}, /* low */
+ {6, 32, 75}, /* high */
+ {6, 0, 0} /* OOB */
};
-const Jbig2HuffmanParams
-jbig2_huffman_params_B = { TRUE, JBIG2_COUNTOF(jbig2_huffman_lines_B), jbig2_huffman_lines_B };
+const Jbig2HuffmanParams jbig2_huffman_params_B = { TRUE, JBIG2_COUNTOF(jbig2_huffman_lines_B), jbig2_huffman_lines_B };
/* Table B.3 */
-const Jbig2HuffmanLine
-jbig2_huffman_lines_C[] = {
- { 8, 8, -256 },
- { 1, 0, 0 },
- { 2, 0, 1 },
- { 3, 0, 2 },
- { 4, 3, 3 },
- { 5, 6, 11 },
- { 8, 32, -257 }, /* low */
- { 7, 32, 75 }, /* high */
- { 6, 0, 0 } /* OOB */
+const Jbig2HuffmanLine jbig2_huffman_lines_C[] = {
+ {8, 8, -256},
+ {1, 0, 0},
+ {2, 0, 1},
+ {3, 0, 2},
+ {4, 3, 3},
+ {5, 6, 11},
+ {8, 32, -257}, /* low */
+ {7, 32, 75}, /* high */
+ {6, 0, 0} /* OOB */
};
-const Jbig2HuffmanParams
-jbig2_huffman_params_C = { TRUE, JBIG2_COUNTOF(jbig2_huffman_lines_C), jbig2_huffman_lines_C };
+const Jbig2HuffmanParams jbig2_huffman_params_C = { TRUE, JBIG2_COUNTOF(jbig2_huffman_lines_C), jbig2_huffman_lines_C };
/* Table B.4 */
-const Jbig2HuffmanLine
-jbig2_huffman_lines_D[] = {
- { 1, 0, 1 },
- { 2, 0, 2 },
- { 3, 0, 3 },
- { 4, 3, 4 },
- { 5, 6, 12 },
- { 0, 32, -1 }, /* low */
- { 5, 32, 76 }, /* high */
+const Jbig2HuffmanLine jbig2_huffman_lines_D[] = {
+ {1, 0, 1},
+ {2, 0, 2},
+ {3, 0, 3},
+ {4, 3, 4},
+ {5, 6, 12},
+ {0, 32, -1}, /* low */
+ {5, 32, 76}, /* high */
};
-const Jbig2HuffmanParams
-jbig2_huffman_params_D = { FALSE, JBIG2_COUNTOF(jbig2_huffman_lines_D), jbig2_huffman_lines_D };
+const Jbig2HuffmanParams jbig2_huffman_params_D = { FALSE, JBIG2_COUNTOF(jbig2_huffman_lines_D), jbig2_huffman_lines_D };
/* Table B.5 */
-const Jbig2HuffmanLine
-jbig2_huffman_lines_E[] = {
- {7, 8, -255},
- {1, 0, 1},
- {2, 0, 2},
- {3, 0, 3},
- {4, 3, 4},
- {5, 6, 12},
- {7, 32, -256}, /* low */
- {6, 32, 76} /* high */
+const Jbig2HuffmanLine jbig2_huffman_lines_E[] = {
+ {7, 8, -255},
+ {1, 0, 1},
+ {2, 0, 2},
+ {3, 0, 3},
+ {4, 3, 4},
+ {5, 6, 12},
+ {7, 32, -256}, /* low */
+ {6, 32, 76} /* high */
};
-const Jbig2HuffmanParams
-jbig2_huffman_params_E = { FALSE, JBIG2_COUNTOF(jbig2_huffman_lines_E), jbig2_huffman_lines_E };
+const Jbig2HuffmanParams jbig2_huffman_params_E = { FALSE, JBIG2_COUNTOF(jbig2_huffman_lines_E), jbig2_huffman_lines_E };
/* Table B.6 */
-const Jbig2HuffmanLine
-jbig2_huffman_lines_F[] = {
- {5, 10, -2048},
- {4, 9, -1024},
- {4, 8, -512},
- {4, 7, -256},
- {5, 6, -128},
- {5, 5, -64},
- {4, 5, -32},
- {2, 7, 0},
- {3, 7, 128},
- {3, 8, 256},
- {4, 9, 512},
- {4, 10, 1024},
- {6, 32, -2049}, /* low */
- {6, 32, 2048} /* high */
+const Jbig2HuffmanLine jbig2_huffman_lines_F[] = {
+ {5, 10, -2048},
+ {4, 9, -1024},
+ {4, 8, -512},
+ {4, 7, -256},
+ {5, 6, -128},
+ {5, 5, -64},
+ {4, 5, -32},
+ {2, 7, 0},
+ {3, 7, 128},
+ {3, 8, 256},
+ {4, 9, 512},
+ {4, 10, 1024},
+ {6, 32, -2049}, /* low */
+ {6, 32, 2048} /* high */
};
-const Jbig2HuffmanParams
-jbig2_huffman_params_F = { FALSE, JBIG2_COUNTOF(jbig2_huffman_lines_F), jbig2_huffman_lines_F };
+const Jbig2HuffmanParams jbig2_huffman_params_F = { FALSE, JBIG2_COUNTOF(jbig2_huffman_lines_F), jbig2_huffman_lines_F };
/* Table B.7 */
-const Jbig2HuffmanLine
-jbig2_huffman_lines_G[] = {
- {4, 9, -1024},
- {3, 8, -512},
- {4, 7, -256},
- {5, 6, -128},
- {5, 5, -64},
- {4, 5, -32},
- {4, 5, 0},
- {5, 5, 32},
- {5, 6, 64},
- {4, 7, 128},
- {3, 8, 256},
- {3, 9, 512},
- {3, 10, 1024},
- {5, 32, -1025}, /* low */
- {5, 32, 2048} /* high */
+const Jbig2HuffmanLine jbig2_huffman_lines_G[] = {
+ {4, 9, -1024},
+ {3, 8, -512},
+ {4, 7, -256},
+ {5, 6, -128},
+ {5, 5, -64},
+ {4, 5, -32},
+ {4, 5, 0},
+ {5, 5, 32},
+ {5, 6, 64},
+ {4, 7, 128},
+ {3, 8, 256},
+ {3, 9, 512},
+ {3, 10, 1024},
+ {5, 32, -1025}, /* low */
+ {5, 32, 2048} /* high */
};
-const Jbig2HuffmanParams
-jbig2_huffman_params_G = { FALSE, JBIG2_COUNTOF(jbig2_huffman_lines_G), jbig2_huffman_lines_G };
+const Jbig2HuffmanParams jbig2_huffman_params_G = { FALSE, JBIG2_COUNTOF(jbig2_huffman_lines_G), jbig2_huffman_lines_G };
/* Table B.8 */
-const Jbig2HuffmanLine
-jbig2_huffman_lines_H[] = {
- {8, 3, -15},
- {9, 1, -7},
- {8, 1, -5},
- {9, 0, -3},
- {7, 0, -2},
- {4, 0, -1},
- {2, 1, 0},
- {5, 0, 2},
- {6, 0, 3},
- {3, 4, 4},
- {6, 1, 20},
- {4, 4, 22},
- {4, 5, 38},
- {5, 6, 70},
- {5, 7, 134},
- {6, 7, 262},
- {7, 8, 390},
- {6, 10, 646},
- {9, 32, -16}, /* low */
- {9, 32, 1670}, /* high */
- {2, 0, 0} /* OOB */
+const Jbig2HuffmanLine jbig2_huffman_lines_H[] = {
+ {8, 3, -15},
+ {9, 1, -7},
+ {8, 1, -5},
+ {9, 0, -3},
+ {7, 0, -2},
+ {4, 0, -1},
+ {2, 1, 0},
+ {5, 0, 2},
+ {6, 0, 3},
+ {3, 4, 4},
+ {6, 1, 20},
+ {4, 4, 22},
+ {4, 5, 38},
+ {5, 6, 70},
+ {5, 7, 134},
+ {6, 7, 262},
+ {7, 8, 390},
+ {6, 10, 646},
+ {9, 32, -16}, /* low */
+ {9, 32, 1670}, /* high */
+ {2, 0, 0} /* OOB */
};
-const Jbig2HuffmanParams
-jbig2_huffman_params_H = { TRUE, JBIG2_COUNTOF(jbig2_huffman_lines_H), jbig2_huffman_lines_H };
+const Jbig2HuffmanParams jbig2_huffman_params_H = { TRUE, JBIG2_COUNTOF(jbig2_huffman_lines_H), jbig2_huffman_lines_H };
/* Table B.9 */
-const Jbig2HuffmanLine
-jbig2_huffman_lines_I[] = {
- {8, 4, -31},
- {9, 2, -15},
- {8, 2, -11},
- {9, 1, -7},
- {7, 1, -5},
- {4, 1, -3},
- {3, 1, -1},
- {3, 1, 1},
- {5, 1, 3},
- {6, 1, 5},
- {3, 5, 7},
- {6, 2, 39},
- {4, 5, 43},
- {4, 6, 75},
- {5, 7, 139},
- {5, 8, 267},
- {6, 8, 523},
- {7, 9, 779},
- {6, 11, 1291},
- {9, 32, -32}, /* low */
- {9, 32, 3339}, /* high */
- {2, 0, 0} /* OOB */
+const Jbig2HuffmanLine jbig2_huffman_lines_I[] = {
+ {8, 4, -31},
+ {9, 2, -15},
+ {8, 2, -11},
+ {9, 1, -7},
+ {7, 1, -5},
+ {4, 1, -3},
+ {3, 1, -1},
+ {3, 1, 1},
+ {5, 1, 3},
+ {6, 1, 5},
+ {3, 5, 7},
+ {6, 2, 39},
+ {4, 5, 43},
+ {4, 6, 75},
+ {5, 7, 139},
+ {5, 8, 267},
+ {6, 8, 523},
+ {7, 9, 779},
+ {6, 11, 1291},
+ {9, 32, -32}, /* low */
+ {9, 32, 3339}, /* high */
+ {2, 0, 0} /* OOB */
};
-const Jbig2HuffmanParams
-jbig2_huffman_params_I = { TRUE, JBIG2_COUNTOF(jbig2_huffman_lines_I), jbig2_huffman_lines_I };
+const Jbig2HuffmanParams jbig2_huffman_params_I = { TRUE, JBIG2_COUNTOF(jbig2_huffman_lines_I), jbig2_huffman_lines_I };
/* Table B.10 */
-const Jbig2HuffmanLine
-jbig2_huffman_lines_J[] = {
- {7, 4, -21},
- {8, 0, -5},
- {7, 0, -4},
- {5, 0, -3},
- {2, 2, -2},
- {5, 0, 2},
- {6, 0, 3},
- {7, 0, 4},
- {8, 0, 5},
- {2, 6, 6},
- {5, 5, 70},
- {6, 5, 102},
- {6, 6, 134},
- {6, 7, 198},
- {6, 8, 326},
- {6, 9, 582},
- {6, 10, 1094},
- {7, 11, 2118},
- {8, 32, -22}, /* low */
- {8, 32, 4166}, /* high */
- {2, 0, 0} /* OOB */
+const Jbig2HuffmanLine jbig2_huffman_lines_J[] = {
+ {7, 4, -21},
+ {8, 0, -5},
+ {7, 0, -4},
+ {5, 0, -3},
+ {2, 2, -2},
+ {5, 0, 2},
+ {6, 0, 3},
+ {7, 0, 4},
+ {8, 0, 5},
+ {2, 6, 6},
+ {5, 5, 70},
+ {6, 5, 102},
+ {6, 6, 134},
+ {6, 7, 198},
+ {6, 8, 326},
+ {6, 9, 582},
+ {6, 10, 1094},
+ {7, 11, 2118},
+ {8, 32, -22}, /* low */
+ {8, 32, 4166}, /* high */
+ {2, 0, 0} /* OOB */
};
-const Jbig2HuffmanParams
-jbig2_huffman_params_J = { TRUE, JBIG2_COUNTOF(jbig2_huffman_lines_J), jbig2_huffman_lines_J };
+const Jbig2HuffmanParams jbig2_huffman_params_J = { TRUE, JBIG2_COUNTOF(jbig2_huffman_lines_J), jbig2_huffman_lines_J };
/* Table B.11 */
-const Jbig2HuffmanLine
-jbig2_huffman_lines_K[] = {
- {1, 0, 1},
- {2, 1, 2},
- {4, 0, 4},
- {4, 1, 5},
- {5, 1, 7},
- {5, 2, 9},
- {6, 2, 13},
- {7, 2, 17},
- {7, 3, 21},
- {7, 4, 29},
- {7, 5, 45},
- {7, 6, 77},
- {0, 32, -1}, /* low */
- {7, 32, 141} /* high */
+const Jbig2HuffmanLine jbig2_huffman_lines_K[] = {
+ {1, 0, 1},
+ {2, 1, 2},
+ {4, 0, 4},
+ {4, 1, 5},
+ {5, 1, 7},
+ {5, 2, 9},
+ {6, 2, 13},
+ {7, 2, 17},
+ {7, 3, 21},
+ {7, 4, 29},
+ {7, 5, 45},
+ {7, 6, 77},
+ {0, 32, -1}, /* low */
+ {7, 32, 141} /* high */
};
-const Jbig2HuffmanParams
-jbig2_huffman_params_K = { FALSE, JBIG2_COUNTOF(jbig2_huffman_lines_K), jbig2_huffman_lines_K };
+const Jbig2HuffmanParams jbig2_huffman_params_K = { FALSE, JBIG2_COUNTOF(jbig2_huffman_lines_K), jbig2_huffman_lines_K };
/* Table B.12 */
-const Jbig2HuffmanLine
-jbig2_huffman_lines_L[] = {
- {1, 0, 1},
- {2, 0, 2},
- {3, 1, 3},
- {5, 0, 5},
- {5, 1, 6},
- {6, 1, 8},
- {7, 0, 10},
- {7, 1, 11},
- {7, 2, 13},
- {7, 3, 17},
- {7, 4, 25},
- {8, 5, 41},
- {8, 32, 73},
- {0, 32, -1}, /* low */
- {0, 32, 0} /* high */
+const Jbig2HuffmanLine jbig2_huffman_lines_L[] = {
+ {1, 0, 1},
+ {2, 0, 2},
+ {3, 1, 3},
+ {5, 0, 5},
+ {5, 1, 6},
+ {6, 1, 8},
+ {7, 0, 10},
+ {7, 1, 11},
+ {7, 2, 13},
+ {7, 3, 17},
+ {7, 4, 25},
+ {8, 5, 41},
+ {8, 32, 73},
+ {0, 32, -1}, /* low */
+ {0, 32, 0} /* high */
};
-const Jbig2HuffmanParams
-jbig2_huffman_params_L = { FALSE, JBIG2_COUNTOF(jbig2_huffman_lines_L), jbig2_huffman_lines_L };
+const Jbig2HuffmanParams jbig2_huffman_params_L = { FALSE, JBIG2_COUNTOF(jbig2_huffman_lines_L), jbig2_huffman_lines_L };
-
/* Table B.13 */
-const Jbig2HuffmanLine
-jbig2_huffman_lines_M[] = {
- {1, 0, 1},
- {3, 0, 2},
- {4, 0, 3},
- {5, 0, 4},
- {4, 1, 5},
- {3, 3, 7},
- {6, 1, 15},
- {6, 2, 17},
- {6, 3, 21},
- {6, 4, 29},
- {6, 5, 45},
- {7, 6, 77},
- {0, 32, -1}, /* low */
- {7, 32, 141} /* high */
+const Jbig2HuffmanLine jbig2_huffman_lines_M[] = {
+ {1, 0, 1},
+ {3, 0, 2},
+ {4, 0, 3},
+ {5, 0, 4},
+ {4, 1, 5},
+ {3, 3, 7},
+ {6, 1, 15},
+ {6, 2, 17},
+ {6, 3, 21},
+ {6, 4, 29},
+ {6, 5, 45},
+ {7, 6, 77},
+ {0, 32, -1}, /* low */
+ {7, 32, 141} /* high */
};
-const Jbig2HuffmanParams
-jbig2_huffman_params_M = { FALSE, JBIG2_COUNTOF(jbig2_huffman_lines_M), jbig2_huffman_lines_M };
+const Jbig2HuffmanParams jbig2_huffman_params_M = { FALSE, JBIG2_COUNTOF(jbig2_huffman_lines_M), jbig2_huffman_lines_M };
/* Table B.14 */
-const Jbig2HuffmanLine
-jbig2_huffman_lines_N[] = {
- { 3, 0, -2 },
- { 3, 0, -1 },
- { 1, 0, 0 },
- { 3, 0, 1 },
- { 3, 0, 2 },
- { 0, 32, -1 }, /* low */
- { 0, 32, 3 }, /* high */
+const Jbig2HuffmanLine jbig2_huffman_lines_N[] = {
+ {3, 0, -2},
+ {3, 0, -1},
+ {1, 0, 0},
+ {3, 0, 1},
+ {3, 0, 2},
+ {0, 32, -1}, /* low */
+ {0, 32, 3}, /* high */
};
-const Jbig2HuffmanParams
-jbig2_huffman_params_N = { FALSE, JBIG2_COUNTOF(jbig2_huffman_lines_N), jbig2_huffman_lines_N };
+const Jbig2HuffmanParams jbig2_huffman_params_N = { FALSE, JBIG2_COUNTOF(jbig2_huffman_lines_N), jbig2_huffman_lines_N };
/* Table B.15 */
-const Jbig2HuffmanLine
-jbig2_huffman_lines_O[] = {
- {7, 4, -24},
- {6, 2, -8},
- {5, 1, -4},
- {4, 0, -2},
- {3, 0, -1},
- {1, 0, 0},
- {3, 0, 1},
- {4, 0, 2},
- {5, 1, 3},
- {6, 2, 5},
- {7, 4, 9},
- {7, 32, -25}, /* low */
- {7, 32, 25} /* high */
+const Jbig2HuffmanLine jbig2_huffman_lines_O[] = {
+ {7, 4, -24},
+ {6, 2, -8},
+ {5, 1, -4},
+ {4, 0, -2},
+ {3, 0, -1},
+ {1, 0, 0},
+ {3, 0, 1},
+ {4, 0, 2},
+ {5, 1, 3},
+ {6, 2, 5},
+ {7, 4, 9},
+ {7, 32, -25}, /* low */
+ {7, 32, 25} /* high */
};
-const Jbig2HuffmanParams
-jbig2_huffman_params_O = { FALSE, JBIG2_COUNTOF(jbig2_huffman_lines_O), jbig2_huffman_lines_O };
+const Jbig2HuffmanParams jbig2_huffman_params_O = { FALSE, JBIG2_COUNTOF(jbig2_huffman_lines_O), jbig2_huffman_lines_O };
#undef JBIG2_COUNTOF
--- a/jbig2_image.c
+++ b/jbig2_image.c
@@ -17,7 +17,6 @@
jbig2dec
*/
-
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
@@ -25,123 +24,114 @@
#include <stdio.h>
#include <stdlib.h>
-#include <string.h> /* memcpy() */
+#include <string.h> /* memcpy() */
#include "jbig2.h"
#include "jbig2_priv.h"
#include "jbig2_image.h"
-
/* allocate a Jbig2Image structure and its associated bitmap */
-Jbig2Image* jbig2_image_new(Jbig2Ctx *ctx, int width, int height)
+Jbig2Image *
+jbig2_image_new(Jbig2Ctx *ctx, int width, int height)
{
- Jbig2Image *image;
- int stride;
- int64_t check;
+ Jbig2Image *image;
+ int stride;
+ int64_t check;
- image = jbig2_new(ctx, Jbig2Image, 1);
- if (image == NULL) {
- jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1,
- "could not allocate image structure in jbig2_image_new");
- return NULL;
- }
+ image = jbig2_new(ctx, Jbig2Image, 1);
+ if (image == NULL) {
+ jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "could not allocate image structure in jbig2_image_new");
+ return NULL;
+ }
- stride = ((width - 1) >> 3) + 1; /* generate a byte-aligned stride */
- /* check for integer multiplication overflow */
- check = ((int64_t)stride)*((int64_t)height);
- if (check != (int)check)
- {
- jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1,
- "integer multiplication overflow from stride(%d)*height(%d)",
- stride, height);
- jbig2_free(ctx->allocator, image);
- return NULL;
- }
- /* Add 1 to accept runs that exceed image width and clamped to width+1 */
- image->data = jbig2_new(ctx, uint8_t, (int)check + 1);
- if (image->data == NULL) {
- jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1,
- "could not allocate image data buffer! [stride(%d)*height(%d) bytes]",
- stride, height);
- jbig2_free(ctx->allocator, image);
- return NULL;
- }
+ stride = ((width - 1) >> 3) + 1; /* generate a byte-aligned stride */
+ /* check for integer multiplication overflow */
+ check = ((int64_t) stride) * ((int64_t) height);
+ if (check != (int)check) {
+ jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "integer multiplication overflow from stride(%d)*height(%d)", stride, height);
+ jbig2_free(ctx->allocator, image);
+ return NULL;
+ }
+ /* Add 1 to accept runs that exceed image width and clamped to width+1 */
+ image->data = jbig2_new(ctx, uint8_t, (int)check + 1);
+ if (image->data == NULL) {
+ jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "could not allocate image data buffer! [stride(%d)*height(%d) bytes]", stride, height);
+ jbig2_free(ctx->allocator, image);
+ return NULL;
+ }
- image->width = width;
- image->height = height;
- image->stride = stride;
- image->refcount = 1;
+ image->width = width;
+ image->height = height;
+ image->stride = stride;
+ image->refcount = 1;
- return image;
+ return image;
}
/* clone an image pointer by bumping its reference count */
-Jbig2Image* jbig2_image_clone(Jbig2Ctx *ctx, Jbig2Image *image)
+Jbig2Image *
+jbig2_image_clone(Jbig2Ctx *ctx, Jbig2Image *image)
{
- if (image)
- image->refcount++;
- return image;
+ if (image)
+ image->refcount++;
+ return image;
}
/* release an image pointer, freeing it it appropriate */
-void jbig2_image_release(Jbig2Ctx *ctx, Jbig2Image *image)
+void
+jbig2_image_release(Jbig2Ctx *ctx, Jbig2Image *image)
{
- if (image == NULL)
- return;
- image->refcount--;
- if (!image->refcount) jbig2_image_free(ctx, image);
+ if (image == NULL)
+ return;
+ image->refcount--;
+ if (!image->refcount)
+ jbig2_image_free(ctx, image);
}
/* free a Jbig2Image structure and its associated memory */
-void jbig2_image_free(Jbig2Ctx *ctx, Jbig2Image *image)
+void
+jbig2_image_free(Jbig2Ctx *ctx, Jbig2Image *image)
{
- if (image)
- jbig2_free(ctx->allocator, image->data);
- jbig2_free(ctx->allocator, image);
+ if (image)
+ jbig2_free(ctx->allocator, image->data);
+ jbig2_free(ctx->allocator, image);
}
/* resize a Jbig2Image */
-Jbig2Image *jbig2_image_resize(Jbig2Ctx *ctx, Jbig2Image *image,
- int width, int height)
+Jbig2Image *
+jbig2_image_resize(Jbig2Ctx *ctx, Jbig2Image *image, int width, int height)
{
- if (width == image->width) {
- /* check for integer multiplication overflow */
- int64_t check = ((int64_t)image->stride)*((int64_t)height);
- if (check != (int)check)
- {
- jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1,
- "integer multiplication overflow during resize stride(%d)*height(%d)",
- image->stride, height);
- return NULL;
- }
- /* use the same stride, just change the length */
- image->data = jbig2_renew(ctx, image->data, uint8_t, (int)check);
- if (image->data == NULL) {
- jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1,
- "could not resize image buffer!");
- return NULL;
- }
- if (height > image->height) {
- memset(image->data + image->height*image->stride,
- 0, (height - image->height)*image->stride);
- }
- image->height = height;
+ if (width == image->width) {
+ /* check for integer multiplication overflow */
+ int64_t check = ((int64_t) image->stride) * ((int64_t) height);
- } else {
- /* we must allocate a new image buffer and copy */
- jbig2_error(ctx, JBIG2_SEVERITY_WARNING, -1,
- "jbig2_image_resize called with a different width (NYI)");
- }
+ if (check != (int)check) {
+ jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "integer multiplication overflow during resize stride(%d)*height(%d)", image->stride, height);
+ return NULL;
+ }
+ /* use the same stride, just change the length */
+ image->data = jbig2_renew(ctx, image->data, uint8_t, (int)check);
+ if (image->data == NULL) {
+ jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "could not resize image buffer!");
+ return NULL;
+ }
+ if (height > image->height) {
+ memset(image->data + image->height * image->stride, 0, (height - image->height) * image->stride);
+ }
+ image->height = height;
- return NULL;
+ } else {
+ /* we must allocate a new image buffer and copy */
+ jbig2_error(ctx, JBIG2_SEVERITY_WARNING, -1, "jbig2_image_resize called with a different width (NYI)");
+ }
+
+ return NULL;
}
/* composite one jbig2_image onto another
slow but general version */
static int
-jbig2_image_compose_unopt(Jbig2Ctx *ctx,
- Jbig2Image *dst, Jbig2Image *src,
- int x, int y, Jbig2ComposeOp op)
+jbig2_image_compose_unopt(Jbig2Ctx *ctx, Jbig2Image *dst, Jbig2Image *src, int x, int y, Jbig2ComposeOp op)
{
int i, j;
int sw = src->width;
@@ -150,56 +140,57 @@
int sy = 0;
/* clip to the dst image boundaries */
- if (x < 0) { sx += -x; sw -= -x; x = 0; }
- if (y < 0) { sy += -y; sh -= -y; y = 0; }
- if (x + sw >= dst->width) sw = dst->width - x;
- if (y + sh >= dst->height) sh = dst->height - y;
+ if (x < 0) {
+ sx += -x;
+ sw -= -x;
+ x = 0;
+ }
+ if (y < 0) {
+ sy += -y;
+ sh -= -y;
+ y = 0;
+ }
+ if (x + sw >= dst->width)
+ sw = dst->width - x;
+ if (y + sh >= dst->height)
+ sh = dst->height - y;
switch (op) {
- case JBIG2_COMPOSE_OR:
- for (j = 0; j < sh; j++) {
- for (i = 0; i < sw; i++) {
- jbig2_image_set_pixel(dst, i+x, j+y,
- jbig2_image_get_pixel(src, i+sx, j+sy) |
- jbig2_image_get_pixel(dst, i+x, j+y));
- }
- }
- break;
- case JBIG2_COMPOSE_AND:
- for (j = 0; j < sh; j++) {
- for (i = 0; i < sw; i++) {
- jbig2_image_set_pixel(dst, i+x, j+y,
- jbig2_image_get_pixel(src, i+sx, j+sy) &
- jbig2_image_get_pixel(dst, i+x, j+y));
- }
- }
- break;
- case JBIG2_COMPOSE_XOR:
- for (j = 0; j < sh; j++) {
- for (i = 0; i < sw; i++) {
- jbig2_image_set_pixel(dst, i+x, j+y,
- jbig2_image_get_pixel(src, i+sx, j+sy) ^
- jbig2_image_get_pixel(dst, i+x, j+y));
- }
- }
- break;
- case JBIG2_COMPOSE_XNOR:
- for (j = 0; j < sh; j++) {
- for (i = 0; i < sw; i++) {
- jbig2_image_set_pixel(dst, i+x, j+y,
- (jbig2_image_get_pixel(src, i+sx, j+sy) ==
- jbig2_image_get_pixel(dst, i+x, j+y)));
- }
- }
- break;
- case JBIG2_COMPOSE_REPLACE:
- for (j = 0; j < sh; j++) {
- for (i = 0; i < sw; i++) {
- jbig2_image_set_pixel(dst, i+x, j+y,
- jbig2_image_get_pixel(src, i+sx, j+sy));
- }
- }
- break;
+ case JBIG2_COMPOSE_OR:
+ for (j = 0; j < sh; j++) {
+ for (i = 0; i < sw; i++) {
+ jbig2_image_set_pixel(dst, i + x, j + y, jbig2_image_get_pixel(src, i + sx, j + sy) | jbig2_image_get_pixel(dst, i + x, j + y));
+ }
+ }
+ break;
+ case JBIG2_COMPOSE_AND:
+ for (j = 0; j < sh; j++) {
+ for (i = 0; i < sw; i++) {
+ jbig2_image_set_pixel(dst, i + x, j + y, jbig2_image_get_pixel(src, i + sx, j + sy) & jbig2_image_get_pixel(dst, i + x, j + y));
+ }
+ }
+ break;
+ case JBIG2_COMPOSE_XOR:
+ for (j = 0; j < sh; j++) {
+ for (i = 0; i < sw; i++) {
+ jbig2_image_set_pixel(dst, i + x, j + y, jbig2_image_get_pixel(src, i + sx, j + sy) ^ jbig2_image_get_pixel(dst, i + x, j + y));
+ }
+ }
+ break;
+ case JBIG2_COMPOSE_XNOR:
+ for (j = 0; j < sh; j++) {
+ for (i = 0; i < sw; i++) {
+ jbig2_image_set_pixel(dst, i + x, j + y, (jbig2_image_get_pixel(src, i + sx, j + sy) == jbig2_image_get_pixel(dst, i + x, j + y)));
+ }
+ }
+ break;
+ case JBIG2_COMPOSE_REPLACE:
+ for (j = 0; j < sh; j++) {
+ for (i = 0; i < sw; i++) {
+ jbig2_image_set_pixel(dst, i + x, j + y, jbig2_image_get_pixel(src, i + sx, j + sy));
+ }
+ }
+ break;
}
return 0;
@@ -206,8 +197,8 @@
}
/* composite one jbig2_image onto another */
-int jbig2_image_compose(Jbig2Ctx *ctx, Jbig2Image *dst, Jbig2Image *src,
- int x, int y, Jbig2ComposeOp op)
+int
+jbig2_image_compose(Jbig2Ctx *ctx, Jbig2Image *dst, Jbig2Image *src, int x, int y, Jbig2ComposeOp op)
{
int i, j;
int w, h;
@@ -218,8 +209,8 @@
uint8_t mask, rightmask;
if (op != JBIG2_COMPOSE_OR) {
- /* hand off the the general routine */
- return jbig2_image_compose_unopt(ctx, dst, src, x, y, op);
+ /* hand off the the general routine */
+ return jbig2_image_compose_unopt(ctx, dst, src, x, y, op);
}
/* clip */
@@ -227,31 +218,33 @@
h = src->height;
ss = src->data;
- if (x < 0) { w += x; x = 0; }
- if (y < 0) { h += y; y = 0; }
+ if (x < 0) {
+ w += x;
+ x = 0;
+ }
+ if (y < 0) {
+ h += y;
+ y = 0;
+ }
w = (x + w < dst->width) ? w : dst->width - x;
h = (y + h < dst->height) ? h : dst->height - y;
#ifdef JBIG2_DEBUG
- jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, -1,
- "compositing %dx%d at (%d, %d) after clipping\n",
- w, h, x, y);
+ jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, -1, "compositing %dx%d at (%d, %d) after clipping\n", w, h, x, y);
#endif
/* check for zero clipping region */
- if ((w <= 0) || (h <= 0))
- {
+ if ((w <= 0) || (h <= 0)) {
#ifdef JBIG2_DEBUG
jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, -1, "zero clipping region");
#endif
return 0;
}
-
#if 0
/* special case complete/strip replacement */
/* disabled because it's only safe to do when the destination
buffer is all-blank. */
if ((x == 0) && (w == src->width)) {
- memcpy(dst->data + y*dst->stride, src->data, h*src->stride);
+ memcpy(dst->data + y * dst->stride, src->data, h * src->stride);
return 0;
}
#endif
@@ -262,14 +255,12 @@
/* general OR case */
s = ss;
- d = dd = dst->data + y*dst->stride + leftbyte;
- if (d < dst->data || leftbyte > dst->stride || h * dst->stride < 0 ||
- d - leftbyte + h * dst->stride > dst->data + dst->height * dst->stride) {
- return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1,
- "preventing heap overflow in jbig2_image_compose");
+ d = dd = dst->data + y * dst->stride + leftbyte;
+ if (d < dst->data || leftbyte > dst->stride || h * dst->stride < 0 || d - leftbyte + h * dst->stride > dst->data + dst->height * dst->stride) {
+ return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "preventing heap overflow in jbig2_image_compose");
}
if (leftbyte == rightbyte) {
- mask = 0x100 - (0x100 >> w);
+ mask = 0x100 - (0x100 >> w);
for (j = 0; j < h; j++) {
*d |= (*s & mask) >> shift;
d += dst->stride;
@@ -276,47 +267,47 @@
s += src->stride;
}
} else if (shift == 0) {
- rightmask = (w & 7) ? 0x100 - (1 << (8 - (w & 7))) : 0xFF;
+ rightmask = (w & 7) ? 0x100 - (1 << (8 - (w & 7))) : 0xFF;
for (j = 0; j < h; j++) {
- for (i = leftbyte; i < rightbyte; i++)
- *d++ |= *s++;
- *d |= *s & rightmask;
+ for (i = leftbyte; i < rightbyte; i++)
+ *d++ |= *s++;
+ *d |= *s & rightmask;
d = (dd += dst->stride);
s = (ss += src->stride);
- }
+ }
} else {
- bool overlap = (((w + 7) >> 3) < ((x + w + 7) >> 3) - (x >> 3));
- mask = 0x100 - (1 << shift);
- if (overlap)
- rightmask = (0x100 - (0x100 >> ((x + w) & 7))) >> (8 - shift);
- else
- rightmask = 0x100 - (0x100 >> (w & 7));
+ bool overlap = (((w + 7) >> 3) < ((x + w + 7) >> 3) - (x >> 3));
+
+ mask = 0x100 - (1 << shift);
+ if (overlap)
+ rightmask = (0x100 - (0x100 >> ((x + w) & 7))) >> (8 - shift);
+ else
+ rightmask = 0x100 - (0x100 >> (w & 7));
for (j = 0; j < h; j++) {
- *d++ |= (*s & mask) >> shift;
- for(i = leftbyte; i < rightbyte - 1; i++) {
- *d |= ((*s++ & ~mask) << (8 - shift));
- *d++ |= ((*s & mask) >> shift);
- }
- if (overlap)
- *d |= (*s & rightmask) << (8 - shift);
- else
- *d |= ((s[0] & ~mask) << (8 - shift)) |
- ((s[1] & rightmask) >> shift);
- d = (dd += dst->stride);
- s = (ss += src->stride);
- }
+ *d++ |= (*s & mask) >> shift;
+ for (i = leftbyte; i < rightbyte - 1; i++) {
+ *d |= ((*s++ & ~mask) << (8 - shift));
+ *d++ |= ((*s & mask) >> shift);
+ }
+ if (overlap)
+ *d |= (*s & rightmask) << (8 - shift);
+ else
+ *d |= ((s[0] & ~mask) << (8 - shift)) | ((s[1] & rightmask) >> shift);
+ d = (dd += dst->stride);
+ s = (ss += src->stride);
+ }
}
return 0;
}
-
/* initialize an image bitmap to a constant value */
-void jbig2_image_clear(Jbig2Ctx *ctx, Jbig2Image *image, int value)
+void
+jbig2_image_clear(Jbig2Ctx *ctx, Jbig2Image *image, int value)
{
const uint8_t fill = value ? 0xFF : 0x00;
- memset(image->data, fill, image->stride*image->height);
+ memset(image->data, fill, image->stride * image->height);
}
/* look up a pixel value in an image.
@@ -323,36 +314,42 @@
returns 0 outside the image frame for the convenience of
the template code
*/
-int jbig2_image_get_pixel(Jbig2Image *image, int x, int y)
+int
+jbig2_image_get_pixel(Jbig2Image *image, int x, int y)
{
- const int w = image->width;
- const int h = image->height;
- const int byte = (x >> 3) + y*image->stride;
- const int bit = 7 - (x & 7);
+ const int w = image->width;
+ const int h = image->height;
+ const int byte = (x >> 3) + y * image->stride;
+ const int bit = 7 - (x & 7);
- if ((x < 0) || (x >= w)) return 0;
- if ((y < 0) || (y >= h)) return 0;
+ if ((x < 0) || (x >= w))
+ return 0;
+ if ((y < 0) || (y >= h))
+ return 0;
- return ((image->data[byte]>>bit) & 1);
+ return ((image->data[byte] >> bit) & 1);
}
/* set an individual pixel value in an image */
-int jbig2_image_set_pixel(Jbig2Image *image, int x, int y, bool value)
+int
+jbig2_image_set_pixel(Jbig2Image *image, int x, int y, bool value)
{
- const int w = image->width;
- const int h = image->height;
- int scratch, mask;
- int bit, byte;
+ const int w = image->width;
+ const int h = image->height;
+ int scratch, mask;
+ int bit, byte;
- if ((x < 0) || (x >= w)) return 0;
- if ((y < 0) || (y >= h)) return 0;
+ if ((x < 0) || (x >= w))
+ return 0;
+ if ((y < 0) || (y >= h))
+ return 0;
- byte = (x >> 3) + y*image->stride;
- bit = 7 - (x & 7);
- mask = (1 << bit) ^ 0xff;
+ byte = (x >> 3) + y * image->stride;
+ bit = 7 - (x & 7);
+ mask = (1 << bit) ^ 0xff;
- scratch = image->data[byte] & mask;
- image->data[byte] = scratch | (value << bit);
+ scratch = image->data[byte] & mask;
+ image->data[byte] = scratch | (value << bit);
- return 1;
+ return 1;
}
--- a/jbig2_image.h
+++ b/jbig2_image.h
@@ -17,8 +17,6 @@
jbig2dec
*/
-
-
#ifndef _JBIG2_IMAGE_H
#define _JBIG2_IMAGE_H
--- a/jbig2_image_pbm.c
+++ b/jbig2_image_pbm.c
@@ -17,7 +17,6 @@
jbig2dec
*/
-
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
@@ -32,10 +31,11 @@
/* take an image structure and write it to a file in pbm format */
-int jbig2_image_write_pbm_file(Jbig2Image *image, char *filename)
+int
+jbig2_image_write_pbm_file(Jbig2Image *image, char *filename)
{
FILE *out;
- int error;
+ int error;
if ((out = fopen(filename, "wb")) == NULL) {
fprintf(stderr, "unable to open '%s' for writing", filename);
@@ -50,29 +50,31 @@
/* write out an image struct as a pbm stream to an open file pointer */
-int jbig2_image_write_pbm(Jbig2Image *image, FILE *out)
+int
+jbig2_image_write_pbm(Jbig2Image *image, FILE *out)
{
- /* pbm header */
- fprintf(out, "P4\n%d %d\n", image->width, image->height);
+ /* pbm header */
+ fprintf(out, "P4\n%d %d\n", image->width, image->height);
- /* pbm format pads to a byte boundary, so we can
- just write out the whole data buffer
- NB: this assumes minimal stride for the width */
- fwrite(image->data, 1, image->height*image->stride, out);
+ /* pbm format pads to a byte boundary, so we can
+ just write out the whole data buffer
+ NB: this assumes minimal stride for the width */
+ fwrite(image->data, 1, image->height * image->stride, out);
- /* success */
- return 0;
+ /* success */
+ return 0;
}
/* take an image from a file in pbm format */
-Jbig2Image *jbig2_image_read_pbm_file(Jbig2Ctx *ctx, char *filename)
+Jbig2Image *
+jbig2_image_read_pbm_file(Jbig2Ctx *ctx, char *filename)
{
FILE *in;
Jbig2Image *image;
if ((in = fopen(filename, "rb")) == NULL) {
- fprintf(stderr, "unable to open '%s' for reading\n", filename);
- return NULL;
+ fprintf(stderr, "unable to open '%s' for reading\n", filename);
+ return NULL;
}
image = jbig2_image_read_pbm(ctx, in);
@@ -83,7 +85,8 @@
}
/* FIXME: should handle multi-image files */
-Jbig2Image *jbig2_image_read_pbm(Jbig2Ctx *ctx, FILE *in)
+Jbig2Image *
+jbig2_image_read_pbm(Jbig2Ctx *ctx, FILE *in)
{
int i, dim[2];
int done;
@@ -93,7 +96,8 @@
/* look for 'P4' magic */
while ((c = fgetc(in)) != 'P') {
- if (feof(in)) return NULL;
+ if (feof(in))
+ return NULL;
}
if ((c = fgetc(in)) != '4') {
fprintf(stderr, "not a binary pbm file.\n");
@@ -108,7 +112,8 @@
while (done < 2) {
c = fgetc(in);
/* skip whitespace */
- if (c == ' ' || c == '\t' || c == '\r' || c == '\n') continue;
+ if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
+ continue;
/* skip comments */
if (c == '#') {
while ((c = fgetc(in)) != '\n');
@@ -116,8 +121,8 @@
}
/* report unexpected eof */
if (c == EOF) {
- fprintf(stderr, "end-of-file parsing pbm header\n");
- return NULL;
+ fprintf(stderr, "end-of-file parsing pbm header\n");
+ return NULL;
}
if (isdigit(c)) {
buf[i++] = c;
@@ -145,7 +150,7 @@
}
/* the pbm data is byte-aligned, so we can
do a simple block read */
- (void)fread(image->data, 1, image->height*image->stride, in);
+ (void)fread(image->data, 1, image->height * image->stride, in);
if (feof(in)) {
fprintf(stderr, "unexpected end of pbm file.\n");
jbig2_image_release(ctx, image);
--- a/jbig2_image_png.c
+++ b/jbig2_image_png.c
@@ -17,7 +17,6 @@
jbig2dec
*/
-
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
@@ -51,15 +50,16 @@
jbig2_png_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
{
png_size_t check;
+
#if OLD_LIB_PNG
- png_FILE_p f = (png_FILE_p)png_ptr->io_ptr;
+ png_FILE_p f = (png_FILE_p) png_ptr->io_ptr;
#else
- png_FILE_p f = (png_FILE_p)png_get_io_ptr(png_ptr);
+ png_FILE_p f = (png_FILE_p) png_get_io_ptr(png_ptr);
#endif
check = fwrite(data, 1, length, f);
if (check != length) {
- png_error(png_ptr, "Write Error");
+ png_error(png_ptr, "Write Error");
}
}
@@ -67,9 +67,9 @@
jbig2_png_flush(png_structp png_ptr)
{
#if OLD_LIB_PNG
- png_FILE_p f = (png_FILE_p)png_ptr->io_ptr;
+ png_FILE_p f = (png_FILE_p) png_ptr->io_ptr;
#else
- png_FILE_p f = (png_FILE_p)png_get_io_ptr(png_ptr);
+ png_FILE_p f = (png_FILE_p) png_get_io_ptr(png_ptr);
#endif
if (f != NULL)
@@ -76,14 +76,15 @@
fflush(f);
}
-int jbig2_image_write_png_file(Jbig2Image *image, char *filename)
+int
+jbig2_image_write_png_file(Jbig2Image *image, char *filename)
{
FILE *out;
- int error;
+ int error;
if ((out = fopen(filename, "wb")) == NULL) {
- fprintf(stderr, "unable to open '%s' for writing\n", filename);
- return 1;
+ fprintf(stderr, "unable to open '%s' for writing\n", filename);
+ return 1;
}
error = jbig2_image_write_png(image, out);
@@ -94,61 +95,58 @@
/* write out an image struct in png format to an open file pointer */
-int jbig2_image_write_png(Jbig2Image *image, FILE *out)
+int
+jbig2_image_write_png(Jbig2Image *image, FILE *out)
{
- int i;
- png_structp png;
- png_infop info;
- png_bytep rowpointer;
+ int i;
+ png_structp png;
+ png_infop info;
+ png_bytep rowpointer;
- png = png_create_write_struct(PNG_LIBPNG_VER_STRING,
- NULL, NULL, NULL);
- if (png == NULL) {
- fprintf(stderr, "unable to create png structure\n");
- return 2;
- }
+ png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
+ if (png == NULL) {
+ fprintf(stderr, "unable to create png structure\n");
+ return 2;
+ }
- info = png_create_info_struct(png);
- if (info == NULL) {
- fprintf(stderr, "unable to create png info structure\n");
- png_destroy_write_struct(&png, (png_infopp)NULL);
- return 3;
- }
+ info = png_create_info_struct(png);
+ if (info == NULL) {
+ fprintf(stderr, "unable to create png info structure\n");
+ png_destroy_write_struct(&png, (png_infopp) NULL);
+ return 3;
+ }
- /* set/check error handling */
- if (setjmp(png_jmpbuf(png))) {
- /* we've returned here after an internal error */
- fprintf(stderr, "internal error in libpng saving file\n");
- png_destroy_write_struct(&png, &info);
- return 4;
- }
+ /* set/check error handling */
+ if (setjmp(png_jmpbuf(png))) {
+ /* we've returned here after an internal error */
+ fprintf(stderr, "internal error in libpng saving file\n");
+ png_destroy_write_struct(&png, &info);
+ return 4;
+ }
- /* png_init_io() doesn't work linking dynamically to libpng on win32
- one has to either link statically or use callbacks because of runtime
- variations */
- /* png_init_io(png, out); */
- png_set_write_fn(png, (png_voidp)out, jbig2_png_write_data,
- jbig2_png_flush);
+ /* png_init_io() doesn't work linking dynamically to libpng on win32
+ one has to either link statically or use callbacks because of runtime
+ variations */
+ /* png_init_io(png, out); */
+ png_set_write_fn(png, (png_voidp) out, jbig2_png_write_data, jbig2_png_flush);
- /* now we fill out the info structure with our format data */
- png_set_IHDR(png, info, image->width, image->height,
- 1, PNG_COLOR_TYPE_GRAY, PNG_INTERLACE_NONE,
- PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
- png_write_info(png, info);
+ /* now we fill out the info structure with our format data */
+ png_set_IHDR(png, info, image->width, image->height, 1, PNG_COLOR_TYPE_GRAY, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
+ png_write_info(png, info);
- /* png natively treates 0 as black. This will convert for us */
- png_set_invert_mono(png);
+ /* png natively treates 0 as black. This will convert for us */
+ png_set_invert_mono(png);
- /* write out each row in turn */
- rowpointer = (png_bytep)image->data;
- for(i = 0; i < image->height; i++) {
- png_write_row(png, rowpointer);
- rowpointer += image->stride;
- }
+ /* write out each row in turn */
+ rowpointer = (png_bytep) image->data;
+ for (i = 0; i < image->height; i++) {
+ png_write_row(png, rowpointer);
+ rowpointer += image->stride;
+ }
- /* finish and clean up */
- png_write_end(png, info);
- png_destroy_write_struct(&png, &info);
+ /* finish and clean up */
+ png_write_end(png, info);
+ png_destroy_write_struct(&png, &info);
- return 0;
+ return 0;
}
--- a/jbig2_metadata.c
+++ b/jbig2_metadata.c
@@ -17,7 +17,6 @@
jbig2dec
*/
-
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
@@ -31,7 +30,8 @@
#include "jbig2_metadata.h"
/* metadata key,value list object */
-Jbig2Metadata *jbig2_metadata_new(Jbig2Ctx *ctx, Jbig2Encoding encoding)
+Jbig2Metadata *
+jbig2_metadata_new(Jbig2Ctx *ctx, Jbig2Encoding encoding)
{
Jbig2Metadata *md = jbig2_new(ctx, Jbig2Metadata, 1);
@@ -39,45 +39,46 @@
md->encoding = encoding;
md->entries = 0;
md->max_entries = 4;
- md->keys = jbig2_new(ctx, char*, md->max_entries);
- md->values = jbig2_new(ctx, char*, md->max_entries);
+ md->keys = jbig2_new(ctx, char *, md->max_entries);
+ md->values = jbig2_new(ctx, char *, md->max_entries);
+
if (md->keys == NULL || md->values == NULL) {
- jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1,
- "failed to allocate storage for metadata keys/values");
+ jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "failed to allocate storage for metadata keys/values");
jbig2_metadata_free(ctx, md);
md = NULL;
}
} else {
- jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1,
- "failed to allocate storage for metadata");
+ jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "failed to allocate storage for metadata");
}
return md;
}
-void jbig2_metadata_free(Jbig2Ctx *ctx, Jbig2Metadata *md)
+void
+jbig2_metadata_free(Jbig2Ctx *ctx, Jbig2Metadata *md)
{
int i;
if (md->keys) {
- /* assume we own the pointers */
- for (i = 0; i < md->entries; i++)
- jbig2_free(ctx->allocator, md->keys[i]);
- jbig2_free(ctx->allocator, md->keys);
+ /* assume we own the pointers */
+ for (i = 0; i < md->entries; i++)
+ jbig2_free(ctx->allocator, md->keys[i]);
+ jbig2_free(ctx->allocator, md->keys);
}
if (md->values) {
- for (i = 0; i < md->entries; i++)
- jbig2_free(ctx->allocator, md->values[i]);
- jbig2_free(ctx->allocator, md->values);
+ for (i = 0; i < md->entries; i++)
+ jbig2_free(ctx->allocator, md->values[i]);
+ jbig2_free(ctx->allocator, md->values);
}
jbig2_free(ctx->allocator, md);
}
-static char *jbig2_strndup(Jbig2Ctx *ctx, const char *c, const int len)
+static char *
+jbig2_strndup(Jbig2Ctx *ctx, const char *c, const int len)
{
char *s = jbig2_new(ctx, char, len);
+
if (s == NULL) {
- jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1,
- "unable to duplicate comment string");
+ jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "unable to duplicate comment string");
} else {
memcpy(s, c, len);
}
@@ -84,9 +85,8 @@
return s;
}
-int jbig2_metadata_add(Jbig2Ctx *ctx, Jbig2Metadata *md,
- const char *key, const int key_length,
- const char *value, const int value_length)
+int
+jbig2_metadata_add(Jbig2Ctx *ctx, Jbig2Metadata *md, const char *key, const int key_length, const char *value, const int value_length)
{
char **keys, **values;
@@ -93,11 +93,11 @@
/* grow the array if necessary */
if (md->entries == md->max_entries) {
md->max_entries <<= 1;
- keys = jbig2_renew(ctx, md->keys, char*, md->max_entries);
- values = jbig2_renew(ctx, md->values, char*, md->max_entries);
+ keys = jbig2_renew(ctx, md->keys, char *, md->max_entries);
+ values = jbig2_renew(ctx, md->values, char *, md->max_entries);
+
if (keys == NULL || values == NULL) {
- jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1,
- "unable to resize metadata structure");
+ jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "unable to resize metadata structure");
return -1;
}
md->keys = keys;
@@ -112,10 +112,9 @@
return 0;
}
-
/* decode an ascii comment segment 7.4.15.1 */
-int jbig2_comment_ascii(Jbig2Ctx *ctx, Jbig2Segment *segment,
- const uint8_t *segment_data)
+int
+jbig2_comment_ascii(Jbig2Ctx *ctx, Jbig2Segment *segment, const uint8_t *segment_data)
{
char *s = (char *)(segment_data + 4);
char *end = (char *)(segment_data + segment->data_length);
@@ -122,13 +121,11 @@
Jbig2Metadata *comment;
char *key, *value;
- jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number,
- "ASCII comment data");
+ jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "ASCII comment data");
comment = jbig2_metadata_new(ctx, JBIG2_ENCODING_ASCII);
if (comment == NULL) {
- jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number,
- "unable to allocate comment structure");
+ jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "unable to allocate comment structure");
return -1;
}
/* loop over the segment data pulling out the key,value pairs */
@@ -135,14 +132,15 @@
while (s < end && *s) {
key = s;
value = memchr(key, '\0', end - key);
- if (!value) goto too_short;
+ if (!value)
+ goto too_short;
value++;
s = memchr(value, '\0', end - value);
- if (!s) goto too_short;
+ if (!s)
+ goto too_short;
s++;
jbig2_metadata_add(ctx, comment, key, value - key, value, s - value);
- jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number,
- "'%s'\t'%s'", key, value);
+ jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "'%s'\t'%s'", key, value);
}
/* TODO: associate with ctx, page, or referred-to segment(s) */
@@ -152,14 +150,12 @@
too_short:
jbig2_metadata_free(ctx, comment);
- return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number,
- "unexpected end of comment segment");
+ return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "unexpected end of comment segment");
}
/* decode a UCS-16 comment segement 7.4.15.2 */
-int jbig2_comment_unicode(Jbig2Ctx *ctx, Jbig2Segment *segment,
- const uint8_t *segment_data)
+int
+jbig2_comment_unicode(Jbig2Ctx *ctx, Jbig2Segment *segment, const uint8_t *segment_data)
{
- return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number,
- "unhandled unicode comment segment");
+ return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "unhandled unicode comment segment");
}
--- a/jbig2_metadata.h
+++ b/jbig2_metadata.h
@@ -17,8 +17,6 @@
jbig2dec
*/
-
-
#ifndef _JBIG2_METADATA_H
#define _JBIG2_METADATA_H
@@ -34,20 +32,16 @@
Jbig2Metadata *jbig2_metadata_new(Jbig2Ctx *ctx, Jbig2Encoding encoding);
void jbig2_metadata_free(Jbig2Ctx *ctx, Jbig2Metadata *md);
-int jbig2_metadata_add(Jbig2Ctx *ctx, Jbig2Metadata *md,
- const char *key, const int key_length,
- const char *value, const int value_length);
+int jbig2_metadata_add(Jbig2Ctx *ctx, Jbig2Metadata *md, const char *key, const int key_length, const char *value, const int value_length);
struct _Jbig2Metadata {
Jbig2Encoding encoding;
- char **keys, **values;
+ char **keys, * *values;
int entries, max_entries;
};
/* these bits can go to jbig2_priv.h */
-int jbig2_comment_ascii(Jbig2Ctx *ctx, Jbig2Segment *segment,
- const uint8_t *segment_data);
-int jbig2_comment_unicode(Jbig2Ctx *ctx, Jbig2Segment *segment,
- const uint8_t *segment_data);
+int jbig2_comment_ascii(Jbig2Ctx *ctx, Jbig2Segment *segment, const uint8_t *segment_data);
+int jbig2_comment_unicode(Jbig2Ctx *ctx, Jbig2Segment *segment, const uint8_t *segment_data);
#endif /* _JBIG2_METADATA_H */
--- a/jbig2_mmr.c
+++ b/jbig2_mmr.c
@@ -17,7 +17,6 @@
jbig2dec
*/
-
/* An implementation of MMR decoding. This is based on the
implementation in Fitz, which in turn is based on the one
in Ghostscript.
@@ -39,45 +38,44 @@
#include "jbig2_mmr.h"
typedef struct {
- int width;
- int height;
- const byte *data;
- size_t size;
- int data_index;
- int bit_index;
- uint32_t word;
+ int width;
+ int height;
+ const byte *data;
+ size_t size;
+ int data_index;
+ int bit_index;
+ uint32_t word;
} Jbig2MmrCtx;
-
static void
jbig2_decode_mmr_init(Jbig2MmrCtx *mmr, int width, int height, const byte *data, size_t size)
{
- int i;
- uint32_t word = 0;
+ int i;
+ uint32_t word = 0;
- mmr->width = width;
- mmr->height = height;
- mmr->data = data;
- mmr->size = size;
- mmr->data_index = 0;
- mmr->bit_index = 0;
+ mmr->width = width;
+ mmr->height = height;
+ mmr->data = data;
+ mmr->size = size;
+ mmr->data_index = 0;
+ mmr->bit_index = 0;
- for (i = 0; i < size && i < 4; i++)
- word |= (data[i] << ((3 - i) << 3));
- mmr->word = word;
+ for (i = 0; i < size && i < 4; i++)
+ word |= (data[i] << ((3 - i) << 3));
+ mmr->word = word;
}
static void
jbig2_decode_mmr_consume(Jbig2MmrCtx *mmr, int n_bits)
{
- mmr->word <<= n_bits;
- mmr->bit_index += n_bits;
- while (mmr->bit_index >= 8) {
- mmr->bit_index -= 8;
- if (mmr->data_index + 4 < mmr->size)
- mmr->word |= (mmr->data[mmr->data_index + 4] << mmr->bit_index);
- mmr->data_index++;
- }
+ mmr->word <<= n_bits;
+ mmr->bit_index += n_bits;
+ while (mmr->bit_index >= 8) {
+ mmr->bit_index -= 8;
+ if (mmr->data_index + 4 < mmr->size)
+ mmr->word |= (mmr->data[mmr->data_index + 4] << mmr->bit_index);
+ mmr->data_index++;
+ }
}
/*
@@ -95,640 +93,640 @@
*/
typedef struct {
- short val;
- short n_bits;
+ short val;
+ short n_bits;
} mmr_table_node;
/* white decode table (runlength huffman codes) */
const mmr_table_node jbig2_mmr_white_decode[] = {
-{ 256, 12 },
-{ 272, 12 },
-{ 29, 8 },
-{ 30, 8 },
-{ 45, 8 },
-{ 46, 8 },
-{ 22, 7 },
-{ 22, 7 },
-{ 23, 7 },
-{ 23, 7 },
-{ 47, 8 },
-{ 48, 8 },
-{ 13, 6 },
-{ 13, 6 },
-{ 13, 6 },
-{ 13, 6 },
-{ 20, 7 },
-{ 20, 7 },
-{ 33, 8 },
-{ 34, 8 },
-{ 35, 8 },
-{ 36, 8 },
-{ 37, 8 },
-{ 38, 8 },
-{ 19, 7 },
-{ 19, 7 },
-{ 31, 8 },
-{ 32, 8 },
-{ 1, 6 },
-{ 1, 6 },
-{ 1, 6 },
-{ 1, 6 },
-{ 12, 6 },
-{ 12, 6 },
-{ 12, 6 },
-{ 12, 6 },
-{ 53, 8 },
-{ 54, 8 },
-{ 26, 7 },
-{ 26, 7 },
-{ 39, 8 },
-{ 40, 8 },
-{ 41, 8 },
-{ 42, 8 },
-{ 43, 8 },
-{ 44, 8 },
-{ 21, 7 },
-{ 21, 7 },
-{ 28, 7 },
-{ 28, 7 },
-{ 61, 8 },
-{ 62, 8 },
-{ 63, 8 },
-{ 0, 8 },
-{ 320, 8 },
-{ 384, 8 },
-{ 10, 5 },
-{ 10, 5 },
-{ 10, 5 },
-{ 10, 5 },
-{ 10, 5 },
-{ 10, 5 },
-{ 10, 5 },
-{ 10, 5 },
-{ 11, 5 },
-{ 11, 5 },
-{ 11, 5 },
-{ 11, 5 },
-{ 11, 5 },
-{ 11, 5 },
-{ 11, 5 },
-{ 11, 5 },
-{ 27, 7 },
-{ 27, 7 },
-{ 59, 8 },
-{ 60, 8 },
-{ 288, 9 },
-{ 290, 9 },
-{ 18, 7 },
-{ 18, 7 },
-{ 24, 7 },
-{ 24, 7 },
-{ 49, 8 },
-{ 50, 8 },
-{ 51, 8 },
-{ 52, 8 },
-{ 25, 7 },
-{ 25, 7 },
-{ 55, 8 },
-{ 56, 8 },
-{ 57, 8 },
-{ 58, 8 },
-{ 192, 6 },
-{ 192, 6 },
-{ 192, 6 },
-{ 192, 6 },
-{ 1664, 6 },
-{ 1664, 6 },
-{ 1664, 6 },
-{ 1664, 6 },
-{ 448, 8 },
-{ 512, 8 },
-{ 292, 9 },
-{ 640, 8 },
-{ 576, 8 },
-{ 294, 9 },
-{ 296, 9 },
-{ 298, 9 },
-{ 300, 9 },
-{ 302, 9 },
-{ 256, 7 },
-{ 256, 7 },
-{ 2, 4 },
-{ 2, 4 },
-{ 2, 4 },
-{ 2, 4 },
-{ 2, 4 },
-{ 2, 4 },
-{ 2, 4 },
-{ 2, 4 },
-{ 2, 4 },
-{ 2, 4 },
-{ 2, 4 },
-{ 2, 4 },
-{ 2, 4 },
-{ 2, 4 },
-{ 2, 4 },
-{ 2, 4 },
-{ 3, 4 },
-{ 3, 4 },
-{ 3, 4 },
-{ 3, 4 },
-{ 3, 4 },
-{ 3, 4 },
-{ 3, 4 },
-{ 3, 4 },
-{ 3, 4 },
-{ 3, 4 },
-{ 3, 4 },
-{ 3, 4 },
-{ 3, 4 },
-{ 3, 4 },
-{ 3, 4 },
-{ 3, 4 },
-{ 128, 5 },
-{ 128, 5 },
-{ 128, 5 },
-{ 128, 5 },
-{ 128, 5 },
-{ 128, 5 },
-{ 128, 5 },
-{ 128, 5 },
-{ 8, 5 },
-{ 8, 5 },
-{ 8, 5 },
-{ 8, 5 },
-{ 8, 5 },
-{ 8, 5 },
-{ 8, 5 },
-{ 8, 5 },
-{ 9, 5 },
-{ 9, 5 },
-{ 9, 5 },
-{ 9, 5 },
-{ 9, 5 },
-{ 9, 5 },
-{ 9, 5 },
-{ 9, 5 },
-{ 16, 6 },
-{ 16, 6 },
-{ 16, 6 },
-{ 16, 6 },
-{ 17, 6 },
-{ 17, 6 },
-{ 17, 6 },
-{ 17, 6 },
-{ 4, 4 },
-{ 4, 4 },
-{ 4, 4 },
-{ 4, 4 },
-{ 4, 4 },
-{ 4, 4 },
-{ 4, 4 },
-{ 4, 4 },
-{ 4, 4 },
-{ 4, 4 },
-{ 4, 4 },
-{ 4, 4 },
-{ 4, 4 },
-{ 4, 4 },
-{ 4, 4 },
-{ 4, 4 },
-{ 5, 4 },
-{ 5, 4 },
-{ 5, 4 },
-{ 5, 4 },
-{ 5, 4 },
-{ 5, 4 },
-{ 5, 4 },
-{ 5, 4 },
-{ 5, 4 },
-{ 5, 4 },
-{ 5, 4 },
-{ 5, 4 },
-{ 5, 4 },
-{ 5, 4 },
-{ 5, 4 },
-{ 5, 4 },
-{ 14, 6 },
-{ 14, 6 },
-{ 14, 6 },
-{ 14, 6 },
-{ 15, 6 },
-{ 15, 6 },
-{ 15, 6 },
-{ 15, 6 },
-{ 64, 5 },
-{ 64, 5 },
-{ 64, 5 },
-{ 64, 5 },
-{ 64, 5 },
-{ 64, 5 },
-{ 64, 5 },
-{ 64, 5 },
-{ 6, 4 },
-{ 6, 4 },
-{ 6, 4 },
-{ 6, 4 },
-{ 6, 4 },
-{ 6, 4 },
-{ 6, 4 },
-{ 6, 4 },
-{ 6, 4 },
-{ 6, 4 },
-{ 6, 4 },
-{ 6, 4 },
-{ 6, 4 },
-{ 6, 4 },
-{ 6, 4 },
-{ 6, 4 },
-{ 7, 4 },
-{ 7, 4 },
-{ 7, 4 },
-{ 7, 4 },
-{ 7, 4 },
-{ 7, 4 },
-{ 7, 4 },
-{ 7, 4 },
-{ 7, 4 },
-{ 7, 4 },
-{ 7, 4 },
-{ 7, 4 },
-{ 7, 4 },
-{ 7, 4 },
-{ 7, 4 },
-{ 7, 4 },
-{ -2, 3 },
-{ -2, 3 },
-{ -1, 0 },
-{ -1, 0 },
-{ -1, 0 },
-{ -1, 0 },
-{ -1, 0 },
-{ -1, 0 },
-{ -1, 0 },
-{ -1, 0 },
-{ -1, 0 },
-{ -1, 0 },
-{ -1, 0 },
-{ -1, 0 },
-{ -1, 0 },
-{ -3, 4 },
-{ 1792, 3 },
-{ 1792, 3 },
-{ 1984, 4 },
-{ 2048, 4 },
-{ 2112, 4 },
-{ 2176, 4 },
-{ 2240, 4 },
-{ 2304, 4 },
-{ 1856, 3 },
-{ 1856, 3 },
-{ 1920, 3 },
-{ 1920, 3 },
-{ 2368, 4 },
-{ 2432, 4 },
-{ 2496, 4 },
-{ 2560, 4 },
-{ 1472, 1 },
-{ 1536, 1 },
-{ 1600, 1 },
-{ 1728, 1 },
-{ 704, 1 },
-{ 768, 1 },
-{ 832, 1 },
-{ 896, 1 },
-{ 960, 1 },
-{ 1024, 1 },
-{ 1088, 1 },
-{ 1152, 1 },
-{ 1216, 1 },
-{ 1280, 1 },
-{ 1344, 1 },
-{ 1408, 1 }
+ {256, 12},
+ {272, 12},
+ {29, 8},
+ {30, 8},
+ {45, 8},
+ {46, 8},
+ {22, 7},
+ {22, 7},
+ {23, 7},
+ {23, 7},
+ {47, 8},
+ {48, 8},
+ {13, 6},
+ {13, 6},
+ {13, 6},
+ {13, 6},
+ {20, 7},
+ {20, 7},
+ {33, 8},
+ {34, 8},
+ {35, 8},
+ {36, 8},
+ {37, 8},
+ {38, 8},
+ {19, 7},
+ {19, 7},
+ {31, 8},
+ {32, 8},
+ {1, 6},
+ {1, 6},
+ {1, 6},
+ {1, 6},
+ {12, 6},
+ {12, 6},
+ {12, 6},
+ {12, 6},
+ {53, 8},
+ {54, 8},
+ {26, 7},
+ {26, 7},
+ {39, 8},
+ {40, 8},
+ {41, 8},
+ {42, 8},
+ {43, 8},
+ {44, 8},
+ {21, 7},
+ {21, 7},
+ {28, 7},
+ {28, 7},
+ {61, 8},
+ {62, 8},
+ {63, 8},
+ {0, 8},
+ {320, 8},
+ {384, 8},
+ {10, 5},
+ {10, 5},
+ {10, 5},
+ {10, 5},
+ {10, 5},
+ {10, 5},
+ {10, 5},
+ {10, 5},
+ {11, 5},
+ {11, 5},
+ {11, 5},
+ {11, 5},
+ {11, 5},
+ {11, 5},
+ {11, 5},
+ {11, 5},
+ {27, 7},
+ {27, 7},
+ {59, 8},
+ {60, 8},
+ {288, 9},
+ {290, 9},
+ {18, 7},
+ {18, 7},
+ {24, 7},
+ {24, 7},
+ {49, 8},
+ {50, 8},
+ {51, 8},
+ {52, 8},
+ {25, 7},
+ {25, 7},
+ {55, 8},
+ {56, 8},
+ {57, 8},
+ {58, 8},
+ {192, 6},
+ {192, 6},
+ {192, 6},
+ {192, 6},
+ {1664, 6},
+ {1664, 6},
+ {1664, 6},
+ {1664, 6},
+ {448, 8},
+ {512, 8},
+ {292, 9},
+ {640, 8},
+ {576, 8},
+ {294, 9},
+ {296, 9},
+ {298, 9},
+ {300, 9},
+ {302, 9},
+ {256, 7},
+ {256, 7},
+ {2, 4},
+ {2, 4},
+ {2, 4},
+ {2, 4},
+ {2, 4},
+ {2, 4},
+ {2, 4},
+ {2, 4},
+ {2, 4},
+ {2, 4},
+ {2, 4},
+ {2, 4},
+ {2, 4},
+ {2, 4},
+ {2, 4},
+ {2, 4},
+ {3, 4},
+ {3, 4},
+ {3, 4},
+ {3, 4},
+ {3, 4},
+ {3, 4},
+ {3, 4},
+ {3, 4},
+ {3, 4},
+ {3, 4},
+ {3, 4},
+ {3, 4},
+ {3, 4},
+ {3, 4},
+ {3, 4},
+ {3, 4},
+ {128, 5},
+ {128, 5},
+ {128, 5},
+ {128, 5},
+ {128, 5},
+ {128, 5},
+ {128, 5},
+ {128, 5},
+ {8, 5},
+ {8, 5},
+ {8, 5},
+ {8, 5},
+ {8, 5},
+ {8, 5},
+ {8, 5},
+ {8, 5},
+ {9, 5},
+ {9, 5},
+ {9, 5},
+ {9, 5},
+ {9, 5},
+ {9, 5},
+ {9, 5},
+ {9, 5},
+ {16, 6},
+ {16, 6},
+ {16, 6},
+ {16, 6},
+ {17, 6},
+ {17, 6},
+ {17, 6},
+ {17, 6},
+ {4, 4},
+ {4, 4},
+ {4, 4},
+ {4, 4},
+ {4, 4},
+ {4, 4},
+ {4, 4},
+ {4, 4},
+ {4, 4},
+ {4, 4},
+ {4, 4},
+ {4, 4},
+ {4, 4},
+ {4, 4},
+ {4, 4},
+ {4, 4},
+ {5, 4},
+ {5, 4},
+ {5, 4},
+ {5, 4},
+ {5, 4},
+ {5, 4},
+ {5, 4},
+ {5, 4},
+ {5, 4},
+ {5, 4},
+ {5, 4},
+ {5, 4},
+ {5, 4},
+ {5, 4},
+ {5, 4},
+ {5, 4},
+ {14, 6},
+ {14, 6},
+ {14, 6},
+ {14, 6},
+ {15, 6},
+ {15, 6},
+ {15, 6},
+ {15, 6},
+ {64, 5},
+ {64, 5},
+ {64, 5},
+ {64, 5},
+ {64, 5},
+ {64, 5},
+ {64, 5},
+ {64, 5},
+ {6, 4},
+ {6, 4},
+ {6, 4},
+ {6, 4},
+ {6, 4},
+ {6, 4},
+ {6, 4},
+ {6, 4},
+ {6, 4},
+ {6, 4},
+ {6, 4},
+ {6, 4},
+ {6, 4},
+ {6, 4},
+ {6, 4},
+ {6, 4},
+ {7, 4},
+ {7, 4},
+ {7, 4},
+ {7, 4},
+ {7, 4},
+ {7, 4},
+ {7, 4},
+ {7, 4},
+ {7, 4},
+ {7, 4},
+ {7, 4},
+ {7, 4},
+ {7, 4},
+ {7, 4},
+ {7, 4},
+ {7, 4},
+ {-2, 3},
+ {-2, 3},
+ {-1, 0},
+ {-1, 0},
+ {-1, 0},
+ {-1, 0},
+ {-1, 0},
+ {-1, 0},
+ {-1, 0},
+ {-1, 0},
+ {-1, 0},
+ {-1, 0},
+ {-1, 0},
+ {-1, 0},
+ {-1, 0},
+ {-3, 4},
+ {1792, 3},
+ {1792, 3},
+ {1984, 4},
+ {2048, 4},
+ {2112, 4},
+ {2176, 4},
+ {2240, 4},
+ {2304, 4},
+ {1856, 3},
+ {1856, 3},
+ {1920, 3},
+ {1920, 3},
+ {2368, 4},
+ {2432, 4},
+ {2496, 4},
+ {2560, 4},
+ {1472, 1},
+ {1536, 1},
+ {1600, 1},
+ {1728, 1},
+ {704, 1},
+ {768, 1},
+ {832, 1},
+ {896, 1},
+ {960, 1},
+ {1024, 1},
+ {1088, 1},
+ {1152, 1},
+ {1216, 1},
+ {1280, 1},
+ {1344, 1},
+ {1408, 1}
};
/* black decode table (runlength huffman codes) */
const mmr_table_node jbig2_mmr_black_decode[] = {
-{ 128, 12 },
-{ 160, 13 },
-{ 224, 12 },
-{ 256, 12 },
-{ 10, 7 },
-{ 11, 7 },
-{ 288, 12 },
-{ 12, 7 },
-{ 9, 6 },
-{ 9, 6 },
-{ 8, 6 },
-{ 8, 6 },
-{ 7, 5 },
-{ 7, 5 },
-{ 7, 5 },
-{ 7, 5 },
-{ 6, 4 },
-{ 6, 4 },
-{ 6, 4 },
-{ 6, 4 },
-{ 6, 4 },
-{ 6, 4 },
-{ 6, 4 },
-{ 6, 4 },
-{ 5, 4 },
-{ 5, 4 },
-{ 5, 4 },
-{ 5, 4 },
-{ 5, 4 },
-{ 5, 4 },
-{ 5, 4 },
-{ 5, 4 },
-{ 1, 3 },
-{ 1, 3 },
-{ 1, 3 },
-{ 1, 3 },
-{ 1, 3 },
-{ 1, 3 },
-{ 1, 3 },
-{ 1, 3 },
-{ 1, 3 },
-{ 1, 3 },
-{ 1, 3 },
-{ 1, 3 },
-{ 1, 3 },
-{ 1, 3 },
-{ 1, 3 },
-{ 1, 3 },
-{ 4, 3 },
-{ 4, 3 },
-{ 4, 3 },
-{ 4, 3 },
-{ 4, 3 },
-{ 4, 3 },
-{ 4, 3 },
-{ 4, 3 },
-{ 4, 3 },
-{ 4, 3 },
-{ 4, 3 },
-{ 4, 3 },
-{ 4, 3 },
-{ 4, 3 },
-{ 4, 3 },
-{ 4, 3 },
-{ 3, 2 },
-{ 3, 2 },
-{ 3, 2 },
-{ 3, 2 },
-{ 3, 2 },
-{ 3, 2 },
-{ 3, 2 },
-{ 3, 2 },
-{ 3, 2 },
-{ 3, 2 },
-{ 3, 2 },
-{ 3, 2 },
-{ 3, 2 },
-{ 3, 2 },
-{ 3, 2 },
-{ 3, 2 },
-{ 3, 2 },
-{ 3, 2 },
-{ 3, 2 },
-{ 3, 2 },
-{ 3, 2 },
-{ 3, 2 },
-{ 3, 2 },
-{ 3, 2 },
-{ 3, 2 },
-{ 3, 2 },
-{ 3, 2 },
-{ 3, 2 },
-{ 3, 2 },
-{ 3, 2 },
-{ 3, 2 },
-{ 3, 2 },
-{ 2, 2 },
-{ 2, 2 },
-{ 2, 2 },
-{ 2, 2 },
-{ 2, 2 },
-{ 2, 2 },
-{ 2, 2 },
-{ 2, 2 },
-{ 2, 2 },
-{ 2, 2 },
-{ 2, 2 },
-{ 2, 2 },
-{ 2, 2 },
-{ 2, 2 },
-{ 2, 2 },
-{ 2, 2 },
-{ 2, 2 },
-{ 2, 2 },
-{ 2, 2 },
-{ 2, 2 },
-{ 2, 2 },
-{ 2, 2 },
-{ 2, 2 },
-{ 2, 2 },
-{ 2, 2 },
-{ 2, 2 },
-{ 2, 2 },
-{ 2, 2 },
-{ 2, 2 },
-{ 2, 2 },
-{ 2, 2 },
-{ 2, 2 },
-{ -2, 4 },
-{ -2, 4 },
-{ -1, 0 },
-{ -1, 0 },
-{ -1, 0 },
-{ -1, 0 },
-{ -1, 0 },
-{ -1, 0 },
-{ -1, 0 },
-{ -1, 0 },
-{ -1, 0 },
-{ -1, 0 },
-{ -1, 0 },
-{ -1, 0 },
-{ -1, 0 },
-{ -3, 5 },
-{ 1792, 4 },
-{ 1792, 4 },
-{ 1984, 5 },
-{ 2048, 5 },
-{ 2112, 5 },
-{ 2176, 5 },
-{ 2240, 5 },
-{ 2304, 5 },
-{ 1856, 4 },
-{ 1856, 4 },
-{ 1920, 4 },
-{ 1920, 4 },
-{ 2368, 5 },
-{ 2432, 5 },
-{ 2496, 5 },
-{ 2560, 5 },
-{ 18, 3 },
-{ 18, 3 },
-{ 18, 3 },
-{ 18, 3 },
-{ 18, 3 },
-{ 18, 3 },
-{ 18, 3 },
-{ 18, 3 },
-{ 52, 5 },
-{ 52, 5 },
-{ 640, 6 },
-{ 704, 6 },
-{ 768, 6 },
-{ 832, 6 },
-{ 55, 5 },
-{ 55, 5 },
-{ 56, 5 },
-{ 56, 5 },
-{ 1280, 6 },
-{ 1344, 6 },
-{ 1408, 6 },
-{ 1472, 6 },
-{ 59, 5 },
-{ 59, 5 },
-{ 60, 5 },
-{ 60, 5 },
-{ 1536, 6 },
-{ 1600, 6 },
-{ 24, 4 },
-{ 24, 4 },
-{ 24, 4 },
-{ 24, 4 },
-{ 25, 4 },
-{ 25, 4 },
-{ 25, 4 },
-{ 25, 4 },
-{ 1664, 6 },
-{ 1728, 6 },
-{ 320, 5 },
-{ 320, 5 },
-{ 384, 5 },
-{ 384, 5 },
-{ 448, 5 },
-{ 448, 5 },
-{ 512, 6 },
-{ 576, 6 },
-{ 53, 5 },
-{ 53, 5 },
-{ 54, 5 },
-{ 54, 5 },
-{ 896, 6 },
-{ 960, 6 },
-{ 1024, 6 },
-{ 1088, 6 },
-{ 1152, 6 },
-{ 1216, 6 },
-{ 64, 3 },
-{ 64, 3 },
-{ 64, 3 },
-{ 64, 3 },
-{ 64, 3 },
-{ 64, 3 },
-{ 64, 3 },
-{ 64, 3 },
-{ 13, 1 },
-{ 13, 1 },
-{ 13, 1 },
-{ 13, 1 },
-{ 13, 1 },
-{ 13, 1 },
-{ 13, 1 },
-{ 13, 1 },
-{ 13, 1 },
-{ 13, 1 },
-{ 13, 1 },
-{ 13, 1 },
-{ 13, 1 },
-{ 13, 1 },
-{ 13, 1 },
-{ 13, 1 },
-{ 23, 4 },
-{ 23, 4 },
-{ 50, 5 },
-{ 51, 5 },
-{ 44, 5 },
-{ 45, 5 },
-{ 46, 5 },
-{ 47, 5 },
-{ 57, 5 },
-{ 58, 5 },
-{ 61, 5 },
-{ 256, 5 },
-{ 16, 3 },
-{ 16, 3 },
-{ 16, 3 },
-{ 16, 3 },
-{ 17, 3 },
-{ 17, 3 },
-{ 17, 3 },
-{ 17, 3 },
-{ 48, 5 },
-{ 49, 5 },
-{ 62, 5 },
-{ 63, 5 },
-{ 30, 5 },
-{ 31, 5 },
-{ 32, 5 },
-{ 33, 5 },
-{ 40, 5 },
-{ 41, 5 },
-{ 22, 4 },
-{ 22, 4 },
-{ 14, 1 },
-{ 14, 1 },
-{ 14, 1 },
-{ 14, 1 },
-{ 14, 1 },
-{ 14, 1 },
-{ 14, 1 },
-{ 14, 1 },
-{ 14, 1 },
-{ 14, 1 },
-{ 14, 1 },
-{ 14, 1 },
-{ 14, 1 },
-{ 14, 1 },
-{ 14, 1 },
-{ 14, 1 },
-{ 15, 2 },
-{ 15, 2 },
-{ 15, 2 },
-{ 15, 2 },
-{ 15, 2 },
-{ 15, 2 },
-{ 15, 2 },
-{ 15, 2 },
-{ 128, 5 },
-{ 192, 5 },
-{ 26, 5 },
-{ 27, 5 },
-{ 28, 5 },
-{ 29, 5 },
-{ 19, 4 },
-{ 19, 4 },
-{ 20, 4 },
-{ 20, 4 },
-{ 34, 5 },
-{ 35, 5 },
-{ 36, 5 },
-{ 37, 5 },
-{ 38, 5 },
-{ 39, 5 },
-{ 21, 4 },
-{ 21, 4 },
-{ 42, 5 },
-{ 43, 5 },
-{ 0, 3 },
-{ 0, 3 },
-{ 0, 3 },
-{ 0, 3 }
+ {128, 12},
+ {160, 13},
+ {224, 12},
+ {256, 12},
+ {10, 7},
+ {11, 7},
+ {288, 12},
+ {12, 7},
+ {9, 6},
+ {9, 6},
+ {8, 6},
+ {8, 6},
+ {7, 5},
+ {7, 5},
+ {7, 5},
+ {7, 5},
+ {6, 4},
+ {6, 4},
+ {6, 4},
+ {6, 4},
+ {6, 4},
+ {6, 4},
+ {6, 4},
+ {6, 4},
+ {5, 4},
+ {5, 4},
+ {5, 4},
+ {5, 4},
+ {5, 4},
+ {5, 4},
+ {5, 4},
+ {5, 4},
+ {1, 3},
+ {1, 3},
+ {1, 3},
+ {1, 3},
+ {1, 3},
+ {1, 3},
+ {1, 3},
+ {1, 3},
+ {1, 3},
+ {1, 3},
+ {1, 3},
+ {1, 3},
+ {1, 3},
+ {1, 3},
+ {1, 3},
+ {1, 3},
+ {4, 3},
+ {4, 3},
+ {4, 3},
+ {4, 3},
+ {4, 3},
+ {4, 3},
+ {4, 3},
+ {4, 3},
+ {4, 3},
+ {4, 3},
+ {4, 3},
+ {4, 3},
+ {4, 3},
+ {4, 3},
+ {4, 3},
+ {4, 3},
+ {3, 2},
+ {3, 2},
+ {3, 2},
+ {3, 2},
+ {3, 2},
+ {3, 2},
+ {3, 2},
+ {3, 2},
+ {3, 2},
+ {3, 2},
+ {3, 2},
+ {3, 2},
+ {3, 2},
+ {3, 2},
+ {3, 2},
+ {3, 2},
+ {3, 2},
+ {3, 2},
+ {3, 2},
+ {3, 2},
+ {3, 2},
+ {3, 2},
+ {3, 2},
+ {3, 2},
+ {3, 2},
+ {3, 2},
+ {3, 2},
+ {3, 2},
+ {3, 2},
+ {3, 2},
+ {3, 2},
+ {3, 2},
+ {2, 2},
+ {2, 2},
+ {2, 2},
+ {2, 2},
+ {2, 2},
+ {2, 2},
+ {2, 2},
+ {2, 2},
+ {2, 2},
+ {2, 2},
+ {2, 2},
+ {2, 2},
+ {2, 2},
+ {2, 2},
+ {2, 2},
+ {2, 2},
+ {2, 2},
+ {2, 2},
+ {2, 2},
+ {2, 2},
+ {2, 2},
+ {2, 2},
+ {2, 2},
+ {2, 2},
+ {2, 2},
+ {2, 2},
+ {2, 2},
+ {2, 2},
+ {2, 2},
+ {2, 2},
+ {2, 2},
+ {2, 2},
+ {-2, 4},
+ {-2, 4},
+ {-1, 0},
+ {-1, 0},
+ {-1, 0},
+ {-1, 0},
+ {-1, 0},
+ {-1, 0},
+ {-1, 0},
+ {-1, 0},
+ {-1, 0},
+ {-1, 0},
+ {-1, 0},
+ {-1, 0},
+ {-1, 0},
+ {-3, 5},
+ {1792, 4},
+ {1792, 4},
+ {1984, 5},
+ {2048, 5},
+ {2112, 5},
+ {2176, 5},
+ {2240, 5},
+ {2304, 5},
+ {1856, 4},
+ {1856, 4},
+ {1920, 4},
+ {1920, 4},
+ {2368, 5},
+ {2432, 5},
+ {2496, 5},
+ {2560, 5},
+ {18, 3},
+ {18, 3},
+ {18, 3},
+ {18, 3},
+ {18, 3},
+ {18, 3},
+ {18, 3},
+ {18, 3},
+ {52, 5},
+ {52, 5},
+ {640, 6},
+ {704, 6},
+ {768, 6},
+ {832, 6},
+ {55, 5},
+ {55, 5},
+ {56, 5},
+ {56, 5},
+ {1280, 6},
+ {1344, 6},
+ {1408, 6},
+ {1472, 6},
+ {59, 5},
+ {59, 5},
+ {60, 5},
+ {60, 5},
+ {1536, 6},
+ {1600, 6},
+ {24, 4},
+ {24, 4},
+ {24, 4},
+ {24, 4},
+ {25, 4},
+ {25, 4},
+ {25, 4},
+ {25, 4},
+ {1664, 6},
+ {1728, 6},
+ {320, 5},
+ {320, 5},
+ {384, 5},
+ {384, 5},
+ {448, 5},
+ {448, 5},
+ {512, 6},
+ {576, 6},
+ {53, 5},
+ {53, 5},
+ {54, 5},
+ {54, 5},
+ {896, 6},
+ {960, 6},
+ {1024, 6},
+ {1088, 6},
+ {1152, 6},
+ {1216, 6},
+ {64, 3},
+ {64, 3},
+ {64, 3},
+ {64, 3},
+ {64, 3},
+ {64, 3},
+ {64, 3},
+ {64, 3},
+ {13, 1},
+ {13, 1},
+ {13, 1},
+ {13, 1},
+ {13, 1},
+ {13, 1},
+ {13, 1},
+ {13, 1},
+ {13, 1},
+ {13, 1},
+ {13, 1},
+ {13, 1},
+ {13, 1},
+ {13, 1},
+ {13, 1},
+ {13, 1},
+ {23, 4},
+ {23, 4},
+ {50, 5},
+ {51, 5},
+ {44, 5},
+ {45, 5},
+ {46, 5},
+ {47, 5},
+ {57, 5},
+ {58, 5},
+ {61, 5},
+ {256, 5},
+ {16, 3},
+ {16, 3},
+ {16, 3},
+ {16, 3},
+ {17, 3},
+ {17, 3},
+ {17, 3},
+ {17, 3},
+ {48, 5},
+ {49, 5},
+ {62, 5},
+ {63, 5},
+ {30, 5},
+ {31, 5},
+ {32, 5},
+ {33, 5},
+ {40, 5},
+ {41, 5},
+ {22, 4},
+ {22, 4},
+ {14, 1},
+ {14, 1},
+ {14, 1},
+ {14, 1},
+ {14, 1},
+ {14, 1},
+ {14, 1},
+ {14, 1},
+ {14, 1},
+ {14, 1},
+ {14, 1},
+ {14, 1},
+ {14, 1},
+ {14, 1},
+ {14, 1},
+ {14, 1},
+ {15, 2},
+ {15, 2},
+ {15, 2},
+ {15, 2},
+ {15, 2},
+ {15, 2},
+ {15, 2},
+ {15, 2},
+ {128, 5},
+ {192, 5},
+ {26, 5},
+ {27, 5},
+ {28, 5},
+ {29, 5},
+ {19, 4},
+ {19, 4},
+ {20, 4},
+ {20, 4},
+ {34, 5},
+ {35, 5},
+ {36, 5},
+ {37, 5},
+ {38, 5},
+ {39, 5},
+ {21, 4},
+ {21, 4},
+ {42, 5},
+ {43, 5},
+ {0, 3},
+ {0, 3},
+ {0, 3},
+ {0, 3}
};
#define getbit(buf, x) ( ( buf[x >> 3] >> ( 7 - (x & 7) ) ) & 1 )
@@ -736,39 +734,38 @@
static int
jbig2_find_changing_element(const byte *line, int x, int w)
{
- int a, b;
+ int a, b;
- if (line == 0)
- return w;
+ if (line == 0)
+ return w;
- if (x == -1) {
- a = 0;
- x = 0;
- }
- else {
- a = getbit(line, x);
- x ++;
- }
+ if (x == -1) {
+ a = 0;
+ x = 0;
+ } else {
+ a = getbit(line, x);
+ x++;
+ }
- while (x < w) {
- b = getbit(line, x);
- if (a != b)
- break;
- x++;
- }
+ while (x < w) {
+ b = getbit(line, x);
+ if (a != b)
+ break;
+ x++;
+ }
- return x;
+ return x;
}
static int
jbig2_find_changing_element_of_color(const byte *line, int x, int w, int color)
{
- if (line == 0)
- return w;
- x = jbig2_find_changing_element(line, x, w);
- if (x < w && getbit(line, x) != color)
- x = jbig2_find_changing_element(line, x, w);
- return x;
+ if (line == 0)
+ return w;
+ x = jbig2_find_changing_element(line, x, w);
+ if (x < w && getbit(line, x) != color)
+ x = jbig2_find_changing_element(line, x, w);
+ return x;
}
static const byte lm[8] = { 0xFF, 0x7F, 0x3F, 0x1F, 0x0F, 0x07, 0x03, 0x01 };
@@ -777,307 +774,302 @@
static void
jbig2_set_bits(byte *line, int x0, int x1)
{
- int a0, a1, b0, b1, a;
+ int a0, a1, b0, b1, a;
- a0 = x0 >> 3;
- a1 = x1 >> 3;
+ a0 = x0 >> 3;
+ a1 = x1 >> 3;
- b0 = x0 & 7;
- b1 = x1 & 7;
+ b0 = x0 & 7;
+ b1 = x1 & 7;
- if (a0 == a1) {
- line[a0] |= lm[b0] & rm[b1];
- }
- else {
- line[a0] |= lm[b0];
- for (a = a0 + 1; a < a1; a++)
- line[a] = 0xFF;
- if (b1) line[a1] |= rm[b1];
- }
+ if (a0 == a1) {
+ line[a0] |= lm[b0] & rm[b1];
+ } else {
+ line[a0] |= lm[b0];
+ for (a = a0 + 1; a < a1; a++)
+ line[a] = 0xFF;
+ if (b1)
+ line[a1] |= rm[b1];
+ }
}
-
static int
jbig2_decode_get_code(Jbig2MmrCtx *mmr, const mmr_table_node *table, int initial_bits)
{
- uint32_t word = mmr->word;
- int table_ix = word >> (32 - initial_bits);
- int val = table[table_ix].val;
- int n_bits = table[table_ix].n_bits;
+ uint32_t word = mmr->word;
+ int table_ix = word >> (32 - initial_bits);
+ int val = table[table_ix].val;
+ int n_bits = table[table_ix].n_bits;
- if (n_bits > initial_bits) {
- int mask = (1 << (32 - initial_bits)) - 1;
- table_ix = val + ((word & mask) >> (32 - n_bits));
- val = table[table_ix].val;
- n_bits = initial_bits + table[table_ix].n_bits;
- }
+ if (n_bits > initial_bits) {
+ int mask = (1 << (32 - initial_bits)) - 1;
- jbig2_decode_mmr_consume(mmr, n_bits);
+ table_ix = val + ((word & mask) >> (32 - n_bits));
+ val = table[table_ix].val;
+ n_bits = initial_bits + table[table_ix].n_bits;
+ }
- return val;
+ jbig2_decode_mmr_consume(mmr, n_bits);
+
+ return val;
}
static int
jbig2_decode_get_run(Jbig2MmrCtx *mmr, const mmr_table_node *table, int initial_bits)
{
- int result = 0;
- int val;
+ int result = 0;
+ int val;
- do {
- val = jbig2_decode_get_code(mmr, table, initial_bits);
- result += val;
- } while (val >= 64);
+ do {
+ val = jbig2_decode_get_code(mmr, table, initial_bits);
+ result += val;
+ } while (val >= 64);
- return result;
+ return result;
}
static int
jbig2_decode_mmr_line(Jbig2MmrCtx *mmr, const byte *ref, byte *dst)
{
- int a0 = -1;
- int a1, a2, b1, b2;
- int c = 0; /* 0 is white, black is 1 */
+ int a0 = -1;
+ int a1, a2, b1, b2;
+ int c = 0; /* 0 is white, black is 1 */
- while (1)
- {
- uint32_t word = mmr->word;
- /* printf ("%08x\n", word); */
+ while (1) {
+ uint32_t word = mmr->word;
- if (a0 >= mmr->width)
- break;
+ /* printf ("%08x\n", word); */
- if ((word >> (32 - 3)) == 1)
- {
- int white_run, black_run;
+ if (a0 >= mmr->width)
+ break;
- jbig2_decode_mmr_consume(mmr, 3);
+ if ((word >> (32 - 3)) == 1) {
+ int white_run, black_run;
- if (a0 == -1)
- a0 = 0;
+ jbig2_decode_mmr_consume(mmr, 3);
- if (c == 0) {
- white_run = jbig2_decode_get_run(mmr, jbig2_mmr_white_decode, 8);
- black_run = jbig2_decode_get_run(mmr, jbig2_mmr_black_decode, 7);
- a1 = a0 + white_run;
- a2 = a1 + black_run;
- if (a1 > mmr->width) a1 = mmr->width;
- if (a2 > mmr->width) a2 = mmr->width;
- if (a2 < a1 || a1 < 0) return -1;
- jbig2_set_bits(dst, a1, a2);
- a0 = a2;
- /* printf ("H %d %d\n", white_run, black_run); */
- }
- else
- {
- black_run = jbig2_decode_get_run(mmr, jbig2_mmr_black_decode, 7);
- white_run = jbig2_decode_get_run(mmr, jbig2_mmr_white_decode, 8);
- a1 = a0 + black_run;
- a2 = a1 + white_run;
- if (a1 > mmr->width) a1 = mmr->width;
- if (a2 > mmr->width) a2 = mmr->width;
- if (a1 < a0 || a0 < 0) return -1;
- jbig2_set_bits(dst, a0, a1);
- a0 = a2;
- /* printf ("H %d %d\n", black_run, white_run); */
- }
- }
+ if (a0 == -1)
+ a0 = 0;
- else if ((word >> (32 - 4)) == 1)
- {
- /* printf ("P\n"); */
- jbig2_decode_mmr_consume(mmr, 4);
- b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c);
- b2 = jbig2_find_changing_element(ref, b1, mmr->width);
- if (c)
- {
- if (b2 < a0 || a0 < 0) return -1;
- jbig2_set_bits(dst, a0, b2);
- }
- a0 = b2;
- }
+ if (c == 0) {
+ white_run = jbig2_decode_get_run(mmr, jbig2_mmr_white_decode, 8);
+ black_run = jbig2_decode_get_run(mmr, jbig2_mmr_black_decode, 7);
+ a1 = a0 + white_run;
+ a2 = a1 + black_run;
+ if (a1 > mmr->width)
+ a1 = mmr->width;
+ if (a2 > mmr->width)
+ a2 = mmr->width;
+ if (a2 < a1 || a1 < 0)
+ return -1;
+ jbig2_set_bits(dst, a1, a2);
+ a0 = a2;
+ /* printf ("H %d %d\n", white_run, black_run); */
+ } else {
+ black_run = jbig2_decode_get_run(mmr, jbig2_mmr_black_decode, 7);
+ white_run = jbig2_decode_get_run(mmr, jbig2_mmr_white_decode, 8);
+ a1 = a0 + black_run;
+ a2 = a1 + white_run;
+ if (a1 > mmr->width)
+ a1 = mmr->width;
+ if (a2 > mmr->width)
+ a2 = mmr->width;
+ if (a1 < a0 || a0 < 0)
+ return -1;
+ jbig2_set_bits(dst, a0, a1);
+ a0 = a2;
+ /* printf ("H %d %d\n", black_run, white_run); */
+ }
+ }
- else if ((word >> (32 - 1)) == 1)
- {
- /* printf ("V(0)\n"); */
- jbig2_decode_mmr_consume(mmr, 1);
- b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c);
- if (c)
- {
- if (b1 < a0 || a0 < 0) return -1;
- jbig2_set_bits(dst, a0, b1);
- }
- a0 = b1;
- c = !c;
- }
+ else if ((word >> (32 - 4)) == 1) {
+ /* printf ("P\n"); */
+ jbig2_decode_mmr_consume(mmr, 4);
+ b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c);
+ b2 = jbig2_find_changing_element(ref, b1, mmr->width);
+ if (c) {
+ if (b2 < a0 || a0 < 0)
+ return -1;
+ jbig2_set_bits(dst, a0, b2);
+ }
+ a0 = b2;
+ }
- else if ((word >> (32 - 3)) == 3)
- {
- /* printf ("VR(1)\n"); */
- jbig2_decode_mmr_consume(mmr, 3);
- b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c);
- if (b1 + 1 > mmr->width) break;
- if (c)
- {
- if (b1 + 1 < a0 || a0 < 0) return -1;
- jbig2_set_bits(dst, a0, b1 + 1);
- }
- a0 = b1 + 1;
- c = !c;
- }
+ else if ((word >> (32 - 1)) == 1) {
+ /* printf ("V(0)\n"); */
+ jbig2_decode_mmr_consume(mmr, 1);
+ b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c);
+ if (c) {
+ if (b1 < a0 || a0 < 0)
+ return -1;
+ jbig2_set_bits(dst, a0, b1);
+ }
+ a0 = b1;
+ c = !c;
+ }
- else if ((word >> (32 - 6)) == 3)
- {
- /* printf ("VR(2)\n"); */
- jbig2_decode_mmr_consume(mmr, 6);
- b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c);
- if (b1 + 2 > mmr->width) break;
- if (c)
- {
- if (b1 + 2 < a0 || a0 < 0) return -1;
- jbig2_set_bits(dst, a0, b1 + 2);
- }
- a0 = b1 + 2;
- c = !c;
- }
+ else if ((word >> (32 - 3)) == 3) {
+ /* printf ("VR(1)\n"); */
+ jbig2_decode_mmr_consume(mmr, 3);
+ b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c);
+ if (b1 + 1 > mmr->width)
+ break;
+ if (c) {
+ if (b1 + 1 < a0 || a0 < 0)
+ return -1;
+ jbig2_set_bits(dst, a0, b1 + 1);
+ }
+ a0 = b1 + 1;
+ c = !c;
+ }
- else if ((word >> (32 - 7)) == 3)
- {
- /* printf ("VR(3)\n"); */
- jbig2_decode_mmr_consume(mmr, 7);
- b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c);
- if (b1 + 3 > mmr->width) break;
- if (c)
- {
- if (b1 + 3 < a0 || a0 < 0) return -1;
- jbig2_set_bits(dst, a0, b1 + 3);
- }
- a0 = b1 + 3;
- c = !c;
- }
+ else if ((word >> (32 - 6)) == 3) {
+ /* printf ("VR(2)\n"); */
+ jbig2_decode_mmr_consume(mmr, 6);
+ b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c);
+ if (b1 + 2 > mmr->width)
+ break;
+ if (c) {
+ if (b1 + 2 < a0 || a0 < 0)
+ return -1;
+ jbig2_set_bits(dst, a0, b1 + 2);
+ }
+ a0 = b1 + 2;
+ c = !c;
+ }
- else if ((word >> (32 - 3)) == 2)
- {
- /* printf ("VL(1)\n"); */
- jbig2_decode_mmr_consume(mmr, 3);
- b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c);
- if (b1 - 1 < 0) break;
- if (c)
- {
- if (b1 - 1 < a0 || a0 < 0) return -1;
- jbig2_set_bits(dst, a0, b1 - 1);
- }
- a0 = b1 - 1;
- c = !c;
- }
+ else if ((word >> (32 - 7)) == 3) {
+ /* printf ("VR(3)\n"); */
+ jbig2_decode_mmr_consume(mmr, 7);
+ b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c);
+ if (b1 + 3 > mmr->width)
+ break;
+ if (c) {
+ if (b1 + 3 < a0 || a0 < 0)
+ return -1;
+ jbig2_set_bits(dst, a0, b1 + 3);
+ }
+ a0 = b1 + 3;
+ c = !c;
+ }
- else if ((word >> (32 - 6)) == 2)
- {
- /* printf ("VL(2)\n"); */
- jbig2_decode_mmr_consume(mmr, 6);
- b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c);
- if (b1 - 2 < 0) break;
- if (c)
- {
- if (b1 - 2 < a0 || a0 < 0) return -1;
- jbig2_set_bits(dst, a0, b1 - 2);
- }
- a0 = b1 - 2;
- c = !c;
- }
+ else if ((word >> (32 - 3)) == 2) {
+ /* printf ("VL(1)\n"); */
+ jbig2_decode_mmr_consume(mmr, 3);
+ b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c);
+ if (b1 - 1 < 0)
+ break;
+ if (c) {
+ if (b1 - 1 < a0 || a0 < 0)
+ return -1;
+ jbig2_set_bits(dst, a0, b1 - 1);
+ }
+ a0 = b1 - 1;
+ c = !c;
+ }
- else if ((word >> (32 - 7)) == 2)
- {
- /* printf ("VL(3)\n"); */
- jbig2_decode_mmr_consume(mmr, 7);
- b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c);
- if (b1 - 3 < 0) break;
- if (c)
- {
- if (b1 - 3 < a0 || a0 < 0) return -1;
- jbig2_set_bits(dst, a0, b1 - 3);
- }
- a0 = b1 - 3;
- c = !c;
- }
+ else if ((word >> (32 - 6)) == 2) {
+ /* printf ("VL(2)\n"); */
+ jbig2_decode_mmr_consume(mmr, 6);
+ b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c);
+ if (b1 - 2 < 0)
+ break;
+ if (c) {
+ if (b1 - 2 < a0 || a0 < 0)
+ return -1;
+ jbig2_set_bits(dst, a0, b1 - 2);
+ }
+ a0 = b1 - 2;
+ c = !c;
+ }
- else
- break;
- }
+ else if ((word >> (32 - 7)) == 2) {
+ /* printf ("VL(3)\n"); */
+ jbig2_decode_mmr_consume(mmr, 7);
+ b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c);
+ if (b1 - 3 < 0)
+ break;
+ if (c) {
+ if (b1 - 3 < a0 || a0 < 0)
+ return -1;
+ jbig2_set_bits(dst, a0, b1 - 3);
+ }
+ a0 = b1 - 3;
+ c = !c;
+ }
- return 0;
+ else
+ break;
+ }
+
+ return 0;
}
int
-jbig2_decode_generic_mmr(Jbig2Ctx *ctx,
- Jbig2Segment *segment,
- const Jbig2GenericRegionParams *params,
- const byte *data, size_t size,
- Jbig2Image *image)
+jbig2_decode_generic_mmr(Jbig2Ctx *ctx, Jbig2Segment *segment, const Jbig2GenericRegionParams *params, const byte *data, size_t size, Jbig2Image *image)
{
- Jbig2MmrCtx mmr;
- const int rowstride = image->stride;
- byte *dst = image->data;
- byte *ref = NULL;
- int y;
- int code = 0;
+ Jbig2MmrCtx mmr;
+ const int rowstride = image->stride;
+ byte *dst = image->data;
+ byte *ref = NULL;
+ int y;
+ int code = 0;
- jbig2_decode_mmr_init(&mmr, image->width, image->height, data, size);
+ jbig2_decode_mmr_init(&mmr, image->width, image->height, data, size);
- for (y = 0; y < image->height; y++) {
- memset(dst, 0, rowstride);
- code = jbig2_decode_mmr_line(&mmr, ref, dst);
- if (code < 0) return code;
- ref = dst;
- dst += rowstride;
- }
+ for (y = 0; y < image->height; y++) {
+ memset(dst, 0, rowstride);
+ code = jbig2_decode_mmr_line(&mmr, ref, dst);
+ if (code < 0)
+ return code;
+ ref = dst;
+ dst += rowstride;
+ }
- return code;
+ return code;
}
/**
* jbig2_decode_halftone_mmr: decode mmr region inside of halftones
- *
+ *
* @ctx: jbig2 decoder context
- * @params: parameters for decoding
+ * @params: parameters for decoding
* @data: pointer to text region data to be decoded
* @size: length of text region data
- * @image: return of decoded image
+ * @image: return of decoded image
* @consumed_bytes: return of consumed bytes from @data
*
* MMR decoding that consumes EOFB and returns consumed bytes (@consumed_bytes)
*
- * returns: 0
+ * returns: 0
**/
int
-jbig2_decode_halftone_mmr(Jbig2Ctx *ctx,
- const Jbig2GenericRegionParams *params,
- const byte *data, size_t size,
- Jbig2Image *image, size_t* consumed_bytes)
+jbig2_decode_halftone_mmr(Jbig2Ctx *ctx, const Jbig2GenericRegionParams *params, const byte *data, size_t size, Jbig2Image *image, size_t *consumed_bytes)
{
- Jbig2MmrCtx mmr;
- const int rowstride = image->stride;
- byte *dst = image->data;
- byte *ref = NULL;
- int y;
- int code = 0;
- const uint32_t EOFB = 0x001001;
+ Jbig2MmrCtx mmr;
+ const int rowstride = image->stride;
+ byte *dst = image->data;
+ byte *ref = NULL;
+ int y;
+ int code = 0;
+ const uint32_t EOFB = 0x001001;
- jbig2_decode_mmr_init(&mmr, image->width, image->height, data, size);
+ jbig2_decode_mmr_init(&mmr, image->width, image->height, data, size);
- for (y = 0; y < image->height; y++) {
- memset(dst, 0, rowstride);
- code = jbig2_decode_mmr_line(&mmr, ref, dst);
- if (code < 0) return code;
- ref = dst;
- dst += rowstride;
- }
+ for (y = 0; y < image->height; y++) {
+ memset(dst, 0, rowstride);
+ code = jbig2_decode_mmr_line(&mmr, ref, dst);
+ if (code < 0)
+ return code;
+ ref = dst;
+ dst += rowstride;
+ }
- /* test for EOFB (see section 6.2.6) */
- if (mmr.word >> 8 == EOFB) {
- mmr.data_index += 3;
- }
+ /* test for EOFB (see section 6.2.6) */
+ if (mmr.word >> 8 == EOFB) {
+ mmr.data_index += 3;
+ }
- *consumed_bytes += mmr.data_index + (mmr.bit_index >> 3) +
- (mmr.bit_index > 0 ? 1 : 0);
- return code;
+ *consumed_bytes += mmr.data_index + (mmr.bit_index >> 3) + (mmr.bit_index > 0 ? 1 : 0);
+ return code;
}
--- a/jbig2_mmr.h
+++ b/jbig2_mmr.h
@@ -17,17 +17,8 @@
jbig2dec
*/
-
int
-jbig2_decode_generic_mmr(Jbig2Ctx *ctx,
- Jbig2Segment *segment,
- const Jbig2GenericRegionParams *params,
- const byte *data, size_t size,
- Jbig2Image *image);
+jbig2_decode_generic_mmr(Jbig2Ctx *ctx, Jbig2Segment *segment, const Jbig2GenericRegionParams *params, const byte *data, size_t size, Jbig2Image *image);
int
-jbig2_decode_halftone_mmr(Jbig2Ctx *ctx,
- const Jbig2GenericRegionParams *params,
- const byte *data, size_t size,
- Jbig2Image *image, size_t* consumed_bytes);
-
+jbig2_decode_halftone_mmr(Jbig2Ctx *ctx, const Jbig2GenericRegionParams *params, const byte *data, size_t size, Jbig2Image *image, size_t *consumed_bytes);
--- a/jbig2_page.c
+++ b/jbig2_page.c
@@ -17,7 +17,6 @@
jbig2dec
*/
-
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
@@ -38,23 +37,15 @@
dump_page_info(Jbig2Ctx *ctx, Jbig2Segment *segment, Jbig2Page *page)
{
if (page->x_resolution == 0) {
- jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number,
- "page %d image is %dx%d (unknown res)", page->number,
- page->width, page->height);
+ jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "page %d image is %dx%d (unknown res)", page->number, page->width, page->height);
} else if (page->x_resolution == page->y_resolution) {
- jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number,
- "page %d image is %dx%d (%d ppm)", page->number,
- page->width, page->height,
- page->x_resolution);
+ jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "page %d image is %dx%d (%d ppm)", page->number, page->width, page->height, page->x_resolution);
} else {
jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number,
- "page %d image is %dx%d (%dx%d ppm)", page->number,
- page->width, page->height,
- page->x_resolution, page->y_resolution);
+ "page %d image is %dx%d (%dx%d ppm)", page->number, page->width, page->height, page->x_resolution, page->y_resolution);
}
if (page->striped) {
- jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number,
- "\tmaximum stripe size: %d", page->stripe_size);
+ jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "\tmaximum stripe size: %d", page->stripe_size);
}
}
@@ -66,30 +57,28 @@
* including allocating an image buffer for the page (or the first stripe)
**/
int
-jbig2_page_info (Jbig2Ctx *ctx, Jbig2Segment *segment, const uint8_t *segment_data)
+jbig2_page_info(Jbig2Ctx *ctx, Jbig2Segment *segment, const uint8_t *segment_data)
{
Jbig2Page *page;
/* a new page info segment implies the previous page is finished */
page = &(ctx->pages[ctx->current_page]);
- if ((page->number != 0) &&
- ((page->state == JBIG2_PAGE_NEW) || (page->state == JBIG2_PAGE_FREE))) {
+ if ((page->number != 0) && ((page->state == JBIG2_PAGE_NEW) || (page->state == JBIG2_PAGE_FREE))) {
page->state = JBIG2_PAGE_COMPLETE;
- jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number,
- "unexpected page info segment, marking previous page finished");
+ jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "unexpected page info segment, marking previous page finished");
}
/* find a free page */
{
int index, j;
+
index = ctx->current_page;
while (ctx->pages[index].state != JBIG2_PAGE_FREE) {
index++;
if (index >= ctx->max_page_index) {
/* grow the list */
- ctx->pages = jbig2_renew(ctx, ctx->pages, Jbig2Page,
- (ctx->max_page_index <<= 2));
- for (j=index; j < ctx->max_page_index; j++) {
+ ctx->pages = jbig2_renew(ctx, ctx->pages, Jbig2Page, (ctx->max_page_index <<= 2));
+ for (j = index; j < ctx->max_page_index; j++) {
ctx->pages[j].state = JBIG2_PAGE_FREE;
ctx->pages[j].number = 0;
ctx->pages[j].image = NULL;
@@ -105,8 +94,7 @@
/* FIXME: would be nice if we tried to work around this */
if (segment->data_length < 19) {
- return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "segment too short");
+ return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment too short");
}
/* 7.4.8.x */
@@ -119,25 +107,24 @@
/* 7.4.8.6 */
{
- int16_t striping = jbig2_get_int16(segment_data +17);
+ int16_t striping = jbig2_get_int16(segment_data + 17);
+
if (striping & 0x8000) {
page->striped = TRUE;
page->stripe_size = striping & 0x7FFF;
} else {
page->striped = FALSE;
- page->stripe_size = 0; /* would page->height be better? */
+ page->stripe_size = 0; /* would page->height be better? */
}
}
if (page->height == 0xFFFFFFFF && page->striped == FALSE) {
- jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number,
- "height is unspecified but page is not markes as striped");
+ jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "height is unspecified but page is not markes as striped");
page->striped = TRUE;
}
page->end_row = 0;
if (segment->data_length > 19) {
- jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number,
- "extra data in segment");
+ jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "extra data in segment");
}
dump_page_info(ctx, segment, page);
@@ -150,15 +137,12 @@
page->image = jbig2_image_new(ctx, page->width, page->height);
}
if (page->image == NULL) {
- return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "failed to allocate buffer for page image");
+ return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to allocate buffer for page image");
} else {
- /* 8.2 (3) fill the page with the default pixel value */
- jbig2_image_clear(ctx, page->image, (page->flags & 4));
+ /* 8.2 (3) fill the page with the default pixel value */
+ jbig2_image_clear(ctx, page->image, (page->flags & 4));
jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
- "allocated %dx%d page image (%d bytes)",
- page->image->width, page->image->height,
- page->image->stride*page->image->height);
+ "allocated %dx%d page image (%d bytes)", page->image->width, page->image->height, page->image->stride * page->image->height);
}
return 0;
@@ -175,13 +159,10 @@
end_row = jbig2_get_int32(segment_data);
if (end_row < page.end_row) {
- jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number,
- "end of stripe segment with non-positive end row advance"
- " (new end row %d vs current end row %d)",
- end_row, page.end_row);
+ jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number,
+ "end of stripe segment with non-positive end row advance" " (new end row %d vs current end row %d)", end_row, page.end_row);
} else {
- jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number,
- "end of stripe: advancing end row to %d", end_row);
+ jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "end of stripe: advancing end row to %d", end_row);
}
page.end_row = end_row;
@@ -198,30 +179,28 @@
* segment handlers.
**/
int
-jbig2_complete_page (Jbig2Ctx *ctx)
+jbig2_complete_page(Jbig2Ctx *ctx)
{
int code = 0;
/* check for unfinished segments */
if (ctx->segment_index != ctx->n_segments) {
- Jbig2Segment *segment = ctx->segments[ctx->segment_index];
- /* Some versions of Xerox Workcentre generate PDF files
- with the segment data length field of the last segment
- set to -1. Try to cope with this here. */
- if ((segment->data_length & 0xffffffff) == 0xffffffff) {
- jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number,
- "File has an invalid segment data length!"
- " Trying to decode using the available data.");
- segment->data_length = ctx->buf_wr_ix - ctx->buf_rd_ix;
- code = jbig2_parse_segment(ctx, segment, ctx->buf + ctx->buf_rd_ix);
- ctx->buf_rd_ix += segment->data_length;
- ctx->segment_index++;
- }
+ Jbig2Segment *segment = ctx->segments[ctx->segment_index];
+
+ /* Some versions of Xerox Workcentre generate PDF files
+ with the segment data length field of the last segment
+ set to -1. Try to cope with this here. */
+ if ((segment->data_length & 0xffffffff) == 0xffffffff) {
+ jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "File has an invalid segment data length!" " Trying to decode using the available data.");
+ segment->data_length = ctx->buf_wr_ix - ctx->buf_rd_ix;
+ code = jbig2_parse_segment(ctx, segment, ctx->buf + ctx->buf_rd_ix);
+ ctx->buf_rd_ix += segment->data_length;
+ ctx->segment_index++;
+ }
}
/* ensure image exists before marking page as complete */
- if (ctx->pages[ctx->current_page].image != NULL)
- {
+ if (ctx->pages[ctx->current_page].image != NULL) {
ctx->pages[ctx->current_page].state = JBIG2_PAGE_COMPLETE;
}
@@ -238,12 +217,10 @@
if (segment->page_association != page_number) {
jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number,
- "end of page marker for page %d doesn't match current page number %d",
- segment->page_association, page_number);
+ "end of page marker for page %d doesn't match current page number %d", segment->page_association, page_number);
}
- jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number,
- "end of page %d", page_number);
+ jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "end of page %d", page_number);
jbig2_complete_page(ctx);
@@ -261,31 +238,25 @@
* is an image) to a page image buffer
**/
int
-jbig2_page_add_result(Jbig2Ctx *ctx, Jbig2Page *page, Jbig2Image *image,
- int x, int y, Jbig2ComposeOp op)
+jbig2_page_add_result(Jbig2Ctx *ctx, Jbig2Page *page, Jbig2Image *image, int x, int y, Jbig2ComposeOp op)
{
/* ensure image exists first */
- if (page->image == NULL)
- {
- jbig2_error(ctx, JBIG2_SEVERITY_WARNING, -1,
- "page info possibly missing, no image defined");
+ if (page->image == NULL) {
+ jbig2_error(ctx, JBIG2_SEVERITY_WARNING, -1, "page info possibly missing, no image defined");
return 0;
}
/* grow the page to accomodate a new stripe if necessary */
if (page->striped) {
- int new_height = y + image->height + page->end_row;
- if (page->image->height < new_height) {
- jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, -1,
- "growing page buffer to %d rows "
- "to accomodate new stripe", new_height);
- jbig2_image_resize(ctx, page->image,
- page->image->width, new_height);
- }
+ int new_height = y + image->height + page->end_row;
+
+ if (page->image->height < new_height) {
+ jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, -1, "growing page buffer to %d rows " "to accomodate new stripe", new_height);
+ jbig2_image_resize(ctx, page->image, page->image->width, new_height);
+ }
}
- jbig2_image_compose(ctx, page->image, image,
- x, y + page->end_row, op);
+ jbig2_image_compose(ctx, page->image, image, x, y + page->end_row, op);
return 0;
}
@@ -302,25 +273,24 @@
* return an image structure pointer, even though the function
* name refers to a page; the page structure is private.
**/
-Jbig2Image *jbig2_page_out(Jbig2Ctx *ctx)
+Jbig2Image *
+jbig2_page_out(Jbig2Ctx *ctx)
{
int index;
/* search for a completed page */
- for (index=0; index < ctx->max_page_index; index++) {
+ for (index = 0; index < ctx->max_page_index; index++) {
if (ctx->pages[index].state == JBIG2_PAGE_COMPLETE) {
Jbig2Image *img = ctx->pages[index].image;
uint32_t page_number = ctx->pages[index].number;
+
ctx->pages[index].state = JBIG2_PAGE_RETURNED;
if (img != NULL) {
- jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, -1,
- "page %d returned to the client", page_number);
+ jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, -1, "page %d returned to the client", page_number);
return jbig2_image_clone(ctx, img);
} else {
- jbig2_error(ctx, JBIG2_SEVERITY_WARNING, -1,
- "page %d returned with no associated image",
- page_number);
- ; /* continue */
+ jbig2_error(ctx, JBIG2_SEVERITY_WARNING, -1, "page %d returned with no associated image", page_number);
+ ; /* continue */
}
}
}
@@ -332,7 +302,8 @@
/**
* jbig2_release_page: tell the library a page can be freed
**/
-int jbig2_release_page(Jbig2Ctx *ctx, Jbig2Image *image)
+int
+jbig2_release_page(Jbig2Ctx *ctx, Jbig2Image *image)
{
int index;
@@ -341,14 +312,12 @@
if (ctx->pages[index].image == image) {
jbig2_image_release(ctx, image);
ctx->pages[index].state = JBIG2_PAGE_RELEASED;
- jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, -1,
- "page %d released by the client", ctx->pages[index].number);
+ jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, -1, "page %d released by the client", ctx->pages[index].number);
return 0;
}
}
/* no matching pages */
- jbig2_error(ctx, JBIG2_SEVERITY_WARNING, -1,
- "jbig2_release_page called on unknown page");
+ jbig2_error(ctx, JBIG2_SEVERITY_WARNING, -1, "jbig2_release_page called on unknown page");
return 1;
}
--- a/jbig2_priv.h
+++ b/jbig2_priv.h
@@ -17,7 +17,6 @@
jbig2dec
*/
-
/* To enable Memento, either uncomment the following, or arrange to
* predefine MEMENTO whilst building. */
/* #define MEMENTO */
@@ -55,72 +54,63 @@
#endif
typedef enum {
- JBIG2_FILE_HEADER,
- JBIG2_FILE_SEQUENTIAL_HEADER,
- JBIG2_FILE_SEQUENTIAL_BODY,
- JBIG2_FILE_RANDOM_HEADERS,
- JBIG2_FILE_RANDOM_BODIES,
- JBIG2_FILE_EOF
+ JBIG2_FILE_HEADER,
+ JBIG2_FILE_SEQUENTIAL_HEADER,
+ JBIG2_FILE_SEQUENTIAL_BODY,
+ JBIG2_FILE_RANDOM_HEADERS,
+ JBIG2_FILE_RANDOM_BODIES,
+ JBIG2_FILE_EOF
} Jbig2FileState;
struct _Jbig2Ctx {
- Jbig2Allocator *allocator;
- Jbig2Options options;
- const Jbig2Ctx *global_ctx;
- Jbig2ErrorCallback error_callback;
- void *error_callback_data;
+ Jbig2Allocator *allocator;
+ Jbig2Options options;
+ const Jbig2Ctx *global_ctx;
+ Jbig2ErrorCallback error_callback;
+ void *error_callback_data;
- byte *buf;
- size_t buf_size;
- unsigned int buf_rd_ix;
- unsigned int buf_wr_ix;
+ byte *buf;
+ size_t buf_size;
+ unsigned int buf_rd_ix;
+ unsigned int buf_wr_ix;
- Jbig2FileState state;
+ Jbig2FileState state;
- uint8_t file_header_flags;
- uint32_t n_pages;
+ uint8_t file_header_flags;
+ uint32_t n_pages;
- int n_segments_max;
- Jbig2Segment **segments;
- int n_segments; /* index of last segment header parsed */
- int segment_index; /* index of last segment body parsed */
+ int n_segments_max;
+ Jbig2Segment **segments;
+ int n_segments; /* index of last segment header parsed */
+ int segment_index; /* index of last segment body parsed */
- /* list of decoded pages, including the one in progress,
- currently stored as a contiguous, 0-indexed array. */
- int current_page;
- int max_page_index;
- Jbig2Page *pages;
+ /* list of decoded pages, including the one in progress,
+ currently stored as a contiguous, 0-indexed array. */
+ int current_page;
+ int max_page_index;
+ Jbig2Page *pages;
};
-uint32_t
-jbig2_get_uint32(const byte *bptr);
+uint32_t jbig2_get_uint32(const byte *bptr);
-int32_t
-jbig2_get_int32 (const byte *buf);
+int32_t jbig2_get_int32(const byte *buf);
-uint16_t
-jbig2_get_uint16(const byte *bptr);
+uint16_t jbig2_get_uint16(const byte *bptr);
-int16_t
-jbig2_get_int16 (const byte *buf);
+int16_t jbig2_get_int16(const byte *buf);
/* dynamic memory management */
-void *
-jbig2_alloc (Jbig2Allocator *allocator, size_t size, size_t num);
+void *jbig2_alloc(Jbig2Allocator *allocator, size_t size, size_t num);
-void
-jbig2_free (Jbig2Allocator *allocator, void *p);
+void jbig2_free(Jbig2Allocator *allocator, void *p);
-void *
-jbig2_realloc (Jbig2Allocator *allocator, void *p, size_t size, size_t num);
+void *jbig2_realloc(Jbig2Allocator *allocator, void *p, size_t size, size_t num);
#define jbig2_new(ctx, t, size) ((t *)jbig2_alloc(ctx->allocator, size, sizeof(t)))
#define jbig2_renew(ctx, p, t, size) ((t *)jbig2_realloc(ctx->allocator, (p), size, sizeof(t)))
-int
-jbig2_error (Jbig2Ctx *ctx, Jbig2Severity severity, int32_t seg_idx,
- const char *fmt, ...);
+int jbig2_error(Jbig2Ctx *ctx, Jbig2Severity severity, int32_t seg_idx, const char *fmt, ...);
/* the page structure handles decoded page
results. it's allocated by a 'page info'
@@ -138,9 +128,8 @@
struct _Jbig2Page {
Jbig2PageState state;
uint32_t number;
- uint32_t height, width; /* in pixels */
- uint32_t x_resolution,
- y_resolution; /* in pixels per meter */
+ uint32_t height, width; /* in pixels */
+ uint32_t x_resolution, y_resolution; /* in pixels per meter */
uint16_t stripe_size;
bool striped;
int end_row;
@@ -148,7 +137,7 @@
Jbig2Image *image;
};
-int jbig2_page_info (Jbig2Ctx *ctx, Jbig2Segment *segment, const uint8_t *segment_data);
+int jbig2_page_info(Jbig2Ctx *ctx, Jbig2Segment *segment, const uint8_t *segment_data);
int jbig2_end_of_stripe(Jbig2Ctx *ctx, Jbig2Segment *segment, const uint8_t *segment_data);
int jbig2_end_of_page(Jbig2Ctx *ctx, Jbig2Segment *segment, const uint8_t *segment_data);
int jbig2_extension_segment(Jbig2Ctx *ctx, Jbig2Segment *segment, const uint8_t *segment_data);
@@ -167,12 +156,12 @@
/* region segment info */
typedef struct {
- int32_t width;
- int32_t height;
- int32_t x;
- int32_t y;
- Jbig2ComposeOp op;
- uint8_t flags;
+ int32_t width;
+ int32_t height;
+ int32_t x;
+ int32_t y;
+ Jbig2ComposeOp op;
+ uint8_t flags;
} Jbig2RegionSegmentInfo;
void jbig2_get_region_segment_info(Jbig2RegionSegmentInfo *info, const uint8_t *segment_data);
@@ -179,17 +168,12 @@
int jbig2_text_region(Jbig2Ctx *ctx, Jbig2Segment *segment, const uint8_t *segment_data);
/* 7.4 */
-int jbig2_immediate_generic_region(Jbig2Ctx *ctx, Jbig2Segment *segment,
- const uint8_t *segment_data);
-int jbig2_refinement_region(Jbig2Ctx *ctx, Jbig2Segment *segment,
- const byte *segment_data);
+int jbig2_immediate_generic_region(Jbig2Ctx *ctx, Jbig2Segment *segment, const uint8_t *segment_data);
+int jbig2_refinement_region(Jbig2Ctx *ctx, Jbig2Segment *segment, const byte *segment_data);
-int jbig2_pattern_dictionary(Jbig2Ctx *ctx, Jbig2Segment *segment,
- const byte *segment_data);
-int jbig2_halftone_region(Jbig2Ctx *ctx, Jbig2Segment *segment,
- const byte *segment_data);
+int jbig2_pattern_dictionary(Jbig2Ctx *ctx, Jbig2Segment *segment, const byte *segment_data);
+int jbig2_halftone_region(Jbig2Ctx *ctx, Jbig2Segment *segment, const byte *segment_data);
-
/* The word stream design is a compromise between simplicity and
trying to amortize the number of method calls. Each ::get_next_word
invocation pulls 4 bytes from the stream, packed big-endian into a
@@ -198,11 +182,9 @@
typedef struct _Jbig2WordStream Jbig2WordStream;
struct _Jbig2WordStream {
- int (*get_next_word) (Jbig2WordStream *self, int offset, uint32_t *word);
+ int (*get_next_word)(Jbig2WordStream *self, int offset, uint32_t *word);
};
-Jbig2WordStream *
-jbig2_word_stream_buf_new(Jbig2Ctx *ctx, const byte *data, size_t size);
+Jbig2WordStream *jbig2_word_stream_buf_new(Jbig2Ctx *ctx, const byte *data, size_t size);
-void
-jbig2_word_stream_buf_free(Jbig2Ctx *ctx, Jbig2WordStream *ws);
+void jbig2_word_stream_buf_free(Jbig2Ctx *ctx, Jbig2WordStream *ws);
--- a/jbig2_refinement.c
+++ b/jbig2_refinement.c
@@ -17,7 +17,6 @@
jbig2dec
*/
-
/**
* Generic Refinement region handlers.
**/
@@ -28,7 +27,7 @@
#include "os_types.h"
#include <stddef.h>
-#include <string.h> /* memcpy(), memset() */
+#include <string.h> /* memcpy(), memset() */
#include <stdio.h>
@@ -38,325 +37,293 @@
#include "jbig2_generic.h"
#include "jbig2_image.h"
-#if 0 /* currently not used */
+#if 0 /* currently not used */
static int
jbig2_decode_refinement_template0(Jbig2Ctx *ctx,
- Jbig2Segment *segment,
- const Jbig2RefinementRegionParams *params,
- Jbig2ArithState *as,
- Jbig2Image *image,
- Jbig2ArithCx *GR_stats)
+ Jbig2Segment *segment,
+ const Jbig2RefinementRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GR_stats)
{
- return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "refinement region template 0 NYI");
+ return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "refinement region template 0 NYI");
}
#endif
static int
jbig2_decode_refinement_template0_unopt(Jbig2Ctx *ctx,
- Jbig2Segment *segment,
- const Jbig2RefinementRegionParams *params,
- Jbig2ArithState *as,
- Jbig2Image *image,
- Jbig2ArithCx *GR_stats)
+ Jbig2Segment *segment,
+ const Jbig2RefinementRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GR_stats)
{
- const int GRW = image->width;
- const int GRH = image->height;
- const int dx = params->DX;
- const int dy = params->DY;
- Jbig2Image *ref = params->reference;
- uint32_t CONTEXT;
- int x,y;
- bool bit;
+ const int GRW = image->width;
+ const int GRH = image->height;
+ const int dx = params->DX;
+ const int dy = params->DY;
+ Jbig2Image *ref = params->reference;
+ uint32_t CONTEXT;
+ int x, y;
+ bool bit;
- for (y = 0; y < GRH; y++) {
- for (x = 0; x < GRW; x++) {
- CONTEXT = 0;
- CONTEXT |= jbig2_image_get_pixel(image, x - 1, y + 0) << 0;
- CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 1) << 1;
- CONTEXT |= jbig2_image_get_pixel(image, x + 0, y - 1) << 2;
- CONTEXT |= jbig2_image_get_pixel(image, x + params->grat[0],
- y + params->grat[1]) << 3;
- CONTEXT |= jbig2_image_get_pixel(ref, x-dx+1, y-dy+1) << 4;
- CONTEXT |= jbig2_image_get_pixel(ref, x-dx+0, y-dy+1) << 5;
- CONTEXT |= jbig2_image_get_pixel(ref, x-dx-1, y-dy+1) << 6;
- CONTEXT |= jbig2_image_get_pixel(ref, x-dx+1, y-dy+0) << 7;
- CONTEXT |= jbig2_image_get_pixel(ref, x-dx+0, y-dy+0) << 8;
- CONTEXT |= jbig2_image_get_pixel(ref, x-dx-1, y-dy+0) << 9;
- CONTEXT |= jbig2_image_get_pixel(ref, x-dx+1, y-dy-1) << 10;
- CONTEXT |= jbig2_image_get_pixel(ref, x-dx+0, y-dy-1) << 11;
- CONTEXT |= jbig2_image_get_pixel(ref, x-dx+params->grat[2],
- y-dy+params->grat[3]) << 12;
- bit = jbig2_arith_decode(as, &GR_stats[CONTEXT]);
- if (bit < 0)
- return -1;
- jbig2_image_set_pixel(image, x, y, bit);
+ for (y = 0; y < GRH; y++) {
+ for (x = 0; x < GRW; x++) {
+ CONTEXT = 0;
+ CONTEXT |= jbig2_image_get_pixel(image, x - 1, y + 0) << 0;
+ CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 1) << 1;
+ CONTEXT |= jbig2_image_get_pixel(image, x + 0, y - 1) << 2;
+ CONTEXT |= jbig2_image_get_pixel(image, x + params->grat[0], y + params->grat[1]) << 3;
+ CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 1, y - dy + 1) << 4;
+ CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 0, y - dy + 1) << 5;
+ CONTEXT |= jbig2_image_get_pixel(ref, x - dx - 1, y - dy + 1) << 6;
+ CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 1, y - dy + 0) << 7;
+ CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 0, y - dy + 0) << 8;
+ CONTEXT |= jbig2_image_get_pixel(ref, x - dx - 1, y - dy + 0) << 9;
+ CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 1, y - dy - 1) << 10;
+ CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 0, y - dy - 1) << 11;
+ CONTEXT |= jbig2_image_get_pixel(ref, x - dx + params->grat[2], y - dy + params->grat[3]) << 12;
+ bit = jbig2_arith_decode(as, &GR_stats[CONTEXT]);
+ if (bit < 0)
+ return -1;
+ jbig2_image_set_pixel(image, x, y, bit);
+ }
}
- }
#ifdef JBIG2_DEBUG_DUMP
- {
- static count = 0;
- char name[32];
- snprintf(name, 32, "refin-%d.pbm", count);
- jbig2_image_write_pbm_file(ref, name);
- snprintf(name, 32, "refout-%d.pbm", count);
- jbig2_image_write_pbm_file(image, name);
- count++;
- }
+ {
+ static count = 0;
+ char name[32];
+
+ snprintf(name, 32, "refin-%d.pbm", count);
+ jbig2_image_write_pbm_file(ref, name);
+ snprintf(name, 32, "refout-%d.pbm", count);
+ jbig2_image_write_pbm_file(image, name);
+ count++;
+ }
#endif
- return 0;
+ return 0;
}
-
static int
jbig2_decode_refinement_template1_unopt(Jbig2Ctx *ctx,
- Jbig2Segment *segment,
- const Jbig2RefinementRegionParams *params,
- Jbig2ArithState *as,
- Jbig2Image *image,
- Jbig2ArithCx *GR_stats)
+ Jbig2Segment *segment,
+ const Jbig2RefinementRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GR_stats)
{
- const int GRW = image->width;
- const int GRH = image->height;
- const int dx = params->DX;
- const int dy = params->DY;
- Jbig2Image *ref = params->reference;
- uint32_t CONTEXT;
- int x,y;
- bool bit;
+ const int GRW = image->width;
+ const int GRH = image->height;
+ const int dx = params->DX;
+ const int dy = params->DY;
+ Jbig2Image *ref = params->reference;
+ uint32_t CONTEXT;
+ int x, y;
+ bool bit;
- for (y = 0; y < GRH; y++) {
- for (x = 0; x < GRW; x++) {
- CONTEXT = 0;
- CONTEXT |= jbig2_image_get_pixel(image, x - 1, y + 0) << 0;
- CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 1) << 1;
- CONTEXT |= jbig2_image_get_pixel(image, x + 0, y - 1) << 2;
- CONTEXT |= jbig2_image_get_pixel(image, x - 1, y - 1) << 3;
- CONTEXT |= jbig2_image_get_pixel(ref, x-dx+1, y-dy+1) << 4;
- CONTEXT |= jbig2_image_get_pixel(ref, x-dx+0, y-dy+1) << 5;
- CONTEXT |= jbig2_image_get_pixel(ref, x-dx+1, y-dy+0) << 6;
- CONTEXT |= jbig2_image_get_pixel(ref, x-dx+0, y-dy+0) << 7;
- CONTEXT |= jbig2_image_get_pixel(ref, x-dx-1, y-dy+0) << 8;
- CONTEXT |= jbig2_image_get_pixel(ref, x-dx+0, y-dy-1) << 9;
- bit = jbig2_arith_decode(as, &GR_stats[CONTEXT]);
- if (bit < 0)
- return -1;
- jbig2_image_set_pixel(image, x, y, bit);
+ for (y = 0; y < GRH; y++) {
+ for (x = 0; x < GRW; x++) {
+ CONTEXT = 0;
+ CONTEXT |= jbig2_image_get_pixel(image, x - 1, y + 0) << 0;
+ CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 1) << 1;
+ CONTEXT |= jbig2_image_get_pixel(image, x + 0, y - 1) << 2;
+ CONTEXT |= jbig2_image_get_pixel(image, x - 1, y - 1) << 3;
+ CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 1, y - dy + 1) << 4;
+ CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 0, y - dy + 1) << 5;
+ CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 1, y - dy + 0) << 6;
+ CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 0, y - dy + 0) << 7;
+ CONTEXT |= jbig2_image_get_pixel(ref, x - dx - 1, y - dy + 0) << 8;
+ CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 0, y - dy - 1) << 9;
+ bit = jbig2_arith_decode(as, &GR_stats[CONTEXT]);
+ if (bit < 0)
+ return -1;
+ jbig2_image_set_pixel(image, x, y, bit);
+ }
}
- }
#ifdef JBIG2_DEBUG_DUMP
- {
- static count = 0;
- char name[32];
- snprintf(name, 32, "refin-%d.pbm", count);
- jbig2_image_write_pbm_file(ref, name);
- snprintf(name, 32, "refout-%d.pbm", count);
- jbig2_image_write_pbm_file(image, name);
- count++;
- }
+ {
+ static count = 0;
+ char name[32];
+
+ snprintf(name, 32, "refin-%d.pbm", count);
+ jbig2_image_write_pbm_file(ref, name);
+ snprintf(name, 32, "refout-%d.pbm", count);
+ jbig2_image_write_pbm_file(image, name);
+ count++;
+ }
#endif
- return 0;
+ return 0;
}
-#if 0 /* currently not used */
+#if 0 /* currently not used */
static int
jbig2_decode_refinement_template1(Jbig2Ctx *ctx,
- Jbig2Segment *segment,
- const Jbig2RefinementRegionParams *params,
- Jbig2ArithState *as,
- Jbig2Image *image,
- Jbig2ArithCx *GR_stats)
+ Jbig2Segment *segment,
+ const Jbig2RefinementRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GR_stats)
{
- const int GRW = image->width;
- const int GRH = image->height;
- const int stride = image->stride;
- const int refstride = params->reference->stride;
- const int dy = params->DY;
- byte *grreg_line = (byte *)image->data;
- byte *grref_line = (byte *)params->reference->data;
- int x,y;
+ const int GRW = image->width;
+ const int GRH = image->height;
+ const int stride = image->stride;
+ const int refstride = params->reference->stride;
+ const int dy = params->DY;
+ byte *grreg_line = (byte *) image->data;
+ byte *grref_line = (byte *) params->reference->data;
+ int x, y;
- for (y = 0; y < GRH; y++) {
- const int padded_width = (GRW + 7) & -8;
- uint32_t CONTEXT;
- uint32_t refline_m1; /* previous line of the reference bitmap */
- uint32_t refline_0; /* current line of the reference bitmap */
- uint32_t refline_1; /* next line of the reference bitmap */
- uint32_t line_m1; /* previous line of the decoded bitmap */
+ for (y = 0; y < GRH; y++) {
+ const int padded_width = (GRW + 7) & -8;
+ uint32_t CONTEXT;
+ uint32_t refline_m1; /* previous line of the reference bitmap */
+ uint32_t refline_0; /* current line of the reference bitmap */
+ uint32_t refline_1; /* next line of the reference bitmap */
+ uint32_t line_m1; /* previous line of the decoded bitmap */
- line_m1 = (y >= 1) ? grreg_line[-stride] : 0;
- refline_m1 = ((y-dy) >= 1) ? grref_line[(-1-dy)*stride] << 2: 0;
- refline_0 = (((y-dy) > 0) && ((y-dy) < GRH)) ? grref_line[(0-dy)*stride] << 4 : 0;
- refline_1 = (y < GRH - 1) ? grref_line[(+1-dy)*stride] << 7 : 0;
- CONTEXT = ((line_m1 >> 5) & 0x00e) |
- ((refline_1 >> 5) & 0x030) |
- ((refline_0 >> 5) & 0x1c0) |
- ((refline_m1 >> 5) & 0x200);
+ line_m1 = (y >= 1) ? grreg_line[-stride] : 0;
+ refline_m1 = ((y - dy) >= 1) ? grref_line[(-1 - dy) * stride] << 2 : 0;
+ refline_0 = (((y - dy) > 0) && ((y - dy) < GRH)) ? grref_line[(0 - dy) * stride] << 4 : 0;
+ refline_1 = (y < GRH - 1) ? grref_line[(+1 - dy) * stride] << 7 : 0;
+ CONTEXT = ((line_m1 >> 5) & 0x00e) | ((refline_1 >> 5) & 0x030) | ((refline_0 >> 5) & 0x1c0) | ((refline_m1 >> 5) & 0x200);
- for (x = 0; x < padded_width; x += 8) {
- byte result = 0;
- int x_minor;
- const int minor_width = GRW - x > 8 ? 8 : GRW - x;
+ for (x = 0; x < padded_width; x += 8) {
+ byte result = 0;
+ int x_minor;
+ const int minor_width = GRW - x > 8 ? 8 : GRW - x;
- if (y >= 1) {
- line_m1 = (line_m1 << 8) |
- (x + 8 < GRW ? grreg_line[-stride + (x >> 3) + 1] : 0);
- refline_m1 = (refline_m1 << 8) |
- (x + 8 < GRW ? grref_line[-refstride + (x >> 3) + 1] << 2 : 0);
- }
+ if (y >= 1) {
+ line_m1 = (line_m1 << 8) | (x + 8 < GRW ? grreg_line[-stride + (x >> 3) + 1] : 0);
+ refline_m1 = (refline_m1 << 8) | (x + 8 < GRW ? grref_line[-refstride + (x >> 3) + 1] << 2 : 0);
+ }
- refline_0 = (refline_0 << 8) |
- (x + 8 < GRW ? grref_line[(x >> 3) + 1] << 4 : 0);
+ refline_0 = (refline_0 << 8) | (x + 8 < GRW ? grref_line[(x >> 3) + 1] << 4 : 0);
- if (y < GRH - 1)
- refline_1 = (refline_1 << 8) |
- (x + 8 < GRW ? grref_line[+refstride + (x >> 3) + 1] << 7 : 0);
- else
- refline_1 = 0;
+ if (y < GRH - 1)
+ refline_1 = (refline_1 << 8) | (x + 8 < GRW ? grref_line[+refstride + (x >> 3) + 1] << 7 : 0);
+ else
+ refline_1 = 0;
- /* this is the speed critical inner-loop */
- for (x_minor = 0; x_minor < minor_width; x_minor++) {
- bool bit;
+ /* this is the speed critical inner-loop */
+ for (x_minor = 0; x_minor < minor_width; x_minor++) {
+ bool bit;
- bit = jbig2_arith_decode(as, &GR_stats[CONTEXT]);
- if (bit < 0)
- return -1;
- result |= bit << (7 - x_minor);
- CONTEXT = ((CONTEXT & 0x0d6) << 1) | bit |
- ((line_m1 >> (9 - x_minor)) & 0x002) |
- ((refline_1 >> (9 - x_minor)) & 0x010) |
- ((refline_0 >> (9 - x_minor)) & 0x040) |
- ((refline_m1 >> (9 - x_minor)) & 0x200);
- }
+ bit = jbig2_arith_decode(as, &GR_stats[CONTEXT]);
+ if (bit < 0)
+ return -1;
+ result |= bit << (7 - x_minor);
+ CONTEXT = ((CONTEXT & 0x0d6) << 1) | bit |
+ ((line_m1 >> (9 - x_minor)) & 0x002) |
+ ((refline_1 >> (9 - x_minor)) & 0x010) | ((refline_0 >> (9 - x_minor)) & 0x040) | ((refline_m1 >> (9 - x_minor)) & 0x200);
+ }
- grreg_line[x>>3] = result;
+ grreg_line[x >> 3] = result;
- }
+ }
- grreg_line += stride;
- grref_line += refstride;
+ grreg_line += stride;
+ grref_line += refstride;
- }
+ }
- return 0;
+ return 0;
}
#endif
+typedef uint32_t(*ContextBuilder)(const Jbig2RefinementRegionParams *, Jbig2Image *, int, int);
-typedef uint32_t (*ContextBuilder)(const Jbig2RefinementRegionParams *,
-Jbig2Image *, int, int);
-
-static int implicit_value( const Jbig2RefinementRegionParams *params, Jbig2Image
-*image, int x, int y )
+static int
+implicit_value(const Jbig2RefinementRegionParams *params, Jbig2Image *image, int x, int y)
{
- Jbig2Image *ref = params->reference;
- int i = x - params->DX;
- int j = y - params->DY;
- int m = jbig2_image_get_pixel(ref, i, j);
- return (
- (jbig2_image_get_pixel(ref, i - 1, j - 1) == m) &&
- (jbig2_image_get_pixel(ref, i , j - 1) == m) &&
- (jbig2_image_get_pixel(ref, i + 1, j - 1) == m) &&
- (jbig2_image_get_pixel(ref, i - 1, j ) == m) &&
- (jbig2_image_get_pixel(ref, i + 1, j ) == m) &&
- (jbig2_image_get_pixel(ref, i - 1, j + 1) == m) &&
- (jbig2_image_get_pixel(ref, i , j + 1) == m) &&
- (jbig2_image_get_pixel(ref, i + 1, j + 1) == m)
- )? m : -1;
+ Jbig2Image *ref = params->reference;
+ int i = x - params->DX;
+ int j = y - params->DY;
+ int m = jbig2_image_get_pixel(ref, i, j);
+
+ return ((jbig2_image_get_pixel(ref, i - 1, j - 1) == m) &&
+ (jbig2_image_get_pixel(ref, i, j - 1) == m) &&
+ (jbig2_image_get_pixel(ref, i + 1, j - 1) == m) &&
+ (jbig2_image_get_pixel(ref, i - 1, j) == m) &&
+ (jbig2_image_get_pixel(ref, i + 1, j) == m) &&
+ (jbig2_image_get_pixel(ref, i - 1, j + 1) == m) && (jbig2_image_get_pixel(ref, i, j + 1) == m) && (jbig2_image_get_pixel(ref, i + 1, j + 1) == m)
+ )? m : -1;
}
-static uint32_t mkctx0( const Jbig2RefinementRegionParams *params, Jbig2Image
-*image, int x, int y )
+static uint32_t
+mkctx0(const Jbig2RefinementRegionParams *params, Jbig2Image *image, int x, int y)
{
- const int dx = params->DX;
- const int dy = params->DY;
- Jbig2Image *ref = params->reference;
- uint32_t CONTEXT;
- CONTEXT = jbig2_image_get_pixel(image, x - 1, y + 0);
- CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 1) << 1;
- CONTEXT |= jbig2_image_get_pixel(image, x + 0, y - 1) << 2;
- CONTEXT |= jbig2_image_get_pixel(image, x + params->grat[0], y +
-params->grat[1]) << 3;
- CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 1, y - dy + 1) << 4;
- CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 0, y - dy + 1) << 5;
- CONTEXT |= jbig2_image_get_pixel(ref, x - dx - 1, y - dy + 1) << 6;
- CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 1, y - dy + 0) << 7;
- CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 0, y - dy + 0) << 8;
- CONTEXT |= jbig2_image_get_pixel(ref, x - dx - 1, y - dy + 0) << 9;
- CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 1, y - dy - 1) << 10;
- CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 0, y - dy - 1) << 11;
- CONTEXT |= jbig2_image_get_pixel(ref, x - dx + params->grat[2], y - dy +
-params->grat[3]) << 12;
- return CONTEXT;
+ const int dx = params->DX;
+ const int dy = params->DY;
+ Jbig2Image *ref = params->reference;
+ uint32_t CONTEXT;
+
+ CONTEXT = jbig2_image_get_pixel(image, x - 1, y + 0);
+ CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 1) << 1;
+ CONTEXT |= jbig2_image_get_pixel(image, x + 0, y - 1) << 2;
+ CONTEXT |= jbig2_image_get_pixel(image, x + params->grat[0], y + params->grat[1]) << 3;
+ CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 1, y - dy + 1) << 4;
+ CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 0, y - dy + 1) << 5;
+ CONTEXT |= jbig2_image_get_pixel(ref, x - dx - 1, y - dy + 1) << 6;
+ CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 1, y - dy + 0) << 7;
+ CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 0, y - dy + 0) << 8;
+ CONTEXT |= jbig2_image_get_pixel(ref, x - dx - 1, y - dy + 0) << 9;
+ CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 1, y - dy - 1) << 10;
+ CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 0, y - dy - 1) << 11;
+ CONTEXT |= jbig2_image_get_pixel(ref, x - dx + params->grat[2], y - dy + params->grat[3]) << 12;
+ return CONTEXT;
}
-static uint32_t mkctx1( const Jbig2RefinementRegionParams *params, Jbig2Image
-*image, int x, int y )
+static uint32_t
+mkctx1(const Jbig2RefinementRegionParams *params, Jbig2Image *image, int x, int y)
{
- const int dx = params->DX;
- const int dy = params->DY;
- Jbig2Image *ref = params->reference;
- uint32_t CONTEXT;
- CONTEXT = jbig2_image_get_pixel(image, x - 1, y + 0);
- CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 1) << 1;
- CONTEXT |= jbig2_image_get_pixel(image, x + 0, y - 1) << 2;
- CONTEXT |= jbig2_image_get_pixel(image, x - 1, y - 1) << 3;
- CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 1, y - dy + 1) << 4;
- CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 0, y - dy + 1) << 5;
- CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 1, y - dy + 0) << 6;
- CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 0, y - dy + 0) << 7;
- CONTEXT |= jbig2_image_get_pixel(ref, x - dx - 1, y - dy + 0) << 8;
- CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 0, y - dy - 1) << 9;
- return CONTEXT;
+ const int dx = params->DX;
+ const int dy = params->DY;
+ Jbig2Image *ref = params->reference;
+ uint32_t CONTEXT;
+
+ CONTEXT = jbig2_image_get_pixel(image, x - 1, y + 0);
+ CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 1) << 1;
+ CONTEXT |= jbig2_image_get_pixel(image, x + 0, y - 1) << 2;
+ CONTEXT |= jbig2_image_get_pixel(image, x - 1, y - 1) << 3;
+ CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 1, y - dy + 1) << 4;
+ CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 0, y - dy + 1) << 5;
+ CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 1, y - dy + 0) << 6;
+ CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 0, y - dy + 0) << 7;
+ CONTEXT |= jbig2_image_get_pixel(ref, x - dx - 1, y - dy + 0) << 8;
+ CONTEXT |= jbig2_image_get_pixel(ref, x - dx + 0, y - dy - 1) << 9;
+ return CONTEXT;
}
-static int jbig2_decode_refinement_TPGRON(const Jbig2RefinementRegionParams
-*params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GR_stats)
+static int
+jbig2_decode_refinement_TPGRON(const Jbig2RefinementRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GR_stats)
{
- const int GRW = image->width;
- const int GRH = image->height;
- int x, y, iv, bit, LTP = 0;
- uint32_t start_context = (params->GRTEMPLATE? 0x40 : 0x100);
- ContextBuilder mkctx = (params->GRTEMPLATE? mkctx1 : mkctx0);
+ const int GRW = image->width;
+ const int GRH = image->height;
+ int x, y, iv, bit, LTP = 0;
+ uint32_t start_context = (params->GRTEMPLATE ? 0x40 : 0x100);
+ ContextBuilder mkctx = (params->GRTEMPLATE ? mkctx1 : mkctx0);
- for (y = 0; y < GRH; y++)
- {
- bit = jbig2_arith_decode(as, &GR_stats[start_context]);
- if (bit < 0) return -1;
- LTP = LTP ^ bit;
- if (!LTP)
- {
- for (x = 0; x < GRW; x++)
- {
- bit = jbig2_arith_decode(as, &GR_stats[mkctx(params, image, x, y)]);
- if (bit < 0) return -1;
- jbig2_image_set_pixel(image, x, y, bit);
- }
- }
- else
- {
- for (x = 0; x < GRW; x++)
- {
- iv = implicit_value(params, image, x, y);
- if (iv < 0)
- {
- bit = jbig2_arith_decode(as, &GR_stats[mkctx(params, image, x, y)]);
- if (bit < 0) return -1;
- jbig2_image_set_pixel(image, x, y, bit);
+ for (y = 0; y < GRH; y++) {
+ bit = jbig2_arith_decode(as, &GR_stats[start_context]);
+ if (bit < 0)
+ return -1;
+ LTP = LTP ^ bit;
+ if (!LTP) {
+ for (x = 0; x < GRW; x++) {
+ bit = jbig2_arith_decode(as, &GR_stats[mkctx(params, image, x, y)]);
+ if (bit < 0)
+ return -1;
+ jbig2_image_set_pixel(image, x, y, bit);
+ }
+ } else {
+ for (x = 0; x < GRW; x++) {
+ iv = implicit_value(params, image, x, y);
+ if (iv < 0) {
+ bit = jbig2_arith_decode(as, &GR_stats[mkctx(params, image, x, y)]);
+ if (bit < 0)
+ return -1;
+ jbig2_image_set_pixel(image, x, y, bit);
+ } else
+ jbig2_image_set_pixel(image, x, y, iv);
+ }
}
- else jbig2_image_set_pixel(image, x, y, iv);
- }
}
- }
- return 0;
+ return 0;
}
-
/**
* jbig2_decode_refinement_region: Decode a generic refinement region.
* @ctx: The context for allocation and error reporting.
@@ -376,27 +343,22 @@
**/
int
jbig2_decode_refinement_region(Jbig2Ctx *ctx,
- Jbig2Segment *segment,
- const Jbig2RefinementRegionParams *params,
- Jbig2ArithState *as,
- Jbig2Image *image,
- Jbig2ArithCx *GR_stats)
+ Jbig2Segment *segment,
+ const Jbig2RefinementRegionParams *params, Jbig2ArithState *as, Jbig2Image *image, Jbig2ArithCx *GR_stats)
{
- {
- jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
- "decoding generic refinement region with offset %d,%x, GRTEMPLATE=%d, TPGRON=%d",
- params->DX, params->DY, params->GRTEMPLATE, params->TPGRON);
- }
+ {
+ jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
+ "decoding generic refinement region with offset %d,%x, GRTEMPLATE=%d, TPGRON=%d",
+ params->DX, params->DY, params->GRTEMPLATE, params->TPGRON);
+ }
- if (params->TPGRON)
- return jbig2_decode_refinement_TPGRON(params, as, image, GR_stats);
+ if (params->TPGRON)
+ return jbig2_decode_refinement_TPGRON(params, as, image, GR_stats);
- if (params->GRTEMPLATE)
- return jbig2_decode_refinement_template1_unopt(ctx, segment, params,
- as, image, GR_stats);
- else
- return jbig2_decode_refinement_template0_unopt(ctx, segment, params,
- as, image, GR_stats);
+ if (params->GRTEMPLATE)
+ return jbig2_decode_refinement_template1_unopt(ctx, segment, params, as, image, GR_stats);
+ else
+ return jbig2_decode_refinement_template0_unopt(ctx, segment, params, as, image, GR_stats);
}
/**
@@ -404,34 +366,32 @@
* with a non-NULL result for use as a reference image
*/
static Jbig2Segment *
-jbig2_region_find_referred(Jbig2Ctx *ctx,Jbig2Segment *segment)
+jbig2_region_find_referred(Jbig2Ctx *ctx, Jbig2Segment *segment)
{
- const int nsegments = segment->referred_to_segment_count;
- Jbig2Segment *rsegment;
- int index;
+ const int nsegments = segment->referred_to_segment_count;
+ Jbig2Segment *rsegment;
+ int index;
- for (index = 0; index < nsegments; index++) {
- rsegment = jbig2_find_segment(ctx,
- segment->referred_to_segments[index]);
- if (rsegment == NULL) {
- jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number,
- "could not find referred to segment %d",
- segment->referred_to_segments[index]);
- continue;
+ for (index = 0; index < nsegments; index++) {
+ rsegment = jbig2_find_segment(ctx, segment->referred_to_segments[index]);
+ if (rsegment == NULL) {
+ jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "could not find referred to segment %d", segment->referred_to_segments[index]);
+ continue;
+ }
+ switch (rsegment->flags & 63) {
+ case 4: /* intermediate text region */
+ case 20: /* intermediate halftone region */
+ case 36: /* intermediate generic region */
+ case 40: /* intermediate generic refinement region */
+ if (rsegment->result)
+ return rsegment;
+ break;
+ default: /* keep looking */
+ break;
+ }
}
- switch (rsegment->flags & 63) {
- case 4: /* intermediate text region */
- case 20: /* intermediate halftone region */
- case 36: /* intermediate generic region */
- case 40: /* intermediate generic refinement region */
- if (rsegment->result) return rsegment;
- break;
- default: /* keep looking */
- break;
- }
- }
- /* no appropriate reference was found. */
- return NULL;
+ /* no appropriate reference was found. */
+ return NULL;
}
/**
@@ -438,152 +398,124 @@
* Handler for generic refinement region segments
*/
int
-jbig2_refinement_region(Jbig2Ctx *ctx, Jbig2Segment *segment,
- const byte *segment_data)
+jbig2_refinement_region(Jbig2Ctx *ctx, Jbig2Segment *segment, const byte *segment_data)
{
- Jbig2RefinementRegionParams params;
- Jbig2RegionSegmentInfo rsi;
- int offset = 0;
- byte seg_flags;
- int code = 0;
+ Jbig2RefinementRegionParams params;
+ Jbig2RegionSegmentInfo rsi;
+ int offset = 0;
+ byte seg_flags;
+ int code = 0;
- /* 7.4.7 */
- if (segment->data_length < 18)
- return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "Segment too short");
+ /* 7.4.7 */
+ if (segment->data_length < 18)
+ return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "Segment too short");
- jbig2_get_region_segment_info(&rsi, segment_data);
- jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number,
- "generic region: %d x %d @ (%d, %d), flags = %02x",
- rsi.width, rsi.height, rsi.x, rsi.y, rsi.flags);
+ jbig2_get_region_segment_info(&rsi, segment_data);
+ jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "generic region: %d x %d @ (%d, %d), flags = %02x", rsi.width, rsi.height, rsi.x, rsi.y, rsi.flags);
- /* 7.4.7.2 */
- seg_flags = segment_data[17];
- params.GRTEMPLATE = seg_flags & 0x01;
- params.TPGRON = seg_flags & 0x02 ? 1 : 0;
- jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number,
- "segment flags = %02x %s%s", seg_flags,
- params.GRTEMPLATE ? " GRTEMPLATE" :"",
- params.TPGRON ? " TPGRON" : "" );
- if (seg_flags & 0xFC)
- jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number,
- "reserved segment flag bits are non-zero");
- offset += 18;
-
- /* 7.4.7.3 */
- if (!params.GRTEMPLATE) {
- if (segment->data_length < 22)
- return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "Segment too short");
- params.grat[0] = segment_data[offset + 0];
- params.grat[1] = segment_data[offset + 1];
- params.grat[2] = segment_data[offset + 2];
- params.grat[3] = segment_data[offset + 3];
+ /* 7.4.7.2 */
+ seg_flags = segment_data[17];
+ params.GRTEMPLATE = seg_flags & 0x01;
+ params.TPGRON = seg_flags & 0x02 ? 1 : 0;
jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number,
- "grat1: (%d, %d) grat2: (%d, %d)",
- params.grat[0], params.grat[1],
- params.grat[2], params.grat[3]);
- offset += 4;
- }
+ "segment flags = %02x %s%s", seg_flags, params.GRTEMPLATE ? " GRTEMPLATE" : "", params.TPGRON ? " TPGRON" : "");
+ if (seg_flags & 0xFC)
+ jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "reserved segment flag bits are non-zero");
+ offset += 18;
- /* 7.4.7.4 - set up the reference image */
- if (segment->referred_to_segment_count) {
- Jbig2Segment *ref;
+ /* 7.4.7.3 */
+ if (!params.GRTEMPLATE) {
+ if (segment->data_length < 22)
+ return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "Segment too short");
+ params.grat[0] = segment_data[offset + 0];
+ params.grat[1] = segment_data[offset + 1];
+ params.grat[2] = segment_data[offset + 2];
+ params.grat[3] = segment_data[offset + 3];
+ jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number,
+ "grat1: (%d, %d) grat2: (%d, %d)", params.grat[0], params.grat[1], params.grat[2], params.grat[3]);
+ offset += 4;
+ }
- ref = jbig2_region_find_referred(ctx, segment);
- if (ref == NULL)
- return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "could not find reference bitmap!");
- /* the reference bitmap is the result of a previous
- intermediate region segment; the reference selection
- rules say to use the first one available, and not to
- reuse any intermediate result, so we simply clone it
- and free the original to keep track of this. */
- params.reference = jbig2_image_clone(ctx, (Jbig2Image*)ref->result);
- jbig2_image_release(ctx, (Jbig2Image*)ref->result);
- ref->result = NULL;
- jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
- "found reference bitmap in segment %d", ref->number);
- } else {
- /* the reference is just (a subset of) the page buffer */
- params.reference = jbig2_image_clone(ctx,
- ctx->pages[ctx->current_page].image);
- if (params.reference == NULL)
- return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "could not clone reference bitmap!");
- /* TODO: subset the image if appropriate */
- }
+ /* 7.4.7.4 - set up the reference image */
+ if (segment->referred_to_segment_count) {
+ Jbig2Segment *ref;
- /* 7.4.7.5 */
- params.DX = 0;
- params.DY = 0;
- {
- Jbig2WordStream *ws = NULL;
- Jbig2ArithState *as = NULL;
- Jbig2ArithCx *GR_stats = NULL;
- int stats_size;
- Jbig2Image *image = NULL;
-
- image = jbig2_image_new(ctx, rsi.width, rsi.height);
- if (image == NULL)
- {
- code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "unable to allocate refinement image");
- goto cleanup;
+ ref = jbig2_region_find_referred(ctx, segment);
+ if (ref == NULL)
+ return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "could not find reference bitmap!");
+ /* the reference bitmap is the result of a previous
+ intermediate region segment; the reference selection
+ rules say to use the first one available, and not to
+ reuse any intermediate result, so we simply clone it
+ and free the original to keep track of this. */
+ params.reference = jbig2_image_clone(ctx, (Jbig2Image *) ref->result);
+ jbig2_image_release(ctx, (Jbig2Image *) ref->result);
+ ref->result = NULL;
+ jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "found reference bitmap in segment %d", ref->number);
+ } else {
+ /* the reference is just (a subset of) the page buffer */
+ params.reference = jbig2_image_clone(ctx, ctx->pages[ctx->current_page].image);
+ if (params.reference == NULL)
+ return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "could not clone reference bitmap!");
+ /* TODO: subset the image if appropriate */
}
- jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
- "allocated %d x %d image buffer for region decode results",
- rsi.width, rsi.height);
- stats_size = params.GRTEMPLATE ? 1 << 10 : 1 << 13;
- GR_stats = jbig2_new(ctx, Jbig2ArithCx, stats_size);
- if (GR_stats == NULL)
+ /* 7.4.7.5 */
+ params.DX = 0;
+ params.DY = 0;
{
- code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1,
- "failed to allocate GR-stats in jbig2_refinement_region");
- goto cleanup;
- }
- memset(GR_stats, 0, stats_size);
+ Jbig2WordStream *ws = NULL;
+ Jbig2ArithState *as = NULL;
+ Jbig2ArithCx *GR_stats = NULL;
+ int stats_size;
+ Jbig2Image *image = NULL;
- ws = jbig2_word_stream_buf_new(ctx, segment_data + offset,
- segment->data_length - offset);
- if (ws == NULL)
- {
- code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1,
- "failed to allocate ws in jbig2_refinement_region");
- goto cleanup;
- }
+ image = jbig2_image_new(ctx, rsi.width, rsi.height);
+ if (image == NULL) {
+ code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "unable to allocate refinement image");
+ goto cleanup;
+ }
+ jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "allocated %d x %d image buffer for region decode results", rsi.width, rsi.height);
- as = jbig2_arith_new(ctx, ws);
- if (as == NULL)
- {
- code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1,
- "failed to allocate as in jbig2_refinement_region");
- goto cleanup;
- }
+ stats_size = params.GRTEMPLATE ? 1 << 10 : 1 << 13;
+ GR_stats = jbig2_new(ctx, Jbig2ArithCx, stats_size);
+ if (GR_stats == NULL) {
+ code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "failed to allocate GR-stats in jbig2_refinement_region");
+ goto cleanup;
+ }
+ memset(GR_stats, 0, stats_size);
- code = jbig2_decode_refinement_region(ctx, segment, ¶ms,
- as, image, GR_stats);
+ ws = jbig2_word_stream_buf_new(ctx, segment_data + offset, segment->data_length - offset);
+ if (ws == NULL) {
+ code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "failed to allocate ws in jbig2_refinement_region");
+ goto cleanup;
+ }
- if ((segment->flags & 63) == 40) {
- /* intermediate region. save the result for later */
- segment->result = jbig2_image_clone(ctx, image);
- } else {
- /* immediate region. composite onto the page */
- jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
- "composing %dx%d decoded refinement region onto page at (%d, %d)",
- rsi.width, rsi.height, rsi.x, rsi.y);
- jbig2_page_add_result(ctx, &ctx->pages[ctx->current_page],
- image, rsi.x, rsi.y, rsi.op);
- }
+ as = jbig2_arith_new(ctx, ws);
+ if (as == NULL) {
+ code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "failed to allocate as in jbig2_refinement_region");
+ goto cleanup;
+ }
+ code = jbig2_decode_refinement_region(ctx, segment, ¶ms, as, image, GR_stats);
+
+ if ((segment->flags & 63) == 40) {
+ /* intermediate region. save the result for later */
+ segment->result = jbig2_image_clone(ctx, image);
+ } else {
+ /* immediate region. composite onto the page */
+ jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
+ "composing %dx%d decoded refinement region onto page at (%d, %d)", rsi.width, rsi.height, rsi.x, rsi.y);
+ jbig2_page_add_result(ctx, &ctx->pages[ctx->current_page], image, rsi.x, rsi.y, rsi.op);
+ }
+
cleanup:
- jbig2_image_release(ctx, image);
- jbig2_image_release(ctx, params.reference);
- jbig2_free(ctx->allocator, as);
- jbig2_word_stream_buf_free(ctx, ws);
- jbig2_free(ctx->allocator, GR_stats);
- }
+ jbig2_image_release(ctx, image);
+ jbig2_image_release(ctx, params.reference);
+ jbig2_free(ctx->allocator, as);
+ jbig2_word_stream_buf_free(ctx, ws);
+ jbig2_free(ctx->allocator, GR_stats);
+ }
- return code;
+ return code;
}
--- a/jbig2_segment.c
+++ b/jbig2_segment.c
@@ -17,13 +17,12 @@
jbig2dec
*/
-
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "os_types.h"
-#include <stddef.h> /* size_t */
+#include <stddef.h> /* size_t */
#include "jbig2.h"
#include "jbig2_priv.h"
@@ -34,152 +33,132 @@
#include "jbig2_halftone.h"
Jbig2Segment *
-jbig2_parse_segment_header (Jbig2Ctx *ctx, uint8_t *buf, size_t buf_size,
- size_t *p_header_size)
+jbig2_parse_segment_header(Jbig2Ctx *ctx, uint8_t *buf, size_t buf_size, size_t *p_header_size)
{
- Jbig2Segment *result;
- uint8_t rtscarf;
- uint32_t rtscarf_long;
- uint32_t *referred_to_segments;
- int referred_to_segment_count;
- int referred_to_segment_size;
- int pa_size;
- int offset;
+ Jbig2Segment *result;
+ uint8_t rtscarf;
+ uint32_t rtscarf_long;
+ uint32_t *referred_to_segments;
+ int referred_to_segment_count;
+ int referred_to_segment_size;
+ int pa_size;
+ int offset;
- /* minimum possible size of a jbig2 segment header */
- if (buf_size < 11)
- return NULL;
+ /* minimum possible size of a jbig2 segment header */
+ if (buf_size < 11)
+ return NULL;
- result = jbig2_new(ctx, Jbig2Segment, 1);
- if (result == NULL)
- {
- jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1,
- "failed to allocate segment in jbig2_parse_segment_header");
- return result;
- }
+ result = jbig2_new(ctx, Jbig2Segment, 1);
+ if (result == NULL) {
+ jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "failed to allocate segment in jbig2_parse_segment_header");
+ return result;
+ }
- /* 7.2.2 */
- result->number = jbig2_get_uint32(buf);
+ /* 7.2.2 */
+ result->number = jbig2_get_uint32(buf);
- /* 7.2.3 */
- result->flags = buf[4];
+ /* 7.2.3 */
+ result->flags = buf[4];
- /* 7.2.4 referred-to segments */
- rtscarf = buf[5];
- if ((rtscarf & 0xe0) == 0xe0)
- {
- rtscarf_long = jbig2_get_uint32(buf + 5);
- referred_to_segment_count = rtscarf_long & 0x1fffffff;
- offset = 5 + 4 + (referred_to_segment_count + 1) / 8;
+ /* 7.2.4 referred-to segments */
+ rtscarf = buf[5];
+ if ((rtscarf & 0xe0) == 0xe0) {
+ rtscarf_long = jbig2_get_uint32(buf + 5);
+ referred_to_segment_count = rtscarf_long & 0x1fffffff;
+ offset = 5 + 4 + (referred_to_segment_count + 1) / 8;
+ } else {
+ referred_to_segment_count = (rtscarf >> 5);
+ offset = 5 + 1;
}
- else
- {
- referred_to_segment_count = (rtscarf >> 5);
- offset = 5 + 1;
- }
- result->referred_to_segment_count = referred_to_segment_count;
+ result->referred_to_segment_count = referred_to_segment_count;
- /* we now have enough information to compute the full header length */
- referred_to_segment_size = result->number <= 256 ? 1:
- result->number <= 65536 ? 2 : 4; /* 7.2.5 */
- pa_size = result->flags & 0x40 ? 4 : 1; /* 7.2.6 */
- if (offset + referred_to_segment_count*referred_to_segment_size + pa_size + 4 > buf_size)
- {
- jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, result->number,
- "jbig2_parse_segment_header() called with insufficient data", -1);
- jbig2_free (ctx->allocator, result);
- return NULL;
+ /* we now have enough information to compute the full header length */
+ referred_to_segment_size = result->number <= 256 ? 1 : result->number <= 65536 ? 2 : 4; /* 7.2.5 */
+ pa_size = result->flags & 0x40 ? 4 : 1; /* 7.2.6 */
+ if (offset + referred_to_segment_count * referred_to_segment_size + pa_size + 4 > buf_size) {
+ jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, result->number, "jbig2_parse_segment_header() called with insufficient data", -1);
+ jbig2_free(ctx->allocator, result);
+ return NULL;
}
- /* 7.2.5 */
- if (referred_to_segment_count)
- {
- int i;
+ /* 7.2.5 */
+ if (referred_to_segment_count) {
+ int i;
- referred_to_segments = jbig2_new(ctx, uint32_t,
- referred_to_segment_count * referred_to_segment_size);
- if (referred_to_segments == NULL)
- {
- jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1,
- "could not allocate referred_to_segments "
- "in jbig2_parse_segment_header");
- return NULL;
- }
+ referred_to_segments = jbig2_new(ctx, uint32_t, referred_to_segment_count * referred_to_segment_size);
+ if (referred_to_segments == NULL) {
+ jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "could not allocate referred_to_segments " "in jbig2_parse_segment_header");
+ return NULL;
+ }
- for (i = 0; i < referred_to_segment_count; i++) {
- referred_to_segments[i] =
- (referred_to_segment_size == 1) ? buf[offset] :
- (referred_to_segment_size == 2) ? jbig2_get_uint16(buf+offset) :
- jbig2_get_uint32(buf + offset);
- offset += referred_to_segment_size;
- jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, result->number,
- "segment %d refers to segment %d",
- result->number, referred_to_segments[i]);
- }
- result->referred_to_segments = referred_to_segments;
+ for (i = 0; i < referred_to_segment_count; i++) {
+ referred_to_segments[i] =
+ (referred_to_segment_size == 1) ? buf[offset] :
+ (referred_to_segment_size == 2) ? jbig2_get_uint16(buf + offset) : jbig2_get_uint32(buf + offset);
+ offset += referred_to_segment_size;
+ jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, result->number, "segment %d refers to segment %d", result->number, referred_to_segments[i]);
+ }
+ result->referred_to_segments = referred_to_segments;
+ } else { /* no referred-to segments */
+
+ result->referred_to_segments = NULL;
}
- else /* no referred-to segments */
- {
- result->referred_to_segments = NULL;
+
+ /* 7.2.6 */
+ if (result->flags & 0x40) {
+ result->page_association = jbig2_get_uint32(buf + offset);
+ offset += 4;
+ } else {
+ result->page_association = buf[offset++];
}
+ jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, result->number, "segment %d is associated with page %d", result->number, result->page_association);
- /* 7.2.6 */
- if (result->flags & 0x40) {
- result->page_association = jbig2_get_uint32(buf + offset);
- offset += 4;
- } else {
- result->page_association = buf[offset++];
- }
- jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, result->number,
- "segment %d is associated with page %d",
- result->number, result->page_association);
+ /* 7.2.7 */
+ result->data_length = jbig2_get_uint32(buf + offset);
+ *p_header_size = offset + 4;
- /* 7.2.7 */
- result->data_length = jbig2_get_uint32(buf + offset);
- *p_header_size = offset + 4;
+ /* no body parsing results yet */
+ result->result = NULL;
- /* no body parsing results yet */
- result->result = NULL;
-
- return result;
+ return result;
}
void
-jbig2_free_segment (Jbig2Ctx *ctx, Jbig2Segment *segment)
+jbig2_free_segment(Jbig2Ctx *ctx, Jbig2Segment *segment)
{
- if (segment->referred_to_segments != NULL) {
- jbig2_free(ctx->allocator, segment->referred_to_segments);
- }
- /* todo: we need either some separate fields or
- a more complex result object rather than this
- brittle special casing */
+ if (segment->referred_to_segments != NULL) {
+ jbig2_free(ctx->allocator, segment->referred_to_segments);
+ }
+ /* todo: we need either some separate fields or
+ a more complex result object rather than this
+ brittle special casing */
switch (segment->flags & 63) {
- case 0: /* symbol dictionary */
- if (segment->result != NULL)
- jbig2_sd_release(ctx, (Jbig2SymbolDict*)segment->result);
- break;
- case 4: /* intermediate text region */
- case 40: /* intermediate refinement region */
- if (segment->result != NULL)
- jbig2_image_release(ctx, (Jbig2Image*)segment->result);
- break;
- case 16: /* pattern dictionary */
- if (segment->result != NULL)
- jbig2_hd_release(ctx, (Jbig2PatternDict*)segment->result);
- break;
- case 53: /* user-supplied huffman table */
- if (segment->result != NULL)
- jbig2_table_free(ctx, (Jbig2HuffmanParams*)segment->result);
- break;
- case 62:
- if (segment->result != NULL)
- jbig2_metadata_free(ctx, (Jbig2Metadata*)segment->result);
- break;
- default:
- /* anything else is probably an undefined pointer */
- break;
+ case 0: /* symbol dictionary */
+ if (segment->result != NULL)
+ jbig2_sd_release(ctx, (Jbig2SymbolDict *) segment->result);
+ break;
+ case 4: /* intermediate text region */
+ case 40: /* intermediate refinement region */
+ if (segment->result != NULL)
+ jbig2_image_release(ctx, (Jbig2Image *) segment->result);
+ break;
+ case 16: /* pattern dictionary */
+ if (segment->result != NULL)
+ jbig2_hd_release(ctx, (Jbig2PatternDict *) segment->result);
+ break;
+ case 53: /* user-supplied huffman table */
+ if (segment->result != NULL)
+ jbig2_table_free(ctx, (Jbig2HuffmanParams *) segment->result);
+ break;
+ case 62:
+ if (segment->result != NULL)
+ jbig2_metadata_free(ctx, (Jbig2Metadata *) segment->result);
+ break;
+ default:
+ /* anything else is probably an undefined pointer */
+ break;
}
- jbig2_free (ctx->allocator, segment);
+ jbig2_free(ctx->allocator, segment);
}
/* find a segment by number */
@@ -195,9 +174,9 @@
return (ctx->segments[index]);
if (global_ctx)
- for (index = global_ctx->segment_index - 1; index >= 0; index--)
- if (global_ctx->segments[index]->number == number)
- return (global_ctx->segments[index]);
+ for (index = global_ctx->segment_index - 1; index >= 0; index--)
+ if (global_ctx->segments[index]->number == number)
+ return (global_ctx->segments[index]);
/* didn't find a match */
return NULL;
@@ -205,100 +184,90 @@
/* parse the generic portion of a region segment data header */
void
-jbig2_get_region_segment_info(Jbig2RegionSegmentInfo *info,
- const uint8_t *segment_data)
+jbig2_get_region_segment_info(Jbig2RegionSegmentInfo *info, const uint8_t *segment_data)
{
- /* 7.4.1 */
- info->width = jbig2_get_int32(segment_data);
- info->height = jbig2_get_int32(segment_data + 4);
- info->x = jbig2_get_int32(segment_data + 8);
- info->y = jbig2_get_int32(segment_data + 12);
- info->flags = segment_data[16];
- info->op = (Jbig2ComposeOp)(info->flags & 0x7);
+ /* 7.4.1 */
+ info->width = jbig2_get_int32(segment_data);
+ info->height = jbig2_get_int32(segment_data + 4);
+ info->x = jbig2_get_int32(segment_data + 8);
+ info->y = jbig2_get_int32(segment_data + 12);
+ info->flags = segment_data[16];
+ info->op = (Jbig2ComposeOp)(info->flags & 0x7);
}
/* dispatch code for extension segment parsing */
static int
-jbig2_parse_extension_segment(Jbig2Ctx *ctx, Jbig2Segment *segment,
- const uint8_t *segment_data)
+jbig2_parse_extension_segment(Jbig2Ctx *ctx, Jbig2Segment *segment, const uint8_t *segment_data)
{
uint32_t type = jbig2_get_uint32(segment_data);
bool reserved = type & 0x20000000;
+
/* bool dependent = type & 0x40000000; (NYI) */
bool necessary = type & 0x80000000;
if (necessary && !reserved) {
- jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number,
- "extension segment is marked 'necessary' but "
- "not 'reservered' contrary to spec");
+ jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "extension segment is marked 'necessary' but " "not 'reservered' contrary to spec");
}
switch (type) {
- case 0x20000000: return jbig2_comment_ascii(ctx, segment, segment_data);
- case 0x20000002: return jbig2_comment_unicode(ctx, segment, segment_data);
- default:
- if (necessary) {
- return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "unhandled necessary extension segment type 0x%08x", type);
- } else {
- return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number,
- "unhandled extension segment");
- }
+ case 0x20000000:
+ return jbig2_comment_ascii(ctx, segment, segment_data);
+ case 0x20000002:
+ return jbig2_comment_unicode(ctx, segment, segment_data);
+ default:
+ if (necessary) {
+ return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "unhandled necessary extension segment type 0x%08x", type);
+ } else {
+ return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "unhandled extension segment");
+ }
}
}
/* general segment parsing dispatch */
-int jbig2_parse_segment (Jbig2Ctx *ctx, Jbig2Segment *segment,
- const uint8_t *segment_data)
+int
+jbig2_parse_segment(Jbig2Ctx *ctx, Jbig2Segment *segment, const uint8_t *segment_data)
{
- jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number,
- "Segment %d, flags=%x, type=%d, data_length=%d",
- segment->number, segment->flags, segment->flags & 63,
- segment->data_length);
- switch (segment->flags & 63)
- {
+ jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number,
+ "Segment %d, flags=%x, type=%d, data_length=%d", segment->number, segment->flags, segment->flags & 63, segment->data_length);
+ switch (segment->flags & 63) {
case 0:
- return jbig2_symbol_dictionary(ctx, segment, segment_data);
- case 4: /* intermediate text region */
- case 6: /* immediate text region */
- case 7: /* immediate lossless text region */
- return jbig2_text_region(ctx, segment, segment_data);
+ return jbig2_symbol_dictionary(ctx, segment, segment_data);
+ case 4: /* intermediate text region */
+ case 6: /* immediate text region */
+ case 7: /* immediate lossless text region */
+ return jbig2_text_region(ctx, segment, segment_data);
case 16:
- return jbig2_pattern_dictionary(ctx, segment, segment_data);
- case 20: /* intermediate halftone region */
- case 22: /* immediate halftone region */
- case 23: /* immediate lossless halftone region */
- return jbig2_halftone_region(ctx, segment, segment_data);
+ return jbig2_pattern_dictionary(ctx, segment, segment_data);
+ case 20: /* intermediate halftone region */
+ case 22: /* immediate halftone region */
+ case 23: /* immediate lossless halftone region */
+ return jbig2_halftone_region(ctx, segment, segment_data);
case 36:
- return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number,
- "unhandled segment type 'intermediate generic region'");
- case 38: /* immediate generic region */
- case 39: /* immediate lossless generic region */
- return jbig2_immediate_generic_region(ctx, segment, segment_data);
- case 40: /* intermediate generic refinement region */
- case 42: /* immediate generic refinement region */
- case 43: /* immediate lossless generic refinement region */
- return jbig2_refinement_region(ctx, segment, segment_data);
+ return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "unhandled segment type 'intermediate generic region'");
+ case 38: /* immediate generic region */
+ case 39: /* immediate lossless generic region */
+ return jbig2_immediate_generic_region(ctx, segment, segment_data);
+ case 40: /* intermediate generic refinement region */
+ case 42: /* immediate generic refinement region */
+ case 43: /* immediate lossless generic refinement region */
+ return jbig2_refinement_region(ctx, segment, segment_data);
case 48:
- return jbig2_page_info(ctx, segment, segment_data);
+ return jbig2_page_info(ctx, segment, segment_data);
case 49:
- return jbig2_end_of_page(ctx, segment, segment_data);
+ return jbig2_end_of_page(ctx, segment, segment_data);
case 50:
- return jbig2_end_of_stripe(ctx, segment, segment_data);
+ return jbig2_end_of_stripe(ctx, segment, segment_data);
case 51:
- ctx->state = JBIG2_FILE_EOF;
- return jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number,
- "end of file");
+ ctx->state = JBIG2_FILE_EOF;
+ return jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "end of file");
case 52:
- return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number,
- "unhandled segment type 'profile'");
- case 53: /* user-supplied huffman table */
- return jbig2_table(ctx, segment, segment_data);
+ return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "unhandled segment type 'profile'");
+ case 53: /* user-supplied huffman table */
+ return jbig2_table(ctx, segment, segment_data);
case 62:
- return jbig2_parse_extension_segment(ctx, segment, segment_data);
+ return jbig2_parse_extension_segment(ctx, segment, segment_data);
default:
- jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number,
- "unknown segment type %d", segment->flags & 63);
+ jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "unknown segment type %d", segment->flags & 63);
}
- return 0;
+ return 0;
}
--- a/jbig2_symbol_dict.c
+++ b/jbig2_symbol_dict.c
@@ -17,7 +17,6 @@
jbig2dec
*/
-
/* symbol dictionary segment decode and support */
#ifdef HAVE_CONFIG_H
@@ -26,7 +25,7 @@
#include "os_types.h"
#include <stddef.h>
-#include <string.h> /* memset() */
+#include <string.h> /* memset() */
#include "jbig2.h"
#include "jbig2_priv.h"
@@ -46,23 +45,22 @@
/* Table 13 */
typedef struct {
- bool SDHUFF;
- bool SDREFAGG;
- uint32_t SDNUMINSYMS;
- Jbig2SymbolDict *SDINSYMS;
- uint32_t SDNUMNEWSYMS;
- uint32_t SDNUMEXSYMS;
- Jbig2HuffmanTable *SDHUFFDH;
- Jbig2HuffmanTable *SDHUFFDW;
- Jbig2HuffmanTable *SDHUFFBMSIZE;
- Jbig2HuffmanTable *SDHUFFAGGINST;
- int SDTEMPLATE;
- int8_t sdat[8];
- bool SDRTEMPLATE;
- int8_t sdrat[4];
+ bool SDHUFF;
+ bool SDREFAGG;
+ uint32_t SDNUMINSYMS;
+ Jbig2SymbolDict *SDINSYMS;
+ uint32_t SDNUMNEWSYMS;
+ uint32_t SDNUMEXSYMS;
+ Jbig2HuffmanTable *SDHUFFDH;
+ Jbig2HuffmanTable *SDHUFFDW;
+ Jbig2HuffmanTable *SDHUFFBMSIZE;
+ Jbig2HuffmanTable *SDHUFFAGGINST;
+ int SDTEMPLATE;
+ int8_t sdat[8];
+ bool SDRTEMPLATE;
+ int8_t sdrat[4];
} Jbig2SymbolDictParams;
-
/* Utility routines */
#ifdef DUMP_SYMDICT
@@ -69,18 +67,16 @@
void
jbig2_dump_symbol_dict(Jbig2Ctx *ctx, Jbig2Segment *segment)
{
- Jbig2SymbolDict *dict = (Jbig2SymbolDict *)segment->result;
+ Jbig2SymbolDict *dict = (Jbig2SymbolDict *) segment->result;
int index;
char filename[24];
- if (dict == NULL) return;
- jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number,
- "dumping symbol dict as %d individual png files\n", dict->n_symbols);
+ if (dict == NULL)
+ return;
+ jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "dumping symbol dict as %d individual png files\n", dict->n_symbols);
for (index = 0; index < dict->n_symbols; index++) {
- snprintf(filename, sizeof(filename), "symbol_%02d-%04d.png",
- segment->number, index);
- jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
- "dumping symbol %d/%d as '%s'", index, dict->n_symbols, filename);
+ snprintf(filename, sizeof(filename), "symbol_%02d-%04d.png", segment->number, index);
+ jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "dumping symbol %d/%d as '%s'", index, dict->n_symbols, filename);
#ifdef HAVE_LIBPNG
jbig2_image_write_png_file(dict->glyphs[index], filename);
#else
@@ -94,34 +90,31 @@
Jbig2SymbolDict *
jbig2_sd_new(Jbig2Ctx *ctx, int n_symbols)
{
- Jbig2SymbolDict *new = NULL;
+ Jbig2SymbolDict *new = NULL;
- if (n_symbols < 0) {
- jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1,
- "Negative number of symbols in symbol dict: %d", n_symbols);
- return NULL;
- }
+ if (n_symbols < 0) {
+ jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "Negative number of symbols in symbol dict: %d", n_symbols);
+ return NULL;
+ }
- new = jbig2_new(ctx, Jbig2SymbolDict, 1);
- if (new != NULL) {
- new->glyphs = jbig2_new(ctx, Jbig2Image*, n_symbols);
- new->n_symbols = n_symbols;
- } else {
- jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1,
- "unable to allocate new empty symbol dict");
- return NULL;
- }
+ new = jbig2_new(ctx, Jbig2SymbolDict, 1);
+ if (new != NULL) {
+ new->glyphs = jbig2_new(ctx, Jbig2Image *, n_symbols);
+ new->n_symbols = n_symbols;
+ } else {
+ jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "unable to allocate new empty symbol dict");
+ return NULL;
+ }
- if (new->glyphs != NULL) {
- memset(new->glyphs, 0, n_symbols*sizeof(Jbig2Image*));
- } else {
- jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1,
- "unable to allocate glyphs for new empty symbol dict");
- jbig2_free(ctx->allocator, new);
- return NULL;
- }
+ if (new->glyphs != NULL) {
+ memset(new->glyphs, 0, n_symbols * sizeof(Jbig2Image *));
+ } else {
+ jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "unable to allocate glyphs for new empty symbol dict");
+ jbig2_free(ctx->allocator, new);
+ return NULL;
+ }
- return new;
+ return new;
}
/* release the memory associated with a symbol dict */
@@ -128,13 +121,15 @@
void
jbig2_sd_release(Jbig2Ctx *ctx, Jbig2SymbolDict *dict)
{
- int i;
+ int i;
- if (dict == NULL) return;
- for (i = 0; i < dict->n_symbols; i++)
- if (dict->glyphs[i]) jbig2_image_release(ctx, dict->glyphs[i]);
- jbig2_free(ctx->allocator, dict->glyphs);
- jbig2_free(ctx->allocator, dict);
+ if (dict == NULL)
+ return;
+ for (i = 0; i < dict->n_symbols; i++)
+ if (dict->glyphs[i])
+ jbig2_image_release(ctx, dict->glyphs[i]);
+ jbig2_free(ctx->allocator, dict->glyphs);
+ jbig2_free(ctx->allocator, dict);
}
/* get a particular glyph by index */
@@ -141,8 +136,9 @@
Jbig2Image *
jbig2_sd_glyph(Jbig2SymbolDict *dict, unsigned int id)
{
- if (dict == NULL) return NULL;
- return dict->glyphs[id];
+ if (dict == NULL)
+ return NULL;
+ return dict->glyphs[id];
}
/* count the number of dictionary segments referred to by the given segment */
@@ -156,9 +152,7 @@
for (index = 0; index < segment->referred_to_segment_count; index++) {
rsegment = jbig2_find_segment(ctx, segment->referred_to_segments[index]);
if (rsegment && ((rsegment->flags & 63) == 0) &&
- rsegment->result &&
- (((Jbig2SymbolDict *)rsegment->result)->n_symbols > 0) &&
- ((*((Jbig2SymbolDict *)rsegment->result)->glyphs) != NULL))
+ rsegment->result && (((Jbig2SymbolDict *) rsegment->result)->n_symbols > 0) && ((*((Jbig2SymbolDict *) rsegment->result)->glyphs) != NULL))
n_dicts++;
}
@@ -175,11 +169,9 @@
int n_dicts = jbig2_sd_count_referred(ctx, segment);
int dindex = 0;
- dicts = jbig2_new(ctx, Jbig2SymbolDict*, n_dicts);
- if (dicts == NULL)
- {
- jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "failed to allocate referred list of symbol dictionaries");
+ dicts = jbig2_new(ctx, Jbig2SymbolDict *, n_dicts);
+ if (dicts == NULL) {
+ jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to allocate referred list of symbol dictionaries");
return NULL;
}
@@ -186,18 +178,15 @@
for (index = 0; index < segment->referred_to_segment_count; index++) {
rsegment = jbig2_find_segment(ctx, segment->referred_to_segments[index]);
if (rsegment && ((rsegment->flags & 63) == 0) && rsegment->result &&
- (((Jbig2SymbolDict *)rsegment->result)->n_symbols > 0) &&
- ((*((Jbig2SymbolDict *)rsegment->result)->glyphs) != NULL)) {
+ (((Jbig2SymbolDict *) rsegment->result)->n_symbols > 0) && ((*((Jbig2SymbolDict *) rsegment->result)->glyphs) != NULL)) {
/* add this referred to symbol dictionary */
- dicts[dindex++] = (Jbig2SymbolDict *)rsegment->result;
+ dicts[dindex++] = (Jbig2SymbolDict *) rsegment->result;
}
}
if (dindex != n_dicts) {
/* should never happen */
- jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "counted %d symbol dictionaries but built a list with %d.\n",
- n_dicts, dindex);
+ jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "counted %d symbol dictionaries but built a list with %d.\n", n_dicts, dindex);
}
return (dicts);
@@ -208,949 +197,834 @@
Jbig2SymbolDict *
jbig2_sd_cat(Jbig2Ctx *ctx, int n_dicts, Jbig2SymbolDict **dicts)
{
- int i,j,k, symbols;
- Jbig2SymbolDict *new = NULL;
+ int i, j, k, symbols;
+ Jbig2SymbolDict *new = NULL;
- /* count the imported symbols and allocate a new array */
- symbols = 0;
- for(i = 0; i < n_dicts; i++)
- symbols += dicts[i]->n_symbols;
-
- /* fill a new array with cloned glyph pointers */
- new = jbig2_sd_new(ctx, symbols);
- if (new != NULL) {
- k = 0;
+ /* count the imported symbols and allocate a new array */
+ symbols = 0;
for (i = 0; i < n_dicts; i++)
- for (j = 0; j < dicts[i]->n_symbols; j++)
- new->glyphs[k++] = jbig2_image_clone(ctx, dicts[i]->glyphs[j]);
- } else {
- jbig2_error(ctx, JBIG2_SEVERITY_WARNING, -1,
- "failed to allocate new symbol dictionary");
- }
+ symbols += dicts[i]->n_symbols;
- return new;
+ /* fill a new array with cloned glyph pointers */
+ new = jbig2_sd_new(ctx, symbols);
+ if (new != NULL) {
+ k = 0;
+ for (i = 0; i < n_dicts; i++)
+ for (j = 0; j < dicts[i]->n_symbols; j++)
+ new->glyphs[k++] = jbig2_image_clone(ctx, dicts[i]->glyphs[j]);
+ } else {
+ jbig2_error(ctx, JBIG2_SEVERITY_WARNING, -1, "failed to allocate new symbol dictionary");
+ }
+
+ return new;
}
-
/* Decoding routines */
/* 6.5 */
static Jbig2SymbolDict *
jbig2_decode_symbol_dict(Jbig2Ctx *ctx,
- Jbig2Segment *segment,
- const Jbig2SymbolDictParams *params,
- const byte *data, size_t size,
- Jbig2ArithCx *GB_stats,
- Jbig2ArithCx *GR_stats)
+ Jbig2Segment *segment,
+ const Jbig2SymbolDictParams *params, const byte *data, size_t size, Jbig2ArithCx *GB_stats, Jbig2ArithCx *GR_stats)
{
- Jbig2SymbolDict *SDNEWSYMS = NULL;
- Jbig2SymbolDict *SDEXSYMS = NULL;
- uint32_t HCHEIGHT;
- uint32_t NSYMSDECODED;
- uint32_t SYMWIDTH, TOTWIDTH;
- uint32_t HCFIRSTSYM;
- uint32_t *SDNEWSYMWIDTHS = NULL;
- int SBSYMCODELEN = 0;
- Jbig2WordStream *ws = NULL;
- Jbig2HuffmanState *hs = NULL;
- Jbig2HuffmanTable *SDHUFFRDX = NULL;
- Jbig2HuffmanTable *SBHUFFRSIZE = NULL;
- Jbig2ArithState *as = NULL;
- Jbig2ArithIntCtx *IADH = NULL;
- Jbig2ArithIntCtx *IADW = NULL;
- Jbig2ArithIntCtx *IAEX = NULL;
- Jbig2ArithIntCtx *IAAI = NULL;
- Jbig2ArithIaidCtx *IAID = NULL;
- Jbig2ArithIntCtx *IARDX = NULL;
- Jbig2ArithIntCtx *IARDY = NULL;
- int code = 0;
- Jbig2SymbolDict **refagg_dicts = NULL;
- int n_refagg_dicts = 1;
+ Jbig2SymbolDict *SDNEWSYMS = NULL;
+ Jbig2SymbolDict *SDEXSYMS = NULL;
+ uint32_t HCHEIGHT;
+ uint32_t NSYMSDECODED;
+ uint32_t SYMWIDTH, TOTWIDTH;
+ uint32_t HCFIRSTSYM;
+ uint32_t *SDNEWSYMWIDTHS = NULL;
+ int SBSYMCODELEN = 0;
+ Jbig2WordStream *ws = NULL;
+ Jbig2HuffmanState *hs = NULL;
+ Jbig2HuffmanTable *SDHUFFRDX = NULL;
+ Jbig2HuffmanTable *SBHUFFRSIZE = NULL;
+ Jbig2ArithState *as = NULL;
+ Jbig2ArithIntCtx *IADH = NULL;
+ Jbig2ArithIntCtx *IADW = NULL;
+ Jbig2ArithIntCtx *IAEX = NULL;
+ Jbig2ArithIntCtx *IAAI = NULL;
+ Jbig2ArithIaidCtx *IAID = NULL;
+ Jbig2ArithIntCtx *IARDX = NULL;
+ Jbig2ArithIntCtx *IARDY = NULL;
+ int code = 0;
+ Jbig2SymbolDict **refagg_dicts = NULL;
+ int n_refagg_dicts = 1;
- Jbig2TextRegionParams *tparams = NULL;
+ Jbig2TextRegionParams *tparams = NULL;
- /* 6.5.5 (3) */
- HCHEIGHT = 0;
- NSYMSDECODED = 0;
+ /* 6.5.5 (3) */
+ HCHEIGHT = 0;
+ NSYMSDECODED = 0;
- ws = jbig2_word_stream_buf_new(ctx, data, size);
- if (ws == NULL)
- {
- jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number,
- "failed to allocate ws in jbig2_decode_symbol_dict");
- return NULL;
- }
+ ws = jbig2_word_stream_buf_new(ctx, data, size);
+ if (ws == NULL) {
+ jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate ws in jbig2_decode_symbol_dict");
+ return NULL;
+ }
- as = jbig2_arith_new(ctx, ws);
- if (as == NULL)
- {
- jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number,
- "failed to allocate as in jbig2_decode_symbol_dict");
- jbig2_word_stream_buf_free(ctx, ws);
- return NULL;
- }
+ as = jbig2_arith_new(ctx, ws);
+ if (as == NULL) {
+ jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate as in jbig2_decode_symbol_dict");
+ jbig2_word_stream_buf_free(ctx, ws);
+ return NULL;
+ }
- if (!params->SDHUFF) {
- IADH = jbig2_arith_int_ctx_new(ctx);
- IADW = jbig2_arith_int_ctx_new(ctx);
- IAEX = jbig2_arith_int_ctx_new(ctx);
- IAAI = jbig2_arith_int_ctx_new(ctx);
- if ((IADH == NULL) || (IADW == NULL) || (IAEX == NULL) || (IAAI == NULL))
- {
- jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number,
- "failed to allocate storage for symbol bitmap");
- goto cleanup1;
- }
- if (params->SDREFAGG) {
- int64_t tmp = params->SDNUMINSYMS + params->SDNUMNEWSYMS;
- for (SBSYMCODELEN = 0; ((int64_t)1 << SBSYMCODELEN) < tmp; SBSYMCODELEN++);
- IAID = jbig2_arith_iaid_ctx_new(ctx, SBSYMCODELEN);
- IARDX = jbig2_arith_int_ctx_new(ctx);
- IARDY = jbig2_arith_int_ctx_new(ctx);
- if ((IAID == NULL) || (IARDX == NULL) || (IARDY == NULL))
- {
- jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number,
- "failed to allocate storage for symbol bitmap");
- goto cleanup2;
- }
- }
- } else {
- jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
- "huffman coded symbol dictionary");
- hs = jbig2_huffman_new(ctx, ws);
- SDHUFFRDX = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_O);
- SBHUFFRSIZE = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_A);
- if ( (hs == NULL) || (SDHUFFRDX == NULL) || (SBHUFFRSIZE == NULL))
- {
- jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number,
- "failed to allocate storage for symbol bitmap");
- goto cleanup2;
- }
- if (!params->SDREFAGG) {
- SDNEWSYMWIDTHS = jbig2_new(ctx, uint32_t, params->SDNUMNEWSYMS);
- if (SDNEWSYMWIDTHS == NULL) {
- jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "could not allocate storage for (%u) symbol widths",
- params->SDNUMNEWSYMS);
- goto cleanup2;
- }
- }
- }
+ if (!params->SDHUFF) {
+ IADH = jbig2_arith_int_ctx_new(ctx);
+ IADW = jbig2_arith_int_ctx_new(ctx);
+ IAEX = jbig2_arith_int_ctx_new(ctx);
+ IAAI = jbig2_arith_int_ctx_new(ctx);
+ if ((IADH == NULL) || (IADW == NULL) || (IAEX == NULL) || (IAAI == NULL)) {
+ jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate storage for symbol bitmap");
+ goto cleanup1;
+ }
+ if (params->SDREFAGG) {
+ int64_t tmp = params->SDNUMINSYMS + params->SDNUMNEWSYMS;
- SDNEWSYMS = jbig2_sd_new(ctx, params->SDNUMNEWSYMS);
- if (SDNEWSYMS == NULL) {
- jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number,
- "could not allocate storage for (%u) new symbols",
- params->SDNUMNEWSYMS);
- goto cleanup2;
- }
+ for (SBSYMCODELEN = 0; ((int64_t) 1 << SBSYMCODELEN) < tmp; SBSYMCODELEN++);
+ IAID = jbig2_arith_iaid_ctx_new(ctx, SBSYMCODELEN);
+ IARDX = jbig2_arith_int_ctx_new(ctx);
+ IARDY = jbig2_arith_int_ctx_new(ctx);
+ if ((IAID == NULL) || (IARDX == NULL) || (IARDY == NULL)) {
+ jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate storage for symbol bitmap");
+ goto cleanup2;
+ }
+ }
+ } else {
+ jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "huffman coded symbol dictionary");
+ hs = jbig2_huffman_new(ctx, ws);
+ SDHUFFRDX = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_O);
+ SBHUFFRSIZE = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_A);
+ if ((hs == NULL) || (SDHUFFRDX == NULL) || (SBHUFFRSIZE == NULL)) {
+ jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate storage for symbol bitmap");
+ goto cleanup2;
+ }
+ if (!params->SDREFAGG) {
+ SDNEWSYMWIDTHS = jbig2_new(ctx, uint32_t, params->SDNUMNEWSYMS);
+ if (SDNEWSYMWIDTHS == NULL) {
+ jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "could not allocate storage for (%u) symbol widths", params->SDNUMNEWSYMS);
+ goto cleanup2;
+ }
+ }
+ }
- /* 6.5.5 (4a) */
- while (NSYMSDECODED < params->SDNUMNEWSYMS) {
- int32_t HCDH, DW;
+ SDNEWSYMS = jbig2_sd_new(ctx, params->SDNUMNEWSYMS);
+ if (SDNEWSYMS == NULL) {
+ jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "could not allocate storage for (%u) new symbols", params->SDNUMNEWSYMS);
+ goto cleanup2;
+ }
- /* 6.5.6 */
- if (params->SDHUFF) {
- HCDH = jbig2_huffman_get(hs, params->SDHUFFDH, &code);
- } else {
- code = jbig2_arith_int_decode(IADH, as, &HCDH);
- }
+ /* 6.5.5 (4a) */
+ while (NSYMSDECODED < params->SDNUMNEWSYMS) {
+ int32_t HCDH, DW;
- if (code != 0) {
- jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number,
- "error or OOB decoding height class delta (%d)\n", code);
- }
+ /* 6.5.6 */
+ if (params->SDHUFF) {
+ HCDH = jbig2_huffman_get(hs, params->SDHUFFDH, &code);
+ } else {
+ code = jbig2_arith_int_decode(IADH, as, &HCDH);
+ }
- if (!params->SDHUFF && jbig2_arith_has_reached_marker(as)) {
- code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "prevent DOS while decoding height classes");
- goto cleanup2;
- }
+ if (code != 0) {
+ jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "error or OOB decoding height class delta (%d)\n", code);
+ }
- /* 6.5.5 (4b) */
- HCHEIGHT = HCHEIGHT + HCDH;
- SYMWIDTH = 0;
- TOTWIDTH = 0;
- HCFIRSTSYM = NSYMSDECODED;
+ if (!params->SDHUFF && jbig2_arith_has_reached_marker(as)) {
+ code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "prevent DOS while decoding height classes");
+ goto cleanup2;
+ }
- if ((int32_t)HCHEIGHT < 0) {
- code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "Invalid HCHEIGHT value");
- goto cleanup2;
- }
+ /* 6.5.5 (4b) */
+ HCHEIGHT = HCHEIGHT + HCDH;
+ SYMWIDTH = 0;
+ TOTWIDTH = 0;
+ HCFIRSTSYM = NSYMSDECODED;
+
+ if ((int32_t) HCHEIGHT < 0) {
+ code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "Invalid HCHEIGHT value");
+ goto cleanup2;
+ }
#ifdef JBIG2_DEBUG
- jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
- "HCHEIGHT = %d", HCHEIGHT);
+ jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "HCHEIGHT = %d", HCHEIGHT);
#endif
- jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
- "decoding height class %d with %d syms decoded", HCHEIGHT, NSYMSDECODED);
+ jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "decoding height class %d with %d syms decoded", HCHEIGHT, NSYMSDECODED);
- for (;;) {
- /* 6.5.7 */
- if (params->SDHUFF) {
- DW = jbig2_huffman_get(hs, params->SDHUFFDW, &code);
- } else {
- code = jbig2_arith_int_decode(IADW, as, &DW);
- }
- if (code < 0) goto cleanup4;
+ for (;;) {
+ /* 6.5.7 */
+ if (params->SDHUFF) {
+ DW = jbig2_huffman_get(hs, params->SDHUFFDW, &code);
+ } else {
+ code = jbig2_arith_int_decode(IADW, as, &DW);
+ }
+ if (code < 0)
+ goto cleanup4;
- /* 6.5.5 (4c.i) */
- if (code == 1) {
- jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
- " OOB signals end of height class %d", HCHEIGHT);
- break;
- }
+ /* 6.5.5 (4c.i) */
+ if (code == 1) {
+ jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, " OOB signals end of height class %d", HCHEIGHT);
+ break;
+ }
- /* check for broken symbol table */
- if (NSYMSDECODED >= params->SDNUMNEWSYMS)
- {
- jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "No OOB signalling end of height class %d", HCHEIGHT);
- goto cleanup4;
- }
+ /* check for broken symbol table */
+ if (NSYMSDECODED >= params->SDNUMNEWSYMS) {
+ jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "No OOB signalling end of height class %d", HCHEIGHT);
+ goto cleanup4;
+ }
- SYMWIDTH = SYMWIDTH + DW;
- TOTWIDTH = TOTWIDTH + SYMWIDTH;
- if ((int32_t)SYMWIDTH < 0) {
- code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "Invalid SYMWIDTH value (%d) at symbol %d", SYMWIDTH, NSYMSDECODED+1);
- goto cleanup4;
- }
+ SYMWIDTH = SYMWIDTH + DW;
+ TOTWIDTH = TOTWIDTH + SYMWIDTH;
+ if ((int32_t) SYMWIDTH < 0) {
+ code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "Invalid SYMWIDTH value (%d) at symbol %d", SYMWIDTH, NSYMSDECODED + 1);
+ goto cleanup4;
+ }
#ifdef JBIG2_DEBUG
- jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
- "SYMWIDTH = %d TOTWIDTH = %d", SYMWIDTH, TOTWIDTH);
+ jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "SYMWIDTH = %d TOTWIDTH = %d", SYMWIDTH, TOTWIDTH);
#endif
- /* 6.5.5 (4c.ii) */
- if (!params->SDHUFF || params->SDREFAGG) {
+ /* 6.5.5 (4c.ii) */
+ if (!params->SDHUFF || params->SDREFAGG) {
#ifdef JBIG2_DEBUG
- jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
- "SDHUFF = %d; SDREFAGG = %d", params->SDHUFF, params->SDREFAGG);
+ jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "SDHUFF = %d; SDREFAGG = %d", params->SDHUFF, params->SDREFAGG);
#endif
- /* 6.5.8 */
- if (!params->SDREFAGG) {
- Jbig2GenericRegionParams region_params;
- int sdat_bytes;
- Jbig2Image *image;
+ /* 6.5.8 */
+ if (!params->SDREFAGG) {
+ Jbig2GenericRegionParams region_params;
+ int sdat_bytes;
+ Jbig2Image *image;
- /* Table 16 */
- region_params.MMR = 0;
- region_params.GBTEMPLATE = params->SDTEMPLATE;
- region_params.TPGDON = 0;
- region_params.USESKIP = 0;
- sdat_bytes = params->SDTEMPLATE == 0 ? 8 : 2;
- memcpy(region_params.gbat, params->sdat, sdat_bytes);
+ /* Table 16 */
+ region_params.MMR = 0;
+ region_params.GBTEMPLATE = params->SDTEMPLATE;
+ region_params.TPGDON = 0;
+ region_params.USESKIP = 0;
+ sdat_bytes = params->SDTEMPLATE == 0 ? 8 : 2;
+ memcpy(region_params.gbat, params->sdat, sdat_bytes);
- image = jbig2_image_new(ctx, SYMWIDTH, HCHEIGHT);
- if (image == NULL)
- {
- code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "failed to allocate image in jbig2_decode_symbol_dict");
- goto cleanup4;
- }
+ image = jbig2_image_new(ctx, SYMWIDTH, HCHEIGHT);
+ if (image == NULL) {
+ code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to allocate image in jbig2_decode_symbol_dict");
+ goto cleanup4;
+ }
- code = jbig2_decode_generic_region(ctx, segment, ®ion_params,
- as, image, GB_stats);
- if (code < 0) {
- jbig2_image_release(ctx, image);
- goto cleanup4;
- }
+ code = jbig2_decode_generic_region(ctx, segment, ®ion_params, as, image, GB_stats);
+ if (code < 0) {
+ jbig2_image_release(ctx, image);
+ goto cleanup4;
+ }
- SDNEWSYMS->glyphs[NSYMSDECODED] = image;
- } else {
- /* 6.5.8.2 refinement/aggregate symbol */
- uint32_t REFAGGNINST;
+ SDNEWSYMS->glyphs[NSYMSDECODED] = image;
+ } else {
+ /* 6.5.8.2 refinement/aggregate symbol */
+ uint32_t REFAGGNINST;
- if (params->SDHUFF) {
- REFAGGNINST = jbig2_huffman_get(hs, params->SDHUFFAGGINST, &code);
- } else {
- code = jbig2_arith_int_decode(IAAI, as, (int32_t*)&REFAGGNINST);
- }
- if (code || (int32_t)REFAGGNINST <= 0) {
- code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "invalid number of symbols or OOB in aggregate glyph");
- goto cleanup4;
- }
+ if (params->SDHUFF) {
+ REFAGGNINST = jbig2_huffman_get(hs, params->SDHUFFAGGINST, &code);
+ } else {
+ code = jbig2_arith_int_decode(IAAI, as, (int32_t *) & REFAGGNINST);
+ }
+ if (code || (int32_t) REFAGGNINST <= 0) {
+ code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "invalid number of symbols or OOB in aggregate glyph");
+ goto cleanup4;
+ }
- jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
- "aggregate symbol coding (%d instances)", REFAGGNINST);
+ jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "aggregate symbol coding (%d instances)", REFAGGNINST);
- if (REFAGGNINST > 1) {
- Jbig2Image *image;
- int i;
+ if (REFAGGNINST > 1) {
+ Jbig2Image *image;
+ int i;
- if (tparams == NULL)
- {
- /* First time through, we need to initialise the */
- /* various tables for Huffman or adaptive encoding */
- /* as well as the text region parameters structure */
- refagg_dicts = jbig2_new(ctx, Jbig2SymbolDict*, n_refagg_dicts);
- if (refagg_dicts == NULL) {
- code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "Out of memory allocating dictionary array");
- goto cleanup4;
- }
- refagg_dicts[0] = jbig2_sd_new(ctx, params->SDNUMINSYMS + params->SDNUMNEWSYMS);
- if (refagg_dicts[0] == NULL) {
- code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "Out of memory allocating symbol dictionary");
- jbig2_free(ctx->allocator, refagg_dicts);
- goto cleanup4;
- }
- for (i=0;i < params->SDNUMINSYMS;i++)
- {
- refagg_dicts[0]->glyphs[i] = jbig2_image_clone(ctx, params->SDINSYMS->glyphs[i]);
- }
+ if (tparams == NULL) {
+ /* First time through, we need to initialise the */
+ /* various tables for Huffman or adaptive encoding */
+ /* as well as the text region parameters structure */
+ refagg_dicts = jbig2_new(ctx, Jbig2SymbolDict *, n_refagg_dicts);
+ if (refagg_dicts == NULL) {
+ code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "Out of memory allocating dictionary array");
+ goto cleanup4;
+ }
+ refagg_dicts[0] = jbig2_sd_new(ctx, params->SDNUMINSYMS + params->SDNUMNEWSYMS);
+ if (refagg_dicts[0] == NULL) {
+ code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "Out of memory allocating symbol dictionary");
+ jbig2_free(ctx->allocator, refagg_dicts);
+ goto cleanup4;
+ }
+ for (i = 0; i < params->SDNUMINSYMS; i++) {
+ refagg_dicts[0]->glyphs[i] = jbig2_image_clone(ctx, params->SDINSYMS->glyphs[i]);
+ }
- tparams = jbig2_new(ctx, Jbig2TextRegionParams, 1);
- if (tparams == NULL) {
- code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "Out of memory creating text region params");
- goto cleanup4;
- }
- if (!params->SDHUFF) {
- /* Values from Table 17, section 6.5.8.2 (2) */
- tparams->IADT = jbig2_arith_int_ctx_new(ctx);
- tparams->IAFS = jbig2_arith_int_ctx_new(ctx);
- tparams->IADS = jbig2_arith_int_ctx_new(ctx);
- tparams->IAIT = jbig2_arith_int_ctx_new(ctx);
- /* Table 31 */
- for (SBSYMCODELEN = 0; (1 << SBSYMCODELEN) <
- (int)(params->SDNUMINSYMS + params->SDNUMNEWSYMS); SBSYMCODELEN++);
- tparams->IAID = jbig2_arith_iaid_ctx_new(ctx, SBSYMCODELEN);
- tparams->IARI = jbig2_arith_int_ctx_new(ctx);
- tparams->IARDW = jbig2_arith_int_ctx_new(ctx);
- tparams->IARDH = jbig2_arith_int_ctx_new(ctx);
- tparams->IARDX = jbig2_arith_int_ctx_new(ctx);
- tparams->IARDY = jbig2_arith_int_ctx_new(ctx);
- } else {
- tparams->SBHUFFFS = jbig2_build_huffman_table(ctx,
- &jbig2_huffman_params_F); /* Table B.6 */
- tparams->SBHUFFDS = jbig2_build_huffman_table(ctx,
- &jbig2_huffman_params_H); /* Table B.8 */
- tparams->SBHUFFDT = jbig2_build_huffman_table(ctx,
- &jbig2_huffman_params_K); /* Table B.11 */
- tparams->SBHUFFRDW = jbig2_build_huffman_table(ctx,
- &jbig2_huffman_params_O); /* Table B.15 */
- tparams->SBHUFFRDH = jbig2_build_huffman_table(ctx,
- &jbig2_huffman_params_O); /* Table B.15 */
- tparams->SBHUFFRDX = jbig2_build_huffman_table(ctx,
- &jbig2_huffman_params_O); /* Table B.15 */
- tparams->SBHUFFRDY = jbig2_build_huffman_table(ctx,
- &jbig2_huffman_params_O); /* Table B.15 */
- }
- tparams->SBHUFF = params->SDHUFF;
- tparams->SBREFINE = 1;
- tparams->SBSTRIPS = 1;
- tparams->SBDEFPIXEL = 0;
- tparams->SBCOMBOP = JBIG2_COMPOSE_OR;
- tparams->TRANSPOSED = 0;
- tparams->REFCORNER = JBIG2_CORNER_TOPLEFT;
- tparams->SBDSOFFSET = 0;
- tparams->SBRTEMPLATE = params->SDRTEMPLATE;
- }
- tparams->SBNUMINSTANCES = REFAGGNINST;
+ tparams = jbig2_new(ctx, Jbig2TextRegionParams, 1);
+ if (tparams == NULL) {
+ code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "Out of memory creating text region params");
+ goto cleanup4;
+ }
+ if (!params->SDHUFF) {
+ /* Values from Table 17, section 6.5.8.2 (2) */
+ tparams->IADT = jbig2_arith_int_ctx_new(ctx);
+ tparams->IAFS = jbig2_arith_int_ctx_new(ctx);
+ tparams->IADS = jbig2_arith_int_ctx_new(ctx);
+ tparams->IAIT = jbig2_arith_int_ctx_new(ctx);
+ /* Table 31 */
+ for (SBSYMCODELEN = 0; (1 << SBSYMCODELEN) < (int)(params->SDNUMINSYMS + params->SDNUMNEWSYMS); SBSYMCODELEN++);
+ tparams->IAID = jbig2_arith_iaid_ctx_new(ctx, SBSYMCODELEN);
+ tparams->IARI = jbig2_arith_int_ctx_new(ctx);
+ tparams->IARDW = jbig2_arith_int_ctx_new(ctx);
+ tparams->IARDH = jbig2_arith_int_ctx_new(ctx);
+ tparams->IARDX = jbig2_arith_int_ctx_new(ctx);
+ tparams->IARDY = jbig2_arith_int_ctx_new(ctx);
+ } else {
+ tparams->SBHUFFFS = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_F); /* Table B.6 */
+ tparams->SBHUFFDS = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_H); /* Table B.8 */
+ tparams->SBHUFFDT = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_K); /* Table B.11 */
+ tparams->SBHUFFRDW = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_O); /* Table B.15 */
+ tparams->SBHUFFRDH = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_O); /* Table B.15 */
+ tparams->SBHUFFRDX = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_O); /* Table B.15 */
+ tparams->SBHUFFRDY = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_O); /* Table B.15 */
+ }
+ tparams->SBHUFF = params->SDHUFF;
+ tparams->SBREFINE = 1;
+ tparams->SBSTRIPS = 1;
+ tparams->SBDEFPIXEL = 0;
+ tparams->SBCOMBOP = JBIG2_COMPOSE_OR;
+ tparams->TRANSPOSED = 0;
+ tparams->REFCORNER = JBIG2_CORNER_TOPLEFT;
+ tparams->SBDSOFFSET = 0;
+ tparams->SBRTEMPLATE = params->SDRTEMPLATE;
+ }
+ tparams->SBNUMINSTANCES = REFAGGNINST;
- image = jbig2_image_new(ctx, SYMWIDTH, HCHEIGHT);
- if (image == NULL) {
- code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "Out of memory creating symbol image");
- goto cleanup4;
- }
+ image = jbig2_image_new(ctx, SYMWIDTH, HCHEIGHT);
+ if (image == NULL) {
+ code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "Out of memory creating symbol image");
+ goto cleanup4;
+ }
- /* multiple symbols are handled as a text region */
- jbig2_decode_text_region(ctx, segment, tparams, (const Jbig2SymbolDict * const *)refagg_dicts,
- n_refagg_dicts, image, data, size, GR_stats, as, ws);
+ /* multiple symbols are handled as a text region */
+ jbig2_decode_text_region(ctx, segment, tparams, (const Jbig2SymbolDict * const *)refagg_dicts,
+ n_refagg_dicts, image, data, size, GR_stats, as, ws);
- SDNEWSYMS->glyphs[NSYMSDECODED] = image;
- refagg_dicts[0]->glyphs[params->SDNUMINSYMS + NSYMSDECODED] = jbig2_image_clone(ctx, SDNEWSYMS->glyphs[NSYMSDECODED]);
- } else {
- /* 6.5.8.2.2 */
- /* bool SBHUFF = params->SDHUFF; */
- Jbig2RefinementRegionParams rparams;
- Jbig2Image *image;
- uint32_t ID;
- int32_t RDX, RDY;
- int BMSIZE = 0;
- int ninsyms = params->SDNUMINSYMS;
- int code1 = 0;
- int code2 = 0;
- int code3 = 0;
- int code4 = 0;
+ SDNEWSYMS->glyphs[NSYMSDECODED] = image;
+ refagg_dicts[0]->glyphs[params->SDNUMINSYMS + NSYMSDECODED] = jbig2_image_clone(ctx, SDNEWSYMS->glyphs[NSYMSDECODED]);
+ } else {
+ /* 6.5.8.2.2 */
+ /* bool SBHUFF = params->SDHUFF; */
+ Jbig2RefinementRegionParams rparams;
+ Jbig2Image *image;
+ uint32_t ID;
+ int32_t RDX, RDY;
+ int BMSIZE = 0;
+ int ninsyms = params->SDNUMINSYMS;
+ int code1 = 0;
+ int code2 = 0;
+ int code3 = 0;
+ int code4 = 0;
- /* 6.5.8.2.2 (2, 3, 4, 5) */
- if (params->SDHUFF) {
- ID = jbig2_huffman_get_bits(hs, SBSYMCODELEN, &code4);
- RDX = jbig2_huffman_get(hs, SDHUFFRDX, &code1);
- RDY = jbig2_huffman_get(hs, SDHUFFRDX, &code2);
- BMSIZE = jbig2_huffman_get(hs, SBHUFFRSIZE, &code3);
- jbig2_huffman_skip(hs);
- } else {
- code1 = jbig2_arith_iaid_decode(IAID, as, (int32_t*)&ID);
- code2 = jbig2_arith_int_decode(IARDX, as, &RDX);
- code3 = jbig2_arith_int_decode(IARDY, as, &RDY);
- }
+ /* 6.5.8.2.2 (2, 3, 4, 5) */
+ if (params->SDHUFF) {
+ ID = jbig2_huffman_get_bits(hs, SBSYMCODELEN, &code4);
+ RDX = jbig2_huffman_get(hs, SDHUFFRDX, &code1);
+ RDY = jbig2_huffman_get(hs, SDHUFFRDX, &code2);
+ BMSIZE = jbig2_huffman_get(hs, SBHUFFRSIZE, &code3);
+ jbig2_huffman_skip(hs);
+ } else {
+ code1 = jbig2_arith_iaid_decode(IAID, as, (int32_t *) & ID);
+ code2 = jbig2_arith_int_decode(IARDX, as, &RDX);
+ code3 = jbig2_arith_int_decode(IARDY, as, &RDY);
+ }
- if ((code1 < 0) || (code2 < 0) || (code3 < 0) || (code4 < 0))
- {
- code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL,
- segment->number, "failed to decode data");
- goto cleanup4;
- }
+ if ((code1 < 0) || (code2 < 0) || (code3 < 0) || (code4 < 0)) {
+ code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to decode data");
+ goto cleanup4;
+ }
- if (ID >= ninsyms+NSYMSDECODED) {
- code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "refinement references unknown symbol %d", ID);
- goto cleanup4;
- }
+ if (ID >= ninsyms + NSYMSDECODED) {
+ code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "refinement references unknown symbol %d", ID);
+ goto cleanup4;
+ }
- jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
- "symbol is a refinement of id %d with the "
- "refinement applied at (%d,%d)", ID, RDX, RDY);
+ jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
+ "symbol is a refinement of id %d with the " "refinement applied at (%d,%d)", ID, RDX, RDY);
- image = jbig2_image_new(ctx, SYMWIDTH, HCHEIGHT);
- if (image == NULL) {
- code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "Out of memory creating symbol image");
- goto cleanup4;
- }
+ image = jbig2_image_new(ctx, SYMWIDTH, HCHEIGHT);
+ if (image == NULL) {
+ code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "Out of memory creating symbol image");
+ goto cleanup4;
+ }
- /* Table 18 */
- rparams.GRTEMPLATE = params->SDRTEMPLATE;
- rparams.reference = (ID < ninsyms) ?
- params->SDINSYMS->glyphs[ID] :
- SDNEWSYMS->glyphs[ID-ninsyms];
- /* SumatraPDF: fail on missing glyphs */
- if (rparams.reference == NULL) {
- code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "missing glyph %d/%d!", ID, ninsyms);
- jbig2_image_release(ctx, image);
- goto cleanup4;
- }
- rparams.DX = RDX;
- rparams.DY = RDY;
- rparams.TPGRON = 0;
- memcpy(rparams.grat, params->sdrat, 4);
- code = jbig2_decode_refinement_region(ctx, segment,
- &rparams, as, image, GR_stats);
- if (code < 0) goto cleanup4;
+ /* Table 18 */
+ rparams.GRTEMPLATE = params->SDRTEMPLATE;
+ rparams.reference = (ID < ninsyms) ? params->SDINSYMS->glyphs[ID] : SDNEWSYMS->glyphs[ID - ninsyms];
+ /* SumatraPDF: fail on missing glyphs */
+ if (rparams.reference == NULL) {
+ code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "missing glyph %d/%d!", ID, ninsyms);
+ jbig2_image_release(ctx, image);
+ goto cleanup4;
+ }
+ rparams.DX = RDX;
+ rparams.DY = RDY;
+ rparams.TPGRON = 0;
+ memcpy(rparams.grat, params->sdrat, 4);
+ code = jbig2_decode_refinement_region(ctx, segment, &rparams, as, image, GR_stats);
+ if (code < 0)
+ goto cleanup4;
- SDNEWSYMS->glyphs[NSYMSDECODED] = image;
+ SDNEWSYMS->glyphs[NSYMSDECODED] = image;
- /* 6.5.8.2.2 (7) */
- if (params->SDHUFF) {
- if (BMSIZE == 0) BMSIZE = image->height * image->stride;
- jbig2_huffman_advance(hs, BMSIZE);
- }
- }
- }
+ /* 6.5.8.2.2 (7) */
+ if (params->SDHUFF) {
+ if (BMSIZE == 0)
+ BMSIZE = image->height * image->stride;
+ jbig2_huffman_advance(hs, BMSIZE);
+ }
+ }
+ }
#ifdef OUTPUT_PBM
- {
- char name[64];
- FILE *out;
- snprintf(name, 64, "sd.%04d.%04d.pbm",
- segment->number, NSYMSDECODED);
- out = fopen(name, "wb");
+ {
+ char name[64];
+ FILE *out;
+
+ snprintf(name, 64, "sd.%04d.%04d.pbm", segment->number, NSYMSDECODED);
+ out = fopen(name, "wb");
jbig2_image_write_pbm(SDNEWSYMS->glyphs[NSYMSDECODED], out);
- jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
- "writing out glyph as '%s' ...", name);
- fclose(out);
- }
+ jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "writing out glyph as '%s' ...", name);
+ fclose(out);
+ }
#endif
- }
+ }
- /* 6.5.5 (4c.iii) */
- if (params->SDHUFF && !params->SDREFAGG) {
- SDNEWSYMWIDTHS[NSYMSDECODED] = SYMWIDTH;
- }
+ /* 6.5.5 (4c.iii) */
+ if (params->SDHUFF && !params->SDREFAGG) {
+ SDNEWSYMWIDTHS[NSYMSDECODED] = SYMWIDTH;
+ }
- /* 6.5.5 (4c.iv) */
- NSYMSDECODED = NSYMSDECODED + 1;
+ /* 6.5.5 (4c.iv) */
+ NSYMSDECODED = NSYMSDECODED + 1;
- jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
- "decoded symbol %u of %u (%ux%u)",
- NSYMSDECODED, params->SDNUMNEWSYMS,
- SYMWIDTH, HCHEIGHT);
+ jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "decoded symbol %u of %u (%ux%u)", NSYMSDECODED, params->SDNUMNEWSYMS, SYMWIDTH, HCHEIGHT);
- } /* end height class decode loop */
+ } /* end height class decode loop */
- /* 6.5.5 (4d) */
- if (params->SDHUFF && !params->SDREFAGG) {
- /* 6.5.9 */
- Jbig2Image *image;
- int BMSIZE = jbig2_huffman_get(hs, params->SDHUFFBMSIZE, &code);
- int j, x;
+ /* 6.5.5 (4d) */
+ if (params->SDHUFF && !params->SDREFAGG) {
+ /* 6.5.9 */
+ Jbig2Image *image;
+ int BMSIZE = jbig2_huffman_get(hs, params->SDHUFFBMSIZE, &code);
+ int j, x;
- if (code || (BMSIZE < 0)) {
- jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "error decoding size of collective bitmap!");
- goto cleanup4;
- }
+ if (code || (BMSIZE < 0)) {
+ jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "error decoding size of collective bitmap!");
+ goto cleanup4;
+ }
- /* skip any bits before the next byte boundary */
- jbig2_huffman_skip(hs);
+ /* skip any bits before the next byte boundary */
+ jbig2_huffman_skip(hs);
- image = jbig2_image_new(ctx, TOTWIDTH, HCHEIGHT);
- if (image == NULL) {
- jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "could not allocate collective bitmap image!");
- goto cleanup4;
- }
+ image = jbig2_image_new(ctx, TOTWIDTH, HCHEIGHT);
+ if (image == NULL) {
+ jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "could not allocate collective bitmap image!");
+ goto cleanup4;
+ }
- if (BMSIZE == 0) {
- /* if BMSIZE == 0 bitmap is uncompressed */
- const byte *src = data + jbig2_huffman_offset(hs);
- const int stride = (image->width >> 3) +
- ((image->width & 7) ? 1 : 0);
- byte *dst = image->data;
+ if (BMSIZE == 0) {
+ /* if BMSIZE == 0 bitmap is uncompressed */
+ const byte *src = data + jbig2_huffman_offset(hs);
+ const int stride = (image->width >> 3) + ((image->width & 7) ? 1 : 0);
+ byte *dst = image->data;
- /* SumatraPDF: prevent read access violation */
- if (size - jbig2_huffman_offset(hs) < image->height * stride) {
- jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "not enough data for decoding (%d/%d)", image->height * stride, size - jbig2_huffman_offset(hs));
- jbig2_image_release(ctx, image);
- goto cleanup4;
- }
+ /* SumatraPDF: prevent read access violation */
+ if (size - jbig2_huffman_offset(hs) < image->height * stride) {
+ jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "not enough data for decoding (%d/%d)", image->height * stride,
+ size - jbig2_huffman_offset(hs));
+ jbig2_image_release(ctx, image);
+ goto cleanup4;
+ }
- BMSIZE = image->height * stride;
- jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
- "reading %dx%d uncompressed bitmap"
- " for %d symbols (%d bytes)",
- image->width, image->height, NSYMSDECODED - HCFIRSTSYM, BMSIZE);
+ BMSIZE = image->height * stride;
+ jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
+ "reading %dx%d uncompressed bitmap" " for %d symbols (%d bytes)", image->width, image->height, NSYMSDECODED - HCFIRSTSYM, BMSIZE);
- for (j = 0; j < image->height; j++) {
- memcpy(dst, src, stride);
- dst += image->stride;
- src += stride;
- }
- } else {
- Jbig2GenericRegionParams rparams;
+ for (j = 0; j < image->height; j++) {
+ memcpy(dst, src, stride);
+ dst += image->stride;
+ src += stride;
+ }
+ } else {
+ Jbig2GenericRegionParams rparams;
- /* SumatraPDF: prevent read access violation */
- if (size - jbig2_huffman_offset(hs) < BMSIZE) {
- jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "not enough data for decoding (%d/%d)", BMSIZE, size - jbig2_huffman_offset(hs));
- jbig2_image_release(ctx, image);
- goto cleanup4;
- }
+ /* SumatraPDF: prevent read access violation */
+ if (size - jbig2_huffman_offset(hs) < BMSIZE) {
+ jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "not enough data for decoding (%d/%d)", BMSIZE, size - jbig2_huffman_offset(hs));
+ jbig2_image_release(ctx, image);
+ goto cleanup4;
+ }
- jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
- "reading %dx%d collective bitmap for %d symbols (%d bytes)",
- image->width, image->height, NSYMSDECODED - HCFIRSTSYM, BMSIZE);
+ jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
+ "reading %dx%d collective bitmap for %d symbols (%d bytes)", image->width, image->height, NSYMSDECODED - HCFIRSTSYM, BMSIZE);
- rparams.MMR = 1;
- code = jbig2_decode_generic_mmr(ctx, segment, &rparams,
- data + jbig2_huffman_offset(hs), BMSIZE, image);
- if (code) {
- jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "error decoding MMR bitmap image!");
- jbig2_image_release(ctx, image);
- goto cleanup4;
- }
- }
+ rparams.MMR = 1;
+ code = jbig2_decode_generic_mmr(ctx, segment, &rparams, data + jbig2_huffman_offset(hs), BMSIZE, image);
+ if (code) {
+ jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "error decoding MMR bitmap image!");
+ jbig2_image_release(ctx, image);
+ goto cleanup4;
+ }
+ }
- /* advance past the data we've just read */
- jbig2_huffman_advance(hs, BMSIZE);
+ /* advance past the data we've just read */
+ jbig2_huffman_advance(hs, BMSIZE);
- /* copy the collective bitmap into the symbol dictionary */
- x = 0;
- for (j = HCFIRSTSYM; j < NSYMSDECODED; j++) {
- Jbig2Image *glyph;
- glyph = jbig2_image_new(ctx, SDNEWSYMWIDTHS[j], HCHEIGHT);
- if (glyph == NULL)
- {
- jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "failed to copy the collective bitmap into symbol dictionary");
- jbig2_image_release(ctx, image);
- goto cleanup4;
- }
- jbig2_image_compose(ctx, glyph, image, -x, 0, JBIG2_COMPOSE_REPLACE);
- x += SDNEWSYMWIDTHS[j];
- SDNEWSYMS->glyphs[j] = glyph;
- }
- jbig2_image_release(ctx, image);
- }
+ /* copy the collective bitmap into the symbol dictionary */
+ x = 0;
+ for (j = HCFIRSTSYM; j < NSYMSDECODED; j++) {
+ Jbig2Image *glyph;
- } /* end of symbol decode loop */
+ glyph = jbig2_image_new(ctx, SDNEWSYMWIDTHS[j], HCHEIGHT);
+ if (glyph == NULL) {
+ jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to copy the collective bitmap into symbol dictionary");
+ jbig2_image_release(ctx, image);
+ goto cleanup4;
+ }
+ jbig2_image_compose(ctx, glyph, image, -x, 0, JBIG2_COMPOSE_REPLACE);
+ x += SDNEWSYMWIDTHS[j];
+ SDNEWSYMS->glyphs[j] = glyph;
+ }
+ jbig2_image_release(ctx, image);
+ }
- /* 6.5.10 */
- SDEXSYMS = jbig2_sd_new(ctx, params->SDNUMEXSYMS);
- if (SDEXSYMS == NULL)
- {
- jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "failed to allocate symbols exported from symbols dictionary");
- goto cleanup4;
- }
- else
- {
- int i = 0;
- int j = 0;
- int k;
- int exflag = 0;
- int64_t limit = params->SDNUMINSYMS + params->SDNUMNEWSYMS;
- int32_t exrunlength;
- int zerolength = 0;
+ } /* end of symbol decode loop */
- while (i < limit) {
- if (params->SDHUFF)
- exrunlength = jbig2_huffman_get(hs, SBHUFFRSIZE, &code);
- else
- code = jbig2_arith_int_decode(IAEX, as, &exrunlength);
- /* prevent infinite loop */
- zerolength = exrunlength > 0 ? 0 : zerolength + 1;
- if (code || (exrunlength > limit - i) || (exrunlength < 0) || (zerolength > 4) ||
- (exflag && (exrunlength > params->SDNUMEXSYMS - j))) {
- if (code)
- jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "failed to decode exrunlength for exported symbols");
- else if (exrunlength <= 0)
- jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "runlength too small in export symbol table (%d <= 0)\n", exrunlength);
- else
- jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "runlength too large in export symbol table (%d > %d - %d)\n",
- exrunlength, params->SDNUMEXSYMS, j);
- /* skip to the cleanup code and return SDEXSYMS = NULL */
- jbig2_sd_release(ctx, SDEXSYMS);
- SDEXSYMS = NULL;
- break;
- }
- for(k = 0; k < exrunlength; k++) {
- if (exflag) {
- SDEXSYMS->glyphs[j++] = (i < params->SDNUMINSYMS) ?
- jbig2_image_clone(ctx, params->SDINSYMS->glyphs[i]) :
- jbig2_image_clone(ctx, SDNEWSYMS->glyphs[i-params->SDNUMINSYMS]);
+ /* 6.5.10 */
+ SDEXSYMS = jbig2_sd_new(ctx, params->SDNUMEXSYMS);
+ if (SDEXSYMS == NULL) {
+ jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to allocate symbols exported from symbols dictionary");
+ goto cleanup4;
+ } else {
+ int i = 0;
+ int j = 0;
+ int k;
+ int exflag = 0;
+ int64_t limit = params->SDNUMINSYMS + params->SDNUMNEWSYMS;
+ int32_t exrunlength;
+ int zerolength = 0;
+
+ while (i < limit) {
+ if (params->SDHUFF)
+ exrunlength = jbig2_huffman_get(hs, SBHUFFRSIZE, &code);
+ else
+ code = jbig2_arith_int_decode(IAEX, as, &exrunlength);
+ /* prevent infinite loop */
+ zerolength = exrunlength > 0 ? 0 : zerolength + 1;
+ if (code || (exrunlength > limit - i) || (exrunlength < 0) || (zerolength > 4) || (exflag && (exrunlength > params->SDNUMEXSYMS - j))) {
+ if (code)
+ jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to decode exrunlength for exported symbols");
+ else if (exrunlength <= 0)
+ jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "runlength too small in export symbol table (%d <= 0)\n", exrunlength);
+ else
+ jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
+ "runlength too large in export symbol table (%d > %d - %d)\n", exrunlength, params->SDNUMEXSYMS, j);
+ /* skip to the cleanup code and return SDEXSYMS = NULL */
+ jbig2_sd_release(ctx, SDEXSYMS);
+ SDEXSYMS = NULL;
+ break;
+ }
+ for (k = 0; k < exrunlength; k++) {
+ if (exflag) {
+ SDEXSYMS->glyphs[j++] = (i < params->SDNUMINSYMS) ?
+ jbig2_image_clone(ctx, params->SDINSYMS->glyphs[i]) : jbig2_image_clone(ctx, SDNEWSYMS->glyphs[i - params->SDNUMINSYMS]);
+ }
+ i++;
+ }
+ exflag = !exflag;
}
- i++;
- }
- exflag = !exflag;
}
- }
cleanup4:
- if (tparams != NULL)
- {
- if (!params->SDHUFF)
- {
- jbig2_arith_int_ctx_free(ctx, tparams->IADT);
- jbig2_arith_int_ctx_free(ctx, tparams->IAFS);
- jbig2_arith_int_ctx_free(ctx, tparams->IADS);
- jbig2_arith_int_ctx_free(ctx, tparams->IAIT);
- jbig2_arith_iaid_ctx_free(ctx, tparams->IAID);
- jbig2_arith_int_ctx_free(ctx, tparams->IARI);
- jbig2_arith_int_ctx_free(ctx, tparams->IARDW);
- jbig2_arith_int_ctx_free(ctx, tparams->IARDH);
- jbig2_arith_int_ctx_free(ctx, tparams->IARDX);
- jbig2_arith_int_ctx_free(ctx, tparams->IARDY);
- }
- else
- {
- jbig2_release_huffman_table(ctx, tparams->SBHUFFFS);
- jbig2_release_huffman_table(ctx, tparams->SBHUFFDS);
- jbig2_release_huffman_table(ctx, tparams->SBHUFFDT);
- jbig2_release_huffman_table(ctx, tparams->SBHUFFRDX);
- jbig2_release_huffman_table(ctx, tparams->SBHUFFRDY);
- jbig2_release_huffman_table(ctx, tparams->SBHUFFRDW);
- jbig2_release_huffman_table(ctx, tparams->SBHUFFRDH);
- }
- jbig2_free(ctx->allocator, tparams);
- }
- if (refagg_dicts != NULL)
- {
- jbig2_sd_release(ctx, refagg_dicts[0]);
- jbig2_free(ctx->allocator, refagg_dicts);
- }
+ if (tparams != NULL) {
+ if (!params->SDHUFF) {
+ jbig2_arith_int_ctx_free(ctx, tparams->IADT);
+ jbig2_arith_int_ctx_free(ctx, tparams->IAFS);
+ jbig2_arith_int_ctx_free(ctx, tparams->IADS);
+ jbig2_arith_int_ctx_free(ctx, tparams->IAIT);
+ jbig2_arith_iaid_ctx_free(ctx, tparams->IAID);
+ jbig2_arith_int_ctx_free(ctx, tparams->IARI);
+ jbig2_arith_int_ctx_free(ctx, tparams->IARDW);
+ jbig2_arith_int_ctx_free(ctx, tparams->IARDH);
+ jbig2_arith_int_ctx_free(ctx, tparams->IARDX);
+ jbig2_arith_int_ctx_free(ctx, tparams->IARDY);
+ } else {
+ jbig2_release_huffman_table(ctx, tparams->SBHUFFFS);
+ jbig2_release_huffman_table(ctx, tparams->SBHUFFDS);
+ jbig2_release_huffman_table(ctx, tparams->SBHUFFDT);
+ jbig2_release_huffman_table(ctx, tparams->SBHUFFRDX);
+ jbig2_release_huffman_table(ctx, tparams->SBHUFFRDY);
+ jbig2_release_huffman_table(ctx, tparams->SBHUFFRDW);
+ jbig2_release_huffman_table(ctx, tparams->SBHUFFRDH);
+ }
+ jbig2_free(ctx->allocator, tparams);
+ }
+ if (refagg_dicts != NULL) {
+ jbig2_sd_release(ctx, refagg_dicts[0]);
+ jbig2_free(ctx->allocator, refagg_dicts);
+ }
cleanup2:
- jbig2_sd_release(ctx, SDNEWSYMS);
- if (params->SDHUFF && !params->SDREFAGG)
- {
- jbig2_free(ctx->allocator, SDNEWSYMWIDTHS);
- }
- jbig2_release_huffman_table(ctx, SDHUFFRDX);
- jbig2_release_huffman_table(ctx, SBHUFFRSIZE);
- jbig2_huffman_free(ctx, hs);
- jbig2_arith_iaid_ctx_free(ctx, IAID);
- jbig2_arith_int_ctx_free(ctx, IARDX);
- jbig2_arith_int_ctx_free(ctx, IARDY);
+ jbig2_sd_release(ctx, SDNEWSYMS);
+ if (params->SDHUFF && !params->SDREFAGG) {
+ jbig2_free(ctx->allocator, SDNEWSYMWIDTHS);
+ }
+ jbig2_release_huffman_table(ctx, SDHUFFRDX);
+ jbig2_release_huffman_table(ctx, SBHUFFRSIZE);
+ jbig2_huffman_free(ctx, hs);
+ jbig2_arith_iaid_ctx_free(ctx, IAID);
+ jbig2_arith_int_ctx_free(ctx, IARDX);
+ jbig2_arith_int_ctx_free(ctx, IARDY);
cleanup1:
- jbig2_word_stream_buf_free(ctx, ws);
- jbig2_free(ctx->allocator, as);
- jbig2_arith_int_ctx_free(ctx, IADH);
- jbig2_arith_int_ctx_free(ctx, IADW);
- jbig2_arith_int_ctx_free(ctx, IAEX);
- jbig2_arith_int_ctx_free(ctx, IAAI);
+ jbig2_word_stream_buf_free(ctx, ws);
+ jbig2_free(ctx->allocator, as);
+ jbig2_arith_int_ctx_free(ctx, IADH);
+ jbig2_arith_int_ctx_free(ctx, IADW);
+ jbig2_arith_int_ctx_free(ctx, IAEX);
+ jbig2_arith_int_ctx_free(ctx, IAAI);
- return SDEXSYMS;
+ return SDEXSYMS;
}
/* 7.4.2 */
int
-jbig2_symbol_dictionary(Jbig2Ctx *ctx, Jbig2Segment *segment,
- const byte *segment_data)
+jbig2_symbol_dictionary(Jbig2Ctx *ctx, Jbig2Segment *segment, const byte *segment_data)
{
- Jbig2SymbolDictParams params;
- uint16_t flags;
- int sdat_bytes;
- int offset;
- Jbig2ArithCx *GB_stats = NULL;
- Jbig2ArithCx *GR_stats = NULL;
- int table_index = 0;
- const Jbig2HuffmanParams *huffman_params;
+ Jbig2SymbolDictParams params;
+ uint16_t flags;
+ int sdat_bytes;
+ int offset;
+ Jbig2ArithCx *GB_stats = NULL;
+ Jbig2ArithCx *GR_stats = NULL;
+ int table_index = 0;
+ const Jbig2HuffmanParams *huffman_params;
- if (segment->data_length < 10)
- goto too_short;
+ if (segment->data_length < 10)
+ goto too_short;
- /* 7.4.2.1.1 */
- flags = jbig2_get_uint16(segment_data);
+ /* 7.4.2.1.1 */
+ flags = jbig2_get_uint16(segment_data);
- /* zero params to ease cleanup later */
- memset(¶ms, 0, sizeof(Jbig2SymbolDictParams));
+ /* zero params to ease cleanup later */
+ memset(¶ms, 0, sizeof(Jbig2SymbolDictParams));
- params.SDHUFF = flags & 1;
- params.SDREFAGG = (flags >> 1) & 1;
- params.SDTEMPLATE = (flags >> 10) & 3;
- params.SDRTEMPLATE = (flags >> 12) & 1;
+ params.SDHUFF = flags & 1;
+ params.SDREFAGG = (flags >> 1) & 1;
+ params.SDTEMPLATE = (flags >> 10) & 3;
+ params.SDRTEMPLATE = (flags >> 12) & 1;
- if (params.SDHUFF) {
- switch ((flags & 0x000c) >> 2) {
- case 0: /* Table B.4 */
- params.SDHUFFDH = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_D);
- break;
- case 1: /* Table B.5 */
- params.SDHUFFDH = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_E);
- break;
- case 3: /* Custom table from referred segment */
- huffman_params = jbig2_find_table(ctx, segment, table_index);
- if (huffman_params == NULL) {
- return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "Custom DH huffman table not found (%d)", table_index);
+ if (params.SDHUFF) {
+ switch ((flags & 0x000c) >> 2) {
+ case 0: /* Table B.4 */
+ params.SDHUFFDH = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_D);
+ break;
+ case 1: /* Table B.5 */
+ params.SDHUFFDH = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_E);
+ break;
+ case 3: /* Custom table from referred segment */
+ huffman_params = jbig2_find_table(ctx, segment, table_index);
+ if (huffman_params == NULL) {
+ return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "Custom DH huffman table not found (%d)", table_index);
+ }
+ params.SDHUFFDH = jbig2_build_huffman_table(ctx, huffman_params);
+ ++table_index;
+ break;
+ case 2:
+ default:
+ return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "symbol dictionary specified invalid huffman table");
}
- params.SDHUFFDH = jbig2_build_huffman_table(ctx, huffman_params);
- ++table_index;
- break;
- case 2:
- default:
- return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "symbol dictionary specified invalid huffman table");
- }
- if (params.SDHUFFDH == NULL)
- {
- jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "failed to allocate DH huffman table");
- goto cleanup;
- }
+ if (params.SDHUFFDH == NULL) {
+ jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to allocate DH huffman table");
+ goto cleanup;
+ }
- switch ((flags & 0x0030) >> 4) {
- case 0: /* Table B.2 */
- params.SDHUFFDW = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_B);
- break;
- case 1: /* Table B.3 */
- params.SDHUFFDW = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_C);
- break;
- case 3: /* Custom table from referred segment */
- huffman_params = jbig2_find_table(ctx, segment, table_index);
- if (huffman_params == NULL) {
- jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "Custom DW huffman table not found (%d)", table_index);
+ switch ((flags & 0x0030) >> 4) {
+ case 0: /* Table B.2 */
+ params.SDHUFFDW = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_B);
break;
+ case 1: /* Table B.3 */
+ params.SDHUFFDW = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_C);
+ break;
+ case 3: /* Custom table from referred segment */
+ huffman_params = jbig2_find_table(ctx, segment, table_index);
+ if (huffman_params == NULL) {
+ jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "Custom DW huffman table not found (%d)", table_index);
+ break;
+ }
+ params.SDHUFFDW = jbig2_build_huffman_table(ctx, huffman_params);
+ ++table_index;
+ break;
+ case 2:
+ default:
+ jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "symbol dictionary specified invalid huffman table");
+ goto cleanup; /* Jump direct to cleanup to avoid 2 errors being given */
}
- params.SDHUFFDW = jbig2_build_huffman_table(ctx, huffman_params);
- ++table_index;
- break;
- case 2:
- default:
- jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "symbol dictionary specified invalid huffman table");
- goto cleanup; /* Jump direct to cleanup to avoid 2 errors being given */
- }
- if (params.SDHUFFDW == NULL)
- {
- jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "failed to allocate DW huffman table");
- goto cleanup;
- }
+ if (params.SDHUFFDW == NULL) {
+ jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to allocate DW huffman table");
+ goto cleanup;
+ }
- if (flags & 0x0040) {
- /* Custom table from referred segment */
- huffman_params = jbig2_find_table(ctx, segment, table_index);
- if (huffman_params == NULL) {
- jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "Custom BMSIZE huffman table not found (%d)", table_index);
+ if (flags & 0x0040) {
+ /* Custom table from referred segment */
+ huffman_params = jbig2_find_table(ctx, segment, table_index);
+ if (huffman_params == NULL) {
+ jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "Custom BMSIZE huffman table not found (%d)", table_index);
+ } else {
+ params.SDHUFFBMSIZE = jbig2_build_huffman_table(ctx, huffman_params);
+ ++table_index;
+ }
} else {
- params.SDHUFFBMSIZE = jbig2_build_huffman_table(ctx, huffman_params);
- ++table_index;
+ /* Table B.1 */
+ params.SDHUFFBMSIZE = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_A);
}
- } else {
- /* Table B.1 */
- params.SDHUFFBMSIZE = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_A);
- }
- if (params.SDHUFFBMSIZE == NULL)
- {
- jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "failed to allocate BMSIZE huffman table");
- goto cleanup;
- }
+ if (params.SDHUFFBMSIZE == NULL) {
+ jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to allocate BMSIZE huffman table");
+ goto cleanup;
+ }
- if (flags & 0x0080) {
- /* Custom table from referred segment */
- huffman_params = jbig2_find_table(ctx, segment, table_index);
- if (huffman_params == NULL) {
- jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "Custom REFAGG huffman table not found (%d)", table_index);
+ if (flags & 0x0080) {
+ /* Custom table from referred segment */
+ huffman_params = jbig2_find_table(ctx, segment, table_index);
+ if (huffman_params == NULL) {
+ jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "Custom REFAGG huffman table not found (%d)", table_index);
+ } else {
+ params.SDHUFFAGGINST = jbig2_build_huffman_table(ctx, huffman_params);
+ ++table_index;
+ }
} else {
- params.SDHUFFAGGINST = jbig2_build_huffman_table(ctx, huffman_params);
- ++table_index;
+ /* Table B.1 */
+ params.SDHUFFAGGINST = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_A);
}
- } else {
- /* Table B.1 */
- params.SDHUFFAGGINST = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_A);
+ if (params.SDHUFFAGGINST == NULL) {
+ jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to allocate REFAGG huffman table");
+ goto cleanup;
+ }
}
- if (params.SDHUFFAGGINST == NULL)
- {
- jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "failed to allocate REFAGG huffman table");
+
+ /* FIXME: there are quite a few of these conditions to check */
+ /* maybe #ifdef CONFORMANCE and a separate routine */
+ if (!params.SDHUFF) {
+ if (flags & 0x000c) {
+ jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "SDHUFF is zero, but contrary to spec SDHUFFDH is not.");
+ goto cleanup;
+ }
+ if (flags & 0x0030) {
+ jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "SDHUFF is zero, but contrary to spec SDHUFFDW is not.");
+ goto cleanup;
+ }
+ }
+
+ if (flags & 0x0080) {
+ jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "bitmap coding context is used (NYI) symbol data likely to be garbage!");
goto cleanup;
}
- }
- /* FIXME: there are quite a few of these conditions to check */
- /* maybe #ifdef CONFORMANCE and a separate routine */
- if (!params.SDHUFF) {
- if (flags & 0x000c) {
- jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "SDHUFF is zero, but contrary to spec SDHUFFDH is not.");
- goto cleanup;
+ /* 7.4.2.1.2 */
+ sdat_bytes = params.SDHUFF ? 0 : params.SDTEMPLATE == 0 ? 8 : 2;
+ memcpy(params.sdat, segment_data + 2, sdat_bytes);
+ offset = 2 + sdat_bytes;
+
+ /* 7.4.2.1.3 */
+ if (params.SDREFAGG && !params.SDRTEMPLATE) {
+ if (offset + 4 > segment->data_length)
+ goto too_short;
+ memcpy(params.sdrat, segment_data + offset, 4);
+ offset += 4;
}
- if (flags & 0x0030) {
- jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "SDHUFF is zero, but contrary to spec SDHUFFDW is not.");
- goto cleanup;
- }
- }
- if (flags & 0x0080) {
- jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "bitmap coding context is used (NYI) symbol data likely to be garbage!");
- goto cleanup;
- }
+ if (offset + 8 > segment->data_length)
+ goto too_short;
- /* 7.4.2.1.2 */
- sdat_bytes = params.SDHUFF ? 0 : params.SDTEMPLATE == 0 ? 8 : 2;
- memcpy(params.sdat, segment_data + 2, sdat_bytes);
- offset = 2 + sdat_bytes;
+ /* 7.4.2.1.4 */
+ params.SDNUMEXSYMS = jbig2_get_uint32(segment_data + offset);
+ /* 7.4.2.1.5 */
+ params.SDNUMNEWSYMS = jbig2_get_uint32(segment_data + offset + 4);
+ offset += 8;
- /* 7.4.2.1.3 */
- if (params.SDREFAGG && !params.SDRTEMPLATE) {
- if (offset + 4 > segment->data_length)
- goto too_short;
- memcpy(params.sdrat, segment_data + offset, 4);
- offset += 4;
- }
+ jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number,
+ "symbol dictionary, flags=%04x, %u exported syms, %u new syms", flags, params.SDNUMEXSYMS, params.SDNUMNEWSYMS);
- if (offset + 8 > segment->data_length)
- goto too_short;
+ /* 7.4.2.2 (2) */
+ {
+ int n_dicts = jbig2_sd_count_referred(ctx, segment);
+ Jbig2SymbolDict **dicts = NULL;
- /* 7.4.2.1.4 */
- params.SDNUMEXSYMS = jbig2_get_uint32(segment_data + offset);
- /* 7.4.2.1.5 */
- params.SDNUMNEWSYMS = jbig2_get_uint32(segment_data + offset + 4);
- offset += 8;
+ if (n_dicts > 0) {
+ dicts = jbig2_sd_list_referred(ctx, segment);
+ if (dicts == NULL) {
+ jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate dicts in symbol dictionary");
+ goto cleanup;
+ }
+ params.SDINSYMS = jbig2_sd_cat(ctx, n_dicts, dicts);
+ if (params.SDINSYMS == NULL) {
+ jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate symbol array in symbol dictionary");
+ jbig2_free(ctx->allocator, dicts);
+ goto cleanup;
+ }
+ jbig2_free(ctx->allocator, dicts);
+ }
+ if (params.SDINSYMS != NULL) {
+ params.SDNUMINSYMS = params.SDINSYMS->n_symbols;
+ }
+ }
- jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number,
- "symbol dictionary, flags=%04x, %u exported syms, %u new syms",
- flags, params.SDNUMEXSYMS, params.SDNUMNEWSYMS);
+ /* 7.4.2.2 (3, 4) */
+ if (flags & 0x0100) {
+ jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "segment marks bitmap coding context as used (NYI)");
+ goto cleanup;
+ } else {
+ int stats_size = params.SDTEMPLATE == 0 ? 65536 : params.SDTEMPLATE == 1 ? 8192 : 1024;
- /* 7.4.2.2 (2) */
- {
- int n_dicts = jbig2_sd_count_referred(ctx, segment);
- Jbig2SymbolDict **dicts = NULL;
+ GB_stats = jbig2_new(ctx, Jbig2ArithCx, stats_size);
+ if (GB_stats == NULL) {
+ jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "failed to allocate GB_stats in jbig2_symbol_dictionary");
+ goto cleanup;
+ }
+ memset(GB_stats, 0, stats_size);
- if (n_dicts > 0) {
- dicts = jbig2_sd_list_referred(ctx, segment);
- if (dicts == NULL)
- {
- jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number,
- "failed to allocate dicts in symbol dictionary");
- goto cleanup;
- }
- params.SDINSYMS = jbig2_sd_cat(ctx, n_dicts, dicts);
- if (params.SDINSYMS == NULL)
- {
- jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number,
- "failed to allocate symbol array in symbol dictionary");
- jbig2_free(ctx->allocator, dicts);
- goto cleanup;
- }
- jbig2_free(ctx->allocator, dicts);
+ stats_size = params.SDRTEMPLATE ? 1 << 10 : 1 << 13;
+ GR_stats = jbig2_new(ctx, Jbig2ArithCx, stats_size);
+ if (GR_stats == NULL) {
+ jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "failed to allocate GR_stats in jbig2_symbol_dictionary");
+ jbig2_free(ctx->allocator, GB_stats);
+ goto cleanup;
+ }
+ memset(GR_stats, 0, stats_size);
}
- if (params.SDINSYMS != NULL) {
- params.SDNUMINSYMS = params.SDINSYMS->n_symbols;
- }
- }
- /* 7.4.2.2 (3, 4) */
- if (flags & 0x0100) {
- jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number,
- "segment marks bitmap coding context as used (NYI)");
- goto cleanup;
- } else {
- int stats_size = params.SDTEMPLATE == 0 ? 65536 :
- params.SDTEMPLATE == 1 ? 8192 : 1024;
- GB_stats = jbig2_new(ctx, Jbig2ArithCx, stats_size);
- if (GB_stats == NULL)
- {
- jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1,
- "failed to allocate GB_stats in jbig2_symbol_dictionary");
- goto cleanup;
- }
- memset(GB_stats, 0, stats_size);
-
- stats_size = params.SDRTEMPLATE ? 1 << 10 : 1 << 13;
- GR_stats = jbig2_new(ctx, Jbig2ArithCx, stats_size);
- if (GR_stats == NULL)
- {
- jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1,
- "failed to allocate GR_stats in jbig2_symbol_dictionary");
- jbig2_free(ctx->allocator, GB_stats);
- goto cleanup;
- }
- memset(GR_stats, 0, stats_size);
- }
-
- segment->result = (void *)jbig2_decode_symbol_dict(ctx, segment,
- ¶ms,
- segment_data + offset,
- segment->data_length - offset,
- GB_stats, GR_stats);
+ segment->result = (void *)jbig2_decode_symbol_dict(ctx, segment, ¶ms, segment_data + offset, segment->data_length - offset, GB_stats, GR_stats);
#ifdef DUMP_SYMDICT
- if (segment->result) jbig2_dump_symbol_dict(ctx, segment);
+ if (segment->result)
+ jbig2_dump_symbol_dict(ctx, segment);
#endif
- /* 7.4.2.2 (7) */
- if (flags & 0x0200) {
- /* todo: retain GB_stats, GR_stats */
- jbig2_free(ctx->allocator, GR_stats);
- jbig2_free(ctx->allocator, GB_stats);
- jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number,
- "segment marks bitmap coding context as retained (NYI)");
- } else {
- jbig2_free(ctx->allocator, GR_stats);
- jbig2_free(ctx->allocator, GB_stats);
- }
+ /* 7.4.2.2 (7) */
+ if (flags & 0x0200) {
+ /* todo: retain GB_stats, GR_stats */
+ jbig2_free(ctx->allocator, GR_stats);
+ jbig2_free(ctx->allocator, GB_stats);
+ jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "segment marks bitmap coding context as retained (NYI)");
+ } else {
+ jbig2_free(ctx->allocator, GR_stats);
+ jbig2_free(ctx->allocator, GB_stats);
+ }
cleanup:
- if (params.SDHUFF) {
- jbig2_release_huffman_table(ctx, params.SDHUFFDH);
- jbig2_release_huffman_table(ctx, params.SDHUFFDW);
- jbig2_release_huffman_table(ctx, params.SDHUFFBMSIZE);
- jbig2_release_huffman_table(ctx, params.SDHUFFAGGINST);
- }
- jbig2_sd_release(ctx, params.SDINSYMS);
+ if (params.SDHUFF) {
+ jbig2_release_huffman_table(ctx, params.SDHUFFDH);
+ jbig2_release_huffman_table(ctx, params.SDHUFFDW);
+ jbig2_release_huffman_table(ctx, params.SDHUFFBMSIZE);
+ jbig2_release_huffman_table(ctx, params.SDHUFFAGGINST);
+ }
+ jbig2_sd_release(ctx, params.SDINSYMS);
- return (segment->result != NULL) ? 0 : -1;
+ return (segment->result != NULL) ? 0 : -1;
- too_short:
- return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "Segment too short");
+too_short:
+ return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "Segment too short");
}
--- a/jbig2_symbol_dict.h
+++ b/jbig2_symbol_dict.h
@@ -17,7 +17,6 @@
jbig2dec
*/
-
/* symbol dictionary header */
/* the results of decoding a symbol dictionary */
@@ -27,35 +26,25 @@
} Jbig2SymbolDict;
/* decode a symbol dictionary segment and store the results */
-int
-jbig2_symbol_dictionary(Jbig2Ctx *ctx, Jbig2Segment *segment,
- const byte *segment_data);
+int jbig2_symbol_dictionary(Jbig2Ctx *ctx, Jbig2Segment *segment, const byte *segment_data);
/* get a particular glyph by index */
-Jbig2Image *
-jbig2_sd_glyph(Jbig2SymbolDict *dict, unsigned int id);
+Jbig2Image *jbig2_sd_glyph(Jbig2SymbolDict *dict, unsigned int id);
/* return a new empty symbol dict */
-Jbig2SymbolDict *
-jbig2_sd_new(Jbig2Ctx *ctx, int n_symbols);
+Jbig2SymbolDict *jbig2_sd_new(Jbig2Ctx *ctx, int n_symbols);
/* release the memory associated with a symbol dict */
-void
-jbig2_sd_release(Jbig2Ctx *ctx, Jbig2SymbolDict *dict);
+void jbig2_sd_release(Jbig2Ctx *ctx, Jbig2SymbolDict *dict);
/* generate a new symbol dictionary by concatenating a list of
existing dictionaries */
-Jbig2SymbolDict *
-jbig2_sd_cat(Jbig2Ctx *ctx, int n_dicts,
- Jbig2SymbolDict **dicts);
+Jbig2SymbolDict *jbig2_sd_cat(Jbig2Ctx *ctx, int n_dicts, Jbig2SymbolDict **dicts);
/* count the number of dictionary segments referred
to by the given segment */
-int
-jbig2_sd_count_referred(Jbig2Ctx *ctx, Jbig2Segment *segment);
+int jbig2_sd_count_referred(Jbig2Ctx *ctx, Jbig2Segment *segment);
/* return an array of pointers to symbol dictionaries referred
to by a segment */
-Jbig2SymbolDict **
-jbig2_sd_list_referred(Jbig2Ctx *ctx, Jbig2Segment *segment);
-
+Jbig2SymbolDict **jbig2_sd_list_referred(Jbig2Ctx *ctx, Jbig2Segment *segment);
--- a/jbig2_text.c
+++ b/jbig2_text.c
@@ -23,7 +23,7 @@
#include "os_types.h"
#include <stddef.h>
-#include <string.h> /* memset() */
+#include <string.h> /* memset() */
#include "jbig2.h"
#include "jbig2_priv.h"
@@ -35,7 +35,6 @@
#include "jbig2_symbol_dict.h"
#include "jbig2_text.h"
-
/**
* jbig2_decode_text_region: decode a text region segment
*
@@ -55,11 +54,9 @@
**/
int
jbig2_decode_text_region(Jbig2Ctx *ctx, Jbig2Segment *segment,
- const Jbig2TextRegionParams *params,
- const Jbig2SymbolDict * const *dicts, const int n_dicts,
- Jbig2Image *image,
- const byte *data, const size_t size,
- Jbig2ArithCx *GR_stats, Jbig2ArithState *as, Jbig2WordStream *ws)
+ const Jbig2TextRegionParams *params,
+ const Jbig2SymbolDict *const *dicts, const int n_dicts,
+ Jbig2Image *image, const byte *data, const size_t size, Jbig2ArithCx *GR_stats, Jbig2ArithState *as, Jbig2WordStream *ws)
{
/* relevent bits of 6.4.4 */
uint32_t NINSTANCES;
@@ -71,8 +68,8 @@
int32_t IDS;
int32_t CURS;
int32_t CURT;
- int S,T;
- int x,y;
+ int S, T;
+ int x, y;
bool first_symbol;
uint32_t index, SBNUMSYMS;
Jbig2Image *IB = NULL;
@@ -85,132 +82,122 @@
for (index = 0; index < n_dicts; index++) {
SBNUMSYMS += dicts[index]->n_symbols;
}
- jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
- "symbol list contains %d glyphs in %d dictionaries", SBNUMSYMS, n_dicts);
+ jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "symbol list contains %d glyphs in %d dictionaries", SBNUMSYMS, n_dicts);
if (params->SBHUFF) {
- Jbig2HuffmanTable *runcodes = NULL;
- Jbig2HuffmanParams runcodeparams;
- Jbig2HuffmanLine runcodelengths[35];
- Jbig2HuffmanLine *symcodelengths = NULL;
- Jbig2HuffmanParams symcodeparams;
- int err, len, range, r;
+ Jbig2HuffmanTable *runcodes = NULL;
+ Jbig2HuffmanParams runcodeparams;
+ Jbig2HuffmanLine runcodelengths[35];
+ Jbig2HuffmanLine *symcodelengths = NULL;
+ Jbig2HuffmanParams symcodeparams;
+ int err, len, range, r;
- jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
- "huffman coded text region");
- hs = jbig2_huffman_new(ctx, ws);
- if (hs == NULL)
- {
- jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number,
- "failed to allocate storage for text region");
- return -1;
- }
+ jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "huffman coded text region");
+ hs = jbig2_huffman_new(ctx, ws);
+ if (hs == NULL) {
+ jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate storage for text region");
+ return -1;
+ }
- /* 7.4.3.1.7 - decode symbol ID Huffman table */
- /* this is actually part of the segment header, but it is more
- convenient to handle it here */
+ /* 7.4.3.1.7 - decode symbol ID Huffman table */
+ /* this is actually part of the segment header, but it is more
+ convenient to handle it here */
- /* parse and build the runlength code huffman table */
- for (index = 0; index < 35; index++) {
- runcodelengths[index].PREFLEN = jbig2_huffman_get_bits(hs, 4, &code);
- if (code < 0)
- goto cleanup1;
- runcodelengths[index].RANGELEN = 0;
- runcodelengths[index].RANGELOW = index;
- jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
- " read runcode%d length %d", index, runcodelengths[index].PREFLEN);
- }
- runcodeparams.HTOOB = 0;
- runcodeparams.lines = runcodelengths;
- runcodeparams.n_lines = 35;
- runcodes = jbig2_build_huffman_table(ctx, &runcodeparams);
- if (runcodes == NULL) {
- jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "error constructing symbol id runcode table!");
- code = -1;
- goto cleanup1;
- }
+ /* parse and build the runlength code huffman table */
+ for (index = 0; index < 35; index++) {
+ runcodelengths[index].PREFLEN = jbig2_huffman_get_bits(hs, 4, &code);
+ if (code < 0)
+ goto cleanup1;
+ runcodelengths[index].RANGELEN = 0;
+ runcodelengths[index].RANGELOW = index;
+ jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, " read runcode%d length %d", index, runcodelengths[index].PREFLEN);
+ }
+ runcodeparams.HTOOB = 0;
+ runcodeparams.lines = runcodelengths;
+ runcodeparams.n_lines = 35;
+ runcodes = jbig2_build_huffman_table(ctx, &runcodeparams);
+ if (runcodes == NULL) {
+ jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "error constructing symbol id runcode table!");
+ code = -1;
+ goto cleanup1;
+ }
- /* decode the symbol id codelengths using the runlength table */
- symcodelengths = jbig2_new(ctx, Jbig2HuffmanLine, SBNUMSYMS);
- if (symcodelengths == NULL) {
- jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "memory allocation failure reading symbol ID huffman table!");
- code = -1;
- goto cleanup1;
- }
- index = 0;
- while (index < SBNUMSYMS) {
- code = jbig2_huffman_get(hs, runcodes, &err);
- if (err != 0 || code < 0 || code >= 35) {
- jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "error reading symbol ID huffman table!");
- code = err ? err : -1;
- goto cleanup1;
- }
-
- if (code < 32) {
- len = code;
- range = 1;
- } else {
- if (code == 32) {
- if (index < 1) {
- jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "error decoding symbol id table: run length with no antecedent!");
- code = -1;
+ /* decode the symbol id codelengths using the runlength table */
+ symcodelengths = jbig2_new(ctx, Jbig2HuffmanLine, SBNUMSYMS);
+ if (symcodelengths == NULL) {
+ jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "memory allocation failure reading symbol ID huffman table!");
+ code = -1;
+ goto cleanup1;
+ }
+ index = 0;
+ while (index < SBNUMSYMS) {
+ code = jbig2_huffman_get(hs, runcodes, &err);
+ if (err != 0 || code < 0 || code >= 35) {
+ jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "error reading symbol ID huffman table!");
+ code = err ? err : -1;
goto cleanup1;
- }
- len = symcodelengths[index-1].PREFLEN;
- } else {
- len = 0; /* code == 33 or 34 */
- }
- err = 0;
- if (code == 32) range = jbig2_huffman_get_bits(hs, 2, &err) + 3;
- else if (code == 33) range = jbig2_huffman_get_bits(hs, 3, &err) + 3;
- else if (code == 34) range = jbig2_huffman_get_bits(hs, 7, &err) + 11;
- if (err < 0)
- goto cleanup1;
- }
- jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
- " read runcode%d at index %d (length %d range %d)", code, index, len, range);
- if (index+range > SBNUMSYMS) {
- jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number,
- "runlength extends %d entries beyond the end of symbol id table!",
- index+range - SBNUMSYMS);
- range = SBNUMSYMS - index;
- }
- for (r = 0; r < range; r++) {
- symcodelengths[index+r].PREFLEN = len;
- symcodelengths[index+r].RANGELEN = 0;
- symcodelengths[index+r].RANGELOW = index + r;
- }
- index += r;
- }
+ }
- if (index < SBNUMSYMS) {
- jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number,
- "runlength codes do not cover the available symbol set");
- }
- symcodeparams.HTOOB = 0;
- symcodeparams.lines = symcodelengths;
- symcodeparams.n_lines = SBNUMSYMS;
+ if (code < 32) {
+ len = code;
+ range = 1;
+ } else {
+ if (code == 32) {
+ if (index < 1) {
+ jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "error decoding symbol id table: run length with no antecedent!");
+ code = -1;
+ goto cleanup1;
+ }
+ len = symcodelengths[index - 1].PREFLEN;
+ } else {
+ len = 0; /* code == 33 or 34 */
+ }
+ err = 0;
+ if (code == 32)
+ range = jbig2_huffman_get_bits(hs, 2, &err) + 3;
+ else if (code == 33)
+ range = jbig2_huffman_get_bits(hs, 3, &err) + 3;
+ else if (code == 34)
+ range = jbig2_huffman_get_bits(hs, 7, &err) + 11;
+ if (err < 0)
+ goto cleanup1;
+ }
+ jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, " read runcode%d at index %d (length %d range %d)", code, index, len, range);
+ if (index + range > SBNUMSYMS) {
+ jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number,
+ "runlength extends %d entries beyond the end of symbol id table!", index + range - SBNUMSYMS);
+ range = SBNUMSYMS - index;
+ }
+ for (r = 0; r < range; r++) {
+ symcodelengths[index + r].PREFLEN = len;
+ symcodelengths[index + r].RANGELEN = 0;
+ symcodelengths[index + r].RANGELOW = index + r;
+ }
+ index += r;
+ }
- /* skip to byte boundary */
- jbig2_huffman_skip(hs);
+ if (index < SBNUMSYMS) {
+ jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "runlength codes do not cover the available symbol set");
+ }
+ symcodeparams.HTOOB = 0;
+ symcodeparams.lines = symcodelengths;
+ symcodeparams.n_lines = SBNUMSYMS;
- /* finally, construct the symbol id huffman table itself */
- SBSYMCODES = jbig2_build_huffman_table(ctx, &symcodeparams);
+ /* skip to byte boundary */
+ jbig2_huffman_skip(hs);
+ /* finally, construct the symbol id huffman table itself */
+ SBSYMCODES = jbig2_build_huffman_table(ctx, &symcodeparams);
+
cleanup1:
- jbig2_free(ctx->allocator, symcodelengths);
- jbig2_release_huffman_table(ctx, runcodes);
+ jbig2_free(ctx->allocator, symcodelengths);
+ jbig2_release_huffman_table(ctx, runcodes);
- if (SBSYMCODES == NULL) {
- jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "could not construct Symbol ID huffman table!");
- jbig2_huffman_free(ctx, hs);
- return ((code != 0) ? code : -1);
- }
+ if (SBSYMCODES == NULL) {
+ jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "could not construct Symbol ID huffman table!");
+ jbig2_huffman_free(ctx, hs);
+ return ((code != 0) ? code : -1);
+ }
}
/* 6.4.5 (1) */
@@ -222,7 +209,8 @@
} else {
code = jbig2_arith_int_decode(params->IADT, as, &STRIPT);
}
- if (code < 0) goto cleanup2;
+ if (code < 0)
+ goto cleanup2;
/* 6.4.5 (2) */
STRIPT *= -(params->SBSTRIPS);
@@ -237,220 +225,238 @@
} else {
code = jbig2_arith_int_decode(params->IADT, as, &DT);
}
- if (code < 0) goto cleanup2;
+ if (code < 0)
+ goto cleanup2;
DT *= params->SBSTRIPS;
STRIPT += DT;
- first_symbol = TRUE;
- /* 6.4.5 (3c) - decode symbols in strip */
- for (;;) {
- /* (3c.i) */
- if (first_symbol) {
- /* 6.4.7 */
- if (params->SBHUFF) {
- DFS = jbig2_huffman_get(hs, params->SBHUFFFS, &code);
- } else {
- code = jbig2_arith_int_decode(params->IAFS, as, &DFS);
- }
- if (code < 0) goto cleanup2;
- FIRSTS += DFS;
- CURS = FIRSTS;
- first_symbol = FALSE;
- } else {
+ first_symbol = TRUE;
+ /* 6.4.5 (3c) - decode symbols in strip */
+ for (;;) {
+ /* (3c.i) */
+ if (first_symbol) {
+ /* 6.4.7 */
+ if (params->SBHUFF) {
+ DFS = jbig2_huffman_get(hs, params->SBHUFFFS, &code);
+ } else {
+ code = jbig2_arith_int_decode(params->IAFS, as, &DFS);
+ }
+ if (code < 0)
+ goto cleanup2;
+ FIRSTS += DFS;
+ CURS = FIRSTS;
+ first_symbol = FALSE;
+ } else {
if (NINSTANCES > params->SBNUMINSTANCES) {
- code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "too many NINSTANCES (%d) decoded", NINSTANCES);
+ code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "too many NINSTANCES (%d) decoded", NINSTANCES);
break;
- }
- /* (3c.ii) / 6.4.8 */
- if (params->SBHUFF) {
- IDS = jbig2_huffman_get(hs, params->SBHUFFDS, &code);
- } else {
- code = jbig2_arith_int_decode(params->IADS, as, &IDS);
- }
- if (code) {
+ }
+ /* (3c.ii) / 6.4.8 */
+ if (params->SBHUFF) {
+ IDS = jbig2_huffman_get(hs, params->SBHUFFDS, &code);
+ } else {
+ code = jbig2_arith_int_decode(params->IADS, as, &IDS);
+ }
+ if (code) {
/* decoded an OOB, reached end of strip */
- break;
- }
- CURS += IDS + params->SBDSOFFSET;
- }
+ break;
+ }
+ CURS += IDS + params->SBDSOFFSET;
+ }
- /* (3c.iii) / 6.4.9 */
- if (params->SBSTRIPS == 1) {
- CURT = 0;
- } else if (params->SBHUFF) {
- CURT = jbig2_huffman_get_bits(hs, params->LOGSBSTRIPS, &code);
- } else {
- code = jbig2_arith_int_decode(params->IAIT, as, &CURT);
- }
- if (code < 0) goto cleanup2;
- T = STRIPT + CURT;
+ /* (3c.iii) / 6.4.9 */
+ if (params->SBSTRIPS == 1) {
+ CURT = 0;
+ } else if (params->SBHUFF) {
+ CURT = jbig2_huffman_get_bits(hs, params->LOGSBSTRIPS, &code);
+ } else {
+ code = jbig2_arith_int_decode(params->IAIT, as, &CURT);
+ }
+ if (code < 0)
+ goto cleanup2;
+ T = STRIPT + CURT;
- /* (3b.iv) / 6.4.10 - decode the symbol id */
- if (params->SBHUFF) {
- ID = jbig2_huffman_get(hs, SBSYMCODES, &code);
- } else {
- code = jbig2_arith_iaid_decode(params->IAID, as, (int *)&ID);
- }
- if (code < 0) goto cleanup2;
- if (ID >= SBNUMSYMS) {
- code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "symbol id out of range! (%d/%d)", ID, SBNUMSYMS);
- goto cleanup2;
- }
+ /* (3b.iv) / 6.4.10 - decode the symbol id */
+ if (params->SBHUFF) {
+ ID = jbig2_huffman_get(hs, SBSYMCODES, &code);
+ } else {
+ code = jbig2_arith_iaid_decode(params->IAID, as, (int *)&ID);
+ }
+ if (code < 0)
+ goto cleanup2;
+ if (ID >= SBNUMSYMS) {
+ code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "symbol id out of range! (%d/%d)", ID, SBNUMSYMS);
+ goto cleanup2;
+ }
- /* (3c.v) / 6.4.11 - look up the symbol bitmap IB */
- {
- uint32_t id = ID;
+ /* (3c.v) / 6.4.11 - look up the symbol bitmap IB */
+ {
+ uint32_t id = ID;
- index = 0;
- while (id >= dicts[index]->n_symbols)
- id -= dicts[index++]->n_symbols;
- IB = jbig2_image_clone(ctx, dicts[index]->glyphs[id]);
- /* SumatraPDF: fail on missing glyphs */
- if (!IB) {
- code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "missing glyph %d/%d!", index, id);
- goto cleanup2;
- }
- }
- if (params->SBREFINE) {
- if (params->SBHUFF) {
- RI = jbig2_huffman_get_bits(hs, 1, &code);
- } else {
- code = jbig2_arith_int_decode(params->IARI, as, &RI);
- }
- if (code < 0) goto cleanup2;
- } else {
- RI = 0;
- }
- if (RI) {
- Jbig2RefinementRegionParams rparams;
- Jbig2Image *IBO;
- int32_t RDW, RDH, RDX, RDY;
- Jbig2Image *refimage;
- int BMSIZE = 0;
- int code1 = 0;
- int code2 = 0;
- int code3 = 0;
- int code4 = 0;
- int code5 = 0;
+ index = 0;
+ while (id >= dicts[index]->n_symbols)
+ id -= dicts[index++]->n_symbols;
+ IB = jbig2_image_clone(ctx, dicts[index]->glyphs[id]);
+ /* SumatraPDF: fail on missing glyphs */
+ if (!IB) {
+ code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "missing glyph %d/%d!", index, id);
+ goto cleanup2;
+ }
+ }
+ if (params->SBREFINE) {
+ if (params->SBHUFF) {
+ RI = jbig2_huffman_get_bits(hs, 1, &code);
+ } else {
+ code = jbig2_arith_int_decode(params->IARI, as, &RI);
+ }
+ if (code < 0)
+ goto cleanup2;
+ } else {
+ RI = 0;
+ }
+ if (RI) {
+ Jbig2RefinementRegionParams rparams;
+ Jbig2Image *IBO;
+ int32_t RDW, RDH, RDX, RDY;
+ Jbig2Image *refimage;
+ int BMSIZE = 0;
+ int code1 = 0;
+ int code2 = 0;
+ int code3 = 0;
+ int code4 = 0;
+ int code5 = 0;
- /* 6.4.11 (1, 2, 3, 4) */
- if (!params->SBHUFF) {
- code1 = jbig2_arith_int_decode(params->IARDW, as, &RDW);
- code2 = jbig2_arith_int_decode(params->IARDH, as, &RDH);
- code3 = jbig2_arith_int_decode(params->IARDX, as, &RDX);
- code4 = jbig2_arith_int_decode(params->IARDY, as, &RDY);
- } else {
- RDW = jbig2_huffman_get(hs, params->SBHUFFRDW, &code1);
- RDH = jbig2_huffman_get(hs, params->SBHUFFRDH, &code2);
- RDX = jbig2_huffman_get(hs, params->SBHUFFRDX, &code3);
- RDY = jbig2_huffman_get(hs, params->SBHUFFRDY, &code4);
- BMSIZE = jbig2_huffman_get(hs, params->SBHUFFRSIZE, &code5);
- jbig2_huffman_skip(hs);
- }
+ /* 6.4.11 (1, 2, 3, 4) */
+ if (!params->SBHUFF) {
+ code1 = jbig2_arith_int_decode(params->IARDW, as, &RDW);
+ code2 = jbig2_arith_int_decode(params->IARDH, as, &RDH);
+ code3 = jbig2_arith_int_decode(params->IARDX, as, &RDX);
+ code4 = jbig2_arith_int_decode(params->IARDY, as, &RDY);
+ } else {
+ RDW = jbig2_huffman_get(hs, params->SBHUFFRDW, &code1);
+ RDH = jbig2_huffman_get(hs, params->SBHUFFRDH, &code2);
+ RDX = jbig2_huffman_get(hs, params->SBHUFFRDX, &code3);
+ RDY = jbig2_huffman_get(hs, params->SBHUFFRDY, &code4);
+ BMSIZE = jbig2_huffman_get(hs, params->SBHUFFRSIZE, &code5);
+ jbig2_huffman_skip(hs);
+ }
- if ((code1 < 0) || (code2 < 0) || (code3 < 0) || (code4 < 0) || (code5 < 0))
- {
- code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "failed to decode data");
- goto cleanup2;
- }
+ if ((code1 < 0) || (code2 < 0) || (code3 < 0) || (code4 < 0) || (code5 < 0)) {
+ code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to decode data");
+ goto cleanup2;
+ }
- /* 6.4.11 (6) */
- IBO = IB;
- refimage = jbig2_image_new(ctx, IBO->width + RDW,
- IBO->height + RDH);
- if (refimage == NULL) {
- jbig2_image_release(ctx, IBO);
- if (params->SBHUFF) {
- jbig2_release_huffman_table(ctx, SBSYMCODES);
- }
- return jbig2_error(ctx, JBIG2_SEVERITY_FATAL,
- segment->number,
- "couldn't allocate reference image");
- }
+ /* 6.4.11 (6) */
+ IBO = IB;
+ refimage = jbig2_image_new(ctx, IBO->width + RDW, IBO->height + RDH);
+ if (refimage == NULL) {
+ jbig2_image_release(ctx, IBO);
+ if (params->SBHUFF) {
+ jbig2_release_huffman_table(ctx, SBSYMCODES);
+ }
+ return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "couldn't allocate reference image");
+ }
jbig2_image_clear(ctx, refimage, 0x00);
- /* Table 12 */
- rparams.GRTEMPLATE = params->SBRTEMPLATE;
- rparams.reference = IBO;
- rparams.DX = (RDW >> 1) + RDX;
- rparams.DY = (RDH >> 1) + RDY;
- rparams.TPGRON = 0;
- memcpy(rparams.grat, params->sbrat, 4);
- code = jbig2_decode_refinement_region(ctx, segment,
- &rparams, as, refimage, GR_stats);
- if (code < 0) {
- jbig2_image_release(ctx, refimage);
- goto cleanup2;
- }
- IB = refimage;
+ /* Table 12 */
+ rparams.GRTEMPLATE = params->SBRTEMPLATE;
+ rparams.reference = IBO;
+ rparams.DX = (RDW >> 1) + RDX;
+ rparams.DY = (RDH >> 1) + RDY;
+ rparams.TPGRON = 0;
+ memcpy(rparams.grat, params->sbrat, 4);
+ code = jbig2_decode_refinement_region(ctx, segment, &rparams, as, refimage, GR_stats);
+ if (code < 0) {
+ jbig2_image_release(ctx, refimage);
+ goto cleanup2;
+ }
+ IB = refimage;
- jbig2_image_release(ctx, IBO);
+ jbig2_image_release(ctx, IBO);
- /* 6.4.11 (7) */
- if (params->SBHUFF) {
- jbig2_huffman_advance(hs, BMSIZE);
- }
+ /* 6.4.11 (7) */
+ if (params->SBHUFF) {
+ jbig2_huffman_advance(hs, BMSIZE);
+ }
- }
+ }
- /* (3c.vi) */
- if ((!params->TRANSPOSED) && (params->REFCORNER > 1)) {
- CURS += IB->width - 1;
- } else if ((params->TRANSPOSED) && !(params->REFCORNER & 1)) {
- CURS += IB->height - 1;
- }
+ /* (3c.vi) */
+ if ((!params->TRANSPOSED) && (params->REFCORNER > 1)) {
+ CURS += IB->width - 1;
+ } else if ((params->TRANSPOSED) && !(params->REFCORNER & 1)) {
+ CURS += IB->height - 1;
+ }
- /* (3c.vii) */
- S = CURS;
+ /* (3c.vii) */
+ S = CURS;
- /* (3c.viii) */
- if (!params->TRANSPOSED) {
- switch (params->REFCORNER) {
- case JBIG2_CORNER_TOPLEFT: x = S; y = T; break;
- case JBIG2_CORNER_TOPRIGHT: x = S - IB->width + 1; y = T; break;
- case JBIG2_CORNER_BOTTOMLEFT: x = S; y = T - IB->height + 1; break;
- default:
- case JBIG2_CORNER_BOTTOMRIGHT: x = S - IB->width + 1; y = T - IB->height + 1; break;
- }
- } else { /* TRANSPOSED */
- switch (params->REFCORNER) {
- case JBIG2_CORNER_TOPLEFT: x = T; y = S; break;
- case JBIG2_CORNER_TOPRIGHT: x = T - IB->width + 1; y = S; break;
- case JBIG2_CORNER_BOTTOMLEFT: x = T; y = S - IB->height + 1; break;
- default:
- case JBIG2_CORNER_BOTTOMRIGHT: x = T - IB->width + 1; y = S - IB->height + 1; break;
- }
- }
+ /* (3c.viii) */
+ if (!params->TRANSPOSED) {
+ switch (params->REFCORNER) {
+ case JBIG2_CORNER_TOPLEFT:
+ x = S;
+ y = T;
+ break;
+ case JBIG2_CORNER_TOPRIGHT:
+ x = S - IB->width + 1;
+ y = T;
+ break;
+ case JBIG2_CORNER_BOTTOMLEFT:
+ x = S;
+ y = T - IB->height + 1;
+ break;
+ default:
+ case JBIG2_CORNER_BOTTOMRIGHT:
+ x = S - IB->width + 1;
+ y = T - IB->height + 1;
+ break;
+ }
+ } else { /* TRANSPOSED */
+ switch (params->REFCORNER) {
+ case JBIG2_CORNER_TOPLEFT:
+ x = T;
+ y = S;
+ break;
+ case JBIG2_CORNER_TOPRIGHT:
+ x = T - IB->width + 1;
+ y = S;
+ break;
+ case JBIG2_CORNER_BOTTOMLEFT:
+ x = T;
+ y = S - IB->height + 1;
+ break;
+ default:
+ case JBIG2_CORNER_BOTTOMRIGHT:
+ x = T - IB->width + 1;
+ y = S - IB->height + 1;
+ break;
+ }
+ }
- /* (3c.ix) */
+ /* (3c.ix) */
#ifdef JBIG2_DEBUG
- jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
- "composing glyph id %d: %dx%d @ (%d,%d) symbol %d/%d",
- ID, IB->width, IB->height, x, y, NINSTANCES + 1,
- params->SBNUMINSTANCES);
+ jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
+ "composing glyph id %d: %dx%d @ (%d,%d) symbol %d/%d", ID, IB->width, IB->height, x, y, NINSTANCES + 1, params->SBNUMINSTANCES);
#endif
- code = jbig2_image_compose(ctx, image, IB, x, y, params->SBCOMBOP);
- if (code < 0) {
- jbig2_image_release(ctx, IB);
- goto cleanup2;
- }
+ code = jbig2_image_compose(ctx, image, IB, x, y, params->SBCOMBOP);
+ if (code < 0) {
+ jbig2_image_release(ctx, IB);
+ goto cleanup2;
+ }
- /* (3c.x) */
- if ((!params->TRANSPOSED) && (params->REFCORNER < 2)) {
- CURS += IB->width -1 ;
- } else if ((params->TRANSPOSED) && (params->REFCORNER & 1)) {
- CURS += IB->height - 1;
- }
+ /* (3c.x) */
+ if ((!params->TRANSPOSED) && (params->REFCORNER < 2)) {
+ CURS += IB->width - 1;
+ } else if ((params->TRANSPOSED) && (params->REFCORNER & 1)) {
+ CURS += IB->height - 1;
+ }
- /* (3c.xi) */
- NINSTANCES++;
+ /* (3c.xi) */
+ NINSTANCES++;
- jbig2_image_release(ctx, IB);
- }
+ jbig2_image_release(ctx, IB);
+ }
/* end strip */
}
/* 6.4.5 (4) */
@@ -457,7 +463,7 @@
cleanup2:
if (params->SBHUFF) {
- jbig2_release_huffman_table(ctx, SBSYMCODES);
+ jbig2_release_huffman_table(ctx, SBSYMCODES);
}
jbig2_huffman_free(ctx, hs);
@@ -495,8 +501,7 @@
flags = jbig2_get_uint16(segment_data + offset);
offset += 2;
- jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
- "text region header flags 0x%04x", flags);
+ jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "text region header flags 0x%04x", flags);
/* zero params to ease cleanup later */
memset(¶ms, 0, sizeof(Jbig2TextRegionParams));
@@ -511,36 +516,32 @@
params.SBDEFPIXEL = flags & 0x0200;
/* SBDSOFFSET is a signed 5 bit integer */
params.SBDSOFFSET = (flags & 0x7C00) >> 10;
- if (params.SBDSOFFSET > 0x0f) params.SBDSOFFSET -= 0x20;
+ if (params.SBDSOFFSET > 0x0f)
+ params.SBDSOFFSET -= 0x20;
params.SBRTEMPLATE = flags & 0x8000;
if (params.SBDSOFFSET) {
- jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
- "text region has SBDSOFFSET %d", params.SBDSOFFSET);
+ jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "text region has SBDSOFFSET %d", params.SBDSOFFSET);
}
- if (params.SBHUFF) /* Huffman coding */
- {
+ if (params.SBHUFF) { /* Huffman coding */
/* 7.4.3.1.2 */
huffman_flags = jbig2_get_uint16(segment_data + offset);
offset += 2;
if (huffman_flags & 0x8000)
- jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number,
- "reserved bit 15 of text region huffman flags is not zero");
- }
- else /* arithmetic coding */
- {
+ jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "reserved bit 15 of text region huffman flags is not zero");
+ } else { /* arithmetic coding */
+
/* 7.4.3.1.3 */
- if ((params.SBREFINE) && !(params.SBRTEMPLATE))
- {
+ if ((params.SBREFINE) && !(params.SBRTEMPLATE)) {
params.sbrat[0] = segment_data[offset];
params.sbrat[1] = segment_data[offset + 1];
params.sbrat[2] = segment_data[offset + 2];
params.sbrat[3] = segment_data[offset + 3];
offset += 4;
- }
- }
+ }
+ }
/* 7.4.3.1.4 */
params.SBNUMINSTANCES = jbig2_get_uint32(segment_data + offset);
@@ -551,260 +552,213 @@
/* ...this is handled in the segment body decoder */
/* 7.4.3.1.6 - Other Huffman table selection */
- switch (huffman_flags & 0x0003) {
- case 0: /* Table B.6 */
- params.SBHUFFFS = jbig2_build_huffman_table(ctx,
- &jbig2_huffman_params_F);
- break;
- case 1: /* Table B.7 */
- params.SBHUFFFS = jbig2_build_huffman_table(ctx,
- &jbig2_huffman_params_G);
- break;
- case 3: /* Custom table from referred segment */
+ switch (huffman_flags & 0x0003) {
+ case 0: /* Table B.6 */
+ params.SBHUFFFS = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_F);
+ break;
+ case 1: /* Table B.7 */
+ params.SBHUFFFS = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_G);
+ break;
+ case 3: /* Custom table from referred segment */
huffman_params = jbig2_find_table(ctx, segment, table_index);
if (huffman_params == NULL) {
- code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "Custom FS huffman table not found (%d)", table_index);
+ code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "Custom FS huffman table not found (%d)", table_index);
goto cleanup1;
}
params.SBHUFFFS = jbig2_build_huffman_table(ctx, huffman_params);
++table_index;
break;
- case 2: /* invalid */
- default:
- code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "text region specified invalid FS huffman table");
- goto cleanup1;
- break;
- }
- if (params.SBHUFFFS == NULL)
- {
- code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "failed to allocate text region specified FS huffman table");
- goto cleanup1;
- }
+ case 2: /* invalid */
+ default:
+ code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "text region specified invalid FS huffman table");
+ goto cleanup1;
+ break;
+ }
+ if (params.SBHUFFFS == NULL) {
+ code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to allocate text region specified FS huffman table");
+ goto cleanup1;
+ }
- switch ((huffman_flags & 0x000c) >> 2) {
- case 0: /* Table B.8 */
- params.SBHUFFDS = jbig2_build_huffman_table(ctx,
- &jbig2_huffman_params_H);
- break;
- case 1: /* Table B.9 */
- params.SBHUFFDS = jbig2_build_huffman_table(ctx,
- &jbig2_huffman_params_I);
- break;
- case 2: /* Table B.10 */
- params.SBHUFFDS = jbig2_build_huffman_table(ctx,
- &jbig2_huffman_params_J);
- break;
- case 3: /* Custom table from referred segment */
+ switch ((huffman_flags & 0x000c) >> 2) {
+ case 0: /* Table B.8 */
+ params.SBHUFFDS = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_H);
+ break;
+ case 1: /* Table B.9 */
+ params.SBHUFFDS = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_I);
+ break;
+ case 2: /* Table B.10 */
+ params.SBHUFFDS = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_J);
+ break;
+ case 3: /* Custom table from referred segment */
huffman_params = jbig2_find_table(ctx, segment, table_index);
if (huffman_params == NULL) {
- code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "Custom DS huffman table not found (%d)", table_index);
+ code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "Custom DS huffman table not found (%d)", table_index);
goto cleanup1;
}
params.SBHUFFDS = jbig2_build_huffman_table(ctx, huffman_params);
++table_index;
break;
- }
- if (params.SBHUFFDS == NULL)
- {
- code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "failed to allocate text region specified DS huffman table");
- goto cleanup1;
- }
+ }
+ if (params.SBHUFFDS == NULL) {
+ code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to allocate text region specified DS huffman table");
+ goto cleanup1;
+ }
- switch ((huffman_flags & 0x0030) >> 4) {
- case 0: /* Table B.11 */
- params.SBHUFFDT = jbig2_build_huffman_table(ctx,
- &jbig2_huffman_params_K);
- break;
- case 1: /* Table B.12 */
- params.SBHUFFDT = jbig2_build_huffman_table(ctx,
- &jbig2_huffman_params_L);
- break;
- case 2: /* Table B.13 */
- params.SBHUFFDT = jbig2_build_huffman_table(ctx,
- &jbig2_huffman_params_M);
- break;
- case 3: /* Custom table from referred segment */
+ switch ((huffman_flags & 0x0030) >> 4) {
+ case 0: /* Table B.11 */
+ params.SBHUFFDT = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_K);
+ break;
+ case 1: /* Table B.12 */
+ params.SBHUFFDT = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_L);
+ break;
+ case 2: /* Table B.13 */
+ params.SBHUFFDT = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_M);
+ break;
+ case 3: /* Custom table from referred segment */
huffman_params = jbig2_find_table(ctx, segment, table_index);
if (huffman_params == NULL) {
- code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "Custom DT huffman table not found (%d)", table_index);
+ code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "Custom DT huffman table not found (%d)", table_index);
goto cleanup1;
}
params.SBHUFFDT = jbig2_build_huffman_table(ctx, huffman_params);
++table_index;
break;
- }
- if (params.SBHUFFDT == NULL)
- {
- code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "failed to allocate text region specified DT huffman table");
- goto cleanup1;
- }
+ }
+ if (params.SBHUFFDT == NULL) {
+ code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to allocate text region specified DT huffman table");
+ goto cleanup1;
+ }
- switch ((huffman_flags & 0x00c0) >> 6) {
- case 0: /* Table B.14 */
- params.SBHUFFRDW = jbig2_build_huffman_table(ctx,
- &jbig2_huffman_params_N);
- break;
- case 1: /* Table B.15 */
- params.SBHUFFRDW = jbig2_build_huffman_table(ctx,
- &jbig2_huffman_params_O);
- break;
- case 3: /* Custom table from referred segment */
+ switch ((huffman_flags & 0x00c0) >> 6) {
+ case 0: /* Table B.14 */
+ params.SBHUFFRDW = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_N);
+ break;
+ case 1: /* Table B.15 */
+ params.SBHUFFRDW = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_O);
+ break;
+ case 3: /* Custom table from referred segment */
huffman_params = jbig2_find_table(ctx, segment, table_index);
if (huffman_params == NULL) {
- code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "Custom RDW huffman table not found (%d)", table_index);
+ code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "Custom RDW huffman table not found (%d)", table_index);
goto cleanup1;
}
params.SBHUFFRDW = jbig2_build_huffman_table(ctx, huffman_params);
++table_index;
- break;
- case 2: /* invalid */
- default:
- code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "text region specified invalid RDW huffman table");
- goto cleanup1;
- break;
- }
- if (params.SBHUFFRDW == NULL)
- {
- code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "failed to allocate text region specified RDW huffman table");
- goto cleanup1;
- }
+ break;
+ case 2: /* invalid */
+ default:
+ code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "text region specified invalid RDW huffman table");
+ goto cleanup1;
+ break;
+ }
+ if (params.SBHUFFRDW == NULL) {
+ code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to allocate text region specified RDW huffman table");
+ goto cleanup1;
+ }
- switch ((huffman_flags & 0x0300) >> 8) {
- case 0: /* Table B.14 */
- params.SBHUFFRDH = jbig2_build_huffman_table(ctx,
- &jbig2_huffman_params_N);
- break;
- case 1: /* Table B.15 */
- params.SBHUFFRDH = jbig2_build_huffman_table(ctx,
- &jbig2_huffman_params_O);
- break;
- case 3: /* Custom table from referred segment */
+ switch ((huffman_flags & 0x0300) >> 8) {
+ case 0: /* Table B.14 */
+ params.SBHUFFRDH = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_N);
+ break;
+ case 1: /* Table B.15 */
+ params.SBHUFFRDH = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_O);
+ break;
+ case 3: /* Custom table from referred segment */
huffman_params = jbig2_find_table(ctx, segment, table_index);
if (huffman_params == NULL) {
- code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "Custom RDH huffman table not found (%d)", table_index);
+ code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "Custom RDH huffman table not found (%d)", table_index);
goto cleanup1;
}
params.SBHUFFRDH = jbig2_build_huffman_table(ctx, huffman_params);
++table_index;
- break;
- case 2: /* invalid */
- default:
- code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "text region specified invalid RDH huffman table");
- goto cleanup1;
- break;
- }
- if (params.SBHUFFRDH == NULL)
- {
- code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "failed to allocate text region specified RDH huffman table");
- goto cleanup1;
- }
+ break;
+ case 2: /* invalid */
+ default:
+ code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "text region specified invalid RDH huffman table");
+ goto cleanup1;
+ break;
+ }
+ if (params.SBHUFFRDH == NULL) {
+ code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to allocate text region specified RDH huffman table");
+ goto cleanup1;
+ }
- switch ((huffman_flags & 0x0c00) >> 10) {
- case 0: /* Table B.14 */
- params.SBHUFFRDX = jbig2_build_huffman_table(ctx,
- &jbig2_huffman_params_N);
- break;
- case 1: /* Table B.15 */
- params.SBHUFFRDX = jbig2_build_huffman_table(ctx,
- &jbig2_huffman_params_O);
- break;
- case 3: /* Custom table from referred segment */
+ switch ((huffman_flags & 0x0c00) >> 10) {
+ case 0: /* Table B.14 */
+ params.SBHUFFRDX = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_N);
+ break;
+ case 1: /* Table B.15 */
+ params.SBHUFFRDX = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_O);
+ break;
+ case 3: /* Custom table from referred segment */
huffman_params = jbig2_find_table(ctx, segment, table_index);
if (huffman_params == NULL) {
- code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "Custom RDX huffman table not found (%d)", table_index);
+ code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "Custom RDX huffman table not found (%d)", table_index);
goto cleanup1;
}
params.SBHUFFRDX = jbig2_build_huffman_table(ctx, huffman_params);
++table_index;
- break;
- case 2: /* invalid */
- default:
- code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "text region specified invalid RDX huffman table");
- goto cleanup1;
- break;
- }
- if (params.SBHUFFRDX == NULL)
- {
- code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "failed to allocate text region specified RDX huffman table");
- goto cleanup1;
- }
+ break;
+ case 2: /* invalid */
+ default:
+ code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "text region specified invalid RDX huffman table");
+ goto cleanup1;
+ break;
+ }
+ if (params.SBHUFFRDX == NULL) {
+ code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to allocate text region specified RDX huffman table");
+ goto cleanup1;
+ }
- switch ((huffman_flags & 0x3000) >> 12) {
- case 0: /* Table B.14 */
- params.SBHUFFRDY = jbig2_build_huffman_table(ctx,
- &jbig2_huffman_params_N);
- break;
- case 1: /* Table B.15 */
- params.SBHUFFRDY = jbig2_build_huffman_table(ctx,
- &jbig2_huffman_params_O);
- break;
- case 3: /* Custom table from referred segment */
+ switch ((huffman_flags & 0x3000) >> 12) {
+ case 0: /* Table B.14 */
+ params.SBHUFFRDY = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_N);
+ break;
+ case 1: /* Table B.15 */
+ params.SBHUFFRDY = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_O);
+ break;
+ case 3: /* Custom table from referred segment */
huffman_params = jbig2_find_table(ctx, segment, table_index);
if (huffman_params == NULL) {
- code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "Custom RDY huffman table not found (%d)", table_index);
+ code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "Custom RDY huffman table not found (%d)", table_index);
goto cleanup1;
}
params.SBHUFFRDY = jbig2_build_huffman_table(ctx, huffman_params);
++table_index;
- break;
- case 2: /* invalid */
- default:
- code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "text region specified invalid RDY huffman table");
- goto cleanup1;
- break;
- }
- if (params.SBHUFFRDY == NULL)
- {
- code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "failed to allocate text region specified RDY huffman table");
- goto cleanup1;
- }
+ break;
+ case 2: /* invalid */
+ default:
+ code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "text region specified invalid RDY huffman table");
+ goto cleanup1;
+ break;
+ }
+ if (params.SBHUFFRDY == NULL) {
+ code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to allocate text region specified RDY huffman table");
+ goto cleanup1;
+ }
- switch ((huffman_flags & 0x4000) >> 14) {
- case 0: /* Table B.1 */
- params.SBHUFFRSIZE = jbig2_build_huffman_table(ctx,
- &jbig2_huffman_params_A);
- break;
- case 1: /* Custom table from referred segment */
+ switch ((huffman_flags & 0x4000) >> 14) {
+ case 0: /* Table B.1 */
+ params.SBHUFFRSIZE = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_A);
+ break;
+ case 1: /* Custom table from referred segment */
huffman_params = jbig2_find_table(ctx, segment, table_index);
if (huffman_params == NULL) {
- code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "Custom RSIZE huffman table not found (%d)", table_index);
+ code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "Custom RSIZE huffman table not found (%d)", table_index);
goto cleanup1;
}
params.SBHUFFRSIZE = jbig2_build_huffman_table(ctx, huffman_params);
++table_index;
- break;
- }
- if (params.SBHUFFRSIZE == NULL)
- {
- code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "failed to allocate text region specified RSIZE huffman table");
- goto cleanup1;
- }
+ break;
+ }
+ if (params.SBHUFFRSIZE == NULL) {
+ code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to allocate text region specified RSIZE huffman table");
+ goto cleanup1;
+ }
if (huffman_flags & 0x8000) {
- jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number,
- "text region huffman flags bit 15 is set, contrary to spec");
- }
+ jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "text region huffman flags bit 15 is set, contrary to spec");
+ }
/* 7.4.3.1.7 */
/* For convenience this is done in the body decoder routine */
@@ -811,9 +765,7 @@
}
jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number,
- "text region: %d x %d @ (%d,%d) %d symbols",
- region_info.width, region_info.height,
- region_info.x, region_info.y, params.SBNUMINSTANCES);
+ "text region: %d x %d @ (%d,%d) %d symbols", region_info.width, region_info.height, region_info.x, region_info.y, params.SBNUMINSTANCES);
/* 7.4.3.2 (2) - compose the list of symbol dictionaries */
n_dicts = jbig2_sd_count_referred(ctx, segment);
@@ -820,111 +772,94 @@
if (n_dicts != 0) {
dicts = jbig2_sd_list_referred(ctx, segment);
} else {
- code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "text region refers to no symbol dictionaries!");
+ code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "text region refers to no symbol dictionaries!");
goto cleanup1;
}
if (dicts == NULL) {
- code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "unable to retrive symbol dictionaries! previous parsing error?");
+ code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "unable to retrive symbol dictionaries! previous parsing error?");
goto cleanup1;
} else {
- int index;
- if (dicts[0] == NULL) {
- code =jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number,
- "unable to find first referenced symbol dictionary!");
- goto cleanup1;
- }
- for (index = 1; index < n_dicts; index++)
- if (dicts[index] == NULL) {
- jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number,
- "unable to find all referenced symbol dictionaries!");
- n_dicts = index;
- }
+ int index;
+
+ if (dicts[0] == NULL) {
+ code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "unable to find first referenced symbol dictionary!");
+ goto cleanup1;
+ }
+ for (index = 1; index < n_dicts; index++)
+ if (dicts[index] == NULL) {
+ jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "unable to find all referenced symbol dictionaries!");
+ n_dicts = index;
+ }
}
/* 7.4.3.2 (3) */
{
- int stats_size = params.SBRTEMPLATE ? 1 << 10 : 1 << 13;
- GR_stats = jbig2_new(ctx, Jbig2ArithCx, stats_size);
- if (GR_stats == NULL)
- {
- code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "could not allocate GR_stats");
- goto cleanup1;
+ int stats_size = params.SBRTEMPLATE ? 1 << 10 : 1 << 13;
+
+ GR_stats = jbig2_new(ctx, Jbig2ArithCx, stats_size);
+ if (GR_stats == NULL) {
+ code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "could not allocate GR_stats");
+ goto cleanup1;
+ }
+ memset(GR_stats, 0, stats_size);
}
- memset(GR_stats, 0, stats_size);
- }
image = jbig2_image_new(ctx, region_info.width, region_info.height);
if (image == NULL) {
- code =jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "couldn't allocate text region image");
+ code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "couldn't allocate text region image");
goto cleanup2;
}
ws = jbig2_word_stream_buf_new(ctx, segment_data + offset, segment->data_length - offset);
- if (ws == NULL)
- {
- code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "couldn't allocate ws in text region image");
+ if (ws == NULL) {
+ code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "couldn't allocate ws in text region image");
goto cleanup2;
}
as = jbig2_arith_new(ctx, ws);
- if (as == NULL)
- {
- code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "couldn't allocate as in text region image");
+ if (as == NULL) {
+ code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "couldn't allocate as in text region image");
goto cleanup2;
}
if (!params.SBHUFF) {
- int SBSYMCODELEN, index;
+ int SBSYMCODELEN, index;
int SBNUMSYMS = 0;
- for (index = 0; index < n_dicts; index++) {
- SBNUMSYMS += dicts[index]->n_symbols;
- }
- params.IADT = jbig2_arith_int_ctx_new(ctx);
- params.IAFS = jbig2_arith_int_ctx_new(ctx);
- params.IADS = jbig2_arith_int_ctx_new(ctx);
- params.IAIT = jbig2_arith_int_ctx_new(ctx);
- if ((params.IADT == NULL) || (params.IAFS == NULL) ||
- (params.IADS == NULL) || (params.IAIT == NULL))
- {
- code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "couldn't allocate text region image data");
- goto cleanup3;
- }
+ for (index = 0; index < n_dicts; index++) {
+ SBNUMSYMS += dicts[index]->n_symbols;
+ }
- /* Table 31 */
- for (SBSYMCODELEN = 0; (1 << SBSYMCODELEN) < SBNUMSYMS; SBSYMCODELEN++)
- {}
- params.IAID = jbig2_arith_iaid_ctx_new(ctx, SBSYMCODELEN);
- params.IARI = jbig2_arith_int_ctx_new(ctx);
- params.IARDW = jbig2_arith_int_ctx_new(ctx);
- params.IARDH = jbig2_arith_int_ctx_new(ctx);
- params.IARDX = jbig2_arith_int_ctx_new(ctx);
- params.IARDY = jbig2_arith_int_ctx_new(ctx);
- if ((params.IAID == NULL) || (params.IARI == NULL) ||
- (params.IARDW == NULL) || (params.IARDH == NULL) ||
- (params.IARDX == NULL) || (params.IARDY == NULL))
- {
- code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "couldn't allocate text region image data");
- goto cleanup4;
+ params.IADT = jbig2_arith_int_ctx_new(ctx);
+ params.IAFS = jbig2_arith_int_ctx_new(ctx);
+ params.IADS = jbig2_arith_int_ctx_new(ctx);
+ params.IAIT = jbig2_arith_int_ctx_new(ctx);
+ if ((params.IADT == NULL) || (params.IAFS == NULL) || (params.IADS == NULL) || (params.IAIT == NULL)) {
+ code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "couldn't allocate text region image data");
+ goto cleanup3;
+ }
+
+ /* Table 31 */
+ for (SBSYMCODELEN = 0; (1 << SBSYMCODELEN) < SBNUMSYMS; SBSYMCODELEN++) {
+ }
+ params.IAID = jbig2_arith_iaid_ctx_new(ctx, SBSYMCODELEN);
+ params.IARI = jbig2_arith_int_ctx_new(ctx);
+ params.IARDW = jbig2_arith_int_ctx_new(ctx);
+ params.IARDH = jbig2_arith_int_ctx_new(ctx);
+ params.IARDX = jbig2_arith_int_ctx_new(ctx);
+ params.IARDY = jbig2_arith_int_ctx_new(ctx);
+ if ((params.IAID == NULL) || (params.IARI == NULL) ||
+ (params.IARDW == NULL) || (params.IARDH == NULL) || (params.IARDX == NULL) || (params.IARDY == NULL)) {
+ code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "couldn't allocate text region image data");
+ goto cleanup4;
+ }
}
- }
code = jbig2_decode_text_region(ctx, segment, ¶ms,
- (const Jbig2SymbolDict * const *)dicts, n_dicts, image,
- segment_data + offset, segment->data_length - offset,
- GR_stats, as, ws);
- if (code < 0)
- {
- jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "failed to decode text region image data");
+ (const Jbig2SymbolDict * const *)dicts, n_dicts, image,
+ segment_data + offset, segment->data_length - offset, GR_stats, as, ws);
+ if (code < 0) {
+ jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to decode text region image data");
goto cleanup4;
}
@@ -934,10 +869,8 @@
} else {
/* otherwise composite onto the page */
jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
- "composing %dx%d decoded text region onto page at (%d, %d)",
- region_info.width, region_info.height, region_info.x, region_info.y);
- jbig2_page_add_result(ctx, &ctx->pages[ctx->current_page], image,
- region_info.x, region_info.y, region_info.op);
+ "composing %dx%d decoded text region onto page at (%d, %d)", region_info.width, region_info.height, region_info.x, region_info.y);
+ jbig2_page_add_result(ctx, &ctx->pages[ctx->current_page], image, region_info.x, region_info.y, region_info.op);
}
cleanup4:
@@ -980,6 +913,5 @@
return code;
too_short:
- return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
- "Segment too short");
+ return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "Segment too short");
}
--- a/jbig2_text.h
+++ b/jbig2_text.h
@@ -69,9 +69,6 @@
int
jbig2_decode_text_region(Jbig2Ctx *ctx, Jbig2Segment *segment,
- const Jbig2TextRegionParams *params,
- const Jbig2SymbolDict * const *dicts, const int n_dicts,
- Jbig2Image *image,
- const byte *data, const size_t size,
- Jbig2ArithCx *GR_stats,
- Jbig2ArithState *as, Jbig2WordStream *ws);
+ const Jbig2TextRegionParams *params,
+ const Jbig2SymbolDict *const *dicts, const int n_dicts,
+ Jbig2Image *image, const byte *data, const size_t size, Jbig2ArithCx *GR_stats, Jbig2ArithState *as, Jbig2WordStream *ws);
--- a/jbig2dec.c
+++ b/jbig2dec.c
@@ -17,7 +17,6 @@
jbig2dec
*/
-
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
@@ -48,7 +47,7 @@
#include "jbig2_image.h"
typedef enum {
- usage,dump,render
+ usage, dump, render
} jbig2dec_mode;
typedef enum {
@@ -59,11 +58,11 @@
} jbig2dec_format;
typedef struct {
- jbig2dec_mode mode;
- int verbose, hash;
- SHA1_CTX *hash_ctx;
- char *output_file;
- jbig2dec_format output_format;
+ jbig2dec_mode mode;
+ int verbose, hash;
+ SHA1_CTX *hash_ctx;
+ char *output_file;
+ jbig2dec_format output_format;
} jbig2dec_params_t;
static int print_version(void);
@@ -73,7 +72,7 @@
static void
hash_init(jbig2dec_params_t *params)
{
- params->hash_ctx = (SHA1_CTX*)malloc(sizeof(SHA1_CTX));
+ params->hash_ctx = (SHA1_CTX *) malloc(sizeof(SHA1_CTX));
if (params->hash_ctx == NULL) {
fprintf(stderr, "unable to allocate hash state\n");
params->hash = 0;
@@ -87,6 +86,7 @@
hash_image(jbig2dec_params_t *params, Jbig2Image *image)
{
unsigned int N = image->stride * image->height;
+
SHA1_Update(params->hash_ctx, image->data, N);
}
@@ -94,12 +94,12 @@
hash_print(jbig2dec_params_t *params, FILE *out)
{
unsigned char md[SHA1_DIGEST_SIZE];
- char digest[2*SHA1_DIGEST_SIZE + 1];
+ char digest[2 * SHA1_DIGEST_SIZE + 1];
int i;
SHA1_Final(params->hash_ctx, md);
for (i = 0; i < SHA1_DIGEST_SIZE; i++) {
- snprintf(&(digest[2*i]), 3, "%02x", md[i]);
+ snprintf(&(digest[2 * i]), 3, "%02x", md[i]);
}
fprintf(out, "%s", digest);
}
@@ -118,12 +118,12 @@
/* this should really by strncasecmp()
TODO: we need to provide our own for portability */
if (!strncmp(format, "png", 3) || !strncmp(format, "PNG", 3)) {
- params->output_format=jbig2dec_format_png;
- return 0;
+ params->output_format = jbig2dec_format_png;
+ return 0;
}
#endif
/* default to pbm */
- params->output_format=jbig2dec_format_pbm;
+ params->output_format = jbig2dec_format_pbm;
return 0;
}
@@ -131,71 +131,72 @@
static int
parse_options(int argc, char *argv[], jbig2dec_params_t *params)
{
- static struct option long_options[] = {
- {"version", 0, NULL, 'V'},
- {"help", 0, NULL, 'h'},
- {"quiet", 0, NULL, 'q'},
- {"verbose", 2, NULL, 'v'},
- {"dump", 0, NULL, 'd'},
- {"hash", 0, NULL, 'm'},
- {"output", 1, NULL, 'o'},
- {"format", 1, NULL, 't'},
- {NULL, 0, NULL, 0}
- };
- int option_idx = 1;
- int option;
+ static struct option long_options[] = {
+ {"version", 0, NULL, 'V'},
+ {"help", 0, NULL, 'h'},
+ {"quiet", 0, NULL, 'q'},
+ {"verbose", 2, NULL, 'v'},
+ {"dump", 0, NULL, 'd'},
+ {"hash", 0, NULL, 'm'},
+ {"output", 1, NULL, 'o'},
+ {"format", 1, NULL, 't'},
+ {NULL, 0, NULL, 0}
+ };
+ int option_idx = 1;
+ int option;
- while (1) {
- option = getopt_long(argc, argv,
- "Vh?qv:do:t:", long_options, &option_idx);
- if (option == -1) break;
+ while (1) {
+ option = getopt_long(argc, argv, "Vh?qv:do:t:", long_options, &option_idx);
+ if (option == -1)
+ break;
- switch (option) {
- case 0: /* unknown long option */
- if (!params->verbose) fprintf(stdout,
- "unrecognized option: --%s\n",
- long_options[option_idx].name);
- break;
- case 'q':
- params->verbose = 0;
- break;
- case 'v':
- if (optarg) params->verbose = atoi(optarg);
- else params->verbose = 2;
- break;
- case 'h':
- case '?':
- params->mode = usage;
- break;
- case 'V':
- /* the GNU Coding Standards suggest --version
- should override all other options */
- print_version();
- exit(0);
- break;
- case 'd':
- params->mode=dump;
- break;
- case 'm':
- params->hash = 1;
- break;
- case 'o':
- params->output_file = strdup(optarg);
- break;
- case 't':
- set_output_format(params, optarg);
- break;
- default:
- if (!params->verbose) fprintf(stdout,
- "unrecognized option: -%c\n", option);
- break;
- }
- }
- return (optind);
+ switch (option) {
+ case 0: /* unknown long option */
+ if (!params->verbose)
+ fprintf(stdout, "unrecognized option: --%s\n", long_options[option_idx].name);
+ break;
+ case 'q':
+ params->verbose = 0;
+ break;
+ case 'v':
+ if (optarg)
+ params->verbose = atoi(optarg);
+ else
+ params->verbose = 2;
+ break;
+ case 'h':
+ case '?':
+ params->mode = usage;
+ break;
+ case 'V':
+ /* the GNU Coding Standards suggest --version
+ should override all other options */
+ print_version();
+ exit(0);
+ break;
+ case 'd':
+ params->mode = dump;
+ break;
+ case 'm':
+ params->hash = 1;
+ break;
+ case 'o':
+ params->output_file = strdup(optarg);
+ break;
+ case 't':
+ set_output_format(params, optarg);
+ break;
+ default:
+ if (!params->verbose)
+ fprintf(stdout, "unrecognized option: -%c\n", option);
+ break;
+ }
+ }
+ return (optind);
}
static int
-print_version (void)
+print_version(void)
{
fprintf(stdout, "%s %s\n", PACKAGE, VERSION);
return 0;
@@ -202,64 +203,73 @@
}
static int
-print_usage (void)
+print_usage(void)
{
- fprintf(stderr,
- "Usage: jbig2dec [options] <file.jbig2>\n"
- " or jbig2dec [options] <global_stream> <page_stream>\n"
- "\n"
- " When invoked with a single file, it attempts to parse it as\n"
- " a normal jbig2 file. Invoked with two files, it treats the\n"
- " first as the global segments, and the second as the segment\n"
- " stream for a particular page. This is useful for examining\n"
- " embedded streams.\n"
- "\n"
- " available options:\n"
- " -h --help this usage summary\n"
- " -q --quiet suppress diagnostic output\n"
- " -v --verbose set the verbosity level\n"
- " -d --dump print the structure of the jbig2 file\n"
- " rather than explicitly decoding\n"
- " --version program name and version information\n"
- " --hash print a hash of the decoded document\n"
- " -o <file> send decoded output to <file>\n"
- " Defaults to the the input with a different\n"
- " extension. Pass '-' for stdout.\n"
- " -t <type> force a particular output file format\n"
- #ifdef HAVE_LIBPNG
- " supported options are 'png' and 'pbm'\n"
- #else
- " the only supported option is 'pbm'\n"
- #endif
- "\n"
- );
+ fprintf(stderr,
+ "Usage: jbig2dec [options] <file.jbig2>\n"
+ " or jbig2dec [options] <global_stream> <page_stream>\n"
+ "\n"
+ " When invoked with a single file, it attempts to parse it as\n"
+ " a normal jbig2 file. Invoked with two files, it treats the\n"
+ " first as the global segments, and the second as the segment\n"
+ " stream for a particular page. This is useful for examining\n"
+ " embedded streams.\n"
+ "\n"
+ " available options:\n"
+ " -h --help this usage summary\n"
+ " -q --quiet suppress diagnostic output\n"
+ " -v --verbose set the verbosity level\n"
+ " -d --dump print the structure of the jbig2 file\n"
+ " rather than explicitly decoding\n"
+ " --version program name and version information\n"
+ " --hash print a hash of the decoded document\n"
+ " -o <file> send decoded output to <file>\n"
+ " Defaults to the the input with a different\n"
+ " extension. Pass '-' for stdout.\n" " -t <type> force a particular output file format\n"
+#ifdef HAVE_LIBPNG
+ " supported options are 'png' and 'pbm'\n"
+#else
+ " the only supported option is 'pbm'\n"
+#endif
+ "\n");
- return 1;
+ return 1;
}
static int
-error_callback(void *error_callback_data, const char *buf, Jbig2Severity severity,
- int32_t seg_idx)
+error_callback(void *error_callback_data, const char *buf, Jbig2Severity severity, int32_t seg_idx)
{
- const jbig2dec_params_t *params = (jbig2dec_params_t *)error_callback_data;
+ const jbig2dec_params_t *params = (jbig2dec_params_t *) error_callback_data;
char *type;
char segment[22];
switch (severity) {
- case JBIG2_SEVERITY_DEBUG:
- if (params->verbose < 3) return 0;
- type = "DEBUG"; break;;
- case JBIG2_SEVERITY_INFO:
- if (params->verbose < 2) return 0;
- type = "info"; break;;
- case JBIG2_SEVERITY_WARNING:
- if (params->verbose < 1) return 0;
- type = "WARNING"; break;;
- case JBIG2_SEVERITY_FATAL: type = "FATAL ERROR"; break;;
- default: type = "unknown message"; break;;
+ case JBIG2_SEVERITY_DEBUG:
+ if (params->verbose < 3)
+ return 0;
+ type = "DEBUG";
+ break;;
+ case JBIG2_SEVERITY_INFO:
+ if (params->verbose < 2)
+ return 0;
+ type = "info";
+ break;;
+ case JBIG2_SEVERITY_WARNING:
+ if (params->verbose < 1)
+ return 0;
+ type = "WARNING";
+ break;;
+ case JBIG2_SEVERITY_FATAL:
+ type = "FATAL ERROR";
+ break;;
+ default:
+ type = "unknown message";
+ break;;
}
- if (seg_idx == -1) segment[0] = '\0';
- else snprintf(segment, sizeof(segment), "(segment 0x%02x)", seg_idx);
+ if (seg_idx == -1)
+ segment[0] = '\0';
+ else
+ snprintf(segment, sizeof(segment), "(segment 0x%02x)", seg_idx);
fprintf(stderr, "jbig2dec %s %s %s\n", type, buf, segment);
@@ -275,37 +285,37 @@
if (extension == NULL) {
fprintf(stderr, "make_output_filename called with no extension!\n");
- exit (1);
+ exit(1);
}
if (input_filename == NULL)
- c = "out";
+ c = "out";
else {
- /* strip any leading path */
- c = strrchr(input_filename, '/'); /* *nix */
- if (c == NULL)
- c = strrchr(input_filename, '\\'); /* win32/dos */
- if (c != NULL)
- c++; /* skip the path separator */
- else
- c = input_filename; /* no leading path */
+ /* strip any leading path */
+ c = strrchr(input_filename, '/'); /* *nix */
+ if (c == NULL)
+ c = strrchr(input_filename, '\\'); /* win32/dos */
+ if (c != NULL)
+ c++; /* skip the path separator */
+ else
+ c = input_filename; /* no leading path */
}
/* make sure we haven't just stripped the last character */
if (*c == '\0')
- c = "out";
+ c = "out";
/* strip the extension */
len = strlen(c);
e = strrchr(c, '.');
if (e != NULL)
- len -= strlen(e);
+ len -= strlen(e);
/* allocate enough space for the base + ext */
- output_filename = (char*)malloc(len + strlen(extension) + 1);
+ output_filename = (char *)malloc(len + strlen(extension) + 1);
if (output_filename == NULL) {
fprintf(stderr, "couldn't allocate memory for output_filename\n");
- exit (1);
+ exit(1);
}
strncpy(output_filename, c, len);
@@ -319,42 +329,39 @@
static int
write_page_image(jbig2dec_params_t *params, Jbig2Image *image)
{
- if (!strncmp(params->output_file, "-", 2))
- {
- switch (params->output_format) {
+ if (!strncmp(params->output_file, "-", 2)) {
+ switch (params->output_format) {
#ifdef HAVE_LIBPNG
- case jbig2dec_format_png:
- jbig2_image_write_png(image, stdout);
- break;
+ case jbig2dec_format_png:
+ jbig2_image_write_png(image, stdout);
+ break;
#endif
- case jbig2dec_format_pbm:
- jbig2_image_write_pbm(image, stdout);
- break;
- default:
- fprintf(stderr, "unsupported output format.\n");
- return 1;
- }
+ case jbig2dec_format_pbm:
+ jbig2_image_write_pbm(image, stdout);
+ break;
+ default:
+ fprintf(stderr, "unsupported output format.\n");
+ return 1;
}
- else
- {
- if (params->verbose > 1)
+ } else {
+ if (params->verbose > 1)
fprintf(stderr, "saving decoded page as '%s'\n", params->output_file);
- switch (params->output_format) {
+ switch (params->output_format) {
#ifdef HAVE_LIBPNG
- case jbig2dec_format_png:
- jbig2_image_write_png_file(image, params->output_file);
- break;
+ case jbig2dec_format_png:
+ jbig2_image_write_png_file(image, params->output_file);
+ break;
#endif
- case jbig2dec_format_pbm:
- jbig2_image_write_pbm_file(image, params->output_file);
- break;
- default:
- fprintf(stderr, "unsupported output format.\n");
- return 1;
- }
+ case jbig2dec_format_pbm:
+ jbig2_image_write_pbm_file(image, params->output_file);
+ break;
+ default:
+ fprintf(stderr, "unsupported output format.\n");
+ return 1;
}
+ }
- return 0;
+ return 0;
}
static int
@@ -376,29 +383,30 @@
}
int
-main (int argc, char **argv)
+main(int argc, char **argv)
{
- FILE *f = NULL, *f_page = NULL;
- Jbig2Ctx *ctx;
- uint8_t buf[4096];
- jbig2dec_params_t params;
- int filearg;
+ FILE *f = NULL, *f_page = NULL;
+ Jbig2Ctx *ctx;
+ uint8_t buf[4096];
+ jbig2dec_params_t params;
+ int filearg;
- /* set defaults */
- params.mode = render;
- params.verbose = 1;
- params.hash = 0;
- params.output_file = NULL;
- params.output_format = jbig2dec_format_none;
+ /* set defaults */
+ params.mode = render;
+ params.verbose = 1;
+ params.hash = 0;
+ params.output_file = NULL;
+ params.output_format = jbig2dec_format_none;
- filearg = parse_options(argc, argv, ¶ms);
+ filearg = parse_options(argc, argv, ¶ms);
- if (params.hash) hash_init(¶ms);
+ if (params.hash)
+ hash_init(¶ms);
- switch (params.mode) {
+ switch (params.mode) {
case usage:
print_usage();
- exit (0);
+ exit(0);
break;
case dump:
fprintf(stderr, "Sorry, segment dump not yet implemented\n");
@@ -405,116 +413,112 @@
break;
case render:
- if ((argc - filearg) == 1)
- /* only one argument--open as a jbig2 file */
- {
- char *fn = argv[filearg];
+ if ((argc - filearg) == 1)
+ /* only one argument--open as a jbig2 file */
+ {
+ char *fn = argv[filearg];
- f = fopen(fn, "rb");
- if (f == NULL)
- {
- fprintf(stderr, "error opening %s\n", fn);
- return 1;
- }
- }
- else if ((argc - filearg) == 2)
- /* two arguments open as separate global and page streams */
- {
- char *fn = argv[filearg];
- char *fn_page = argv[filearg+1];
+ f = fopen(fn, "rb");
+ if (f == NULL) {
+ fprintf(stderr, "error opening %s\n", fn);
+ return 1;
+ }
+ } else if ((argc - filearg) == 2)
+ /* two arguments open as separate global and page streams */
+ {
+ char *fn = argv[filearg];
+ char *fn_page = argv[filearg + 1];
- f = fopen(fn, "rb");
- if (f == NULL)
- {
- fprintf(stderr, "error opening %s\n", fn);
- return 1;
- }
+ f = fopen(fn, "rb");
+ if (f == NULL) {
+ fprintf(stderr, "error opening %s\n", fn);
+ return 1;
+ }
- f_page = fopen(fn_page, "rb");
- if (f_page == NULL)
- {
- fclose(f);
- fprintf(stderr, "error opening %s\n", fn_page);
- return 1;
- }
- }
- else
- /* any other number of arguments */
- return print_usage();
+ f_page = fopen(fn_page, "rb");
+ if (f_page == NULL) {
+ fclose(f);
+ fprintf(stderr, "error opening %s\n", fn_page);
+ return 1;
+ }
+ } else
+ /* any other number of arguments */
+ return print_usage();
- ctx = jbig2_ctx_new(NULL, (Jbig2Options)(f_page != NULL ? JBIG2_OPTIONS_EMBEDDED : 0),
- NULL,
- error_callback, ¶ms);
+ ctx = jbig2_ctx_new(NULL, (Jbig2Options)(f_page != NULL ? JBIG2_OPTIONS_EMBEDDED : 0), NULL, error_callback, ¶ms);
- /* pull the whole file/global stream into memory */
- for (;;)
- {
- int n_bytes = fread(buf, 1, sizeof(buf), f);
- if (n_bytes <= 0)
- break;
- if (jbig2_data_in(ctx, buf, n_bytes))
- break;
- }
- fclose(f);
+ /* pull the whole file/global stream into memory */
+ for (;;) {
+ int n_bytes = fread(buf, 1, sizeof(buf), f);
- /* if there's a local page stream read that in its entirety */
- if (f_page != NULL)
- {
- Jbig2GlobalCtx *global_ctx = jbig2_make_global_ctx(ctx);
- ctx = jbig2_ctx_new(NULL, JBIG2_OPTIONS_EMBEDDED, global_ctx,
- error_callback, ¶ms);
- for (;;)
- {
- int n_bytes = fread(buf, 1, sizeof(buf), f_page);
- if (n_bytes <= 0)
- break;
- if (jbig2_data_in(ctx, buf, n_bytes))
- break;
- }
- fclose(f_page);
- jbig2_global_ctx_free(global_ctx);
- }
+ if (n_bytes <= 0)
+ break;
+ if (jbig2_data_in(ctx, buf, n_bytes))
+ break;
+ }
+ fclose(f);
- /* retrieve and output the returned pages */
- {
- Jbig2Image *image;
+ /* if there's a local page stream read that in its entirety */
+ if (f_page != NULL) {
+ Jbig2GlobalCtx *global_ctx = jbig2_make_global_ctx(ctx);
- /* work around broken CVision embedded streams */
- if (f_page != NULL)
- jbig2_complete_page(ctx);
+ ctx = jbig2_ctx_new(NULL, JBIG2_OPTIONS_EMBEDDED, global_ctx, error_callback, ¶ms);
+ for (;;) {
+ int n_bytes = fread(buf, 1, sizeof(buf), f_page);
- if (params.output_file == NULL)
- {
+ if (n_bytes <= 0)
+ break;
+ if (jbig2_data_in(ctx, buf, n_bytes))
+ break;
+ }
+ fclose(f_page);
+ jbig2_global_ctx_free(global_ctx);
+ }
+
+ /* retrieve and output the returned pages */
+ {
+ Jbig2Image *image;
+
+ /* work around broken CVision embedded streams */
+ if (f_page != NULL)
+ jbig2_complete_page(ctx);
+
+ if (params.output_file == NULL) {
#ifdef HAVE_LIBPNG
- params.output_file = make_output_filename(argv[filearg], ".png");
- params.output_format = jbig2dec_format_png;
+ params.output_file = make_output_filename(argv[filearg], ".png");
+ params.output_format = jbig2dec_format_png;
#else
- params.output_file = make_output_filename(argv[filearg], ".pbm");
- params.output_format = jbig2dec_format_pbm;
+ params.output_file = make_output_filename(argv[filearg], ".pbm");
+ params.output_format = jbig2dec_format_pbm;
#endif
- } else {
- int len = strlen(params.output_file);
- if ((len >= 3) && (params.output_format == jbig2dec_format_none))
- /* try to set the output type by the given extension */
- set_output_format(¶ms, params.output_file + len - 3);
- }
+ } else {
+ int len = strlen(params.output_file);
- /* retrieve and write out all the completed pages */
- while ((image = jbig2_page_out(ctx)) != NULL) {
- write_page_image(¶ms, image);
- if (params.hash) hash_image(¶ms, image);
- jbig2_release_page(ctx, image);
- }
- if (params.hash) write_document_hash(¶ms);
- }
+ if ((len >= 3) && (params.output_format == jbig2dec_format_none))
+ /* try to set the output type by the given extension */
+ set_output_format(¶ms, params.output_file + len - 3);
+ }
- jbig2_ctx_free(ctx);
+ /* retrieve and write out all the completed pages */
+ while ((image = jbig2_page_out(ctx)) != NULL) {
+ write_page_image(¶ms, image);
+ if (params.hash)
+ hash_image(¶ms, image);
+ jbig2_release_page(ctx, image);
+ }
+ if (params.hash)
+ write_document_hash(¶ms);
+ }
- } /* end params.mode switch */
+ jbig2_ctx_free(ctx);
- if (params.output_file) free(params.output_file);
- if (params.hash) hash_free(¶ms);
+ } /* end params.mode switch */
- /* fin */
- return 0;
+ if (params.output_file)
+ free(params.output_file);
+ if (params.hash)
+ hash_free(¶ms);
+
+ /* fin */
+ return 0;
}
--- a/memcmp.c
+++ b/memcmp.c
@@ -32,22 +32,23 @@
* considered as unsigned chars
*/
-int memcmp(const void *b1, const void *b2, size_t len)
+int
+memcmp(const void *b1, const void *b2, size_t len)
{
- unsigned char *a, *b;
- size_t i;
+ unsigned char *a, *b;
+ size_t i;
- a = (unsigned char *)b1;
- b = (unsigned char *)b2;
- for(i = 0; i < len; i++) {
- if (*a != *b) {
- /* strings differ */
- return (*a < *b) ? -1 : 1;
- }
- a++;
- b++;
- }
+ a = (unsigned char *)b1;
+ b = (unsigned char *)b2;
+ for (i = 0; i < len; i++) {
+ if (*a != *b) {
+ /* strings differ */
+ return (*a < *b) ? -1 : 1;
+ }
+ a++;
+ b++;
+ }
- /* strings match */
- return 0;
+ /* strings match */
+ return 0;
}
--- a/memento.c
+++ b/memento.c
@@ -13,10 +13,8 @@
CA 94903, U.S.A., +1(415)492-9861, for further information.
*/
-
/* Inspired by Fortify by Simon P Bullen. */
-
/* Set the following if you're only looking for leaks, not memory overwrites
* to speed the operation */
/* #define MEMENTO_LEAKONLY */
@@ -40,7 +38,7 @@
#include <string.h>
#else
#ifndef memset
-void *memset(void *,int,size_t);
+void *memset(void *, int, size_t);
#endif
#endif
int atexit(void (*)(void));
@@ -59,8 +57,8 @@
/* Define the underlying allocators, just in case */
void *MEMENTO_UNDERLYING_MALLOC(size_t);
void MEMENTO_UNDERLYING_FREE(void *);
-void *MEMENTO_UNDERLYING_REALLOC(void *,size_t);
-void *MEMENTO_UNDERLYING_CALLOC(size_t,size_t);
+void *MEMENTO_UNDERLYING_REALLOC(void *, size_t);
+void *MEMENTO_UNDERLYING_CALLOC(size_t, size_t);
/* And some other standard functions we use. We don't include the header
* files, just in case they pull in unexpected others. */
@@ -90,7 +88,7 @@
#endif
enum {
- Memento_PreSize = 16,
+ Memento_PreSize = 16,
Memento_PostSize = 16
};
@@ -111,7 +109,7 @@
* As a hack to get around this, we have a define MEMENTO_SKIP_SEARCH that
* indicates how many bytes to skip over at the start of the chunk.
* This may cause us to miss true nestings, but such is life...
- */
+ */
#ifndef MEMENTO_SEARCH_SKIP
#ifdef MEMENTO_GS_HACKS
#define MEMENTO_SEARCH_SKIP (2*sizeof(void *))
@@ -122,61 +120,59 @@
typedef struct Memento_BlkHeader Memento_BlkHeader;
-struct Memento_BlkHeader
-{
- size_t rawsize;
- int sequence;
- int lastCheckedOK;
- int flags;
+struct Memento_BlkHeader {
+ size_t rawsize;
+ int sequence;
+ int lastCheckedOK;
+ int flags;
Memento_BlkHeader *next;
- Memento_BlkHeader *parent; /* Only used while printing out nested list */
+ Memento_BlkHeader *parent; /* Only used while printing out nested list */
- const char *label;
+ const char *label;
/* Entries for nesting display calculations */
Memento_BlkHeader *child;
Memento_BlkHeader *sibling;
- char preblk[Memento_PreSize];
+ char preblk[Memento_PreSize];
};
/* In future this could (should) be a smarter data structure, like, say,
* splay trees. For now, we use a list.
*/
-typedef struct Memento_Blocks
-{
- Memento_BlkHeader *head;
+typedef struct Memento_Blocks {
+ Memento_BlkHeader *head;
Memento_BlkHeader **tail;
} Memento_Blocks;
/* And our global structure */
static struct {
- int inited;
+ int inited;
Memento_Blocks used;
Memento_Blocks free;
- size_t freeListSize;
- int sequence;
- int paranoia;
- int paranoidAt;
- int countdown;
- int lastChecked;
- int breakAt;
- int failAt;
- int failing;
- int nextFailAt;
- int squeezeAt;
- int squeezing;
- int segv;
- int pattern;
- int nextPattern;
- int patternBit;
- size_t maxMemory;
- size_t alloc;
- size_t peakAlloc;
- size_t totalAlloc;
- size_t numMallocs;
- size_t numFrees;
- size_t numReallocs;
+ size_t freeListSize;
+ int sequence;
+ int paranoia;
+ int paranoidAt;
+ int countdown;
+ int lastChecked;
+ int breakAt;
+ int failAt;
+ int failing;
+ int nextFailAt;
+ int squeezeAt;
+ int squeezing;
+ int segv;
+ int pattern;
+ int nextPattern;
+ int patternBit;
+ size_t maxMemory;
+ size_t alloc;
+ size_t peakAlloc;
+ size_t totalAlloc;
+ size_t numMallocs;
+ size_t numFrees;
+ size_t numReallocs;
} globals;
#define MEMENTO_EXTRASIZE (sizeof(Memento_BlkHeader) + Memento_PostSize)
@@ -191,10 +187,11 @@
#define MEMBLK_POSTPTR(B) \
(&((char *)(void *)(B))[(B)->rawsize + sizeof(Memento_BlkHeader)])
-void Memento_breakpoint(void)
+void
+Memento_breakpoint(void)
{
/* A handy externally visible function for breakpointing */
-#if 0 /* Enable this to force automatic breakpointing */
+#if 0 /* Enable this to force automatic breakpointing */
#ifdef DEBUG
#ifdef _MSC_VER
__asm int 3;
@@ -203,15 +200,14 @@
#endif
}
-static void Memento_addBlockHead(Memento_Blocks *blks,
- Memento_BlkHeader *b,
- int type)
+static void
+Memento_addBlockHead(Memento_Blocks *blks, Memento_BlkHeader *b, int type)
{
if (blks->tail == &blks->head) {
/* Adding into an empty list, means the tail changes too */
blks->tail = &b->next;
}
- b->next = blks->head;
+ b->next = blks->head;
blks->head = b;
#ifndef MEMENTO_LEAKONLY
memset(b->preblk, MEMENTO_PREFILL, Memento_PreSize);
@@ -218,21 +214,20 @@
memset(MEMBLK_POSTPTR(b), MEMENTO_POSTFILL, Memento_PostSize);
#endif
VALGRIND_MAKE_MEM_NOACCESS(MEMBLK_POSTPTR(b), Memento_PostSize);
- if (type == 0) { /* malloc */
+ if (type == 0) { /* malloc */
VALGRIND_MAKE_MEM_UNDEFINED(MEMBLK_TOBLK(b), b->rawsize);
- } else if (type == 1) { /* free */
+ } else if (type == 1) { /* free */
VALGRIND_MAKE_MEM_NOACCESS(MEMBLK_TOBLK(b), b->rawsize);
}
VALGRIND_MAKE_MEM_NOACCESS(b, sizeof(Memento_BlkHeader));
}
-static void Memento_addBlockTail(Memento_Blocks *blks,
- Memento_BlkHeader *b,
- int type)
+static void
+Memento_addBlockTail(Memento_Blocks *blks, Memento_BlkHeader *b, int type)
{
VALGRIND_MAKE_MEM_DEFINED(blks->tail, sizeof(Memento_BlkHeader *));
*blks->tail = b;
- blks->tail = &b->next;
+ blks->tail = &b->next;
b->next = NULL;
VALGRIND_MAKE_MEM_NOACCESS(blks->tail, sizeof(Memento_BlkHeader *));
#ifndef MEMENTO_LEAKONLY
@@ -240,9 +235,9 @@
memset(MEMBLK_POSTPTR(b), MEMENTO_POSTFILL, Memento_PostSize);
#endif
VALGRIND_MAKE_MEM_NOACCESS(MEMBLK_POSTPTR(b), Memento_PostSize);
- if (type == 0) { /* malloc */
+ if (type == 0) { /* malloc */
VALGRIND_MAKE_MEM_UNDEFINED(MEMBLK_TOBLK(b), b->rawsize);
- } else if (type == 1) { /* free */
+ } else if (type == 1) { /* free */
VALGRIND_MAKE_MEM_NOACCESS(MEMBLK_TOBLK(b), b->rawsize);
}
VALGRIND_MAKE_MEM_NOACCESS(b, sizeof(Memento_BlkHeader));
@@ -256,13 +251,14 @@
int index;
} BlkCheckData;
-static int Memento_Internal_checkAllocedBlock(Memento_BlkHeader *b, void *arg)
+static int
+Memento_Internal_checkAllocedBlock(Memento_BlkHeader *b, void *arg)
{
#ifndef MEMENTO_LEAKONLY
- int i;
- char *p;
- int corrupt = 0;
- BlkCheckData *data = (BlkCheckData *)arg;
+ int i;
+ char *p;
+ int corrupt = 0;
+ BlkCheckData *data = (BlkCheckData *) arg;
p = b->preblk;
i = Memento_PreSize;
@@ -288,18 +284,19 @@
return 0;
}
-static int Memento_Internal_checkFreedBlock(Memento_BlkHeader *b, void *arg)
+static int
+Memento_Internal_checkFreedBlock(Memento_BlkHeader *b, void *arg)
{
#ifndef MEMENTO_LEAKONLY
- int i;
- char *p;
- BlkCheckData *data = (BlkCheckData *)arg;
+ int i;
+ char *p;
+ BlkCheckData *data = (BlkCheckData *) arg;
p = MEMBLK_TOBLK(b);
i = b->rawsize;
/* Attempt to speed this up by checking an (aligned) int at a time */
do {
- if (((size_t)p) & 1) {
+ if (((size_t) p) & 1) {
if (*p++ != (char)MEMENTO_FREEFILL)
break;
i--;
@@ -306,8 +303,8 @@
if (i == 0)
break;
}
- if ((i >= 2) && (((size_t)p) & 2)) {
- if (*(short *)p != (short)(MEMENTO_FREEFILL | (MEMENTO_FREEFILL<<8)))
+ if ((i >= 2) && (((size_t) p) & 2)) {
+ if (*(short *)p != (short)(MEMENTO_FREEFILL | (MEMENTO_FREEFILL << 8)))
goto mismatch;
p += 2;
i -= 2;
@@ -316,17 +313,14 @@
}
i -= 4;
while (i >= 0) {
- if (*(int *)p != (MEMENTO_FREEFILL |
- (MEMENTO_FREEFILL<<8) |
- (MEMENTO_FREEFILL<<16) |
- (MEMENTO_FREEFILL<<24)))
+ if (*(int *)p != (MEMENTO_FREEFILL | (MEMENTO_FREEFILL << 8) | (MEMENTO_FREEFILL << 16) | (MEMENTO_FREEFILL << 24)))
goto mismatch;
p += 4;
i -= 4;
}
i += 4;
- if ((i >= 2) && (((size_t)p) & 2)) {
- if (*(short *)p != (short)(MEMENTO_FREEFILL | (MEMENTO_FREEFILL<<8)))
+ if ((i >= 2) && (((size_t) p) & 2)) {
+ if (*(short *)p != (short)(MEMENTO_FREEFILL | (MEMENTO_FREEFILL << 8)))
goto mismatch;
p += 2;
i -= 2;
@@ -340,7 +334,7 @@
} while (0);
if (i) {
data->freeCorrupt = 1;
- data->index = b->rawsize-i;
+ data->index = b->rawsize - i;
}
return Memento_Internal_checkAllocedBlock(b, arg);
#else
@@ -348,11 +342,12 @@
#endif
}
-static void Memento_removeBlock(Memento_Blocks *blks,
- Memento_BlkHeader *b)
+static void
+Memento_removeBlock(Memento_Blocks *blks, Memento_BlkHeader *b)
{
Memento_BlkHeader *head = blks->head;
Memento_BlkHeader *prev = NULL;
+
while ((head) && (head != b)) {
VALGRIND_MAKE_MEM_DEFINED(head, sizeof(*head));
prev = head;
@@ -389,7 +384,8 @@
}
}
-static int Memento_Internal_makeSpace(size_t space)
+static int
+Memento_Internal_makeSpace(size_t space)
{
/* If too big, it can never go on the freelist */
if (space > MEMENTO_FREELIST_MAX_SINGLE_BLOCK)
@@ -399,6 +395,7 @@
/* Ditch blocks until it fits within our limit */
while (globals.freeListSize > MEMENTO_FREELIST_MAX) {
Memento_BlkHeader *head = globals.free.head;
+
VALGRIND_MAKE_MEM_DEFINED(head, sizeof(*head));
globals.free.head = head->next;
globals.freeListSize -= MEMBLK_SIZE(head->rawsize);
@@ -411,18 +408,16 @@
return 1;
}
-static int Memento_appBlocks(Memento_Blocks *blks,
- int (*app)(Memento_BlkHeader *,
- void *),
- void *arg)
+static int
+Memento_appBlocks(Memento_Blocks *blks, int (*app)(Memento_BlkHeader *, void *), void *arg)
{
Memento_BlkHeader *head = blks->head;
Memento_BlkHeader *next;
- int result;
+ int result;
+
while (head) {
VALGRIND_MAKE_MEM_DEFINED(head, sizeof(Memento_BlkHeader));
- VALGRIND_MAKE_MEM_DEFINED(MEMBLK_TOBLK(head),
- head->rawsize + Memento_PostSize);
+ VALGRIND_MAKE_MEM_DEFINED(MEMBLK_TOBLK(head), head->rawsize + Memento_PostSize);
result = app(head, arg);
next = head->next;
VALGRIND_MAKE_MEM_NOACCESS(MEMBLK_POSTPTR(head), Memento_PostSize);
@@ -434,15 +429,13 @@
return 0;
}
-static int Memento_appBlock(Memento_Blocks *blks,
- int (*app)(Memento_BlkHeader *,
- void *),
- void *arg,
- Memento_BlkHeader *b)
+static int
+Memento_appBlock(Memento_Blocks *blks, int (*app)(Memento_BlkHeader *, void *), void *arg, Memento_BlkHeader *b)
{
Memento_BlkHeader *head = blks->head;
Memento_BlkHeader *next;
- int result;
+ int result;
+
while (head && head != b) {
VALGRIND_MAKE_MEM_DEFINED(head, sizeof(Memento_BlkHeader));
next = head->next;
@@ -451,8 +444,7 @@
}
if (head == b) {
VALGRIND_MAKE_MEM_DEFINED(head, sizeof(Memento_BlkHeader));
- VALGRIND_MAKE_MEM_DEFINED(MEMBLK_TOBLK(head),
- head->rawsize + Memento_PostSize);
+ VALGRIND_MAKE_MEM_DEFINED(MEMBLK_TOBLK(head), head->rawsize + Memento_PostSize);
result = app(head, arg);
VALGRIND_MAKE_MEM_NOACCESS(MEMBLK_POSTPTR(head), Memento_PostSize);
VALGRIND_MAKE_MEM_NOACCESS(head, sizeof(Memento_BlkHeader));
@@ -461,56 +453,60 @@
return 0;
}
-static void showBlock(Memento_BlkHeader *b, int space)
+static void
+showBlock(Memento_BlkHeader *b, int space)
{
- fprintf(stderr, "0x%p:(size=%d,num=%d)",
- MEMBLK_TOBLK(b), (int)b->rawsize, b->sequence);
+ fprintf(stderr, "0x%p:(size=%d,num=%d)", MEMBLK_TOBLK(b), (int)b->rawsize, b->sequence);
if (b->label)
fprintf(stderr, "%c(%s)", space, b->label);
}
-static void blockDisplay(Memento_BlkHeader *b, int n)
+static void
+blockDisplay(Memento_BlkHeader *b, int n)
{
n++;
- while(n > 0)
- {
+ while (n > 0) {
int i = n;
+
if (i > 32)
i = 32;
n -= i;
- fprintf(stderr, "%s", &" "[32-i]);
+ fprintf(stderr, "%s", &" "[32 - i]);
}
showBlock(b, '\t');
fprintf(stderr, "\n");
}
-static int Memento_listBlock(Memento_BlkHeader *b,
- void *arg)
+static int
+Memento_listBlock(Memento_BlkHeader *b, void *arg)
{
int *counts = (int *)arg;
+
blockDisplay(b, 0);
counts[0]++;
- counts[1]+= b->rawsize;
+ counts[1] += b->rawsize;
return 0;
}
-static void doNestedDisplay(Memento_BlkHeader *b,
- int depth)
+static void
+doNestedDisplay(Memento_BlkHeader *b, int depth)
{
blockDisplay(b, depth);
for (b = b->child; b; b = b->sibling)
- doNestedDisplay(b, depth+1);
+ doNestedDisplay(b, depth + 1);
}
-static int ptrcmp(const void *a_, const void *b_)
+static int
+ptrcmp(const void *a_, const void *b_)
{
const char **a = (const char **)a_;
const char **b = (const char **)b_;
- return (int)(*a-*b);
+
+ return (int)(*a - *b);
}
-static
-int Memento_listBlocksNested(void)
+static int
+Memento_listBlocksNested(void)
{
int count, size, i;
Memento_BlkHeader *b;
@@ -536,6 +532,7 @@
mask = (long)minptr;
for (i = 0; b; b = b->next, i++) {
void *p = MEMBLK_TOBLK(b);
+
mask &= (long)p;
if (p < minptr)
minptr = p;
@@ -543,9 +540,9 @@
maxptr = p;
blocks[i] = p;
b->flags &= ~Memento_Flag_HasParent;
- b->child = NULL;
+ b->child = NULL;
b->sibling = NULL;
- b->parent = NULL;
+ b->parent = NULL;
}
qsort(blocks, count, sizeof(void *), ptrcmp);
@@ -553,6 +550,7 @@
for (b = globals.used.head; b; b = b->next) {
char *p = MEMBLK_TOBLK(b);
int end = (b->rawsize < MEMENTO_PTRSEARCH ? b->rawsize : MEMENTO_PTRSEARCH);
+
for (i = MEMENTO_SEARCH_SKIP; i < end; i += sizeof(void *)) {
void *q = *(void **)(&p[i]);
void **r;
@@ -603,12 +601,13 @@
return 0;
}
-void Memento_listBlocks(void)
+void
+Memento_listBlocks(void)
{
fprintf(stderr, "Allocated blocks:\n");
- if (Memento_listBlocksNested())
- {
+ if (Memento_listBlocksNested()) {
int counts[2];
+
counts[0] = 0;
counts[1] = 0;
Memento_appBlocks(&globals.used, Memento_listBlock, &counts[0]);
@@ -617,8 +616,8 @@
}
}
-static int Memento_listNewBlock(Memento_BlkHeader *b,
- void *arg)
+static int
+Memento_listNewBlock(Memento_BlkHeader *b, void *arg)
{
if (b->flags & Memento_Flag_OldBlock)
return 0;
@@ -626,8 +625,11 @@
return Memento_listBlock(b, arg);
}
-void Memento_listNewBlocks(void) {
+void
+Memento_listNewBlocks(void)
+{
int counts[2];
+
counts[0] = 0;
counts[1] = 0;
fprintf(stderr, "Blocks allocated and still extant since last list:\n");
@@ -636,23 +638,25 @@
fprintf(stderr, " Total size of blocks = %d\n", counts[1]);
}
-static void Memento_endStats(void)
+static void
+Memento_endStats(void)
{
fprintf(stderr, "Total memory malloced = %u bytes\n", (unsigned int)globals.totalAlloc);
fprintf(stderr, "Peak memory malloced = %u bytes\n", (unsigned int)globals.peakAlloc);
- fprintf(stderr, "%u mallocs, %u frees, %u reallocs\n", (unsigned int)globals.numMallocs,
- (unsigned int)globals.numFrees, (unsigned int)globals.numReallocs);
+ fprintf(stderr, "%u mallocs, %u frees, %u reallocs\n", (unsigned int)globals.numMallocs, (unsigned int)globals.numFrees, (unsigned int)globals.numReallocs);
fprintf(stderr, "Average allocation size %u bytes\n", (unsigned int)
- (globals.numMallocs != 0 ? globals.totalAlloc/globals.numMallocs: 0));
+ (globals.numMallocs != 0 ? globals.totalAlloc / globals.numMallocs : 0));
}
-void Memento_stats(void)
+void
+Memento_stats(void)
{
fprintf(stderr, "Current memory malloced = %u bytes\n", (unsigned int)globals.alloc);
Memento_endStats();
}
-static void Memento_fin(void)
+static void
+Memento_fin(void)
{
Memento_checkAllMemory();
Memento_endStats();
@@ -668,33 +672,34 @@
else
fprintf(stderr, "Memory squeezing @ %d (%d) complete\n", globals.squeezeAt, globals.pattern);
}
- if (globals.failing)
- {
+ if (globals.failing) {
fprintf(stderr, "MEMENTO_FAILAT=%d\n", globals.failAt);
fprintf(stderr, "MEMENTO_PATTERN=%d\n", globals.pattern);
}
- if (globals.nextFailAt != 0)
- {
+ if (globals.nextFailAt != 0) {
fprintf(stderr, "MEMENTO_NEXTFAILAT=%d\n", globals.nextFailAt);
fprintf(stderr, "MEMENTO_NEXTPATTERN=%d\n", globals.nextPattern);
}
}
-static void Memento_inited(void)
+static void
+Memento_inited(void)
{
/* A good place for a breakpoint */
}
-static void Memento_init(void)
+static void
+Memento_init(void)
{
char *env;
+
memset(&globals, 0, sizeof(globals));
- globals.inited = 1;
+ globals.inited = 1;
globals.used.head = NULL;
globals.used.tail = &globals.used.head;
globals.free.head = NULL;
globals.free.tail = &globals.free.head;
- globals.sequence = 0;
+ globals.sequence = 0;
globals.countdown = 1024;
env = getenv("MEMENTO_FAILAT");
@@ -741,7 +746,8 @@
extern size_t backtrace(void **, int);
extern void backtrace_symbols_fd(void **, size_t, int);
-static void Memento_signal(void)
+static void
+Memento_signal(void)
{
fprintf(stderr, "SEGV after Memory squeezing @ %d\n", globals.squeezeAt);
@@ -748,14 +754,14 @@
#ifdef MEMENTO_STACKTRACE_METHOD
#if MEMENTO_STACKTRACE_METHOD == 1
{
- void *array[100];
- size_t size;
+ void *array[100];
+ size_t size;
- size = backtrace(array, 100);
- fprintf(stderr, "------------------------------------------------------------------------\n");
- fprintf(stderr, "Backtrace:\n");
- backtrace_symbols_fd(array, size, 2);
- fprintf(stderr, "------------------------------------------------------------------------\n");
+ size = backtrace(array, 100);
+ fprintf(stderr, "------------------------------------------------------------------------\n");
+ fprintf(stderr, "Backtrace:\n");
+ backtrace_symbols_fd(array, size, 2);
+ fprintf(stderr, "------------------------------------------------------------------------\n");
}
#endif
#endif
@@ -763,7 +769,8 @@
exit(1);
}
-static int squeeze(void)
+static int
+squeeze(void)
{
pid_t pid;
int i, status;
@@ -787,7 +794,8 @@
for (i = 0; i < OPEN_MAX; i++) {
if (stashed_map[i] == 0) {
int j = dup(i);
- stashed_map[j] = i+1;
+
+ stashed_map[j] = i + 1;
}
}
@@ -819,7 +827,7 @@
/* Put the files back */
for (i = 0; i < OPEN_MAX; i++) {
if (stashed_map[i] != 0) {
- dup2(i, stashed_map[i]-1);
+ dup2(i, stashed_map[i] - 1);
close(i);
stashed_map[i] = 0;
}
@@ -830,7 +838,8 @@
#else
#include <signal.h>
-static void Memento_signal(void)
+static void
+Memento_signal(void)
{
globals.segv = 1;
/* If we just return from this function the SEGV will be unhandled, and
@@ -843,7 +852,8 @@
Memento_fin();
}
-int squeeze(void)
+int
+squeeze(void)
{
fprintf(stderr, "Memento memory squeezing disabled as no fork!\n");
return 0;
@@ -850,7 +860,8 @@
}
#endif
-static void Memento_startFailing(void)
+static void
+Memento_startFailing(void)
{
if (!globals.failing) {
fprintf(stderr, "Starting to fail...\n");
@@ -857,7 +868,7 @@
fflush(stderr);
globals.failing = 1;
globals.failAt = globals.sequence;
- globals.nextFailAt = globals.sequence+1;
+ globals.nextFailAt = globals.sequence + 1;
globals.pattern = 0;
globals.patternBit = 0;
signal(SIGSEGV, Memento_signal);
@@ -866,7 +877,8 @@
}
}
-static void Memento_event(void)
+static void
+Memento_event(void)
{
globals.sequence++;
if ((globals.sequence >= globals.paranoidAt) && (globals.paranoidAt != 0)) {
@@ -884,13 +896,15 @@
}
}
-int Memento_breakAt(int event)
+int
+Memento_breakAt(int event)
{
globals.breakAt = event;
return event;
}
-void *Memento_label(void *ptr, const char *label)
+void *
+Memento_label(void *ptr, const char *label)
{
Memento_BlkHeader *block;
@@ -901,7 +915,8 @@
return ptr;
}
-int Memento_failThisEvent(void)
+int
+Memento_failThisEvent(void)
{
int failThisOne;
@@ -922,11 +937,7 @@
/* If we are failing, and we've reached the end of the pattern and we've
* still got bits available in the pattern word, and we haven't already
* set a nextPattern, then extend the pattern. */
- if (globals.failing &&
- ((~(globals.patternBit-1) & globals.pattern) == 0) &&
- (globals.patternBit != 0) &&
- globals.nextPattern == 0)
- {
+ if (globals.failing && ((~(globals.patternBit - 1) & globals.pattern) == 0) && (globals.patternBit != 0) && globals.nextPattern == 0) {
/* We'll fail this one, and set the 'next' one to pass it. */
globals.nextFailAt = globals.failAt;
globals.nextPattern = globals.pattern | globals.patternBit;
@@ -936,10 +947,11 @@
return failThisOne;
}
-void *Memento_malloc(size_t s)
+void *
+Memento_malloc(size_t s)
{
Memento_BlkHeader *memblk;
- size_t smem = MEMBLK_SIZE(s);
+ size_t smem = MEMBLK_SIZE(s);
if (Memento_failThisEvent())
return NULL;
@@ -956,7 +968,7 @@
if (memblk == NULL)
return NULL;
- globals.alloc += s;
+ globals.alloc += s;
globals.totalAlloc += s;
if (globals.peakAlloc < globals.alloc)
globals.peakAlloc = globals.alloc;
@@ -963,34 +975,35 @@
#ifndef MEMENTO_LEAKONLY
memset(MEMBLK_TOBLK(memblk), MEMENTO_ALLOCFILL, s);
#endif
- memblk->rawsize = s;
- memblk->sequence = globals.sequence;
+ memblk->rawsize = s;
+ memblk->sequence = globals.sequence;
memblk->lastCheckedOK = memblk->sequence;
- memblk->flags = 0;
- memblk->label = 0;
- memblk->child = NULL;
- memblk->sibling = NULL;
+ memblk->flags = 0;
+ memblk->label = 0;
+ memblk->child = NULL;
+ memblk->sibling = NULL;
Memento_addBlockHead(&globals.used, memblk, 0);
return MEMBLK_TOBLK(memblk);
}
-void *Memento_calloc(size_t n, size_t s)
+void *
+Memento_calloc(size_t n, size_t s)
{
- void *block = Memento_malloc(n*s);
+ void *block = Memento_malloc(n * s);
if (block)
- memset(block, 0, n*s);
+ memset(block, 0, n * s);
return block;
}
-static int checkBlock(Memento_BlkHeader *memblk, const char *action)
+static int
+checkBlock(Memento_BlkHeader *memblk, const char *action)
{
#ifndef MEMENTO_LEAKONLY
BlkCheckData data;
memset(&data, 0, sizeof(data));
- Memento_appBlock(&globals.used, Memento_Internal_checkAllocedBlock,
- &data, memblk);
+ Memento_appBlock(&globals.used, Memento_Internal_checkAllocedBlock, &data, memblk);
if (!data.found) {
/* Failure! */
fprintf(stderr, "Attempt to %s block ", action);
@@ -1007,8 +1020,7 @@
if (data.postCorrupt) {
fprintf(stderr, "Postguard corrupted\n");
}
- fprintf(stderr, "Block last checked OK at allocation %d. Now %d.\n",
- memblk->lastCheckedOK, globals.sequence);
+ fprintf(stderr, "Block last checked OK at allocation %d. Now %d.\n", memblk->lastCheckedOK, globals.sequence);
Memento_breakpoint();
return 1;
}
@@ -1016,7 +1028,8 @@
return 0;
}
-void Memento_free(void *blk)
+void
+Memento_free(void *blk)
{
Memento_BlkHeader *memblk;
@@ -1045,8 +1058,7 @@
VALGRIND_MAKE_MEM_DEFINED(memblk, sizeof(*memblk));
if (Memento_Internal_makeSpace(MEMBLK_SIZE(memblk->rawsize))) {
VALGRIND_MAKE_MEM_DEFINED(memblk, sizeof(*memblk));
- VALGRIND_MAKE_MEM_DEFINED(MEMBLK_TOBLK(memblk),
- memblk->rawsize + Memento_PostSize);
+ VALGRIND_MAKE_MEM_DEFINED(MEMBLK_TOBLK(memblk), memblk->rawsize + Memento_PostSize);
#ifndef MEMENTO_LEAKONLY
memset(MEMBLK_TOBLK(memblk), MEMENTO_FREEFILL, memblk->rawsize);
#endif
@@ -1056,11 +1068,12 @@
}
}
-void *Memento_realloc(void *blk, size_t newsize)
+void *
+Memento_realloc(void *blk, size_t newsize)
{
Memento_BlkHeader *memblk, *newmemblk;
- size_t newsizemem;
- int flags;
+ size_t newsizemem;
+ int flags;
if (blk == NULL)
return Memento_malloc(newsize);
@@ -1072,7 +1085,7 @@
if (Memento_failThisEvent())
return NULL;
- memblk = MEMBLK_FROMBLK(blk);
+ memblk = MEMBLK_FROMBLK(blk);
if (checkBlock(memblk, "realloc"))
return NULL;
@@ -1085,21 +1098,21 @@
newsizemem = MEMBLK_SIZE(newsize);
Memento_removeBlock(&globals.used, memblk);
flags = memblk->flags;
- newmemblk = MEMENTO_UNDERLYING_REALLOC(memblk, newsizemem);
- if (newmemblk == NULL)
- {
+ newmemblk = MEMENTO_UNDERLYING_REALLOC(memblk, newsizemem);
+ if (newmemblk == NULL) {
Memento_addBlockHead(&globals.used, memblk, 2);
return NULL;
}
globals.numReallocs++;
globals.totalAlloc += newsize;
- globals.alloc -= newmemblk->rawsize;
- globals.alloc += newsize;
+ globals.alloc -= newmemblk->rawsize;
+ globals.alloc += newsize;
if (globals.peakAlloc < globals.alloc)
globals.peakAlloc = globals.alloc;
newmemblk->flags = flags;
if (newmemblk->rawsize < newsize) {
- char *newbytes = ((char *)MEMBLK_TOBLK(newmemblk))+newmemblk->rawsize;
+ char *newbytes = ((char *)MEMBLK_TOBLK(newmemblk)) + newmemblk->rawsize;
+
#ifndef MEMENTO_LEAKONLY
memset(newbytes, MEMENTO_ALLOCFILL, newsize - newmemblk->rawsize);
#endif
@@ -1114,7 +1127,8 @@
return MEMBLK_TOBLK(newmemblk);
}
-int Memento_checkBlock(void *blk)
+int
+Memento_checkBlock(void *blk)
{
Memento_BlkHeader *memblk;
@@ -1124,9 +1138,10 @@
return checkBlock(memblk, "check");
}
-static int Memento_Internal_checkAllAlloced(Memento_BlkHeader *memblk, void *arg)
+static int
+Memento_Internal_checkAllAlloced(Memento_BlkHeader *memblk, void *arg)
{
- BlkCheckData *data = (BlkCheckData *)arg;
+ BlkCheckData *data = (BlkCheckData *) arg;
Memento_Internal_checkAllocedBlock(memblk, data);
if (data->preCorrupt || data->postCorrupt) {
@@ -1140,24 +1155,21 @@
fprintf(stderr, " Preguard ");
}
if (data->postCorrupt) {
- fprintf(stderr, "%s Postguard ",
- (data->preCorrupt ? "&" : ""));
+ fprintf(stderr, "%s Postguard ", (data->preCorrupt ? "&" : ""));
}
- fprintf(stderr, "corrupted.\n "
- "Block last checked OK at allocation %d. Now %d.\n",
- memblk->lastCheckedOK, globals.sequence);
- data->preCorrupt = 0;
+ fprintf(stderr, "corrupted.\n " "Block last checked OK at allocation %d. Now %d.\n", memblk->lastCheckedOK, globals.sequence);
+ data->preCorrupt = 0;
data->postCorrupt = 0;
data->freeCorrupt = 0;
- }
- else
+ } else
memblk->lastCheckedOK = globals.sequence;
return 0;
}
-static int Memento_Internal_checkAllFreed(Memento_BlkHeader *memblk, void *arg)
+static int
+Memento_Internal_checkAllFreed(Memento_BlkHeader *memblk, void *arg)
{
- BlkCheckData *data = (BlkCheckData *)arg;
+ BlkCheckData *data = (BlkCheckData *) arg;
Memento_Internal_checkFreedBlock(memblk, data);
if (data->preCorrupt || data->postCorrupt || data->freeCorrupt) {
@@ -1168,8 +1180,7 @@
fprintf(stderr, " ");
showBlock(memblk, ' ');
if (data->freeCorrupt) {
- fprintf(stderr, " index %d (address 0x%p) onwards", data->index,
- &((char *)MEMBLK_TOBLK(memblk))[data->index]);
+ fprintf(stderr, " index %d (address 0x%p) onwards", data->index, &((char *)MEMBLK_TOBLK(memblk))[data->index]);
if (data->preCorrupt) {
fprintf(stderr, "+ preguard");
}
@@ -1181,23 +1192,20 @@
fprintf(stderr, " preguard");
}
if (data->postCorrupt) {
- fprintf(stderr, "%s Postguard",
- (data->preCorrupt ? "+" : ""));
+ fprintf(stderr, "%s Postguard", (data->preCorrupt ? "+" : ""));
}
}
- fprintf(stderr, " corrupted.\n"
- " Block last checked OK at allocation %d. Now %d.\n",
- memblk->lastCheckedOK, globals.sequence);
- data->preCorrupt = 0;
+ fprintf(stderr, " corrupted.\n" " Block last checked OK at allocation %d. Now %d.\n", memblk->lastCheckedOK, globals.sequence);
+ data->preCorrupt = 0;
data->postCorrupt = 0;
data->freeCorrupt = 0;
- }
- else
+ } else
memblk->lastCheckedOK = globals.sequence;
return 0;
}
-int Memento_checkAllMemory(void)
+int
+Memento_checkAllMemory(void)
{
#ifndef MEMENTO_LEAKONLY
BlkCheckData data;
@@ -1213,7 +1221,8 @@
return 0;
}
-int Memento_setParanoia(int i)
+int
+Memento_setParanoia(int i)
{
globals.paranoia = i;
globals.countdown = globals.paranoia;
@@ -1220,15 +1229,18 @@
return i;
}
-int Memento_paranoidAt(int i)
+int
+Memento_paranoidAt(int i)
{
globals.paranoidAt = i;
return i;
}
-int Memento_getBlockNum(void *b)
+int
+Memento_getBlockNum(void *b)
{
Memento_BlkHeader *memblk;
+
if (b == NULL)
return 0;
memblk = MEMBLK_FROMBLK(b);
@@ -1235,7 +1247,8 @@
return (memblk->sequence);
}
-int Memento_check(void)
+int
+Memento_check(void)
{
int result;
@@ -1246,30 +1259,28 @@
}
typedef struct findBlkData {
- void *addr;
+ void *addr;
Memento_BlkHeader *blk;
- int flags;
+ int flags;
} findBlkData;
-static int Memento_containsAddr(Memento_BlkHeader *b,
- void *arg)
+static int
+Memento_containsAddr(Memento_BlkHeader *b, void *arg)
{
- findBlkData *data = (findBlkData *)arg;
+ findBlkData *data = (findBlkData *) arg;
char *blkend = &((char *)MEMBLK_TOBLK(b))[b->rawsize];
- if ((MEMBLK_TOBLK(b) <= data->addr) &&
- ((void *)blkend > data->addr)) {
+
+ if ((MEMBLK_TOBLK(b) <= data->addr) && ((void *)blkend > data->addr)) {
data->blk = b;
data->flags = 1;
return 1;
}
- if (((void *)b <= data->addr) &&
- (MEMBLK_TOBLK(b) > data->addr)) {
+ if (((void *)b <= data->addr) && (MEMBLK_TOBLK(b) > data->addr)) {
data->blk = b;
data->flags = 2;
return 1;
}
- if (((void *)blkend <= data->addr) &&
- ((void *)(blkend + Memento_PostSize) > data->addr)) {
+ if (((void *)blkend <= data->addr) && ((void *)(blkend + Memento_PostSize) > data->addr)) {
data->blk = b;
data->flags = 3;
return 1;
@@ -1277,31 +1288,26 @@
return 0;
}
-int Memento_find(void *a)
+int
+Memento_find(void *a)
{
findBlkData data;
- data.addr = a;
- data.blk = NULL;
+ data.addr = a;
+ data.blk = NULL;
data.flags = 0;
Memento_appBlocks(&globals.used, Memento_containsAddr, &data);
if (data.blk != NULL) {
- fprintf(stderr, "Address 0x%p is in %sallocated block ",
- data.addr,
- (data.flags == 1 ? "" : (data.flags == 2 ?
- "preguard of " : "postguard of ")));
+ fprintf(stderr, "Address 0x%p is in %sallocated block ", data.addr, (data.flags == 1 ? "" : (data.flags == 2 ? "preguard of " : "postguard of ")));
showBlock(data.blk, ' ');
fprintf(stderr, "\n");
return data.blk->sequence;
}
- data.blk = NULL;
+ data.blk = NULL;
data.flags = 0;
Memento_appBlocks(&globals.free, Memento_containsAddr, &data);
if (data.blk != NULL) {
- fprintf(stderr, "Address 0x%p is in %sfreed block ",
- data.addr,
- (data.flags == 1 ? "" : (data.flags == 2 ?
- "preguard of " : "postguard of ")));
+ fprintf(stderr, "Address 0x%p is in %sfreed block ", data.addr, (data.flags == 1 ? "" : (data.flags == 2 ? "preguard of " : "postguard of ")));
showBlock(data.blk, ' ');
fprintf(stderr, "\n");
return data.blk->sequence;
@@ -1309,32 +1315,29 @@
return 0;
}
-void Memento_breakOnFree(void *a)
+void
+Memento_breakOnFree(void *a)
{
findBlkData data;
- data.addr = a;
- data.blk = NULL;
+ data.addr = a;
+ data.blk = NULL;
data.flags = 0;
Memento_appBlocks(&globals.used, Memento_containsAddr, &data);
if (data.blk != NULL) {
fprintf(stderr, "Will stop when address 0x%p (in %sallocated block ",
- data.addr,
- (data.flags == 1 ? "" : (data.flags == 2 ?
- "preguard of " : "postguard of ")));
+ data.addr, (data.flags == 1 ? "" : (data.flags == 2 ? "preguard of " : "postguard of ")));
showBlock(data.blk, ' ');
fprintf(stderr, ") is freed\n");
data.blk->flags |= Memento_Flag_BreakOnFree;
return;
}
- data.blk = NULL;
+ data.blk = NULL;
data.flags = 0;
Memento_appBlocks(&globals.free, Memento_containsAddr, &data);
if (data.blk != NULL) {
fprintf(stderr, "Can't stop on free; address 0x%p is in %sfreed block ",
- data.addr,
- (data.flags == 1 ? "" : (data.flags == 2 ?
- "preguard of " : "postguard of ")));
+ data.addr, (data.flags == 1 ? "" : (data.flags == 2 ? "preguard of " : "postguard of ")));
showBlock(data.blk, ' ');
fprintf(stderr, "\n");
return;
@@ -1342,32 +1345,29 @@
fprintf(stderr, "Can't stop on free; address 0x%p is not in a known block.\n", a);
}
-void Memento_breakOnRealloc(void *a)
+void
+Memento_breakOnRealloc(void *a)
{
findBlkData data;
- data.addr = a;
- data.blk = NULL;
+ data.addr = a;
+ data.blk = NULL;
data.flags = 0;
Memento_appBlocks(&globals.used, Memento_containsAddr, &data);
if (data.blk != NULL) {
fprintf(stderr, "Will stop when address 0x%p (in %sallocated block ",
- data.addr,
- (data.flags == 1 ? "" : (data.flags == 2 ?
- "preguard of " : "postguard of ")));
+ data.addr, (data.flags == 1 ? "" : (data.flags == 2 ? "preguard of " : "postguard of ")));
showBlock(data.blk, ' ');
fprintf(stderr, ") is freed (or realloced)\n");
data.blk->flags |= Memento_Flag_BreakOnFree | Memento_Flag_BreakOnRealloc;
return;
}
- data.blk = NULL;
+ data.blk = NULL;
data.flags = 0;
Memento_appBlocks(&globals.free, Memento_containsAddr, &data);
if (data.blk != NULL) {
fprintf(stderr, "Can't stop on free/realloc; address 0x%p is in %sfreed block ",
- data.addr,
- (data.flags == 1 ? "" : (data.flags == 2 ?
- "preguard of " : "postguard of ")));
+ data.addr, (data.flags == 1 ? "" : (data.flags == 2 ? "preguard of " : "postguard of ")));
showBlock(data.blk, ' ');
fprintf(stderr, "\n");
return;
@@ -1375,16 +1375,17 @@
fprintf(stderr, "Can't stop on free/realloc; address 0x%p is not in a known block.\n", a);
}
-int Memento_failAt(int i)
+int
+Memento_failAt(int i)
{
globals.failAt = i;
- if ((globals.sequence > globals.failAt) &&
- (globals.failing != 0))
+ if ((globals.sequence > globals.failAt) && (globals.failing != 0))
Memento_startFailing();
return i;
}
-size_t Memento_setMax(size_t max)
+size_t
+Memento_setMax(size_t max)
{
globals.maxMemory = max;
return max;
@@ -1427,7 +1428,7 @@
return 0;
}
-int (Memento_getBlockNum)(void *i)
+int (Memento_getBlockNum)(void *i)
{
return 0;
}
@@ -1455,22 +1456,26 @@
#undef Memento_realloc
#undef Memento_calloc
-void *Memento_malloc(size_t size)
+void *
+Memento_malloc(size_t size)
{
return MEMENTO_UNDERLYING_MALLOC(size);
}
-void Memento_free(void *b)
+void
+Memento_free(void *b)
{
MEMENTO_UNDERLYING_FREE(b);
}
-void *Memento_realloc(void *b, size_t s)
+void *
+Memento_realloc(void *b, size_t s)
{
return MEMENTO_UNDERLYING_REALLOC(b, s);
}
-void *Memento_calloc(size_t n, size_t s)
+void *
+Memento_calloc(size_t n, size_t s)
{
return MEMENTO_UNDERLYING_CALLOC(n, s);
}
@@ -1483,7 +1488,7 @@
{
}
-size_t (Memento_setMax)(size_t max)
+size_t(Memento_setMax)(size_t max)
{
return 0;
}
--- a/memento.h
+++ b/memento.h
@@ -13,7 +13,6 @@
CA 94903, U.S.A., +1(415)492-9861, for further information.
*/
-
/* Memento: A library to aid debugging of memory leaks/heap corruption.
*
* Usage:
@@ -187,7 +186,7 @@
void *Memento_malloc(size_t s);
void *Memento_realloc(void *, size_t s);
-void Memento_free(void *);
+void Memento_free(void *);
void *Memento_calloc(size_t, size_t);
#ifdef MEMENTO
--- a/os_types.h
+++ b/os_types.h
@@ -31,14 +31,14 @@
#if defined(__CYGWIN__) && !defined(HAVE_STDINT_H)
# include <sys/types.h>
# if defined(OLD_CYGWIN_SYS_TYPES)
- /*
- * Old versions of Cygwin have no stdint.h but define "MS types". Some of
- * them conflict with a standard type emulation provided by config_types.h
- * so we do a fixup here.
- */
- typedef u_int8_t uint8_t;
- typedef u_int16_t uint16_t;
- typedef u_int32_t uint32_t;
+/*
+ * Old versions of Cygwin have no stdint.h but define "MS types". Some of
+ * them conflict with a standard type emulation provided by config_types.h
+ * so we do a fixup here.
+ */
+typedef u_int8_t uint8_t;
+typedef u_int16_t uint16_t;
+typedef u_int32_t uint32_t;
#endif
#elif defined(HAVE_CONFIG_H)
# include "config_types.h"
@@ -53,12 +53,12 @@
#elif defined (STD_INT_USE_SYS_INT_TYPES_H)
# include <sys/int_types.h>
#elif !defined(HAVE_STDINT_H)
- typedef unsigned char uint8_t;
- typedef unsigned short uint16_t;
- typedef unsigned int uint32_t;
- typedef signed char int8_t;
- typedef signed short int16_t;
- typedef signed int int32_t;
+typedef unsigned char uint8_t;
+typedef unsigned short uint16_t;
+typedef unsigned int uint32_t;
+typedef signed char int8_t;
+typedef signed short int16_t;
+typedef signed int int32_t;
#endif
#if defined(HAVE_STDINT_H) || defined(__MACOS__)
--- a/pbm2png.c
+++ b/pbm2png.c
@@ -34,7 +34,8 @@
#include "jbig2.h"
#include "jbig2_image.h"
-int main(int argc, char *argv[])
+int
+main(int argc, char *argv[])
{
Jbig2Ctx *ctx;
Jbig2Image *image;
@@ -49,7 +50,7 @@
}
image = jbig2_image_read_pbm_file(ctx, argv[1]);
- if(image == NULL) {
+ if (image == NULL) {
fprintf(stderr, "error reading pbm file '%s'\n", argv[1]);
return 1;
} else {
--- a/sha1.c
+++ b/sha1.c
@@ -111,22 +111,18 @@
#define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30);
#define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30);
-
-#ifdef VERBOSE /* SAK */
-void SHAPrintContext(SHA1_CTX *context, char *msg){
- printf("%s (%d,%d) %x %x %x %x %x\n",
- msg,
- context->count[0], context->count[1],
- context->state[0],
- context->state[1],
- context->state[2],
- context->state[3],
- context->state[4]);
+#ifdef VERBOSE /* SAK */
+void
+SHAPrintContext(SHA1_CTX *context, char *msg)
+{
+ printf("%s (%d,%d) %x %x %x %x %x\n",
+ msg, context->count[0], context->count[1], context->state[0], context->state[1], context->state[2], context->state[3], context->state[4]);
}
#endif /* VERBOSE */
/* Hash a single 512-bit block. This is the core of the algorithm. */
-void SHA1_Transform(uint32_t state[5], const uint8_t buffer[64])
+void
+SHA1_Transform(uint32_t state[5], const uint8_t buffer[64])
{
uint32_t a, b, c, d, e;
typedef union {
@@ -133,14 +129,15 @@
uint8_t c[64];
uint32_t l[16];
} CHAR64LONG16;
- CHAR64LONG16* block;
+ CHAR64LONG16 *block;
#ifdef SHA1HANDSOFF
static uint8_t workspace[64];
- block = (CHAR64LONG16*)workspace;
+
+ block = (CHAR64LONG16 *) workspace;
memcpy(block, buffer, 64);
#else
- block = (CHAR64LONG16*)buffer;
+ block = (CHAR64LONG16 *) buffer;
#endif
/* Copy context->state[] to working vars */
@@ -183,9 +180,9 @@
a = b = c = d = e = 0;
}
-
/* SHA1Init - Initialize new context */
-void SHA1_Init(SHA1_CTX* context)
+void
+SHA1_Init(SHA1_CTX *context)
{
/* SHA1 initialization constants */
context->state[0] = 0x67452301;
@@ -196,9 +193,9 @@
context->count[0] = context->count[1] = 0;
}
-
/* Run your data through this. */
-void SHA1_Update(SHA1_CTX* context, const uint8_t* data, const size_t len)
+void
+SHA1_Update(SHA1_CTX *context, const uint8_t *data, const size_t len)
{
size_t i, j;
@@ -207,17 +204,18 @@
#endif
j = (context->count[0] >> 3) & 63;
- if ((context->count[0] += len << 3) < (len << 3)) context->count[1]++;
+ if ((context->count[0] += len << 3) < (len << 3))
+ context->count[1]++;
context->count[1] += (len >> 29);
if ((j + len) > 63) {
- memcpy(&context->buffer[j], data, (i = 64-j));
+ memcpy(&context->buffer[j], data, (i = 64 - j));
SHA1_Transform(context->state, context->buffer);
- for ( ; i + 63 < len; i += 64) {
+ for (; i + 63 < len; i += 64) {
SHA1_Transform(context->state, data + i);
}
j = 0;
- }
- else i = 0;
+ } else
+ i = 0;
memcpy(&context->buffer[j], &data[i], len - i);
#ifdef VERBOSE
@@ -225,25 +223,25 @@
#endif
}
-
/* Add padding and return the message digest. */
-void SHA1_Final(SHA1_CTX* context, uint8_t digest[SHA1_DIGEST_SIZE])
+void
+SHA1_Final(SHA1_CTX *context, uint8_t digest[SHA1_DIGEST_SIZE])
{
uint32_t i;
- uint8_t finalcount[8];
+ uint8_t finalcount[8];
for (i = 0; i < 8; i++) {
finalcount[i] = (unsigned char)((context->count[(i >= 4 ? 0 : 1)]
- >> ((3-(i & 3)) * 8) ) & 255); /* Endian independent */
+ >> ((3 - (i & 3)) * 8)) & 255); /* Endian independent */
}
- SHA1_Update(context, (uint8_t *)"\200", 1);
+ SHA1_Update(context, (uint8_t *) "\200", 1);
while ((context->count[0] & 504) != 448) {
- SHA1_Update(context, (uint8_t *)"\0", 1);
+ SHA1_Update(context, (uint8_t *) "\0", 1);
}
- SHA1_Update(context, finalcount, 8); /* Should cause a SHA1_Transform() */
+ SHA1_Update(context, finalcount, 8); /* Should cause a SHA1_Transform() */
for (i = 0; i < SHA1_DIGEST_SIZE; i++) {
digest[i] = (uint8_t)
- ((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255);
+ ((context->state[i >> 2] >> ((3 - (i & 3)) * 8)) & 255);
}
/* Wipe variables */
@@ -251,9 +249,9 @@
memset(context->buffer, 0, 64);
memset(context->state, 0, 20);
memset(context->count, 0, 8);
- memset(finalcount, 0, 8); /* SWR */
+ memset(finalcount, 0, 8); /* SWR */
-#ifdef SHA1HANDSOFF /* make SHA1Transform overwrite its own static vars */
+#ifdef SHA1HANDSOFF /* make SHA1Transform overwrite its own static vars */
SHA1_Transform(context->state, context->buffer);
#endif
}
@@ -261,43 +259,43 @@
/*************************************************************/
#if 0
-int main(int argc, char** argv)
+int
+main(int argc, char **argv)
{
-int i, j;
-SHA1_CTX context;
-unsigned char digest[SHA1_DIGEST_SIZE], buffer[16384];
-FILE* file;
+ int i, j;
+ SHA1_CTX context;
+ unsigned char digest[SHA1_DIGEST_SIZE], buffer[16384];
+ FILE *file;
if (argc > 2) {
puts("Public domain SHA-1 implementation - by Steve Reid <[email protected]>");
- puts("Modified for 16 bit environments 7/98 - by James H. Brown <[email protected]>"); /* JHB */
+ puts("Modified for 16 bit environments 7/98 - by James H. Brown <[email protected]>"); /* JHB */
puts("Produces the SHA-1 hash of a file, or stdin if no file is specified.");
- return(0);
+ return (0);
}
if (argc < 2) {
file = stdin;
- }
- else {
+ } else {
if (!(file = fopen(argv[1], "rb"))) {
fputs("Unable to open file.", stderr);
- return(-1);
+ return (-1);
}
}
SHA1_Init(&context);
- while (!feof(file)) { /* note: what if ferror(file) */
+ while (!feof(file)) { /* note: what if ferror(file) */
i = fread(buffer, 1, 16384, file);
SHA1_Update(&context, buffer, i);
}
SHA1_Final(&context, digest);
fclose(file);
- for (i = 0; i < SHA1_DIGEST_SIZE/4; i++) {
+ for (i = 0; i < SHA1_DIGEST_SIZE / 4; i++) {
for (j = 0; j < 4; j++) {
- printf("%02X", digest[i*4+j]);
+ printf("%02X", digest[i * 4 + j]);
}
putchar(' ');
}
putchar('\n');
- return(0); /* JHB */
+ return (0); /* JHB */
}
#endif
@@ -308,21 +306,23 @@
static char *test_data[] = {
"abc",
"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
- "A million repetitions of 'a'"};
+ "A million repetitions of 'a'"
+};
static char *test_results[] = {
"A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D",
"84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1",
- "34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F"};
+ "34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F"
+};
-
-void digest_to_hex(const uint8_t digest[SHA1_DIGEST_SIZE], char *output)
+void
+digest_to_hex(const uint8_t digest[SHA1_DIGEST_SIZE], char *output)
{
- int i,j;
+ int i, j;
char *c = output;
- for (i = 0; i < SHA1_DIGEST_SIZE/4; i++) {
+ for (i = 0; i < SHA1_DIGEST_SIZE / 4; i++) {
for (j = 0; j < 4; j++) {
- sprintf(c,"%02X", digest[i*4+j]);
+ sprintf(c, "%02X", digest[i * 4 + j]);
c += 2;
}
sprintf(c, " ");
@@ -331,7 +331,8 @@
*(c - 1) = '\0';
}
-int main(int argc, char** argv)
+int
+main(int argc, char **argv)
{
int k;
SHA1_CTX context;
@@ -340,17 +341,17 @@
fprintf(stdout, "verifying SHA-1 implementation... ");
- for (k = 0; k < 2; k++){
+ for (k = 0; k < 2; k++) {
SHA1_Init(&context);
- SHA1_Update(&context, (uint8_t*)test_data[k], strlen(test_data[k]));
+ SHA1_Update(&context, (uint8_t *) test_data[k], strlen(test_data[k]));
SHA1_Final(&context, digest);
- digest_to_hex(digest, output);
+ digest_to_hex(digest, output);
if (strcmp(output, test_results[k])) {
fprintf(stdout, "FAIL\n");
- fprintf(stderr,"* hash of \"%s\" incorrect:\n", test_data[k]);
- fprintf(stderr,"\t%s returned\n", output);
- fprintf(stderr,"\t%s is correct\n", test_results[k]);
+ fprintf(stderr, "* hash of \"%s\" incorrect:\n", test_data[k]);
+ fprintf(stderr, "\t%s returned\n", output);
+ fprintf(stderr, "\t%s is correct\n", test_results[k]);
return (1);
}
}
@@ -357,19 +358,19 @@
/* million 'a' vector we feed separately */
SHA1_Init(&context);
for (k = 0; k < 1000000; k++)
- SHA1_Update(&context, (uint8_t*)"a", 1);
+ SHA1_Update(&context, (uint8_t *) "a", 1);
SHA1_Final(&context, digest);
digest_to_hex(digest, output);
if (strcmp(output, test_results[2])) {
fprintf(stdout, "FAIL\n");
- fprintf(stderr,"* hash of \"%s\" incorrect:\n", test_data[2]);
- fprintf(stderr,"\t%s returned\n", output);
- fprintf(stderr,"\t%s is correct\n", test_results[2]);
+ fprintf(stderr, "* hash of \"%s\" incorrect:\n", test_data[2]);
+ fprintf(stderr, "\t%s returned\n", output);
+ fprintf(stderr, "\t%s is correct\n", test_results[2]);
return (1);
}
/* success */
fprintf(stdout, "ok\n");
- return(0);
+ return (0);
}
#endif /* TEST */
--- a/sha1.h
+++ b/sha1.h
@@ -5,23 +5,24 @@
#define __SHA1_H
#ifdef __cplusplus
-extern "C" {
+extern "C"
+{
#endif
typedef struct {
uint32_t state[5];
uint32_t count[2];
- uint8_t buffer[64];
+ uint8_t buffer[64];
} SHA1_CTX;
#define SHA1_DIGEST_SIZE 20
-void SHA1_Init(SHA1_CTX* context);
-void SHA1_Update(SHA1_CTX* context, const uint8_t* data, const size_t len);
-void SHA1_Final(SHA1_CTX* context, uint8_t digest[SHA1_DIGEST_SIZE]);
+void SHA1_Init(SHA1_CTX *context);
+void SHA1_Update(SHA1_CTX *context, const uint8_t *data, const size_t len);
+void SHA1_Final(SHA1_CTX *context, uint8_t digest[SHA1_DIGEST_SIZE]);
#ifdef __cplusplus
}
#endif
-#endif /* __SHA1_H */
+#endif /* __SHA1_H */
--- a/snprintf.c
+++ b/snprintf.c
@@ -56,53 +56,51 @@
static int caught;
static sigjmp_buf bail;
-#define EXTRABYTES 2 /* XXX: why 2? you don't want to know */
+#define EXTRABYTES 2 /* XXX: why 2? you don't want to know */
static char *
msetup(str, n)
- char *str;
- size_t n;
+char *str;
+size_t n;
{
- char *e;
+ char *e;
- if (n == 0)
- return NULL;
- if (pgsize == 0)
- pgsize = getpagesize();
- curobj = (char *)malloc(n + EXTRABYTES + pgsize * 2);
- if (curobj == NULL)
- return NULL;
- e = curobj + n + EXTRABYTES;
- e = (char *)roundup((unsigned long)e, pgsize);
- if (mprotect(e, pgsize, PROT_NONE) == -1) {
- free(curobj);
- curobj = NULL;
- return NULL;
- }
- e = e - n - EXTRABYTES;
- *e = '\0';
- return (e);
+ if (n == 0)
+ return NULL;
+ if (pgsize == 0)
+ pgsize = getpagesize();
+ curobj = (char *)malloc(n + EXTRABYTES + pgsize * 2);
+ if (curobj == NULL)
+ return NULL;
+ e = curobj + n + EXTRABYTES;
+ e = (char *)roundup((unsigned long)e, pgsize);
+ if (mprotect(e, pgsize, PROT_NONE) == -1) {
+ free(curobj);
+ curobj = NULL;
+ return NULL;
+ }
+ e = e - n - EXTRABYTES;
+ *e = '\0';
+ return (e);
}
static void
- mcatch( int a )
+mcatch(int a)
{
- siglongjmp(bail, 1);
+ siglongjmp(bail, 1);
}
static void
mcleanup(str, n, p)
- char *str;
- size_t n;
- char *p;
+char *str;
+size_t n;
+char *p;
{
- strncpy(str, p, n-1);
- str[n-1] = '\0';
- if (mprotect((caddr_t)(p + n + EXTRABYTES), pgsize,
- PROT_READ|PROT_WRITE|PROT_EXEC) == -1)
- mprotect((caddr_t)(p + n + EXTRABYTES), pgsize,
- PROT_READ|PROT_WRITE);
- free(curobj);
+ strncpy(str, p, n - 1);
+ str[n - 1] = '\0';
+ if (mprotect((caddr_t)(p + n + EXTRABYTES), pgsize, PROT_READ | PROT_WRITE | PROT_EXEC) == -1)
+ mprotect((caddr_t)(p + n + EXTRABYTES), pgsize, PROT_READ | PROT_WRITE);
+ free(curobj);
}
int
@@ -110,35 +108,35 @@
vsnprintf(char *str, size_t n, char const *fmt, va_list ap)
#else
vsnprintf(str, n, fmt, ap)
- char *str;
- size_t n;
- char *fmt;
- char *ap;
+char *str;
+size_t n;
+char *fmt;
+char *ap;
#endif
{
- struct sigaction osa, nsa;
- char *p;
- int ret = n + 1; /* if we bail, indicated we overflowed */
+ struct sigaction osa, nsa;
+ char *p;
+ int ret = n + 1; /* if we bail, indicated we overflowed */
- memset(&nsa, 0, sizeof nsa);
- nsa.sa_handler = mcatch;
- sigemptyset(&nsa.sa_mask);
+ memset(&nsa, 0, sizeof nsa);
+ nsa.sa_handler = mcatch;
+ sigemptyset(&nsa.sa_mask);
- p = msetup(str, n);
- if (p == NULL) {
- *str = '\0';
- return 0;
- }
- if (sigsetjmp(bail, 1) == 0) {
- if (sigaction(SIGSEGV, &nsa, &osa) == -1) {
- mcleanup(str, n, p);
- return (0);
- }
- ret = vsprintf(p, fmt, ap);
- }
- mcleanup(str, n, p);
- (void) sigaction(SIGSEGV, &osa, NULL);
- return (ret);
+ p = msetup(str, n);
+ if (p == NULL) {
+ *str = '\0';
+ return 0;
+ }
+ if (sigsetjmp(bail, 1) == 0) {
+ if (sigaction(SIGSEGV, &nsa, &osa) == -1) {
+ mcleanup(str, n, p);
+ return (0);
+ }
+ ret = vsprintf(p, fmt, ap);
+ }
+ mcleanup(str, n, p);
+ (void)sigaction(SIGSEGV, &osa, NULL);
+ return (ret);
}
int
@@ -146,20 +144,20 @@
snprintf(char *str, size_t n, char const *fmt, ...)
#else
snprintf(str, n, fmt, va_alist)
- char *str;
- size_t n;
- char *fmt;
- va_dcl
+char *str;
+size_t n;
+char *fmt;
+va_dcl
#endif
{
- va_list ap;
+ va_list ap;
+
#if __STDC__
- va_start(ap, fmt);
+ va_start(ap, fmt);
#else
- va_start(ap);
+ va_start(ap);
#endif
- return (vsnprintf(str, n, fmt, ap));
- va_end(ap);
+ return (vsnprintf(str, n, fmt, ap));
+ va_end(ap);
}
-