50#define SIZEOF_WORDTYPE SIZEOF_SIZE_T
72#define MAXCODE(n) ((1 << (n)) - 1)
83#define CODE_MAX MAXCODE(BITS_MAX)
85#define HSHIFT (13 - 8)
88#define CSIZE (MAXCODE(BITS_MAX) + 1024L)
90#define CSIZE (MAXCODE(BITS_MAX) + 1L)
125#define lzw_nbits base.nbits
126#define lzw_maxcode base.maxcode
127#define lzw_free_ent base.free_ent
128#define lzw_nextdata base.nextdata
129#define lzw_nextbits base.nextbits
247#define CHECK_GAP 10000
260#define LZWState(tif) ((LZWBaseState *)(tif)->tif_data)
261#define LZWDecoderState(tif) ((LZWCodecState *)LZWState(tif))
262#define LZWEncoderState(tif) ((LZWCodecState *)LZWState(tif))
304 static const char module[] = "LZWSetupDecode";
322 sp->dec_codetab = NULL;
323 sp->dec_decode = NULL;
331 if (sp->dec_codetab == NULL)
334 if (sp->dec_codetab == NULL)
345 sp->dec_codetab[code].firstchar = (
unsigned char)code;
346 sp->dec_codetab[code].value = (
unsigned char)code;
347 sp->dec_codetab[code].repeated =
true;
348 sp->dec_codetab[code].length = 1;
349 sp->dec_codetab[code].next = NULL;
391 static const char module[] = "LZWPreDecode";
396 if (sp->dec_codetab == NULL)
399 if (sp->dec_codetab == NULL)
435 TIFFErrorExtR(tif, module,
"Old-style LZW codes not supported");
447 sp->lzw_nextbits = 0;
448 sp->lzw_nextdata = 0;
452 sp->dec_bitsleft = 0;
453 sp->old_tif_rawcc = 0;
454 sp->dec_free_entp = sp->dec_codetab - 1;
462 sp->dec_oldcodep = &sp->dec_codetab[0];
463 sp->dec_maxcodep = &sp->dec_codetab[sp->dec_nbitsmask - 1];
474#define GetNextData(nextdata, bp) memcpy(&nextdata, bp, sizeof(nextdata))
475#elif SIZEOF_WORDTYPE == 8
477#define GetNextData(nextdata, bp) nextdata = _byteswap_uint64(*(uint64_t *)(bp))
478#elif defined(__GNUC__)
479#define GetNextData(nextdata, bp) \
480 memcpy(&nextdata, bp, sizeof(nextdata)); \
481 nextdata = __builtin_bswap64(nextdata)
483#define GetNextData(nextdata, bp) \
484 nextdata = (((uint64_t)bp[0]) << 56) | (((uint64_t)bp[1]) << 48) | \
485 (((uint64_t)bp[2]) << 40) | (((uint64_t)bp[3]) << 32) | \
486 (((uint64_t)bp[4]) << 24) | (((uint64_t)bp[5]) << 16) | \
487 (((uint64_t)bp[6]) << 8) | (((uint64_t)bp[7]))
489#elif SIZEOF_WORDTYPE == 4
491#define GetNextData(nextdata, bp) \
492 nextdata = _byteswap_ulong(*(unsigned long *)(bp))
493#elif defined(__GNUC__)
494#define GetNextData(nextdata, bp) \
495 memcpy(&nextdata, bp, sizeof(nextdata)); \
496 nextdata = __builtin_bswap32(nextdata)
498#define GetNextData(nextdata, bp) \
499 nextdata = (((uint32_t)bp[0]) << 24) | (((uint32_t)bp[1]) << 16) | \
500 (((uint32_t)bp[2]) << 8) | (((uint32_t)bp[3]))
503#error "Unhandled SIZEOF_WORDTYPE"
506#define GetNextCodeLZW() \
512 if (dec_bitsleft >= 8 * SIZEOF_WORDTYPE) \
514 unsigned codetmp = (unsigned)(nextdata << (-nextbits)); \
515 GetNextData(nextdata, bp); \
516 bp += SIZEOF_WORDTYPE; \
517 nextbits += 8 * SIZEOF_WORDTYPE; \
518 dec_bitsleft -= 8 * SIZEOF_WORDTYPE; \
519 code = (WordType)((codetmp | (nextdata >> nextbits)) & \
520 (WordType)nbitsmask); \
525 if (dec_bitsleft < 8) \
529 nextdata = (nextdata << 8) | *(bp)++; \
534 if (dec_bitsleft < 8) \
538 nextdata = (nextdata << 8) | *(bp)++; \
544 code = (WordType)((nextdata >> nextbits) & (WordType)nbitsmask); \
573 static const char module[] = "LZWDecode";
575 uint8_t *op = (uint8_t *)op0;
578 long nbits, nextbits, nbitsmask;
580 code_t *free_entp, *maxcodep, *oldcodep;
584 assert(sp->dec_codetab != NULL);
588 memset(op, 0, (
size_t)occ);
590 "LZWDecode: Scanline %" PRIu32
" cannot be read due to "
603 code_t *codep = sp->dec_codep;
604 residue = codep->
length - sp->dec_restart;
613 sp->dec_restart += occ;
617 }
while (--residue > occ && codep);
620 uint8_t *tp = op + occ;
623 *--tp = codep->
value;
625 }
while (--occ && codep);
637 *--tp = codep->
value;
639 }
while (--residue && codep);
645 (((uint64_t)tif->
tif_rawcc - (uint64_t)sp->old_tif_rawcc) << 3);
646 uint64_t dec_bitsleft = sp->dec_bitsleft;
647 nbits = sp->lzw_nbits;
648 nextdata = sp->lzw_nextdata;
649 nextbits = sp->lzw_nextbits;
650 nbitsmask = sp->dec_nbitsmask;
651 oldcodep = sp->dec_oldcodep;
652 free_entp = sp->dec_free_entp;
653 maxcodep = sp->dec_maxcodep;
654 code_t *
const dec_codetab = sp->dec_codetab;
666 codep = dec_codetab + (
unsigned long)code;
668 goto code_above_or_equal_to_258;
677 if (codep > free_entp)
679 free_entp->
next = oldcodep;
682 free_entp->
value = (uint8_t)code;
685 if (++free_entp > maxcodep)
690 maxcodep = dec_codetab + nbitsmask - 1;
691 if (free_entp >= &dec_codetab[
CSIZE])
698 free_entp = dec_codetab - 1;
702 *op++ = (uint8_t)code;
709code_above_or_equal_to_258:
715 if (codep >= free_entp)
717 if (codep != free_entp)
727 free_entp->
next = oldcodep;
731 if (++free_entp > maxcodep)
736 maxcodep = dec_codetab + nbitsmask - 1;
737 if (free_entp >= &dec_codetab[
CSIZE])
744 free_entp = dec_codetab - 1;
754 unsigned short len = codep->
length;
767 goto too_short_buffer;
784 op[2] = codep->
value;
789 goto too_short_buffer;
794 op[2] = codep->
value;
802 goto too_short_buffer;
807 memset(op, codep->
value, len);
815 uint8_t *tp = op + len;
819 *--tp = codep->
value;
821 *--tp = codep->
value;
823 *--tp = codep->
value;
825 *--tp = codep->
value;
831 *--tp = codep->
value;
848 maxcodep = dec_codetab + (
unsigned long)nbitsmask - 1;
859 *op++ = (uint8_t)code;
861 oldcodep = dec_codetab + code;
876 sp->dec_codep = codep;
880 }
while (codep->
length > occ);
882 sp->dec_restart = occ;
883 uint8_t *tp = op + occ;
886 *--tp = codep->
value;
895 sp->dec_bitsleft = dec_bitsleft;
896 sp->lzw_nbits = (
unsigned short)nbits;
897 sp->lzw_nextdata = nextdata;
898 sp->lzw_nextbits = nextbits;
899 sp->dec_nbitsmask = nbitsmask;
900 sp->dec_oldcodep = oldcodep;
901 sp->dec_free_entp = free_entp;
902 sp->dec_maxcodep = maxcodep;
906 memset(op, 0, (
size_t)occ);
909 "Not enough data at scanline %" PRIu32
" (short %" PRIu64
917 memset(op, 0, (
size_t)occ);
920 "LZWDecode: Strip %" PRIu32
" not terminated with EOI code",
924 memset(op, 0, (
size_t)occ);
936#define NextCode(_tif, _sp, _bp, _code, _get, dec_bitsleft) \
938 if (dec_bitsleft < (uint64_t)nbits) \
940 TIFFWarningExtR(_tif, module, \
941 "LZWDecode: Strip %" PRIu32 \
942 " not terminated with EOI code", \
943 _tif->tif_dir.td_curstrip); \
948 _get(_sp, _bp, _code); \
949 dec_bitsleft -= (uint64_t)nbits; \
956#define GetNextCodeCompat(sp, bp, code) \
958 nextdata |= (unsigned long)*(bp)++ << nextbits; \
960 if (nextbits < nbits) \
962 nextdata |= (unsigned long)*(bp)++ << nextbits; \
965 code = (hcode_t)(nextdata & (unsigned long)nbitsmask); \
966 nextdata >>= nbits; \
998 static const char module[] = "LZWDecodeCompat";
1000 uint8_t *op = (uint8_t *)op0;
1006 long nextbits, nbitsmask;
1008 code_t *codep, *free_entp, *maxcodep, *oldcodep;
1015 memset(op, 0, (
size_t)occ);
1017 "LZWDecodeCompat: Scanline %" PRIu32
1018 " cannot be read due to previous error",
1026 if (sp->dec_restart)
1030 codep = sp->dec_codep;
1031 residue = codep->
length - sp->dec_restart;
1040 sp->dec_restart += occ;
1043 codep = codep->
next;
1044 }
while (--residue > occ);
1048 *--tp = codep->
value;
1049 codep = codep->
next;
1061 *--tp = codep->
value;
1062 codep = codep->
next;
1063 }
while (--residue);
1064 sp->dec_restart = 0;
1070 (((uint64_t)tif->
tif_rawcc - (uint64_t)sp->old_tif_rawcc) << 3);
1071 uint64_t dec_bitsleft = sp->dec_bitsleft;
1073 nbits = sp->lzw_nbits;
1074 nextdata = sp->lzw_nextdata;
1075 nextbits = sp->lzw_nextbits;
1076 nbitsmask = sp->dec_nbitsmask;
1077 oldcodep = sp->dec_oldcodep;
1078 free_entp = sp->dec_free_entp;
1079 maxcodep = sp->dec_maxcodep;
1095 maxcodep = sp->dec_codetab + nbitsmask;
1102 memset(op0, 0, (
size_t)occ0);
1106 "LZWDecode: Corrupted LZW table at scanline %" PRIu32,
1110 *op++ = (uint8_t)code;
1112 oldcodep = sp->dec_codetab + code;
1115 codep = sp->dec_codetab + code;
1120 if (free_entp < &sp->dec_codetab[0] ||
1121 free_entp >= &sp->dec_codetab[
CSIZE])
1123 memset(op0, 0, (
size_t)occ0);
1126 "Corrupted LZW table at scanline %" PRIu32,
1131 free_entp->
next = oldcodep;
1132 if (free_entp->
next < &sp->dec_codetab[0] ||
1133 free_entp->
next >= &sp->dec_codetab[
CSIZE])
1135 memset(op0, 0, (
size_t)occ0);
1138 "Corrupted LZW table at scanline %" PRIu32,
1146 if (++free_entp > maxcodep)
1151 maxcodep = sp->dec_codetab + nbitsmask;
1162 memset(op0, 0, (
size_t)occ0);
1166 "Wrong length of decoded "
1167 "string: data probably corrupted at scanline %" PRIu32,
1179 sp->dec_codep = codep;
1182 codep = codep->
next;
1183 }
while (codep->
length > occ);
1184 sp->dec_restart = occ;
1188 *--tp = codep->
value;
1189 codep = codep->
next;
1197 *--tp = codep->
value;
1198 codep = codep->
next;
1199 }
while (codep && tp > op);
1206 *op++ = (uint8_t)code;
1215 sp->dec_bitsleft = dec_bitsleft;
1217 sp->lzw_nbits = (
unsigned short)nbits;
1218 sp->lzw_nextdata = nextdata;
1219 sp->lzw_nextbits = nextbits;
1220 sp->dec_nbitsmask = nbitsmask;
1221 sp->dec_oldcodep = oldcodep;
1222 sp->dec_free_entp = free_entp;
1223 sp->dec_maxcodep = maxcodep;
1227 memset(op, 0, (
size_t)occ);
1230 "Not enough data at scanline %" PRIu32
" (short %" PRIu64
1239#ifndef LZW_READ_ONLY
1261 static const char module[] = "LZWSetupEncode";
1266 if (sp->enc_hashtab == NULL)
1299 static const char module[] = "LZWPreEncode";
1314 " bytes is too small for LZW encoding, at least 5 bytes"
1320 if (sp->enc_hashtab == NULL)
1328 sp->lzw_nextbits = 0;
1329 sp->lzw_nextdata = 0;
1332 sp->enc_incount = 0;
1333 sp->enc_outcount = 0;
1340 sp->enc_oldcode = (
hcode_t)-1;
1344#define CALCRATIO(sp, rat) \
1346 if (incount > 0x007fffff) \
1348 rat = outcount >> 8; \
1349 rat = (rat == 0 ? 0x7fffffff : incount / rat); \
1352 rat = (incount << 8) / outcount; \
1356#define PutNextCode(op, c) \
1358 nextdata = (nextdata << nbits) | c; \
1359 nextbits += nbits; \
1360 *op++ = (unsigned char)((nextdata >> (nextbits - 8)) & 0xff); \
1362 if (nextbits >= 8) \
1364 *op++ = (unsigned char)((nextdata >> (nextbits - 8)) & 0xff); \
1367 outcount += nbits; \
1411 tmsize_t incount, outcount, checkpoint;
1414 int free_ent, maxcode, nbits;
1430 nextdata = sp->lzw_nextdata;
1431 nextbits = sp->lzw_nextbits;
1432 free_ent = sp->lzw_free_ent;
1433 maxcode = sp->lzw_maxcode;
1434 nbits = sp->lzw_nbits;
1439 if (ent == (
hcode_t)-1 && cc > 0)
1456 fcode = ((long)c <<
BITS_MAX) + ent;
1459 if (hp->
hash == fcode)
1478 if ((h -= (
int)disp) < 0)
1481 if (hp->
hash == fcode)
1486 }
while (hp->
hash >= 0);
1526 if (free_ent > maxcode)
1530 maxcode = (int)
MAXCODE(nbits);
1532 else if (incount >= checkpoint)
1543 if (rat <= sp->enc_ratio)
1568 sp->lzw_nextdata = nextdata;
1569 sp->lzw_nextbits = nextbits;
1570 sp->lzw_free_ent = (
unsigned short)free_ent;
1571 sp->lzw_maxcode = (
unsigned short)maxcode;
1572 sp->lzw_nbits = (
unsigned short)nbits;
1599 long nextbits = sp->lzw_nextbits;
1600 WordType nextdata = sp->lzw_nextdata;
1602 int nbits = sp->lzw_nbits;
1613 int free_ent = sp->lzw_free_ent;
1632 if (free_ent > sp->lzw_maxcode)
1642 *op++ = (
unsigned char)((nextdata << (8 - nextbits)) & 0xff);
1680 for (i += 8; i > 0; i--, hp--)
1744 static const char module[] = "TIFFInitLZW";
1767#ifndef LZW_READ_ONLY
void _TIFFSetDefaultCompressionState(TIFF *tif)
Install fallback codec callbacks and reset raw-access flags.
#define TIFF_SSIZE_FORMAT
Provide the printf conversion fragment for tmsize_t, without a percent sign.
int TIFFFlushData1(TIFF *tif)
Append buffered encoded bytes without invoking postencode recursively.
#define CODE_EOI
Reserved code 257 that ends the strip or tile.
static uint64_t LZWGetMaxCompressionRatio(TIFF *tif)
Report the empirical LZW expansion heuristic.
#define BITS_MIN
Initial and post-clear code width: 9 bits.
#define PutNextCode(op, c)
Append the nbits-wide code c MSB-first to the encoder output at op.
#define MAXCODE(n)
Compute the inclusive maximum code for a valid bit width.
static int LZWPreEncode(TIFF *tif, uint16_t s)
Reset the LZW encoder dictionary bit cache and counters.
static int LZWPreDecode(TIFF *tif, uint16_t s)
Reset LZW bit and dictionary state for a strile.
#define CODE_MAX
Largest 12-bit code: MAXCODE(BITS_MAX), i.e. 4095.
#define BITS_MAX
Maximum code width allowed by TIFF LZW: 12 bits.
#define CALCRATIO(sp, rat)
Compute the encoder compression ratio incount/outcount as a 24.8 fixed-point value into rat.
#define LZWEncoderState(tif)
Cast tif->tif_data to the full LZWCodecState for encoding.
struct code_ent code_t
Link one decoder string to its preceding dictionary string.
#define LZWDecoderState(tif)
Cast tif->tif_data to the full LZWCodecState for decoding.
uint16_t hcode_t
Store an encoder dictionary code in sixteen bits.
#define CODE_CLEAR
Reserved code 256 that resets the string table.
static int LZWDecode(TIFF *tif, uint8_t *op0, tmsize_t occ0, uint16_t s)
Expand standard TIFF LZW codes into caller output.
int TIFFInitLZW(TIFF *tif, int scheme)
Allocate LZW codec state and install predictor-aware methods.
#define GetNextCodeLZW()
Extract the next nbits-wide MSB-first code from the decoder bit cache into code, refilling from bp as...
static int LZWEncode(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s)
Compress borrowed bytes into buffered TIFF LZW codes.
static int LZWFixupTags(TIFF *tif)
Accept LZW tag fixup without changes.
size_t WordType
Select the native size_t word for buffered LZW bits.
#define NextCode(_tif, _sp, _bp, _code, _get, dec_bitsleft)
Fetch the next compatibility-mode code with _get, or yield CODE_EOI with a warning when fewer than nb...
static void LZWCleanup(TIFF *tif)
Release LZW tables and codec state and reset methods.
#define CHECK_GAP
Encoder input bytes between compression-ratio checks: 10000.
static void cl_hash(LZWCodecState *)
Mark every LZW encoder hash slot unused.
#define CODE_FIRST
First dictionary code assigned to a new string: 258.
static int LZWDecodeCompat(TIFF *tif, uint8_t *op0, tmsize_t occ0, uint16_t s)
Expand historical reversed-order LZW codes into caller output.
#define HSIZE
Number of entries in the encoder hash table: 9001.
static int LZWSetupEncode(TIFF *tif)
Allocate the LZW encoder hash table.
static int LZWPostEncode(TIFF *tif)
Emit the final LZW string end code and padding bits.
#define CSIZE
Number of entries allocated for the decoder string table.
#define HSHIFT
Left shift applied to the input byte when hashing encoder strings.
#define LZWState(tif)
Cast tif->tif_data to the shared LZWBaseState prefix.
int(* decodeFunc)(TIFF *tif, uint8_t *buf, tmsize_t size, uint16_t sample)
Signature shared by the LZW decode entry points (recorded in dec_decode).
#define GetNextCodeCompat(sp, bp, code)
Extract the next nbits-wide LSB-first code for old reversed-order LZW data.
static int LZWSetupDecode(TIFF *tif)
Allocate and initialise the LZW decode dictionary.
void _TIFFfreeExt(TIFF *tif, void *p)
Release a handle-aware allocation and its payload charge.
void * _TIFFmallocExt(TIFF *tif, tmsize_t s)
Allocate uninitialised bytes under a handle's allocation policy.
int TIFFPredictorCleanup(TIFF *tif)
Restore saved Predictor tag print and setup hooks.
int TIFFPredictorInit(TIFF *tif)
Install Predictor tag and setup hooks around a codec.
void TIFFWarningExtR(TIFF *tif, const char *module, const char *fmt,...)
Dispatch a warning diagnostic to the configured handlers.
void TIFFErrorExtR(TIFF *tif, const char *module, const char *fmt,...)
Dispatch a error diagnostic to the configured handlers.
void _TIFFmemset(void *p, int v, tmsize_t c)
Fill a byte range with one repeated byte.
TIFF_SSIZE_T tmsize_t
Represent a signed memory or I/O size in bytes.
Prefix LZW state with Predictor hooks and shared bit-code state.
WordType nextdata
Buffered input/output code bits.
unsigned short free_ent
Next available dictionary code.
unsigned short maxcode
Largest code under the current width convention.
unsigned short nbits
Current code width in bits.
long nextbits
Number of valid buffered code bits.
TIFFPredictorState predict
Predictor prefix; must be the first member.
int rw_mode
Open mode saved at codec initialisation.
Own LZW dictionaries hash storage and transfer restart state.
uint8_t * enc_rawlimit
Borrowed raw-buffer limit leaving room for final codes.
code_t * dec_codep
Borrowed current/restart entry within dec_codetab.
tmsize_t enc_outcount
Output code bits counted for ratio estimation.
tmsize_t old_tif_rawcc
Raw byte count saved after the preceding decoder call.
code_t * dec_maxcodep
Current width-dependent dictionary limit cursor.
hash_t * enc_hashtab
Owned HSIZE-entry encoding table, released by cleanup.
tmsize_t enc_ratio
Cached compression ratio in fixed-point form.
long dec_nbitsmask
Right-adjusted mask for current decode width.
tmsize_t enc_checkpoint
Input-byte threshold for the next ratio test.
int read_error
Latched same-strile failure cleared by predecode.
tmsize_t enc_incount
Input bytes counted for ratio estimation.
LZWBaseState base
Shared LZW/Predictor prefix, first in the codec allocation.
code_t * dec_oldcodep
Borrowed preceding recognised entry.
uint64_t dec_bitsleft
Number of cached raw-input bits remaining.
tmsize_t dec_restart
Bytes already returned from an interrupted decoded string.
int enc_oldcode
Pending previous encoder string code.
code_t * dec_codetab
Owned decoder entry allocation, released by cleanup.
code_t * dec_free_entp
Next available decoder table entry cursor.
decodeFunc dec_decode
Decoder last recorded by predecode; NULL until the first predecode. Used only as a first-strile detec...
uint32_t td_row
Current scanline index; UINT32_MAX before traversal starts.
uint32_t td_curstrip
Current strip index, or NOSTRIP.
Prefix codec state with Predictor method chains and row geometry.
Link one decoder string to its preceding dictionary string.
struct code_ent * next
Borrowed link within the decoder table to the preceding string entry.
unsigned char value
Final byte represented by this entry.
bool repeated
Whether the string consists of one repeated byte.
unsigned char firstchar
First byte of the decoded string; immediately precedes value for fast paths.
unsigned short length
Decoded string length including this entry.
Pair an encoder hash key with its dictionary code.
long hash
Encoder lookup key; minus one means unused.
hcode_t code
Encoder dictionary code for the key.
Hold private state for one open TIFF library instance.
TIFFCodeMethod tif_encodestrip
Codec callback encoding strip data.
TIFFCodeMethod tif_encodetile
Codec callback encoding tile data.
TIFFPreMethod tif_preencode
Codec callback preparing a row, strip, or tile encode.
TIFFBoolMethod tif_fixuptags
Codec callback for directory-dependent tag corrections.
tmsize_t tif_rawcc
Raw-buffer byte count interpreted according to read/write state.
TIFFPreMethod tif_predecode
Codec callback preparing a row, strip, or tile decode.
TIFFCodeMethod tif_decodestrip
Codec callback decoding strip data.
TIFFGetMaxCompressionRatioMethod tif_getmaxcompressionratio
Codec callback returning its maximum compression ratio.
char * tif_name
Current filename pointer; may refer to inline or client-supplied storage.
TIFFCodeMethod tif_decoderow
Codec callback decoding a scanline.
TIFFBoolMethod tif_setupencode
Codec setup callback run before preencode.
TIFFDirectory tif_dir
Inline representation of the current image directory.
TIFFBoolMethod tif_postencode
Codec callback completing buffered encoding.
TIFFCodeMethod tif_encoderow
Codec callback encoding a scanline.
TIFFVoidMethod tif_cleanup
Codec callback releasing codec-private state.
uint8_t * tif_data
Codec-private storage; allocation and cleanup follow the selected codec.
uint8_t * tif_rawcp
Borrowed current-position pointer into the raw buffer.
TIFFBoolMethod tif_setupdecode
Codec setup callback run before predecode.
uint8_t * tif_rawdata
Raw buffer; ownership/mapping follow TIFF_MYBUFFER and TIFF_BUFFERMMAP.
tmsize_t tif_rawdatasize
Raw-buffer capacity in bytes; kept in step with tif_rawdata, including zero whenever tif_rawdata is N...
TIFFCodeMethod tif_decodetile
Codec callback decoding tile data.
int tif_mode
Current access mode using the O_* constants.
Define the predictor prefix and method-chain contracts.
Declare private library state, callbacks, and implementation helpers.